ir_to_mesa.cpp revision 2d577ee730c30caacf711babde6542766aa0b655
184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt/*
2bd3b835e7c32e093f91f636330fd93b3dedd8362Eric Anholt * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
3bd3b835e7c32e093f91f636330fd93b3dedd8362Eric Anholt * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * Copyright © 2010 Intel Corporation
584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt *
684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * copy of this software and associated documentation files (the "Software"),
884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * to deal in the Software without restriction, including without limitation
984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * and/or sell copies of the Software, and to permit persons to whom the
1184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * Software is furnished to do so, subject to the following conditions:
1284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt *
1384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * The above copyright notice and this permission notice (including the next
1484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * paragraph) shall be included in all copies or substantial portions of the
1584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * Software.
1684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt *
1784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * DEALINGS IN THE SOFTWARE.
2484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt */
2584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
2684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt/**
2784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt * \file ir_to_mesa.cpp
2884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt *
29f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * Translate GLSL IR to Mesa's gl_program representation.
3084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt */
3184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
320161515c395c44233529c8d51f823b60050bc7baEric Anholt#include <stdio.h>
33261bbc011d11ab9e390cd5fe9f5151821eefaffaIan Romanick#include "main/compiler.h"
3484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt#include "ir.h"
3584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt#include "ir_visitor.h"
3684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt#include "ir_print_visitor.h"
3784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt#include "ir_expression_flattening.h"
3884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt#include "glsl_types.h"
39364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt#include "glsl_parser_extras.h"
40364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt#include "../glsl/program.h"
41364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt#include "ir_optimization.h"
42364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt#include "ast.h"
4384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
44aaee40e107cf07a12c8e373d8bb910254f4ba30bEric Anholtextern "C" {
450a1b54df7ac118722bb627c61cb322cb4e248aceEric Anholt#include "main/mtypes.h"
46658e25987fbec3b826f500baa6d4d936b9552b13Eric Anholt#include "main/shaderapi.h"
47afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "main/shaderobj.h"
48afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "main/uniforms.h"
49fe1918c71c3e387939cef9359d4b31ebc5c11a17Eric Anholt#include "program/hash_table.h"
50afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "program/prog_instruction.h"
51afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "program/prog_optimize.h"
52afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "program/prog_print.h"
53afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "program/program.h"
54afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "program/prog_uniform.h"
55afe125e0a18ac3886c45c7e6b02b122fb2d327b5Eric Anholt#include "program/prog_parameter.h"
56a32893221ce253da7bb465e0ec9d0df5f7208d8fEric Anholt#include "program/sampler.h"
57aaee40e107cf07a12c8e373d8bb910254f4ba30bEric Anholt}
5884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
599c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholtstatic int swizzle_for_size(int size);
609c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt
61554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt/**
62554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt * This struct is a corresponding struct to Mesa prog_src_register, with
63554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt * wider fields.
64554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt */
65554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholttypedef struct ir_to_mesa_src_reg {
669c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   ir_to_mesa_src_reg(int file, int index, const glsl_type *type)
679c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   {
68caf974c5259f14b50257e8dd9b325a87378259afBrian Paul      this->file = (gl_register_file) file;
699c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt      this->index = index;
709c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt      if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
719c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt	 this->swizzle = swizzle_for_size(type->vector_elements);
729c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt      else
739c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt	 this->swizzle = SWIZZLE_XYZW;
749c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt      this->negate = 0;
759c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt      this->reladdr = NULL;
769c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   }
779c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt
789c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   ir_to_mesa_src_reg()
799c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   {
809c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt      this->file = PROGRAM_UNDEFINED;
8148c289fb552a3d363b505514b6ea22467f00e318Vinson Lee      this->index = 0;
8248c289fb552a3d363b505514b6ea22467f00e318Vinson Lee      this->swizzle = 0;
8348c289fb552a3d363b505514b6ea22467f00e318Vinson Lee      this->negate = 0;
8448c289fb552a3d363b505514b6ea22467f00e318Vinson Lee      this->reladdr = NULL;
859c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   }
869c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt
87caf974c5259f14b50257e8dd9b325a87378259afBrian Paul   gl_register_file file; /**< PROGRAM_* from Mesa */
88554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
89582b73fe691ef7ea12a002cb2ae57505c3b1c21eEric Anholt   GLuint swizzle; /**< SWIZZLE_XYZWONEZERO swizzles from Mesa. */
90554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   int negate; /**< NEGATE_XYZW mask from mesa */
91f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   /** Register index should be offset by the integer in this reg. */
92f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   ir_to_mesa_src_reg *reladdr;
93554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt} ir_to_mesa_src_reg;
94554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
95554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholttypedef struct ir_to_mesa_dst_reg {
96554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   int file; /**< PROGRAM_* from Mesa */
97554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   int index; /**< temporary index, VERT_ATTRIB_*, FRAG_ATTRIB_*, etc. */
98554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   int writemask; /**< Bitfield of WRITEMASK_[XYZW] */
99854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt   GLuint cond_mask:4;
100f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   /** Register index should be offset by the integer in this reg. */
101f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   ir_to_mesa_src_reg *reladdr;
102554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt} ir_to_mesa_dst_reg;
103554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
104554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholtextern ir_to_mesa_src_reg ir_to_mesa_undef;
105554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
106554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholtclass ir_to_mesa_instruction : public exec_node {
107554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholtpublic:
1087406898441bfec937840d575500fb6d43192310dEric Anholt   /* Callers of this talloc-based new need not call delete. It's
1097406898441bfec937840d575500fb6d43192310dEric Anholt    * easier to just talloc_free 'ctx' (or any of its ancestors). */
1107406898441bfec937840d575500fb6d43192310dEric Anholt   static void* operator new(size_t size, void *ctx)
1117406898441bfec937840d575500fb6d43192310dEric Anholt   {
1127406898441bfec937840d575500fb6d43192310dEric Anholt      void *node;
1137406898441bfec937840d575500fb6d43192310dEric Anholt
1147406898441bfec937840d575500fb6d43192310dEric Anholt      node = talloc_zero_size(ctx, size);
1157406898441bfec937840d575500fb6d43192310dEric Anholt      assert(node != NULL);
1167406898441bfec937840d575500fb6d43192310dEric Anholt
1177406898441bfec937840d575500fb6d43192310dEric Anholt      return node;
1187406898441bfec937840d575500fb6d43192310dEric Anholt   }
1197406898441bfec937840d575500fb6d43192310dEric Anholt
120554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   enum prog_opcode op;
121554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_to_mesa_dst_reg dst_reg;
122554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_to_mesa_src_reg src_reg[3];
123554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   /** Pointer to the ir source this tree came from for debugging */
124554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_instruction *ir;
125854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt   GLboolean cond_update;
126ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   bool saturate;
127d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   int sampler; /**< sampler index */
128d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   int tex_target; /**< One of TEXTURE_*_INDEX */
129b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt   GLboolean tex_shadow;
1307b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1317b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   class function_entry *function; /* Set on OPCODE_CAL or OPCODE_BGNSUB */
132554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt};
133554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
134b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholtclass variable_storage : public exec_node {
135554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholtpublic:
136caf974c5259f14b50257e8dd9b325a87378259afBrian Paul   variable_storage(ir_variable *var, gl_register_file file, int index)
137554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt      : file(file), index(index), var(var)
138554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   {
139554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt      /* empty */
140554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   }
141554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
142caf974c5259f14b50257e8dd9b325a87378259afBrian Paul   gl_register_file file;
143554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   int index;
144554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_variable *var; /* variable that maps to this, if any */
145554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt};
146554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
1477b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholtclass function_entry : public exec_node {
1487b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholtpublic:
1497b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   ir_function_signature *sig;
1507b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1517b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /**
1527b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * identifier of this function signature used by the program.
1537b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    *
1547b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * At the point that Mesa instructions for function calls are
1557b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * generated, we don't know the address of the first instruction of
1567b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * the function body.  So we make the BranchTarget that is called a
1577b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * small integer and rewrite them during set_branchtargets().
1587b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    */
1597b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   int sig_id;
1607b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1617b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /**
1627b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * Pointer to first instruction of the function body.
1637b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    *
1647b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * Set during function body emits after main() is processed.
1657b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    */
1667b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   ir_to_mesa_instruction *bgn_inst;
1677b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1687b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /**
1697b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * Index of the first instruction of the function body in actual
1707b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * Mesa IR.
1717b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    *
1727b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    * Set after convertion from ir_to_mesa_instruction to prog_instruction.
1737b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt    */
1747b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   int inst;
1757b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1767b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /** Storage for the return value. */
1777b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   ir_to_mesa_src_reg return_reg;
1787b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt};
1797b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
180554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholtclass ir_to_mesa_visitor : public ir_visitor {
181554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholtpublic:
182554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_to_mesa_visitor();
183fe1918c71c3e387939cef9359d4b31ebc5c11a17Eric Anholt   ~ir_to_mesa_visitor();
184554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
1857b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   function_entry *current_function;
1867b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
187f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx;
188364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   struct gl_program *prog;
189aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt   struct gl_shader_program *shader_program;
1906d3a2c97f4a78e85545286e0e126cd3a27bd1cbdLuca Barbieri   struct gl_shader_compiler_options *options;
191364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
192554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   int next_temp;
193a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt
194b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt   variable_storage *find_variable_storage(ir_variable *var);
195554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
1967b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   function_entry *get_function_signature(ir_function_signature *sig);
1977b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1988364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt   ir_to_mesa_src_reg get_temp(const glsl_type *type);
199f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   void reladdr_to_temp(ir_instruction *ir,
200f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt			ir_to_mesa_src_reg *reg, int *num_reladdr);
201554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
202554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   struct ir_to_mesa_src_reg src_reg_for_float(float val);
203554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
204554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   /**
205554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt    * \name Visit methods
206554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt    *
207554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt    * As typical for the visitor pattern, there must be one \c visit method for
208554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt    * each concrete subclass of \c ir_instruction.  Virtual base classes within
209554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt    * the hierarchy should not have \c visit methods.
210554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt    */
211554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   /*@{*/
212554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_variable *);
213554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_loop *);
214554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_loop_jump *);
215554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_function_signature *);
216554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_function *);
217554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_expression *);
218554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_swizzle *);
219554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_dereference_variable  *);
220554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_dereference_array *);
221554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_dereference_record *);
222554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_assignment *);
223554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_constant *);
224554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_call *);
225554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_return *);
22616efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke   virtual void visit(ir_discard *);
227554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_texture *);
228554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   virtual void visit(ir_if *);
229554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   /*@}*/
230554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
231554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   struct ir_to_mesa_src_reg result;
232554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
233b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt   /** List of variable_storage */
234b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt   exec_list variables;
235554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
2367b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /** List of function_entry */
2377b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   exec_list function_signatures;
2387b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   int next_signature_id;
2397b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
240554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   /** List of ir_to_mesa_instruction */
241554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   exec_list instructions;
242554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
243021222c6a872ca2eef770ebadb8754f659775204Eric Anholt   ir_to_mesa_instruction *ir_to_mesa_emit_op0(ir_instruction *ir,
244021222c6a872ca2eef770ebadb8754f659775204Eric Anholt					       enum prog_opcode op);
245021222c6a872ca2eef770ebadb8754f659775204Eric Anholt
246554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_to_mesa_instruction *ir_to_mesa_emit_op1(ir_instruction *ir,
247554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       enum prog_opcode op,
248554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_dst_reg dst,
249554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_src_reg src0);
250554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
251554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_to_mesa_instruction *ir_to_mesa_emit_op2(ir_instruction *ir,
252554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       enum prog_opcode op,
253554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_dst_reg dst,
254554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_src_reg src0,
255554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_src_reg src1);
256554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
257554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   ir_to_mesa_instruction *ir_to_mesa_emit_op3(ir_instruction *ir,
258554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       enum prog_opcode op,
259554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_dst_reg dst,
260554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_src_reg src0,
261554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_src_reg src1,
262554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt					       ir_to_mesa_src_reg src2);
263554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
264ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick   /**
265ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick    * Emit the correct dot-product instruction for the type of arguments
266ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick    *
267ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick    * \sa ir_to_mesa_emit_op2
268ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick    */
269ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick   void ir_to_mesa_emit_dp(ir_instruction *ir,
270ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick			   ir_to_mesa_dst_reg dst,
271ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick			   ir_to_mesa_src_reg src0,
272ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick			   ir_to_mesa_src_reg src1,
273ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick			   unsigned elements);
274ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick
275554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt   void ir_to_mesa_emit_scalar_op1(ir_instruction *ir,
276554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt				   enum prog_opcode op,
277554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt				   ir_to_mesa_dst_reg dst,
278554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt				   ir_to_mesa_src_reg src0);
2790ee7d80269bfab14683623b0c8fc12da43db8d78Eric Anholt
280904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt   void ir_to_mesa_emit_scalar_op2(ir_instruction *ir,
281904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				   enum prog_opcode op,
282904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				   ir_to_mesa_dst_reg dst,
283904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				   ir_to_mesa_src_reg src0,
284904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				   ir_to_mesa_src_reg src1);
285904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt
286f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   void emit_scs(ir_instruction *ir, enum prog_opcode op,
287f2616e56de8a48360cae8f269727b58490555f4dIan Romanick		 ir_to_mesa_dst_reg dst,
288f2616e56de8a48360cae8f269727b58490555f4dIan Romanick		 const ir_to_mesa_src_reg &src);
289f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
2903f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   GLboolean try_emit_mad(ir_expression *ir,
2913f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt			  int mul_operand);
292ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   GLboolean try_emit_sat(ir_expression *ir);
2933f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt
29411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   void emit_swz(ir_expression *ir);
29511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
296c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   bool process_move_condition(ir_rvalue *ir);
297c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
298364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   void *mem_ctx;
299554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt};
300554dbcce77cc7eb38b786c77eee87a5f391b090bEric Anholt
3019c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholtir_to_mesa_src_reg ir_to_mesa_undef = ir_to_mesa_src_reg(PROGRAM_UNDEFINED, 0, NULL);
30284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
303c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholtir_to_mesa_dst_reg ir_to_mesa_undef_dst = {
304f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   PROGRAM_UNDEFINED, 0, SWIZZLE_NOOP, COND_TR, NULL,
305c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt};
306c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
3070161515c395c44233529c8d51f823b60050bc7baEric Anholtir_to_mesa_dst_reg ir_to_mesa_address_reg = {
308f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   PROGRAM_ADDRESS, 0, WRITEMASK_X, COND_TR, NULL
3090161515c395c44233529c8d51f823b60050bc7baEric Anholt};
3100161515c395c44233529c8d51f823b60050bc7baEric Anholt
311f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paulstatic void
312f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paulfail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3);
313500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt
314f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paulstatic void
315f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paulfail_link(struct gl_shader_program *prog, const char *fmt, ...)
316f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul{
317f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul   va_list args;
318f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul   va_start(args, fmt);
319f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul   prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, args);
320f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul   va_end(args);
321500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt
322f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul   prog->LinkStatus = GL_FALSE;
323f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul}
324500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt
325f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paulstatic int
326f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paulswizzle_for_size(int size)
3270161515c395c44233529c8d51f823b60050bc7baEric Anholt{
3280161515c395c44233529c8d51f823b60050bc7baEric Anholt   int size_swizzles[4] = {
3290161515c395c44233529c8d51f823b60050bc7baEric Anholt      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
3300161515c395c44233529c8d51f823b60050bc7baEric Anholt      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
3310161515c395c44233529c8d51f823b60050bc7baEric Anholt      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
3320161515c395c44233529c8d51f823b60050bc7baEric Anholt      MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
3330161515c395c44233529c8d51f823b60050bc7baEric Anholt   };
3340161515c395c44233529c8d51f823b60050bc7baEric Anholt
3359fea9e5e2115bcb52435648d2ef753638733d7d9Ian Romanick   assert((size >= 1) && (size <= 4));
3360161515c395c44233529c8d51f823b60050bc7baEric Anholt   return size_swizzles[size - 1];
3370161515c395c44233529c8d51f823b60050bc7baEric Anholt}
3380161515c395c44233529c8d51f823b60050bc7baEric Anholt
33984771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_instruction *
3400161515c395c44233529c8d51f823b60050bc7baEric Anholtir_to_mesa_visitor::ir_to_mesa_emit_op3(ir_instruction *ir,
3410161515c395c44233529c8d51f823b60050bc7baEric Anholt					enum prog_opcode op,
3420161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_dst_reg dst,
3430161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_src_reg src0,
3440161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_src_reg src1,
3450161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_src_reg src2)
34684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
347364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   ir_to_mesa_instruction *inst = new(mem_ctx) ir_to_mesa_instruction();
348f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   int num_reladdr = 0;
349f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
350f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   /* If we have to do relative addressing, we want to load the ARL
351f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt    * reg directly for one of the regs, and preload the other reladdr
352f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt    * sources into temps.
353f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt    */
354f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   num_reladdr += dst.reladdr != NULL;
355f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   num_reladdr += src0.reladdr != NULL;
356f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   num_reladdr += src1.reladdr != NULL;
357f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   num_reladdr += src2.reladdr != NULL;
358f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
359f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   reladdr_to_temp(ir, &src2, &num_reladdr);
360f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   reladdr_to_temp(ir, &src1, &num_reladdr);
361f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   reladdr_to_temp(ir, &src0, &num_reladdr);
362f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
363f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   if (dst.reladdr) {
364f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_ARL, ir_to_mesa_address_reg,
365f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt                          *dst.reladdr);
366f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
367f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt      num_reladdr--;
368f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   }
369f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   assert(num_reladdr == 0);
37084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
37184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   inst->op = op;
37284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   inst->dst_reg = dst;
37384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   inst->src_reg[0] = src0;
37484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   inst->src_reg[1] = src1;
37584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   inst->src_reg[2] = src2;
376c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   inst->ir = ir;
37784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
3787b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   inst->function = NULL;
3797b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
3800161515c395c44233529c8d51f823b60050bc7baEric Anholt   this->instructions.push_tail(inst);
38184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
38284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   return inst;
38384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
38484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
38584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
38684771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_instruction *
3870161515c395c44233529c8d51f823b60050bc7baEric Anholtir_to_mesa_visitor::ir_to_mesa_emit_op2(ir_instruction *ir,
3880161515c395c44233529c8d51f823b60050bc7baEric Anholt					enum prog_opcode op,
3890161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_dst_reg dst,
3900161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_src_reg src0,
3910161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_src_reg src1)
39284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
3930161515c395c44233529c8d51f823b60050bc7baEric Anholt   return ir_to_mesa_emit_op3(ir, op, dst, src0, src1, ir_to_mesa_undef);
39484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
39584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
39684771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_instruction *
3970161515c395c44233529c8d51f823b60050bc7baEric Anholtir_to_mesa_visitor::ir_to_mesa_emit_op1(ir_instruction *ir,
3980161515c395c44233529c8d51f823b60050bc7baEric Anholt					enum prog_opcode op,
3990161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_dst_reg dst,
4000161515c395c44233529c8d51f823b60050bc7baEric Anholt					ir_to_mesa_src_reg src0)
401bf9953335031b3de721245ec7a2986d0b4f70027Eric Anholt{
4025a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   assert(dst.writemask != 0);
4030161515c395c44233529c8d51f823b60050bc7baEric Anholt   return ir_to_mesa_emit_op3(ir, op, dst,
4040161515c395c44233529c8d51f823b60050bc7baEric Anholt			      src0, ir_to_mesa_undef, ir_to_mesa_undef);
405bf9953335031b3de721245ec7a2986d0b4f70027Eric Anholt}
406bf9953335031b3de721245ec7a2986d0b4f70027Eric Anholt
407021222c6a872ca2eef770ebadb8754f659775204Eric Anholtir_to_mesa_instruction *
408021222c6a872ca2eef770ebadb8754f659775204Eric Anholtir_to_mesa_visitor::ir_to_mesa_emit_op0(ir_instruction *ir,
409021222c6a872ca2eef770ebadb8754f659775204Eric Anholt					enum prog_opcode op)
410021222c6a872ca2eef770ebadb8754f659775204Eric Anholt{
411021222c6a872ca2eef770ebadb8754f659775204Eric Anholt   return ir_to_mesa_emit_op3(ir, op, ir_to_mesa_undef_dst,
412021222c6a872ca2eef770ebadb8754f659775204Eric Anholt			      ir_to_mesa_undef,
413021222c6a872ca2eef770ebadb8754f659775204Eric Anholt			      ir_to_mesa_undef,
414021222c6a872ca2eef770ebadb8754f659775204Eric Anholt			      ir_to_mesa_undef);
415021222c6a872ca2eef770ebadb8754f659775204Eric Anholt}
416021222c6a872ca2eef770ebadb8754f659775204Eric Anholt
417ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanickvoid
418ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanickir_to_mesa_visitor::ir_to_mesa_emit_dp(ir_instruction *ir,
419ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick				       ir_to_mesa_dst_reg dst,
420ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick				       ir_to_mesa_src_reg src0,
421ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick				       ir_to_mesa_src_reg src1,
422ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick				       unsigned elements)
423ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick{
424ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick   static const gl_inst_opcode dot_opcodes[] = {
425ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick      OPCODE_DP2, OPCODE_DP3, OPCODE_DP4
426ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick   };
427ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick
428ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick   ir_to_mesa_emit_op3(ir, dot_opcodes[elements - 2],
429ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick		       dst, src0, src1, ir_to_mesa_undef);
430ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick}
431ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick
4320161515c395c44233529c8d51f823b60050bc7baEric Anholtinline ir_to_mesa_dst_reg
4330161515c395c44233529c8d51f823b60050bc7baEric Anholtir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg reg)
43484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
4350161515c395c44233529c8d51f823b60050bc7baEric Anholt   ir_to_mesa_dst_reg dst_reg;
43684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
4370161515c395c44233529c8d51f823b60050bc7baEric Anholt   dst_reg.file = reg.file;
4380161515c395c44233529c8d51f823b60050bc7baEric Anholt   dst_reg.index = reg.index;
4390161515c395c44233529c8d51f823b60050bc7baEric Anholt   dst_reg.writemask = WRITEMASK_XYZW;
440854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt   dst_reg.cond_mask = COND_TR;
441f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   dst_reg.reladdr = reg.reladdr;
4420161515c395c44233529c8d51f823b60050bc7baEric Anholt
4430161515c395c44233529c8d51f823b60050bc7baEric Anholt   return dst_reg;
444bf9953335031b3de721245ec7a2986d0b4f70027Eric Anholt}
445bf9953335031b3de721245ec7a2986d0b4f70027Eric Anholt
4462d1789e667c4180777829f96856daf91326721b9Eric Anholtinline ir_to_mesa_src_reg
4472d1789e667c4180777829f96856daf91326721b9Eric Anholtir_to_mesa_src_reg_from_dst(ir_to_mesa_dst_reg reg)
4482d1789e667c4180777829f96856daf91326721b9Eric Anholt{
4499c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   return ir_to_mesa_src_reg(reg.file, reg.index, NULL);
4502d1789e667c4180777829f96856daf91326721b9Eric Anholt}
4512d1789e667c4180777829f96856daf91326721b9Eric Anholt
45212f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt/**
45312f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt * Emits Mesa scalar opcodes to produce unique answers across channels.
45412f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt *
45512f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt * Some Mesa opcodes are scalar-only, like ARB_fp/vp.  The src X
45612f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt * channel determines the result across all channels.  So to do a vec4
45712f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt * of this operation, we want to emit a scalar per source channel used
45812f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt * to produce dest channels.
45912f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt */
46012f654c63bc42d353e258cde989d9114cdde26c6Eric Anholtvoid
461904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholtir_to_mesa_visitor::ir_to_mesa_emit_scalar_op2(ir_instruction *ir,
4620161515c395c44233529c8d51f823b60050bc7baEric Anholt					       enum prog_opcode op,
4630161515c395c44233529c8d51f823b60050bc7baEric Anholt					       ir_to_mesa_dst_reg dst,
464904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt					       ir_to_mesa_src_reg orig_src0,
465904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt					       ir_to_mesa_src_reg orig_src1)
46612f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt{
46712f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt   int i, j;
468315c638b8cf0a92f9f0a8ee496e77e90e4b66d09Eric Anholt   int done_mask = ~dst.writemask;
46912f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt
47012f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt   /* Mesa RCP is a scalar operation splatting results to all channels,
47112f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt    * like ARB_fp/vp.  So emit as many RCPs as necessary to cover our
47212f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt    * dst channels.
47312f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt    */
47412f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt   for (i = 0; i < 4; i++) {
475582b73fe691ef7ea12a002cb2ae57505c3b1c21eEric Anholt      GLuint this_mask = (1 << i);
47612f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt      ir_to_mesa_instruction *inst;
477904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      ir_to_mesa_src_reg src0 = orig_src0;
478904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      ir_to_mesa_src_reg src1 = orig_src1;
47912f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt
48012f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt      if (done_mask & this_mask)
48112f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt	 continue;
48212f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt
483904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      GLuint src0_swiz = GET_SWZ(src0.swizzle, i);
484904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      GLuint src1_swiz = GET_SWZ(src1.swizzle, i);
48512f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt      for (j = i + 1; j < 4; j++) {
486f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 /* If there is another enabled component in the destination that is
487f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  * derived from the same inputs, generate its value on this pass as
488f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  * well.
489f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  */
490904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt	 if (!(done_mask & (1 << j)) &&
491904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt	     GET_SWZ(src0.swizzle, j) == src0_swiz &&
492904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt	     GET_SWZ(src1.swizzle, j) == src1_swiz) {
49312f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt	    this_mask |= (1 << j);
49412f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt	 }
49512f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt      }
496904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
497904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				   src0_swiz, src0_swiz);
498904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      src1.swizzle = MAKE_SWIZZLE4(src1_swiz, src1_swiz,
499904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				  src1_swiz, src1_swiz);
50012f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt
501904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      inst = ir_to_mesa_emit_op2(ir, op,
5020161515c395c44233529c8d51f823b60050bc7baEric Anholt				 dst,
503904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				 src0,
504904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt				 src1);
50512f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt      inst->dst_reg.writemask = this_mask;
50612f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt      done_mask |= this_mask;
50712f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt   }
50812f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt}
50912f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt
510904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholtvoid
511904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholtir_to_mesa_visitor::ir_to_mesa_emit_scalar_op1(ir_instruction *ir,
512904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt					       enum prog_opcode op,
513904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt					       ir_to_mesa_dst_reg dst,
514904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt					       ir_to_mesa_src_reg src0)
515904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt{
51659a23d7fb93603b2449db4c5d786934a07aebfcbEric Anholt   ir_to_mesa_src_reg undef = ir_to_mesa_undef;
517904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt
518904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt   undef.swizzle = SWIZZLE_XXXX;
519904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt
520904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt   ir_to_mesa_emit_scalar_op2(ir, op, dst, src0, undef);
521904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt}
522904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt
523f2616e56de8a48360cae8f269727b58490555f4dIan Romanick/**
524f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * Emit an OPCODE_SCS instruction
525f2616e56de8a48360cae8f269727b58490555f4dIan Romanick *
526f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * The \c SCS opcode functions a bit differently than the other Mesa (or
527f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * ARB_fragment_program) opcodes.  Instead of splatting its result across all
528f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * four components of the destination, it writes one value to the \c x
529f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * component and another value to the \c y component.
530f2616e56de8a48360cae8f269727b58490555f4dIan Romanick *
531f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * \param ir        IR instruction being processed
532f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * \param op        Either \c OPCODE_SIN or \c OPCODE_COS depending on which
533f2616e56de8a48360cae8f269727b58490555f4dIan Romanick *                  value is desired.
534f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * \param dst       Destination register
535f2616e56de8a48360cae8f269727b58490555f4dIan Romanick * \param src       Source register
536f2616e56de8a48360cae8f269727b58490555f4dIan Romanick */
537f2616e56de8a48360cae8f269727b58490555f4dIan Romanickvoid
538f2616e56de8a48360cae8f269727b58490555f4dIan Romanickir_to_mesa_visitor::emit_scs(ir_instruction *ir, enum prog_opcode op,
539f2616e56de8a48360cae8f269727b58490555f4dIan Romanick			     ir_to_mesa_dst_reg dst,
540f2616e56de8a48360cae8f269727b58490555f4dIan Romanick			     const ir_to_mesa_src_reg &src)
541f2616e56de8a48360cae8f269727b58490555f4dIan Romanick{
542f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   /* Vertex programs cannot use the SCS opcode.
543f2616e56de8a48360cae8f269727b58490555f4dIan Romanick    */
544f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   if (this->prog->Target == GL_VERTEX_PROGRAM_ARB) {
545f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      ir_to_mesa_emit_scalar_op1(ir, op, dst, src);
546f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      return;
547f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   }
548f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
549f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   const unsigned component = (op == OPCODE_SIN) ? 0 : 1;
550f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   const unsigned scs_mask = (1U << component);
551f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   int done_mask = ~dst.writemask;
552f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   ir_to_mesa_src_reg tmp;
553f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
554f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   assert(op == OPCODE_SIN || op == OPCODE_COS);
555f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
556f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   /* If there are compnents in the destination that differ from the component
557f2616e56de8a48360cae8f269727b58490555f4dIan Romanick    * that will be written by the SCS instrution, we'll need a temporary.
558f2616e56de8a48360cae8f269727b58490555f4dIan Romanick    */
559f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   if (scs_mask != unsigned(dst.writemask)) {
560f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      tmp = get_temp(glsl_type::vec4_type);
561f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   }
562f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
563f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   for (unsigned i = 0; i < 4; i++) {
564f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      unsigned this_mask = (1U << i);
565f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      ir_to_mesa_src_reg src0 = src;
566f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
567f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      if ((done_mask & this_mask) != 0)
568f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 continue;
569f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
570f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      /* The source swizzle specified which component of the source generates
571f2616e56de8a48360cae8f269727b58490555f4dIan Romanick       * sine / cosine for the current component in the destination.  The SCS
572f2616e56de8a48360cae8f269727b58490555f4dIan Romanick       * instruction requires that this value be swizzle to the X component.
573f2616e56de8a48360cae8f269727b58490555f4dIan Romanick       * Replace the current swizzle with a swizzle that puts the source in
574f2616e56de8a48360cae8f269727b58490555f4dIan Romanick       * the X component.
575f2616e56de8a48360cae8f269727b58490555f4dIan Romanick       */
576f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      unsigned src0_swiz = GET_SWZ(src.swizzle, i);
577f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
578f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      src0.swizzle = MAKE_SWIZZLE4(src0_swiz, src0_swiz,
579f2616e56de8a48360cae8f269727b58490555f4dIan Romanick				   src0_swiz, src0_swiz);
580f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      for (unsigned j = i + 1; j < 4; j++) {
581f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 /* If there is another enabled component in the destination that is
582f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  * derived from the same inputs, generate its value on this pass as
583f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  * well.
584f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  */
585f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 if (!(done_mask & (1 << j)) &&
586f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	     GET_SWZ(src0.swizzle, j) == src0_swiz) {
587f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	    this_mask |= (1 << j);
588f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 }
589f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      }
590f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
591f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      if (this_mask != scs_mask) {
592f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 ir_to_mesa_instruction *inst;
593f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 ir_to_mesa_dst_reg tmp_dst = ir_to_mesa_dst_reg_from_src(tmp);
594f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
595f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 /* Emit the SCS instruction.
596f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  */
597f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 inst = ir_to_mesa_emit_op1(ir, OPCODE_SCS, tmp_dst, src0);
598f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 inst->dst_reg.writemask = scs_mask;
599f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
600f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 /* Move the result of the SCS instruction to the desired location in
601f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  * the destination.
602f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  */
603f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 tmp.swizzle = MAKE_SWIZZLE4(component, component,
604f2616e56de8a48360cae8f269727b58490555f4dIan Romanick				     component, component);
605f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 inst = ir_to_mesa_emit_op1(ir, OPCODE_SCS, dst, tmp);
606f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 inst->dst_reg.writemask = this_mask;
607f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      } else {
608f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 /* Emit the SCS instruction to write directly to the destination.
609f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	  */
610f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 ir_to_mesa_instruction *inst =
611f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	    ir_to_mesa_emit_op1(ir, OPCODE_SCS, dst, src0);
612f2616e56de8a48360cae8f269727b58490555f4dIan Romanick	 inst->dst_reg.writemask = scs_mask;
613f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      }
614f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
615f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      done_mask |= this_mask;
616f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   }
617f2616e56de8a48360cae8f269727b58490555f4dIan Romanick}
618f2616e56de8a48360cae8f269727b58490555f4dIan Romanick
6190161515c395c44233529c8d51f823b60050bc7baEric Anholtstruct ir_to_mesa_src_reg
6200161515c395c44233529c8d51f823b60050bc7baEric Anholtir_to_mesa_visitor::src_reg_for_float(float val)
621b07cc372c6360d0e59c84bb7586597f028c74b02Eric Anholt{
6229c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   ir_to_mesa_src_reg src_reg(PROGRAM_CONSTANT, -1, NULL);
6231d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt
624582b73fe691ef7ea12a002cb2ae57505c3b1c21eEric Anholt   src_reg.index = _mesa_add_unnamed_constant(this->prog->Parameters,
625582b73fe691ef7ea12a002cb2ae57505c3b1c21eEric Anholt					      &val, 1, &src_reg.swizzle);
6261d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt
6270161515c395c44233529c8d51f823b60050bc7baEric Anholt   return src_reg;
6281d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt}
6291d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt
6302c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholtstatic int
6312c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholttype_size(const struct glsl_type *type)
6322c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt{
6332c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   unsigned int i;
6342c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   int size;
6352c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt
6362c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   switch (type->base_type) {
6372c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   case GLSL_TYPE_UINT:
6382c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   case GLSL_TYPE_INT:
6392c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   case GLSL_TYPE_FLOAT:
6402c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   case GLSL_TYPE_BOOL:
641a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt      if (type->is_matrix()) {
6429968f1b23c475c99139f0209c7a049ed00df01afEric Anholt	 return type->matrix_columns;
643a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt      } else {
644a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt	 /* Regardless of size of vector, it gets a vec4. This is bad
645a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt	  * packing for things like floats, but otherwise arrays become a
646a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt	  * mess.  Hopefully a later pass over the code can pack scalars
647a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt	  * down if appropriate.
648a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt	  */
649a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt	 return 1;
650a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt      }
6512c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   case GLSL_TYPE_ARRAY:
6522c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt      return type_size(type->fields.array) * type->length;
6532c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   case GLSL_TYPE_STRUCT:
6542c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt      size = 0;
6552c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt      for (i = 0; i < type->length; i++) {
6562c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt	 size += type_size(type->fields.structure[i].type);
6572c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt      }
6582c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt      return size;
6598d61a23b1a1d0d4b21f0fab64f6d863a8ee3d7f1Eric Anholt   case GLSL_TYPE_SAMPLER:
6600924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      /* Samplers take up one slot in UNIFORMS[], but they're baked in
6610924ba0c3496160a134d37cec800f902ae805b9cEric Anholt       * at link time.
6628d61a23b1a1d0d4b21f0fab64f6d863a8ee3d7f1Eric Anholt       */
6630924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      return 1;
6642c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   default:
6652c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt      assert(0);
66619acfa42ed47edb63f5ec3de8051a3102e62e96bJosé Fonseca      return 0;
6672c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   }
6682c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt}
6692c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt
670d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt/**
671d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt * In the initial pass of codegen, we assign temporary numbers to
672d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt * intermediate results.  (not SSA -- variable assignments will reuse
673d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt * storage).  Actual register allocation for the Mesa VM occurs in a
674d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt * pass over the Mesa IR later.
675d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt */
676d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholtir_to_mesa_src_reg
677d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholtir_to_mesa_visitor::get_temp(const glsl_type *type)
678d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt{
679d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt   ir_to_mesa_src_reg src_reg;
680d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt   int swizzle[4];
681d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt   int i;
682d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt
683d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt   src_reg.file = PROGRAM_TEMPORARY;
684d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt   src_reg.index = next_temp;
685f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   src_reg.reladdr = NULL;
686d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt   next_temp += type_size(type);
687d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt
68820c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt   if (type->is_array() || type->is_record()) {
68920c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      src_reg.swizzle = SWIZZLE_NOOP;
69020c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt   } else {
69120c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      for (i = 0; i < type->vector_elements; i++)
69220c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	 swizzle[i] = i;
69320c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      for (; i < 4; i++)
69420c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	 swizzle[i] = type->vector_elements - 1;
69520c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      src_reg.swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
69620c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt				      swizzle[2], swizzle[3]);
69720c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt   }
698ea6b34cce4471d6239201101a3b24db17eaae870Eric Anholt   src_reg.negate = 0;
699d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt
700d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt   return src_reg;
701d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt}
702d5a5df45a4af93bb845483bdeeae7c8e042b03d8Eric Anholt
703b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholtvariable_storage *
704a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholtir_to_mesa_visitor::find_variable_storage(ir_variable *var)
70584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
706a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt
707b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt   variable_storage *entry;
70884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
709b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt   foreach_iter(exec_list_iterator, iter, this->variables) {
710b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt      entry = (variable_storage *)iter.get();
71184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
7120161515c395c44233529c8d51f823b60050bc7baEric Anholt      if (entry->var == var)
713a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt	 return entry;
71484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
71584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
716a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt   return NULL;
717a9b619bb3b96a90d14650771dedaf1db840d70a6Eric Anholt}
71884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
71984771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
72084771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_variable *ir)
72184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
7224a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   if (strcmp(ir->name, "gl_FragCoord") == 0) {
7234a962170d7cf4243d6ae156fca20a6167388925dEric Anholt      struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
7244a962170d7cf4243d6ae156fca20a6167388925dEric Anholt
7254a962170d7cf4243d6ae156fca20a6167388925dEric Anholt      fp->OriginUpperLeft = ir->origin_upper_left;
7264a962170d7cf4243d6ae156fca20a6167388925dEric Anholt      fp->PixelCenterInteger = ir->pixel_center_integer;
7274a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   }
7286c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7296c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt   if (ir->mode == ir_var_uniform && strncmp(ir->name, "gl_", 3) == 0) {
7306c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      unsigned int i;
731daacaac3c8b91c87ab2cfc2d9f821474552b8181Eric Anholt      const struct gl_builtin_uniform_desc *statevar;
7326c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
733daacaac3c8b91c87ab2cfc2d9f821474552b8181Eric Anholt      for (i = 0; _mesa_builtin_uniform_desc[i].name; i++) {
734daacaac3c8b91c87ab2cfc2d9f821474552b8181Eric Anholt	 if (strcmp(ir->name, _mesa_builtin_uniform_desc[i].name) == 0)
7356c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    break;
7366c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      }
7376c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
738daacaac3c8b91c87ab2cfc2d9f821474552b8181Eric Anholt      if (!_mesa_builtin_uniform_desc[i].name) {
7396c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 fail_link(this->shader_program,
7406c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt		   "Failed to find builtin uniform `%s'\n", ir->name);
7416c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 return;
7426c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      }
7436c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
744daacaac3c8b91c87ab2cfc2d9f821474552b8181Eric Anholt      statevar = &_mesa_builtin_uniform_desc[i];
7456c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7466c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      int array_count;
7476c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      if (ir->type->is_array()) {
7486c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 array_count = ir->type->length;
7496c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      } else {
7506c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 array_count = 1;
7516c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      }
7526c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7536c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      /* Check if this statevar's setup in the STATE file exactly
7546c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt       * matches how we'll want to reference it as a
7556c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt       * struct/array/whatever.  If not, then we need to move it into
7566c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt       * temporary storage and hope that it'll get copy-propagated
7576c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt       * out.
7586c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt       */
7596c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      for (i = 0; i < statevar->num_elements; i++) {
7606c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 if (statevar->elements[i].swizzle != SWIZZLE_XYZW) {
7616c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    break;
7626c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 }
7636c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      }
7646c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7656c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      struct variable_storage *storage;
7666c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      ir_to_mesa_dst_reg dst;
7676c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      if (i == statevar->num_elements) {
7686c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 /* We'll set the index later. */
7696c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 storage = new(mem_ctx) variable_storage(ir, PROGRAM_STATE_VAR, -1);
7706c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 this->variables.push_tail(storage);
7716c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7726c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 dst = ir_to_mesa_undef_dst;
7736c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      } else {
7746c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 storage = new(mem_ctx) variable_storage(ir, PROGRAM_TEMPORARY,
7756c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt						 this->next_temp);
7766c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 this->variables.push_tail(storage);
7776c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 this->next_temp += type_size(ir->type);
7786c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7796c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 dst = ir_to_mesa_dst_reg_from_src(ir_to_mesa_src_reg(PROGRAM_TEMPORARY,
7806c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt							      storage->index,
7816c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt							      NULL));
7826c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      }
7836c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7846c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7856c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      for (int a = 0; a < array_count; a++) {
7866c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 for (unsigned int i = 0; i < statevar->num_elements; i++) {
787daacaac3c8b91c87ab2cfc2d9f821474552b8181Eric Anholt	    struct gl_builtin_uniform_element *element = &statevar->elements[i];
7886c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    int tokens[STATE_LENGTH];
7896c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7906c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    memcpy(tokens, element->tokens, sizeof(element->tokens));
7916c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    if (ir->type->is_array()) {
7926c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       tokens[1] = a;
7936c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    }
7946c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7956c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    int index = _mesa_add_state_reference(this->prog->Parameters,
7966c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt						  (gl_state_index *)tokens);
7976c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt
7986c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    if (storage->file == PROGRAM_STATE_VAR) {
7996c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       if (storage->index == -1) {
8006c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt		  storage->index = index;
8016c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       } else {
8027545514fb65f807fa2c888954f0ac153a918e2ceBrian Paul		  assert(index ==
8037545514fb65f807fa2c888954f0ac153a918e2ceBrian Paul                         (int)(storage->index + a * statevar->num_elements + i));
8046c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       }
8056c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    } else {
8066c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       ir_to_mesa_src_reg src(PROGRAM_STATE_VAR, index, NULL);
8076c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       src.swizzle = element->swizzle;
8086c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       ir_to_mesa_emit_op1(ir, OPCODE_MOV, dst, src);
8096c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       /* even a float takes up a whole vec4 reg in a struct/array. */
8106c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	       dst.index++;
8116c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	    }
8126c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 }
8136c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      }
8146c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      if (storage->file == PROGRAM_TEMPORARY &&
8156c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	  dst.index != storage->index + type_size(ir->type)) {
8166c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt	 fail_link(this->shader_program,
8176c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt		   "failed to load builtin uniform `%s'  (%d/%d regs loaded)\n",
8186c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt		   ir->name, dst.index - storage->index,
8196c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt		   type_size(ir->type));
8206c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt      }
8216c0ba32fd1466e8c1700acab3003dc1fe1deb337Eric Anholt   }
82284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
82384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
82484771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
82584771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_loop *ir)
82684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
8270f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   ir_dereference_variable *counter = NULL;
8280f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8290f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   if (ir->counter != NULL)
8300f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      counter = new(ir) ir_dereference_variable(ir->counter);
8310f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8320f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   if (ir->from != NULL) {
8330f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      assert(ir->counter != NULL);
8340f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8350f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      ir_assignment *a = new(ir) ir_assignment(counter, ir->from, NULL);
8360f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8370f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      a->accept(this);
8380f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      delete a;
8390f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   }
84084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
841021222c6a872ca2eef770ebadb8754f659775204Eric Anholt   ir_to_mesa_emit_op0(NULL, OPCODE_BGNLOOP);
8420f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8430f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   if (ir->to) {
8440f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      ir_expression *e =
8450f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick	 new(ir) ir_expression(ir->cmp, glsl_type::bool_type,
8460f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick			       counter, ir->to);
8470f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      ir_if *if_stmt =  new(ir) ir_if(e);
8480f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8490f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      ir_loop_jump *brk = new(ir) ir_loop_jump(ir_loop_jump::jump_break);
8500f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8510f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      if_stmt->then_instructions.push_tail(brk);
8520f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8530f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      if_stmt->accept(this);
8540f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8550f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      delete if_stmt;
8560f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      delete e;
8570f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      delete brk;
8580f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   }
8590f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
86064fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   visit_exec_list(&ir->body_instructions, this);
8610f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8620f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   if (ir->increment) {
8630f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      ir_expression *e =
8640f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick	 new(ir) ir_expression(ir_binop_add, counter->type,
8650f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick			       counter, ir->increment);
8660f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8670f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      ir_assignment *a = new(ir) ir_assignment(counter, e, NULL);
8680f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
8690f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      a->accept(this);
8700f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      delete a;
8710f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick      delete e;
8720f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick   }
8730f4f8c73644a9cc436500d605413207c44cfb4eeIan Romanick
874021222c6a872ca2eef770ebadb8754f659775204Eric Anholt   ir_to_mesa_emit_op0(NULL, OPCODE_ENDLOOP);
87584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
87684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
87784771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
87884771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_loop_jump *ir)
87984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
88064fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   switch (ir->mode) {
88164fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   case ir_loop_jump::jump_break:
882021222c6a872ca2eef770ebadb8754f659775204Eric Anholt      ir_to_mesa_emit_op0(NULL, OPCODE_BRK);
88364fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      break;
88464fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   case ir_loop_jump::jump_continue:
885021222c6a872ca2eef770ebadb8754f659775204Eric Anholt      ir_to_mesa_emit_op0(NULL, OPCODE_CONT);
88664fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      break;
88764fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   }
88884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
88984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
89084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
89184771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
89284771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_function_signature *ir)
89384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
89484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   assert(0);
89584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   (void)ir;
89684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
89784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
89884771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
89984771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_function *ir)
90084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
90184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   /* Ignore function bodies other than main() -- we shouldn't see calls to
90284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt    * them since they should all be inlined before we get to ir_to_mesa.
90384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt    */
90484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   if (strcmp(ir->name, "main") == 0) {
90584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      const ir_function_signature *sig;
90684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      exec_list empty;
90784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
90884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      sig = ir->matching_signature(&empty);
90984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
91084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      assert(sig);
91184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
91284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      foreach_iter(exec_list_iterator, iter, sig->body) {
91384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 ir_instruction *ir = (ir_instruction *)iter.get();
91484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
91584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 ir->accept(this);
91684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      }
91784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
91884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
91984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
9203f08989267d9cdd944787fcf7a300c6f1f84462cEric AnholtGLboolean
9213f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholtir_to_mesa_visitor::try_emit_mad(ir_expression *ir, int mul_operand)
9223f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt{
9233f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   int nonmul_operand = 1 - mul_operand;
9243f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   ir_to_mesa_src_reg a, b, c;
9253f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt
9263f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   ir_expression *expr = ir->operands[mul_operand]->as_expression();
9273f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   if (!expr || expr->operation != ir_binop_mul)
9283f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt      return false;
9293f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt
9303f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   expr->operands[0]->accept(this);
9313f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   a = this->result;
9323f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   expr->operands[1]->accept(this);
9333f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   b = this->result;
9343f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   ir->operands[nonmul_operand]->accept(this);
9353f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   c = this->result;
9363f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt
9373f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   this->result = get_temp(ir->type);
9383f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   ir_to_mesa_emit_op3(ir, OPCODE_MAD,
9393f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt		       ir_to_mesa_dst_reg_from_src(this->result), a, b, c);
9403f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt
9413f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   return true;
9423f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt}
9433f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt
944ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric AnholtGLboolean
945ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholtir_to_mesa_visitor::try_emit_sat(ir_expression *ir)
946ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt{
947ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   /* Saturates were only introduced to vertex programs in
948ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt    * NV_vertex_program3, so don't give them to drivers in the VP.
949ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt    */
950ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   if (this->prog->Target == GL_VERTEX_PROGRAM_ARB)
951ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt      return false;
952ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt
953ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   ir_rvalue *sat_src = ir->as_rvalue_to_saturate();
954ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   if (!sat_src)
955ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt      return false;
956ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt
957ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   sat_src->accept(this);
958ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   ir_to_mesa_src_reg src = this->result;
959ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt
960ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   this->result = get_temp(ir->type);
961ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   ir_to_mesa_instruction *inst;
962ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   inst = ir_to_mesa_emit_op1(ir, OPCODE_MOV,
963ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt			      ir_to_mesa_dst_reg_from_src(this->result),
964ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt			      src);
965ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   inst->saturate = true;
966ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt
967ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   return true;
968ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt}
969ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt
97084771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
971f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholtir_to_mesa_visitor::reladdr_to_temp(ir_instruction *ir,
972f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt				    ir_to_mesa_src_reg *reg, int *num_reladdr)
973f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt{
974f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   if (!reg->reladdr)
975f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt      return;
976f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
977f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   ir_to_mesa_emit_op1(ir, OPCODE_ARL, ir_to_mesa_address_reg, *reg->reladdr);
978f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
979f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   if (*num_reladdr != 1) {
980f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt      ir_to_mesa_src_reg temp = get_temp(glsl_type::vec4_type);
981f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
982f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_MOV,
983f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt			  ir_to_mesa_dst_reg_from_src(temp), *reg);
984f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt      *reg = temp;
985f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   }
986f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
987f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   (*num_reladdr)--;
988f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt}
989f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
990f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholtvoid
99111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanickir_to_mesa_visitor::emit_swz(ir_expression *ir)
99211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick{
99311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   /* Assume that the vector operator is in a form compatible with OPCODE_SWZ.
99411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    * This means that each of the operands is either an immediate value of -1,
99511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    * 0, or 1, or is a component from one source register (possibly with
99611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    * negation).
99711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    */
99811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   uint8_t components[4] = { 0 };
99911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   bool negate[4] = { false };
100011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   ir_variable *var = NULL;
100111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
100211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   for (unsigned i = 0; i < ir->type->vector_elements; i++) {
100311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      ir_rvalue *op = ir->operands[i];
100411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
100511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      assert(op->type->is_scalar());
100611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
100711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      while (op != NULL) {
100811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 switch (op->ir_type) {
100911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 case ir_type_constant: {
101011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
101111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    assert(op->type->is_scalar());
101211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
101311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    const ir_constant *const c = op->as_constant();
101411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    if (c->is_one()) {
101511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	       components[i] = SWIZZLE_ONE;
101611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    } else if (c->is_zero()) {
101711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	       components[i] = SWIZZLE_ZERO;
101811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    } else if (c->is_negative_one()) {
101911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	       components[i] = SWIZZLE_ONE;
102011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	       negate[i] = true;
102111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    } else {
102211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	       assert(!"SWZ constant must be 0.0 or 1.0.");
102311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    }
102411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
102511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    op = NULL;
102611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    break;
102711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 }
102811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
102911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 case ir_type_dereference_variable: {
103011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    ir_dereference_variable *const deref =
103111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	       (ir_dereference_variable *) op;
103211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
103311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    assert((var == NULL) || (deref->var == var));
103411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    components[i] = SWIZZLE_X;
103511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    var = deref->var;
103611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    op = NULL;
103711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    break;
103811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 }
103911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
104011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 case ir_type_expression: {
104111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    ir_expression *const expr = (ir_expression *) op;
104211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
104311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    assert(expr->operation == ir_unop_neg);
104411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    negate[i] = true;
104511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
104611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    op = expr->operands[0];
104711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    break;
104811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 }
104911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
105011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 case ir_type_swizzle: {
105111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    ir_swizzle *const swiz = (ir_swizzle *) op;
105211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
105311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    components[i] = swiz->mask.x;
105411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    op = swiz->val;
105511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    break;
105611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 }
105711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
105811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 default:
105911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    assert(!"Should not get here.");
106011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	    return;
106111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 }
106211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      }
106311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   }
106411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
106511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   assert(var != NULL);
106611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
106711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   ir_dereference_variable *const deref =
106811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      new(mem_ctx) ir_dereference_variable(var);
106911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
107011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   this->result.file = PROGRAM_UNDEFINED;
107111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   deref->accept(this);
107211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   if (this->result.file == PROGRAM_UNDEFINED) {
107311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      ir_print_visitor v;
107411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      printf("Failed to get tree for expression operand:\n");
107511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      deref->accept(&v);
107611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      exit(1);
107711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   }
107811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
107911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   ir_to_mesa_src_reg src;
108011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
108111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   src = this->result;
108211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   src.swizzle = MAKE_SWIZZLE4(components[0],
108311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick			       components[1],
108411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick			       components[2],
108511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick			       components[3]);
108611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   src.negate = ((unsigned(negate[0]) << 0)
108711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick		 | (unsigned(negate[1]) << 1)
108811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick		 | (unsigned(negate[2]) << 2)
108911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick		 | (unsigned(negate[3]) << 3));
109011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
109111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   /* Storage for our result.  Ideally for an assignment we'd be using the
109211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    * actual storage for the result here, instead.
109311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    */
109411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   const ir_to_mesa_src_reg result_src = get_temp(ir->type);
109511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   ir_to_mesa_dst_reg result_dst = ir_to_mesa_dst_reg_from_src(result_src);
109611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
109711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   /* Limit writes to the channels that will be used by result_src later.
109811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    * This does limit this temp's use as a temporary for multi-instruction
109911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    * sequences.
110011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick    */
110111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   result_dst.writemask = (1 << ir->type->vector_elements) - 1;
110211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
110311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   ir_to_mesa_emit_op1(ir, OPCODE_SWZ, result_dst, src);
110411d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   this->result = result_src;
110511d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick}
110611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
110711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanickvoid
110884771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_expression *ir)
110984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
111084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   unsigned int operand;
1111fc92e87b9757eda01caf0bb3e2c31b1dbbd73aa0Ian Romanick   struct ir_to_mesa_src_reg op[Elements(ir->operands)];
11120161515c395c44233529c8d51f823b60050bc7baEric Anholt   struct ir_to_mesa_src_reg result_src;
11130161515c395c44233529c8d51f823b60050bc7baEric Anholt   struct ir_to_mesa_dst_reg result_dst;
111484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
11153f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   /* Quick peephole: Emit OPCODE_MAD(a, b, c) instead of ADD(MUL(a, b), c)
11163f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt    */
11173f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   if (ir->operation == ir_binop_add) {
11183f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt      if (try_emit_mad(ir, 1))
11193f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt	 return;
11203f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt      if (try_emit_mad(ir, 0))
11213f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt	 return;
11223f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt   }
1123ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt   if (try_emit_sat(ir))
1124ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt      return;
11253f08989267d9cdd944787fcf7a300c6f1f84462cEric Anholt
112611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   if (ir->operation == ir_quadop_vector) {
112711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      this->emit_swz(ir);
112811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      return;
112911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   }
113011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
113184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   for (operand = 0; operand < ir->get_num_operands(); operand++) {
11320161515c395c44233529c8d51f823b60050bc7baEric Anholt      this->result.file = PROGRAM_UNDEFINED;
113384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      ir->operands[operand]->accept(this);
11340161515c395c44233529c8d51f823b60050bc7baEric Anholt      if (this->result.file == PROGRAM_UNDEFINED) {
113584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 ir_print_visitor v;
113684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 printf("Failed to get tree for expression operand:\n");
113784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 ir->operands[operand]->accept(&v);
113884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 exit(1);
113984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      }
114084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      op[operand] = this->result;
11418364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt
11424ca07882afad656bf0a0f56b68038ce556bceec4Eric Anholt      /* Matrix expression operands should have been broken down to vector
11434ca07882afad656bf0a0f56b68038ce556bceec4Eric Anholt       * operations already.
11444ca07882afad656bf0a0f56b68038ce556bceec4Eric Anholt       */
11454ca07882afad656bf0a0f56b68038ce556bceec4Eric Anholt      assert(!ir->operands[operand]->type->is_matrix());
114684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
114784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
1148eaa6bf59db68f97fa32c3395eb4aa6e589f34b26Eric Anholt   int vector_elements = ir->operands[0]->type->vector_elements;
1149eaa6bf59db68f97fa32c3395eb4aa6e589f34b26Eric Anholt   if (ir->operands[1]) {
1150eaa6bf59db68f97fa32c3395eb4aa6e589f34b26Eric Anholt      vector_elements = MAX2(vector_elements,
1151eaa6bf59db68f97fa32c3395eb4aa6e589f34b26Eric Anholt			     ir->operands[1]->type->vector_elements);
1152eaa6bf59db68f97fa32c3395eb4aa6e589f34b26Eric Anholt   }
1153eaa6bf59db68f97fa32c3395eb4aa6e589f34b26Eric Anholt
11540161515c395c44233529c8d51f823b60050bc7baEric Anholt   this->result.file = PROGRAM_UNDEFINED;
11550161515c395c44233529c8d51f823b60050bc7baEric Anholt
11560161515c395c44233529c8d51f823b60050bc7baEric Anholt   /* Storage for our result.  Ideally for an assignment we'd be using
11570161515c395c44233529c8d51f823b60050bc7baEric Anholt    * the actual storage for the result here, instead.
11580161515c395c44233529c8d51f823b60050bc7baEric Anholt    */
11598364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt   result_src = get_temp(ir->type);
11600161515c395c44233529c8d51f823b60050bc7baEric Anholt   /* convenience for the emit functions below. */
11610161515c395c44233529c8d51f823b60050bc7baEric Anholt   result_dst = ir_to_mesa_dst_reg_from_src(result_src);
11629cd8cad9f3dc4774366193acbfc5ab22198096e7Eric Anholt   /* Limit writes to the channels that will be used by result_src later.
11639cd8cad9f3dc4774366193acbfc5ab22198096e7Eric Anholt    * This does limit this temp's use as a temporary for multi-instruction
11649cd8cad9f3dc4774366193acbfc5ab22198096e7Eric Anholt    * sequences.
11659cd8cad9f3dc4774366193acbfc5ab22198096e7Eric Anholt    */
11669cd8cad9f3dc4774366193acbfc5ab22198096e7Eric Anholt   result_dst.writemask = (1 << ir->type->vector_elements) - 1;
116784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
116884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   switch (ir->operation) {
11691d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt   case ir_unop_logic_not:
1170f4bd7f262e43301158f059af90176a476ffdbf60Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SEQ, result_dst,
1171f4bd7f262e43301158f059af90176a476ffdbf60Eric Anholt			  op[0], src_reg_for_float(0.0));
11721d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt      break;
1173c45b615a379e5b9cbcf951f9d738a1be77a5964bEric Anholt   case ir_unop_neg:
11740161515c395c44233529c8d51f823b60050bc7baEric Anholt      op[0].negate = ~op[0].negate;
11750161515c395c44233529c8d51f823b60050bc7baEric Anholt      result_src = op[0];
1176c45b615a379e5b9cbcf951f9d738a1be77a5964bEric Anholt      break;
1177524745bc55dd23c612aebdb545125727bfb16e4dEric Anholt   case ir_unop_abs:
1178524745bc55dd23c612aebdb545125727bfb16e4dEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_ABS, result_dst, op[0]);
1179524745bc55dd23c612aebdb545125727bfb16e4dEric Anholt      break;
11803acd92a91f3e799b9f839a074f4d76a0e88d71feEric Anholt   case ir_unop_sign:
11813acd92a91f3e799b9f839a074f4d76a0e88d71feEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_SSG, result_dst, op[0]);
11823acd92a91f3e799b9f839a074f4d76a0e88d71feEric Anholt      break;
11838761fcc2bf964d26c70229c712ce446dbe504ab7Eric Anholt   case ir_unop_rcp:
11848761fcc2bf964d26c70229c712ce446dbe504ab7Eric Anholt      ir_to_mesa_emit_scalar_op1(ir, OPCODE_RCP, result_dst, op[0]);
11858761fcc2bf964d26c70229c712ce446dbe504ab7Eric Anholt      break;
1186524745bc55dd23c612aebdb545125727bfb16e4dEric Anholt
11878c29a1d84d738cfddf16d5f013876ee2cca96a81Eric Anholt   case ir_unop_exp2:
11880161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_scalar_op1(ir, OPCODE_EX2, result_dst, op[0]);
11898c29a1d84d738cfddf16d5f013876ee2cca96a81Eric Anholt      break;
1190bc4034b243975089c06c4415d4e26edaaaec7a46Eric Anholt   case ir_unop_exp:
11918c29a1d84d738cfddf16d5f013876ee2cca96a81Eric Anholt   case ir_unop_log:
1192bc4034b243975089c06c4415d4e26edaaaec7a46Eric Anholt      assert(!"not reached: should be handled by ir_explog_to_explog2");
11938c29a1d84d738cfddf16d5f013876ee2cca96a81Eric Anholt      break;
11948c29a1d84d738cfddf16d5f013876ee2cca96a81Eric Anholt   case ir_unop_log2:
11950161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_scalar_op1(ir, OPCODE_LG2, result_dst, op[0]);
11968c29a1d84d738cfddf16d5f013876ee2cca96a81Eric Anholt      break;
11973c5979565facebc82000a611b991d2977b8e9bbfEric Anholt   case ir_unop_sin:
11980161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_scalar_op1(ir, OPCODE_SIN, result_dst, op[0]);
11993c5979565facebc82000a611b991d2977b8e9bbfEric Anholt      break;
12003c5979565facebc82000a611b991d2977b8e9bbfEric Anholt   case ir_unop_cos:
12010161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_scalar_op1(ir, OPCODE_COS, result_dst, op[0]);
12023c5979565facebc82000a611b991d2977b8e9bbfEric Anholt      break;
1203f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   case ir_unop_sin_reduced:
1204f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      emit_scs(ir, OPCODE_SIN, result_dst, op[0]);
1205f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      break;
1206f2616e56de8a48360cae8f269727b58490555f4dIan Romanick   case ir_unop_cos_reduced:
1207f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      emit_scs(ir, OPCODE_COS, result_dst, op[0]);
1208f2616e56de8a48360cae8f269727b58490555f4dIan Romanick      break;
1209ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt
1210ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt   case ir_unop_dFdx:
1211ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_DDX, result_dst, op[0]);
1212ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt      break;
1213ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt   case ir_unop_dFdy:
1214ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_DDY, result_dst, op[0]);
1215ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt      break;
1216ba9bd708cb3480817e18cc47e57d83a4e4c305bbEric Anholt
12173a5ce85cfa4914711e56c8cf831699242618928eIan Romanick   case ir_unop_noise: {
12183a5ce85cfa4914711e56c8cf831699242618928eIan Romanick      const enum prog_opcode opcode =
12193a5ce85cfa4914711e56c8cf831699242618928eIan Romanick	 prog_opcode(OPCODE_NOISE1
12203a5ce85cfa4914711e56c8cf831699242618928eIan Romanick		     + (ir->operands[0]->type->vector_elements) - 1);
12213a5ce85cfa4914711e56c8cf831699242618928eIan Romanick      assert((opcode >= OPCODE_NOISE1) && (opcode <= OPCODE_NOISE4));
12223a5ce85cfa4914711e56c8cf831699242618928eIan Romanick
12233a5ce85cfa4914711e56c8cf831699242618928eIan Romanick      ir_to_mesa_emit_op1(ir, opcode, result_dst, op[0]);
12243a5ce85cfa4914711e56c8cf831699242618928eIan Romanick      break;
12253a5ce85cfa4914711e56c8cf831699242618928eIan Romanick   }
12263a5ce85cfa4914711e56c8cf831699242618928eIan Romanick
122784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   case ir_binop_add:
12287b48843ecd6690902e4f3bd709a041133b7fb540Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_ADD, result_dst, op[0], op[1]);
122984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      break;
123084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   case ir_binop_sub:
12317b48843ecd6690902e4f3bd709a041133b7fb540Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SUB, result_dst, op[0], op[1]);
123284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      break;
12339b68b88e43c424439d425534ef280ee7a9406a1bEric Anholt
123484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   case ir_binop_mul:
12354ca07882afad656bf0a0f56b68038ce556bceec4Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_MUL, result_dst, op[0], op[1]);
123684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      break;
123784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   case ir_binop_div:
12389a0e421983edc31371440c08687fa2bb2207924dEric Anholt      assert(!"not reached: should be handled by ir_div_to_mul_rcp");
1239411fb36b7cee223e090b4b9ef9bc14e058201a68Eric Anholt   case ir_binop_mod:
1240411fb36b7cee223e090b4b9ef9bc14e058201a68Eric Anholt      assert(!"ir_binop_mod should have been converted to b * fract(a/b)");
1241411fb36b7cee223e090b4b9ef9bc14e058201a68Eric Anholt      break;
124238315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt
124338315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt   case ir_binop_less:
1244f4bd7f262e43301158f059af90176a476ffdbf60Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SLT, result_dst, op[0], op[1]);
124538315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt      break;
124638315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt   case ir_binop_greater:
1247f4bd7f262e43301158f059af90176a476ffdbf60Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SGT, result_dst, op[0], op[1]);
124838315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt      break;
124938315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt   case ir_binop_lequal:
1250f4bd7f262e43301158f059af90176a476ffdbf60Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SLE, result_dst, op[0], op[1]);
125138315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt      break;
125238315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt   case ir_binop_gequal:
1253f4bd7f262e43301158f059af90176a476ffdbf60Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SGE, result_dst, op[0], op[1]);
125438315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt      break;
125538315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt   case ir_binop_equal:
12564dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_to_mesa_emit_op2(ir, OPCODE_SEQ, result_dst, op[0], op[1]);
12574dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      break;
12584dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri   case ir_binop_nequal:
12594dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_to_mesa_emit_op2(ir, OPCODE_SNE, result_dst, op[0], op[1]);
12604dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      break;
12614dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri   case ir_binop_all_equal:
12626992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      /* "==" operator producing a scalar boolean. */
12636992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      if (ir->operands[0]->type->is_vector() ||
12646992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	  ir->operands[1]->type->is_vector()) {
12656992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_src_reg temp = get_temp(glsl_type::vec4_type);
12666992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_SNE,
12676992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt			     ir_to_mesa_dst_reg_from_src(temp), op[0], op[1]);
1268ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick	 ir_to_mesa_emit_dp(ir, result_dst, temp, temp, vector_elements);
12696992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_SEQ,
12706992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt			     result_dst, result_src, src_reg_for_float(0.0));
12716992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      } else {
12726992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_SEQ, result_dst, op[0], op[1]);
12736992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      }
127438315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt      break;
12754dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri   case ir_binop_any_nequal:
12766992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      /* "!=" operator producing a scalar boolean. */
12776992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      if (ir->operands[0]->type->is_vector() ||
12786992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	  ir->operands[1]->type->is_vector()) {
12796992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_src_reg temp = get_temp(glsl_type::vec4_type);
12806992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_SNE,
12816992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt			     ir_to_mesa_dst_reg_from_src(temp), op[0], op[1]);
1282ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick	 ir_to_mesa_emit_dp(ir, result_dst, temp, temp, vector_elements);
12836992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_SNE,
12846992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt			     result_dst, result_src, src_reg_for_float(0.0));
12856992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      } else {
12866992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_SNE, result_dst, op[0], op[1]);
12876992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      }
12886992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt      break;
12895e9ac94cc44ef4f97063d7b696411b2a4be16f36Eric Anholt
12905e9ac94cc44ef4f97063d7b696411b2a4be16f36Eric Anholt   case ir_unop_any:
1291ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick      assert(ir->operands[0]->type->is_vector());
1292ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick      ir_to_mesa_emit_dp(ir, result_dst, op[0], op[0],
1293ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick			 ir->operands[0]->type->vector_elements);
12945e9ac94cc44ef4f97063d7b696411b2a4be16f36Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SNE,
12955e9ac94cc44ef4f97063d7b696411b2a4be16f36Eric Anholt			  result_dst, result_src, src_reg_for_float(0.0));
12965e9ac94cc44ef4f97063d7b696411b2a4be16f36Eric Anholt      break;
12975e9ac94cc44ef4f97063d7b696411b2a4be16f36Eric Anholt
12986992c3c3739dad249e8c396057d5cbeedcdf91deEric Anholt   case ir_binop_logic_xor:
1299f4bd7f262e43301158f059af90176a476ffdbf60Eric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SNE, result_dst, op[0], op[1]);
130038315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt      break;
130138315079571512dc5b502d9522d7a8c3eaf2cc8fEric Anholt
13024380099c98119611ceee684669d00be26195c7d7Eric Anholt   case ir_binop_logic_or:
13030161515c395c44233529c8d51f823b60050bc7baEric Anholt      /* This could be a saturated add and skip the SNE. */
13040161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_ADD,
13050161515c395c44233529c8d51f823b60050bc7baEric Anholt			  result_dst,
13060161515c395c44233529c8d51f823b60050bc7baEric Anholt			  op[0], op[1]);
13070161515c395c44233529c8d51f823b60050bc7baEric Anholt
13080161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SNE,
13090161515c395c44233529c8d51f823b60050bc7baEric Anholt			  result_dst,
13100161515c395c44233529c8d51f823b60050bc7baEric Anholt			  result_src, src_reg_for_float(0.0));
13114380099c98119611ceee684669d00be26195c7d7Eric Anholt      break;
13124380099c98119611ceee684669d00be26195c7d7Eric Anholt
13134380099c98119611ceee684669d00be26195c7d7Eric Anholt   case ir_binop_logic_and:
13144380099c98119611ceee684669d00be26195c7d7Eric Anholt      /* the bool args are stored as float 0.0 or 1.0, so "mul" gives us "and". */
13150161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_MUL,
13160161515c395c44233529c8d51f823b60050bc7baEric Anholt			  result_dst,
13170161515c395c44233529c8d51f823b60050bc7baEric Anholt			  op[0], op[1]);
13184380099c98119611ceee684669d00be26195c7d7Eric Anholt      break;
13194380099c98119611ceee684669d00be26195c7d7Eric Anholt
132084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   case ir_binop_dot:
1321ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick      assert(ir->operands[0]->type->is_vector());
1322ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick      assert(ir->operands[0]->type == ir->operands[1]->type);
1323ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick      ir_to_mesa_emit_dp(ir, result_dst, op[0], op[1],
1324ad8cb131d8f73dee75e7be39dbc004783cd7f350Ian Romanick			 ir->operands[0]->type->vector_elements);
132584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      break;
13269be7f638130f46a9df2bfbcd4a03b36de9e4f3aaEric Anholt
132784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   case ir_unop_sqrt:
13284f189b3bf57a6500953dac49105f160af5fa6468Marek Olšák      /* sqrt(x) = x * rsq(x). */
13290161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_scalar_op1(ir, OPCODE_RSQ, result_dst, op[0]);
13304f189b3bf57a6500953dac49105f160af5fa6468Marek Olšák      ir_to_mesa_emit_op2(ir, OPCODE_MUL, result_dst, result_src, op[0]);
1331c9039fdb167865547dc9b3828d69b99209344999Brian Paul      /* For incoming channels <= 0, set the result to 0. */
1332c9039fdb167865547dc9b3828d69b99209344999Brian Paul      op[0].negate = ~op[0].negate;
13338f62ad6d0ff3c11808739c74441f82f6f12485d6Eric Anholt      ir_to_mesa_emit_op3(ir, OPCODE_CMP, result_dst,
1334c9039fdb167865547dc9b3828d69b99209344999Brian Paul			  op[0], result_src, src_reg_for_float(0.0));
133584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      break;
1336878740bedf418e5bf42ed6d350c938d29abaaf25Eric Anholt   case ir_unop_rsq:
13370161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_scalar_op1(ir, OPCODE_RSQ, result_dst, op[0]);
1338878740bedf418e5bf42ed6d350c938d29abaaf25Eric Anholt      break;
133950ad96ebce6ea19b414a02d2d45f0b0c73586abfEric Anholt   case ir_unop_i2f:
1340d6ebe9b16b25f25ba763baf3738addc50676d5d0Eric Anholt   case ir_unop_b2f:
1341d6ebe9b16b25f25ba763baf3738addc50676d5d0Eric Anholt   case ir_unop_b2i:
1342423a75c5d607a33cb5fe76a0a9c903cccc645fa7Eric Anholt      /* Mesa IR lacks types, ints are stored as truncated floats. */
13430161515c395c44233529c8d51f823b60050bc7baEric Anholt      result_src = op[0];
134450ad96ebce6ea19b414a02d2d45f0b0c73586abfEric Anholt      break;
1345423a75c5d607a33cb5fe76a0a9c903cccc645fa7Eric Anholt   case ir_unop_f2i:
13460161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_TRUNC, result_dst, op[0]);
1347423a75c5d607a33cb5fe76a0a9c903cccc645fa7Eric Anholt      break;
13481d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt   case ir_unop_f2b:
1349411fb36b7cee223e090b4b9ef9bc14e058201a68Eric Anholt   case ir_unop_i2b:
13500161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_SNE, result_dst,
135166afcb560771b6ba6ad668156e9f442e86b9a7a2Eric Anholt			  op[0], src_reg_for_float(0.0));
13521d20862c8a0e100e43458f01217c047c76da05f3Eric Anholt      break;
1353c2014f03e8d6b7e21e2d0c31270ced04e1025653Eric Anholt   case ir_unop_trunc:
13540161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_TRUNC, result_dst, op[0]);
1355c2014f03e8d6b7e21e2d0c31270ced04e1025653Eric Anholt      break;
1356c2014f03e8d6b7e21e2d0c31270ced04e1025653Eric Anholt   case ir_unop_ceil:
13570161515c395c44233529c8d51f823b60050bc7baEric Anholt      op[0].negate = ~op[0].negate;
13580161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_FLR, result_dst, op[0]);
13590161515c395c44233529c8d51f823b60050bc7baEric Anholt      result_src.negate = ~result_src.negate;
1360c2014f03e8d6b7e21e2d0c31270ced04e1025653Eric Anholt      break;
1361c2014f03e8d6b7e21e2d0c31270ced04e1025653Eric Anholt   case ir_unop_floor:
13620161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_FLR, result_dst, op[0]);
1363c2014f03e8d6b7e21e2d0c31270ced04e1025653Eric Anholt      break;
1364d925c9173009e9e5d48df30b30aaef22753183aaEric Anholt   case ir_unop_fract:
1365d925c9173009e9e5d48df30b30aaef22753183aaEric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_FRC, result_dst, op[0]);
1366d925c9173009e9e5d48df30b30aaef22753183aaEric Anholt      break;
1367d925c9173009e9e5d48df30b30aaef22753183aaEric Anholt
1368c23c6c773a5c79b458e52ff42bd9f431c87d4036Eric Anholt   case ir_binop_min:
13690161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_MIN, result_dst, op[0], op[1]);
1370c23c6c773a5c79b458e52ff42bd9f431c87d4036Eric Anholt      break;
1371c23c6c773a5c79b458e52ff42bd9f431c87d4036Eric Anholt   case ir_binop_max:
13720161515c395c44233529c8d51f823b60050bc7baEric Anholt      ir_to_mesa_emit_op2(ir, OPCODE_MAX, result_dst, op[0], op[1]);
1373c23c6c773a5c79b458e52ff42bd9f431c87d4036Eric Anholt      break;
1374904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt   case ir_binop_pow:
1375904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      ir_to_mesa_emit_scalar_op2(ir, OPCODE_POW, result_dst, op[0], op[1]);
1376904b5bfe9986a297dc71fe081ce0f2661d43b00bEric Anholt      break;
1377e64a4aaacbc682f24180dff3627b84861844476dEric Anholt
1378e64a4aaacbc682f24180dff3627b84861844476dEric Anholt   case ir_unop_bit_not:
1379e64a4aaacbc682f24180dff3627b84861844476dEric Anholt   case ir_unop_u2f:
1380e64a4aaacbc682f24180dff3627b84861844476dEric Anholt   case ir_binop_lshift:
1381e64a4aaacbc682f24180dff3627b84861844476dEric Anholt   case ir_binop_rshift:
1382e64a4aaacbc682f24180dff3627b84861844476dEric Anholt   case ir_binop_bit_and:
1383e64a4aaacbc682f24180dff3627b84861844476dEric Anholt   case ir_binop_bit_xor:
1384e64a4aaacbc682f24180dff3627b84861844476dEric Anholt   case ir_binop_bit_or:
1385d85d25dd1f4fd281bd210ba6ba5135ba1e3b535fKenneth Graunke   case ir_unop_round_even:
1386e64a4aaacbc682f24180dff3627b84861844476dEric Anholt      assert(!"GLSL 1.30 features unsupported");
1387e64a4aaacbc682f24180dff3627b84861844476dEric Anholt      break;
138811d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
138911d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick   case ir_quadop_vector:
139011d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      /* This operation should have already been handled.
139111d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick       */
139211d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      assert(!"Should not get here.");
139311d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick      break;
139484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
1395b2ed4dd7b0270e469302965269007292117d02e2Eric Anholt
13960161515c395c44233529c8d51f823b60050bc7baEric Anholt   this->result = result_src;
139784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
139884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
139984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
140084771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
140184771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_swizzle *ir)
140284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
14030161515c395c44233529c8d51f823b60050bc7baEric Anholt   ir_to_mesa_src_reg src_reg;
140484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   int i;
140584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   int swizzle[4];
140684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
1407b07cc372c6360d0e59c84bb7586597f028c74b02Eric Anholt   /* Note that this is only swizzles in expressions, not those on the left
1408b07cc372c6360d0e59c84bb7586597f028c74b02Eric Anholt    * hand side of an assignment, which do write masking.  See ir_assignment
1409b07cc372c6360d0e59c84bb7586597f028c74b02Eric Anholt    * for that.
1410b07cc372c6360d0e59c84bb7586597f028c74b02Eric Anholt    */
141184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
141284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   ir->val->accept(this);
14134006424f5b5b3b189209faf03f2335f45c22b148Eric Anholt   src_reg = this->result;
14144006424f5b5b3b189209faf03f2335f45c22b148Eric Anholt   assert(src_reg.file != PROGRAM_UNDEFINED);
141584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
141684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   for (i = 0; i < 4; i++) {
141784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      if (i < ir->type->vector_elements) {
141884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 switch (i) {
141984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 case 0:
1420698b84444343189357ad252856d3c5493e47e4faEric Anholt	    swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.x);
142184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	    break;
142284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 case 1:
1423698b84444343189357ad252856d3c5493e47e4faEric Anholt	    swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.y);
142484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	    break;
142584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 case 2:
1426698b84444343189357ad252856d3c5493e47e4faEric Anholt	    swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.z);
142784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	    break;
142884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 case 3:
1429698b84444343189357ad252856d3c5493e47e4faEric Anholt	    swizzle[i] = GET_SWZ(src_reg.swizzle, ir->mask.w);
143084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	    break;
143184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 }
143284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      } else {
143384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	 /* If the type is smaller than a vec4, replicate the last
143484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	  * channel out.
143584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt	  */
1436698b84444343189357ad252856d3c5493e47e4faEric Anholt	 swizzle[i] = swizzle[ir->type->vector_elements - 1];
143784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      }
143884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
143984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
14400161515c395c44233529c8d51f823b60050bc7baEric Anholt   src_reg.swizzle = MAKE_SWIZZLE4(swizzle[0],
14410161515c395c44233529c8d51f823b60050bc7baEric Anholt				   swizzle[1],
14420161515c395c44233529c8d51f823b60050bc7baEric Anholt				   swizzle[2],
14430161515c395c44233529c8d51f823b60050bc7baEric Anholt				   swizzle[3]);
144484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
14450161515c395c44233529c8d51f823b60050bc7baEric Anholt   this->result = src_reg;
144684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
144784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
144884771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
144984771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_dereference_variable *ir)
145084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
1451b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt   variable_storage *entry = find_variable_storage(ir->var);
145284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
14538364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt   if (!entry) {
14548364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt      switch (ir->var->mode) {
14558364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt      case ir_var_uniform:
14560924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 entry = new(mem_ctx) variable_storage(ir->var, PROGRAM_UNIFORM,
14570924ba0c3496160a134d37cec800f902ae805b9cEric Anholt					       ir->var->location);
1458b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt	 this->variables.push_tail(entry);
14598364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt	 break;
14608364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt      case ir_var_in:
14618364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt      case ir_var_out:
14628364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt      case ir_var_inout:
1463a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	 /* The linker assigns locations for varyings and attributes,
1464a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	  * including deprecated builtins (like gl_Color), user-assign
1465a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	  * generic attributes (glBindVertexLocation), and
1466a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	  * user-defined varyings.
1467a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	  *
1468a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	  * FINISHME: We would hit this path for function arguments.  Fix!
1469f9ffccb06bcc90c862f20f8849b824022fbeebbfEric Anholt	  */
1470f9ffccb06bcc90c862f20f8849b824022fbeebbfEric Anholt	 assert(ir->var->location != -1);
1471a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	 if (ir->var->mode == ir_var_in ||
1472a0b3b9302978ab6d4db62f0c9b2b313ebc7ed0b4Eric Anholt	     ir->var->mode == ir_var_inout) {
1473b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt	    entry = new(mem_ctx) variable_storage(ir->var,
1474b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt						  PROGRAM_INPUT,
1475b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt						  ir->var->location);
1476edcb9c2b062693a5974aa74725f6259023fff794Eric Anholt
1477edcb9c2b062693a5974aa74725f6259023fff794Eric Anholt	    if (this->prog->Target == GL_VERTEX_PROGRAM_ARB &&
1478edcb9c2b062693a5974aa74725f6259023fff794Eric Anholt		ir->var->location >= VERT_ATTRIB_GENERIC0) {
1479edcb9c2b062693a5974aa74725f6259023fff794Eric Anholt	       _mesa_add_attribute(prog->Attributes,
1480edcb9c2b062693a5974aa74725f6259023fff794Eric Anholt				   ir->var->name,
1481658e25987fbec3b826f500baa6d4d936b9552b13Eric Anholt				   _mesa_sizeof_glsl_type(ir->var->type->gl_type),
1482edcb9c2b062693a5974aa74725f6259023fff794Eric Anholt				   ir->var->type->gl_type,
1483c64da87611823b4b53e93188f861f748a69936a3Eric Anholt				   ir->var->location - VERT_ATTRIB_GENERIC0);
1484edcb9c2b062693a5974aa74725f6259023fff794Eric Anholt	    }
1485f9ffccb06bcc90c862f20f8849b824022fbeebbfEric Anholt	 } else {
1486b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt	    entry = new(mem_ctx) variable_storage(ir->var,
1487b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt						  PROGRAM_OUTPUT,
1488b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt						  ir->var->location);
1489f9ffccb06bcc90c862f20f8849b824022fbeebbfEric Anholt	 }
1490f9ffccb06bcc90c862f20f8849b824022fbeebbfEric Anholt
14918364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt	 break;
14928364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt      case ir_var_auto:
14937e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      case ir_var_temporary:
1494b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt	 entry = new(mem_ctx) variable_storage(ir->var, PROGRAM_TEMPORARY,
1495b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt					       this->next_temp);
1496b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt	 this->variables.push_tail(entry);
1497224f712950494730c76b48864f2ca19acde1c8cfEric Anholt
14988364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt	 next_temp += type_size(ir->var->type);
14998364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt	 break;
150084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      }
15018364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt
15028364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt      if (!entry) {
15038364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt	 printf("Failed to make storage for %s\n", ir->var->name);
15048364fc85b8273b4d0f2ecebe7e0085e250d29990Eric Anholt	 exit(1);
1505224f712950494730c76b48864f2ca19acde1c8cfEric Anholt      }
150684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
150784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
15089c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   this->result = ir_to_mesa_src_reg(entry->file, entry->index, ir->var->type);
150984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
151084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
151184771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
151284771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_dereference_array *ir)
151384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
1514ab386f18b045fe260112bd9a239cb503e737c1dbEric Anholt   ir_constant *index;
15150161515c395c44233529c8d51f823b60050bc7baEric Anholt   ir_to_mesa_src_reg src_reg;
15168258a6a2c36c9769428f4525415d6c0d565e588cEric Anholt   int element_size = type_size(ir->type);
1517ab386f18b045fe260112bd9a239cb503e737c1dbEric Anholt
1518ab386f18b045fe260112bd9a239cb503e737c1dbEric Anholt   index = ir->array_index->constant_expression_value();
15194e5e0f018baedb2d0aa0e1f43efe339da16a09c6Eric Anholt
1520ab386f18b045fe260112bd9a239cb503e737c1dbEric Anholt   ir->array->accept(this);
15210161515c395c44233529c8d51f823b60050bc7baEric Anholt   src_reg = this->result;
15224e5e0f018baedb2d0aa0e1f43efe339da16a09c6Eric Anholt
15234d5da50b94115d055ba8d0ff8717054582665384Eric Anholt   if (index) {
15244d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      src_reg.index += index->value.i[0] * element_size;
15254e5e0f018baedb2d0aa0e1f43efe339da16a09c6Eric Anholt   } else {
15264d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      ir_to_mesa_src_reg array_base = this->result;
15274d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      /* Variable index array dereference.  It eats the "vec4" of the
15284d5da50b94115d055ba8d0ff8717054582665384Eric Anholt       * base of the array and an index that offsets the Mesa register
15294d5da50b94115d055ba8d0ff8717054582665384Eric Anholt       * index.
15304d5da50b94115d055ba8d0ff8717054582665384Eric Anholt       */
15314d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      ir->array_index->accept(this);
15328258a6a2c36c9769428f4525415d6c0d565e588cEric Anholt
15334d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      ir_to_mesa_src_reg index_reg;
15348258a6a2c36c9769428f4525415d6c0d565e588cEric Anholt
15354d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      if (element_size == 1) {
15364d5da50b94115d055ba8d0ff8717054582665384Eric Anholt	 index_reg = this->result;
15374d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      } else {
15384d5da50b94115d055ba8d0ff8717054582665384Eric Anholt	 index_reg = get_temp(glsl_type::float_type);
1539f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt
15404d5da50b94115d055ba8d0ff8717054582665384Eric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_MUL,
15414d5da50b94115d055ba8d0ff8717054582665384Eric Anholt			     ir_to_mesa_dst_reg_from_src(index_reg),
15424d5da50b94115d055ba8d0ff8717054582665384Eric Anholt			     this->result, src_reg_for_float(element_size));
1543bdbd9f112e2832eeddce8fc4f70f11005bbe4027Eric Anholt      }
15444d5da50b94115d055ba8d0ff8717054582665384Eric Anholt
15454d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      src_reg.reladdr = talloc(mem_ctx, ir_to_mesa_src_reg);
15464d5da50b94115d055ba8d0ff8717054582665384Eric Anholt      memcpy(src_reg.reladdr, &index_reg, sizeof(index_reg));
15474e5e0f018baedb2d0aa0e1f43efe339da16a09c6Eric Anholt   }
154884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
154984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   /* If the type is smaller than a vec4, replicate the last channel out. */
155085e93da18ca2c967ca12870b62ab1aac2f0b880cEric Anholt   if (ir->type->is_scalar() || ir->type->is_vector())
155185e93da18ca2c967ca12870b62ab1aac2f0b880cEric Anholt      src_reg.swizzle = swizzle_for_size(ir->type->vector_elements);
155285e93da18ca2c967ca12870b62ab1aac2f0b880cEric Anholt   else
155385e93da18ca2c967ca12870b62ab1aac2f0b880cEric Anholt      src_reg.swizzle = SWIZZLE_NOOP;
155484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
15550161515c395c44233529c8d51f823b60050bc7baEric Anholt   this->result = src_reg;
155684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
155784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
15582c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholtvoid
15592c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholtir_to_mesa_visitor::visit(ir_dereference_record *ir)
15602c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt{
15612c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   unsigned int i;
15620161515c395c44233529c8d51f823b60050bc7baEric Anholt   const glsl_type *struct_type = ir->record->type;
15632c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   int offset = 0;
15642c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt
15650161515c395c44233529c8d51f823b60050bc7baEric Anholt   ir->record->accept(this);
15662c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt
15672c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   for (i = 0; i < struct_type->length; i++) {
15680161515c395c44233529c8d51f823b60050bc7baEric Anholt      if (strcmp(struct_type->fields.structure[i].name, ir->field) == 0)
15692c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt	 break;
15702c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt      offset += type_size(struct_type->fields.structure[i].type);
15712c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   }
15722d577ee730c30caacf711babde6542766aa0b655Ian Romanick
15732d577ee730c30caacf711babde6542766aa0b655Ian Romanick   /* If the type is smaller than a vec4, replicate the last channel out. */
15742d577ee730c30caacf711babde6542766aa0b655Ian Romanick   if (ir->type->is_scalar() || ir->type->is_vector())
15752d577ee730c30caacf711babde6542766aa0b655Ian Romanick      this->result.swizzle = swizzle_for_size(ir->type->vector_elements);
15762d577ee730c30caacf711babde6542766aa0b655Ian Romanick   else
15772d577ee730c30caacf711babde6542766aa0b655Ian Romanick      this->result.swizzle = SWIZZLE_NOOP;
15782d577ee730c30caacf711babde6542766aa0b655Ian Romanick
15790161515c395c44233529c8d51f823b60050bc7baEric Anholt   this->result.index += offset;
15802c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt}
15812c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt
15822c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt/**
15832c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt * We want to be careful in assignment setup to hit the actual storage
15842c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt * instead of potentially using a temporary like we might with the
15852c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt * ir_dereference handler.
15862c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt */
15870161515c395c44233529c8d51f823b60050bc7baEric Anholtstatic struct ir_to_mesa_dst_reg
1588fc63e37b971b641dfdff000ba353c4810414c20eIan Romanickget_assignment_lhs(ir_dereference *ir, ir_to_mesa_visitor *v)
1589b07cc372c6360d0e59c84bb7586597f028c74b02Eric Anholt{
15905a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   /* The LHS must be a dereference.  If the LHS is a variable indexed array
15915a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick    * access of a vector, it must be separated into a series conditional moves
15925a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick    * before reaching this point (see ir_vec_index_to_cond_assign).
15935a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick    */
15945a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   assert(ir->as_dereference());
1595ea2a03f0a5b8b58ea88ecb607664ea50c9d6e96eEric Anholt   ir_dereference_array *deref_array = ir->as_dereference_array();
1596ea2a03f0a5b8b58ea88ecb607664ea50c9d6e96eEric Anholt   if (deref_array) {
1597ea2a03f0a5b8b58ea88ecb607664ea50c9d6e96eEric Anholt      assert(!deref_array->array->type->is_vector());
1598ea2a03f0a5b8b58ea88ecb607664ea50c9d6e96eEric Anholt   }
1599ea2a03f0a5b8b58ea88ecb607664ea50c9d6e96eEric Anholt
16000161515c395c44233529c8d51f823b60050bc7baEric Anholt   /* Use the rvalue deref handler for the most part.  We'll ignore
16010161515c395c44233529c8d51f823b60050bc7baEric Anholt    * swizzles in it and write swizzles using writemask, though.
16020161515c395c44233529c8d51f823b60050bc7baEric Anholt    */
16032c432637d0960aa522ccd09416ba1d8a65c6988bEric Anholt   ir->accept(v);
16045a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   return ir_to_mesa_dst_reg_from_src(v->result);
1605cab95c228b12f0fc568164d57475c561c8d6053eEric Anholt}
1606cab95c228b12f0fc568164d57475c561c8d6053eEric Anholt
1607c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick/**
1608c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick * Process the condition of a conditional assignment
1609c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick *
1610c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick * Examines the condition of a conditional assignment to generate the optimal
1611c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick * first operand of a \c CMP instruction.  If the condition is a relational
1612c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick * operator with 0 (e.g., \c ir_binop_less), the value being compared will be
1613c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick * used as the source for the \c CMP instruction.  Otherwise the comparison
1614c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick * is processed to a boolean result, and the boolean result is used as the
1615c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick * operand to the CMP instruction.
1616c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick */
1617c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanickbool
1618c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanickir_to_mesa_visitor::process_move_condition(ir_rvalue *ir)
1619c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick{
1620c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   ir_rvalue *src_ir = ir;
1621c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   bool negate = true;
1622c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   bool switch_order = false;
1623c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1624c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   ir_expression *const expr = ir->as_expression();
1625c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   if ((expr != NULL) && (expr->get_num_operands() == 2)) {
1626c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      bool zero_on_left = false;
1627c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1628c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      if (expr->operands[0]->is_zero()) {
1629c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 src_ir = expr->operands[1];
1630c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 zero_on_left = true;
1631c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      } else if (expr->operands[1]->is_zero()) {
1632c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 src_ir = expr->operands[0];
1633c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 zero_on_left = false;
1634c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      }
1635c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1636c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      /*      a is -  0  +            -  0  +
1637c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (a <  0)  T  F  F  ( a < 0)  T  F  F
1638c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (0 <  a)  F  F  T  (-a < 0)  F  F  T
1639c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (a <= 0)  T  T  F  (-a < 0)  F  F  T  (swap order of other operands)
1640c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (0 <= a)  F  T  T  ( a < 0)  T  F  F  (swap order of other operands)
1641c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (a >  0)  F  F  T  (-a < 0)  F  F  T
1642c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (0 >  a)  T  F  F  ( a < 0)  T  F  F
1643c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (a >= 0)  F  T  T  ( a < 0)  T  F  F  (swap order of other operands)
1644c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * (0 >= a)  T  T  F  (-a < 0)  F  F  T  (swap order of other operands)
1645c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       *
1646c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * Note that exchanging the order of 0 and 'a' in the comparison simply
1647c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       * means that the value of 'a' should be negated.
1648c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick       */
1649c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      if (src_ir != ir) {
1650c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 switch (expr->operation) {
1651c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 case ir_binop_less:
1652c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    switch_order = false;
1653c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    negate = zero_on_left;
1654c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    break;
1655c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1656c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 case ir_binop_greater:
1657c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    switch_order = false;
1658c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    negate = !zero_on_left;
1659c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    break;
1660c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1661c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 case ir_binop_lequal:
1662c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    switch_order = true;
1663c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    negate = !zero_on_left;
1664c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    break;
1665c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1666c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 case ir_binop_gequal:
1667c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    switch_order = true;
1668c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    negate = zero_on_left;
1669c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    break;
1670c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1671c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 default:
1672c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    /* This isn't the right kind of comparison afterall, so make sure
1673c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	     * the whole condition is visited.
1674c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	     */
1675c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    src_ir = ir;
1676c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    break;
1677c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 }
1678c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      }
1679c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   }
1680c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1681c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   src_ir->accept(this);
1682c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1683c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   /* We use the OPCODE_CMP (a < 0 ? b : c) for conditional moves, and the
1684c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick    * condition we produced is 0.0 or 1.0.  By flipping the sign, we can
1685c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick    * choose which value OPCODE_CMP produces without an extra instruction
1686c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick    * computing the condition.
1687c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick    */
1688c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   if (negate)
1689c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      this->result.negate = ~this->result.negate;
1690c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
1691c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick   return switch_order;
1692c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick}
1693c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
169484771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
169584771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_assignment *ir)
169684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
16970161515c395c44233529c8d51f823b60050bc7baEric Anholt   struct ir_to_mesa_dst_reg l;
16980161515c395c44233529c8d51f823b60050bc7baEric Anholt   struct ir_to_mesa_src_reg r;
16997d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt   int i;
170084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
170184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   ir->rhs->accept(this);
170284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   r = this->result;
1703cab95c228b12f0fc568164d57475c561c8d6053eEric Anholt
1704fc63e37b971b641dfdff000ba353c4810414c20eIan Romanick   l = get_assignment_lhs(ir->lhs, this);
1705cab95c228b12f0fc568164d57475c561c8d6053eEric Anholt
17065a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   /* FINISHME: This should really set to the correct maximal writemask for each
17075a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick    * FINISHME: component written (in the loops below).  This case can only
17085a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick    * FINISHME: occur for matrices, arrays, and structures.
17095a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick    */
17105a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   if (ir->write_mask == 0) {
17115a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick      assert(!ir->lhs->type->is_scalar() && !ir->lhs->type->is_vector());
17125a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick      l.writemask = WRITEMASK_XYZW;
17135a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   } else if (ir->lhs->type->is_scalar()) {
1714b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      /* FINISHME: This hack makes writing to gl_FragDepth, which lives in the
17155a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick       * FINISHME: W component of fragment shader output zero, work correctly.
17165a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick       */
17175a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick      l.writemask = WRITEMASK_XYZW;
17185a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   } else {
1719b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      int swizzles[4];
1720b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      int first_enabled_chan = 0;
1721b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      int rhs_chan = 0;
1722b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt
17235a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick      assert(ir->lhs->type->is_vector());
17245a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick      l.writemask = ir->write_mask;
1725b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt
1726b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      for (int i = 0; i < 4; i++) {
1727b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	 if (l.writemask & (1 << i)) {
1728b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	    first_enabled_chan = GET_SWZ(r.swizzle, i);
1729b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	    break;
1730b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	 }
1731b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      }
1732b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt
1733b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      /* Swizzle a small RHS vector into the channels being written.
1734b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt       *
1735b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt       * glsl ir treats write_mask as dictating how many channels are
1736b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt       * present on the RHS while Mesa IR treats write_mask as just
1737b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt       * showing which channels of the vec4 RHS get written.
1738b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt       */
1739b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      for (int i = 0; i < 4; i++) {
1740b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	 if (l.writemask & (1 << i))
1741b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	    swizzles[i] = GET_SWZ(r.swizzle, rhs_chan++);
1742b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	 else
1743b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt	    swizzles[i] = first_enabled_chan;
1744b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      }
1745b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt      r.swizzle = MAKE_SWIZZLE4(swizzles[0], swizzles[1],
1746b39e6f33b60ef9bbaf81f320aaca6a440d8a6a8fEric Anholt				swizzles[2], swizzles[3]);
17475a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   }
17485a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick
17490161515c395c44233529c8d51f823b60050bc7baEric Anholt   assert(l.file != PROGRAM_UNDEFINED);
17500161515c395c44233529c8d51f823b60050bc7baEric Anholt   assert(r.file != PROGRAM_UNDEFINED);
175184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
1752346daeca07d3c19c051799f96fa9f442262bd49fEric Anholt   if (ir->condition) {
1753c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      const bool switch_order = this->process_move_condition(ir->condition);
1754c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick      ir_to_mesa_src_reg condition = this->result;
17552d1789e667c4180777829f96856daf91326721b9Eric Anholt
17567d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt      for (i = 0; i < type_size(ir->lhs->type); i++) {
1757c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 if (switch_order) {
1758c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    ir_to_mesa_emit_op3(ir, OPCODE_CMP, l,
1759c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick				condition, ir_to_mesa_src_reg_from_dst(l), r);
1760c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 } else {
1761c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	    ir_to_mesa_emit_op3(ir, OPCODE_CMP, l,
1762c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick				condition, r, ir_to_mesa_src_reg_from_dst(l));
1763c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick	 }
1764c05ccc1ebde177646ac09c1bd6d1b4719e745f82Ian Romanick
17657d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt	 l.index++;
17667d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt	 r.index++;
17677d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt      }
17682d1789e667c4180777829f96856daf91326721b9Eric Anholt   } else {
17697d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt      for (i = 0; i < type_size(ir->lhs->type); i++) {
17707d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt	 ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
17717d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt	 l.index++;
17727d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt	 r.index++;
17737d8091f7cca0314dd66599bdce5bfcf09fe8b578Eric Anholt      }
1774346daeca07d3c19c051799f96fa9f442262bd49fEric Anholt   }
177584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
177684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
177784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
177884771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
177984771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_constant *ir)
178084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
17810161515c395c44233529c8d51f823b60050bc7baEric Anholt   ir_to_mesa_src_reg src_reg;
17820a46497a4ee3325fab47929cb17cfe2525e1fc33Vinson Lee   GLfloat stack_vals[4] = { 0 };
17830bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   GLfloat *values = stack_vals;
17840bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   unsigned int i;
178584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
17865b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt   /* Unfortunately, 4 floats is all we can get into
17875b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt    * _mesa_add_unnamed_constant.  So, make a temp to store an
17885b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt    * aggregate constant and move each constant value into it.  If we
17895b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt    * get lucky, copy propagation will eliminate the extra moves.
17905b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt    */
17915b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt
17925b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt   if (ir->type->base_type == GLSL_TYPE_STRUCT) {
17935b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt      ir_to_mesa_src_reg temp_base = get_temp(ir->type);
17945b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt      ir_to_mesa_dst_reg temp = ir_to_mesa_dst_reg_from_src(temp_base);
17955b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt
17965b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt      foreach_iter(exec_list_iterator, iter, ir->components) {
17975b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	 ir_constant *field_value = (ir_constant *)iter.get();
17985b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	 int size = type_size(field_value->type);
17995b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt
18005b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	 assert(size > 0);
18015b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt
18025b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	 field_value->accept(this);
18035b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	 src_reg = this->result;
18045b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt
18055b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	 for (i = 0; i < (unsigned int)size; i++) {
18065b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	    ir_to_mesa_emit_op1(ir, OPCODE_MOV, temp, src_reg);
18075b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt
18085b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	    src_reg.index++;
18095b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	    temp.index++;
18105b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt	 }
18115b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt      }
18125b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt      this->result = temp_base;
18135b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt      return;
18145b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt   }
18155b6890a388d554f06880e88d61c73dcd62c5f141Eric Anholt
181620c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt   if (ir->type->is_array()) {
181720c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      ir_to_mesa_src_reg temp_base = get_temp(ir->type);
181820c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      ir_to_mesa_dst_reg temp = ir_to_mesa_dst_reg_from_src(temp_base);
181920c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      int size = type_size(ir->type->fields.array);
182020c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt
182120c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      assert(size > 0);
182220c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt
182320c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      for (i = 0; i < ir->type->length; i++) {
182420c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	 ir->array_elements[i]->accept(this);
182520c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	 src_reg = this->result;
182620c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	 for (int j = 0; j < size; j++) {
182720c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	    ir_to_mesa_emit_op1(ir, OPCODE_MOV, temp, src_reg);
182820c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt
182920c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	    src_reg.index++;
183020c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	    temp.index++;
183120c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt	 }
183220c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      }
183320c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      this->result = temp_base;
183420c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt      return;
183520c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt   }
183620c074ae28b310348a6a1920ad0ddf1e5cbb7a46Eric Anholt
1837ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt   if (ir->type->is_matrix()) {
1838c91809e1e4a4fa8884e6588159368ea32431ee0eEric Anholt      ir_to_mesa_src_reg mat = get_temp(ir->type);
1839ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt      ir_to_mesa_dst_reg mat_column = ir_to_mesa_dst_reg_from_src(mat);
1840ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt
1841ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt      for (i = 0; i < ir->type->matrix_columns; i++) {
1842ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt	 assert(ir->type->base_type == GLSL_TYPE_FLOAT);
1843ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt	 values = &ir->value.f[i * ir->type->vector_elements];
1844ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt
18459c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt	 src_reg = ir_to_mesa_src_reg(PROGRAM_CONSTANT, -1, NULL);
1846ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt	 src_reg.index = _mesa_add_unnamed_constant(this->prog->Parameters,
18479c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt						values,
18489c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt						ir->type->vector_elements,
18499c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt						&src_reg.swizzle);
1850ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt	 ir_to_mesa_emit_op1(ir, OPCODE_MOV, mat_column, src_reg);
1851ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt
1852ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt	 mat_column.index++;
1853ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt      }
1854ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt
1855ffd24b0a6844871eed0c78608431e2f82d5615e1Eric Anholt      this->result = mat;
1856ebef04011736ea8e13692fed87623d425c4d1b08Eric Anholt      return;
1857582b73fe691ef7ea12a002cb2ae57505c3b1c21eEric Anholt   }
18580bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt
18590bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   src_reg.file = PROGRAM_CONSTANT;
18600bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   switch (ir->type->base_type) {
18610bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   case GLSL_TYPE_FLOAT:
18620bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      values = &ir->value.f[0];
18630bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      break;
18640bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   case GLSL_TYPE_UINT:
18650bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      for (i = 0; i < ir->type->vector_elements; i++) {
18660bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt	 values[i] = ir->value.u[i];
18670bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      }
18680bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      break;
18690bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   case GLSL_TYPE_INT:
18700bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      for (i = 0; i < ir->type->vector_elements; i++) {
18710bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt	 values[i] = ir->value.i[i];
18720bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      }
18730bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      break;
18740bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   case GLSL_TYPE_BOOL:
18750bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      for (i = 0; i < ir->type->vector_elements; i++) {
18760bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt	 values[i] = ir->value.b[i];
18770bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      }
18780bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      break;
18790bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   default:
18800bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt      assert(!"Non-float/uint/int/bool constant");
18810bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt   }
18820bef5b97a9eccebc4b59dff42b2863770da770feEric Anholt
18839c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   this->result = ir_to_mesa_src_reg(PROGRAM_CONSTANT, -1, ir->type);
18849c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   this->result.index = _mesa_add_unnamed_constant(this->prog->Parameters,
18859c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt						   values,
18869c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt						   ir->type->vector_elements,
18879c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt						   &this->result.swizzle);
188884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
188984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
18907b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholtfunction_entry *
18917b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholtir_to_mesa_visitor::get_function_signature(ir_function_signature *sig)
18927b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt{
18937b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   function_entry *entry;
18947b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
18957b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   foreach_iter(exec_list_iterator, iter, this->function_signatures) {
18967b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      entry = (function_entry *)iter.get();
18977b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
18987b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      if (entry->sig == sig)
18997b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 return entry;
19007b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   }
19017b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19027b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   entry = talloc(mem_ctx, function_entry);
19037b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   entry->sig = sig;
19047b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   entry->sig_id = this->next_signature_id++;
19057b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   entry->bgn_inst = NULL;
19067b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19077b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /* Allocate storage for all the parameters. */
19087b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   foreach_iter(exec_list_iterator, iter, sig->parameters) {
19097b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir_variable *param = (ir_variable *)iter.get();
1910b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt      variable_storage *storage;
19117b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19127b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      storage = find_variable_storage(param);
19137b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      assert(!storage);
19147b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1915b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt      storage = new(mem_ctx) variable_storage(param, PROGRAM_TEMPORARY,
1916b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt					      this->next_temp);
1917b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt      this->variables.push_tail(storage);
19187b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19197b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      this->next_temp += type_size(param->type);
19207b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   }
19217b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
1922576d01ad8c8b8aa57b4711c98d8e004d4f20fc0bEric Anholt   if (!sig->return_type->is_void()) {
19237b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      entry->return_reg = get_temp(sig->return_type);
19247b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   } else {
19257b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      entry->return_reg = ir_to_mesa_undef;
19267b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   }
19277b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19287b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   this->function_signatures.push_tail(entry);
19297b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   return entry;
19307b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt}
193184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
193284771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
193384771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_call *ir)
193484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
19357b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   ir_to_mesa_instruction *call_inst;
19367b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   ir_function_signature *sig = ir->get_callee();
19377b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   function_entry *entry = get_function_signature(sig);
19387b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   int i;
19397b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19407b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /* Process in parameters. */
19417b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   exec_list_iterator sig_iter = sig->parameters.iterator();
19427b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   foreach_iter(exec_list_iterator, iter, *ir) {
19437b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir_rvalue *param_rval = (ir_rvalue *)iter.get();
19447b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir_variable *param = (ir_variable *)sig_iter.get();
19457b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19467b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      if (param->mode == ir_var_in ||
19477b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	  param->mode == ir_var_inout) {
1948b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt	 variable_storage *storage = find_variable_storage(param);
19497b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 assert(storage);
19507b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19517b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 param_rval->accept(this);
19527b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 ir_to_mesa_src_reg r = this->result;
19537b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19547b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 ir_to_mesa_dst_reg l;
19557b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 l.file = storage->file;
19567b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 l.index = storage->index;
19577b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 l.reladdr = NULL;
19587b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 l.writemask = WRITEMASK_XYZW;
19597b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 l.cond_mask = COND_TR;
19607b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19617b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 for (i = 0; i < type_size(param->type); i++) {
19627b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
19637b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    l.index++;
19647b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    r.index++;
19657b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 }
19667b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      }
19677b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19687b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      sig_iter.next();
19697b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   }
19707b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   assert(!sig_iter.has_next());
19717b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19727b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /* Emit call instruction */
19737b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   call_inst = ir_to_mesa_emit_op1(ir, OPCODE_CAL,
19747b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt				   ir_to_mesa_undef_dst, ir_to_mesa_undef);
19757b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   call_inst->function = entry;
19767b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19777b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /* Process out parameters. */
19787b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   sig_iter = sig->parameters.iterator();
19797b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   foreach_iter(exec_list_iterator, iter, *ir) {
19807b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir_rvalue *param_rval = (ir_rvalue *)iter.get();
19817b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir_variable *param = (ir_variable *)sig_iter.get();
19827b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19837b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      if (param->mode == ir_var_out ||
19847b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	  param->mode == ir_var_inout) {
1985b29d31cd67a423995b5673fdeedea82dfa12ec3cEric Anholt	 variable_storage *storage = find_variable_storage(param);
19867b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 assert(storage);
19877b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19887b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 ir_to_mesa_src_reg r;
19897b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 r.file = storage->file;
19907b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 r.index = storage->index;
19917b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 r.reladdr = NULL;
19927b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 r.swizzle = SWIZZLE_NOOP;
19937b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 r.negate = 0;
19947b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19957b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 param_rval->accept(this);
19967b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 ir_to_mesa_dst_reg l = ir_to_mesa_dst_reg_from_src(this->result);
19977b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
19987b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 for (i = 0; i < type_size(param->type); i++) {
19997b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
20007b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    l.index++;
20017b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    r.index++;
20027b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 }
20037b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      }
20047b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
20057b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      sig_iter.next();
20067b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   }
20077b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   assert(!sig_iter.has_next());
20087b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
20097b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /* Process return value. */
20107b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   this->result = entry->return_reg;
201184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
201284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
201384771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
201484771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_texture *ir)
201584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
20169c02412cdc0270f2b0dc64afe709721e049fd5b0Eric Anholt   ir_to_mesa_src_reg result_src, coord, lod_info, projector;
2017d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   ir_to_mesa_dst_reg result_dst, coord_dst;
2018d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   ir_to_mesa_instruction *inst = NULL;
2019d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   prog_opcode opcode = OPCODE_NOP;
202084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
202184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   ir->coordinate->accept(this);
2022d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt
2023d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   /* Put our coords in a temp.  We'll need to modify them for shadow,
2024d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt    * projection, or LOD, so the only case we'd use it as is is if
2025d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt    * we're doing plain old texturing.  Mesa IR optimization should
2026d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt    * handle cleaning up our mess in that case.
2027d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt    */
2028d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   coord = get_temp(glsl_type::vec4_type);
2029d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   coord_dst = ir_to_mesa_dst_reg_from_src(coord);
2030d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst,
2031d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt		       this->result);
2032d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2033de75dfac4ea2cad64dc91f6ac16fe205b5015af6Eric Anholt   if (ir->projector) {
2034de75dfac4ea2cad64dc91f6ac16fe205b5015af6Eric Anholt      ir->projector->accept(this);
2035de75dfac4ea2cad64dc91f6ac16fe205b5015af6Eric Anholt      projector = this->result;
2036de75dfac4ea2cad64dc91f6ac16fe205b5015af6Eric Anholt   }
2037de75dfac4ea2cad64dc91f6ac16fe205b5015af6Eric Anholt
2038d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   /* Storage for our result.  Ideally for an assignment we'd be using
2039d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt    * the actual storage for the result here, instead.
2040d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt    */
2041d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   result_src = get_temp(glsl_type::vec4_type);
2042d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   result_dst = ir_to_mesa_dst_reg_from_src(result_src);
2043d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2044d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   switch (ir->op) {
2045d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case ir_tex:
2046d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      opcode = OPCODE_TEX;
2047d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
2048d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case ir_txb:
2049d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      opcode = OPCODE_TXB;
2050d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      ir->lod_info.bias->accept(this);
2051d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      lod_info = this->result;
2052d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
2053d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case ir_txl:
2054d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      opcode = OPCODE_TXL;
2055d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      ir->lod_info.lod->accept(this);
2056d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      lod_info = this->result;
2057d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
2058d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case ir_txd:
2059d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case ir_txf:
2060d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      assert(!"GLSL 1.30 features unsupported");
2061d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
2062d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   }
2063d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2064d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   if (ir->projector) {
2065d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      if (opcode == OPCODE_TEX) {
2066d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 /* Slot the projector in as the last component of the coord. */
2067d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 coord_dst.writemask = WRITEMASK_W;
2068d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst, projector);
2069d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 coord_dst.writemask = WRITEMASK_XYZW;
2070d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 opcode = OPCODE_TXP;
2071d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      } else {
2072d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 ir_to_mesa_src_reg coord_w = coord;
2073d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 coord_w.swizzle = SWIZZLE_WWWW;
2074d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt
2075d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 /* For the other TEX opcodes there's no projective version
2076d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	  * since the last slot is taken up by lod info.  Do the
2077d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	  * projective divide now.
2078d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	  */
2079d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 coord_dst.writemask = WRITEMASK_W;
2080d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 ir_to_mesa_emit_op1(ir, OPCODE_RCP, coord_dst, projector);
2081d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt
2082d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 coord_dst.writemask = WRITEMASK_XYZ;
2083d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 ir_to_mesa_emit_op2(ir, OPCODE_MUL, coord_dst, coord, coord_w);
2084d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt
2085d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 coord_dst.writemask = WRITEMASK_XYZW;
2086d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt	 coord.swizzle = SWIZZLE_XYZW;
2087d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      }
2088d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   }
2089d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt
2090b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt   if (ir->shadow_comparitor) {
2091b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt      /* Slot the shadow value in as the second to last component of the
2092b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt       * coord.
2093b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt       */
2094b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt      ir->shadow_comparitor->accept(this);
2095b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt      coord_dst.writemask = WRITEMASK_Z;
2096b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst, this->result);
2097b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt      coord_dst.writemask = WRITEMASK_XYZW;
2098b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt   }
2099b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt
2100d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   if (opcode == OPCODE_TXL || opcode == OPCODE_TXB) {
2101d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      /* Mesa IR stores lod or lod bias in the last channel of the coords. */
2102d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      coord_dst.writemask = WRITEMASK_W;
2103d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      ir_to_mesa_emit_op1(ir, OPCODE_MOV, coord_dst, lod_info);
2104d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt      coord_dst.writemask = WRITEMASK_XYZW;
2105d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   }
2106d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt
2107d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt   inst = ir_to_mesa_emit_op1(ir, opcode, result_dst, coord);
2108d3983ca03248092d92b5240fbc6a30c24f80d313Eric Anholt
2109b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt   if (ir->shadow_comparitor)
2110b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt      inst->tex_shadow = GL_TRUE;
2111b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt
2112a32893221ce253da7bb465e0ec9d0df5f7208d8fEric Anholt   inst->sampler = _mesa_get_sampler_uniform_value(ir->sampler,
2113a32893221ce253da7bb465e0ec9d0df5f7208d8fEric Anholt						   this->shader_program,
2114a32893221ce253da7bb465e0ec9d0df5f7208d8fEric Anholt						   this->prog);
2115c234d0b25f622a7bdd3c40bc72fdbd59d8494c7cEric Anholt
2116aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt   const glsl_type *sampler_type = ir->sampler->type;
2117d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2118c234d0b25f622a7bdd3c40bc72fdbd59d8494c7cEric Anholt   switch (sampler_type->sampler_dimensionality) {
2119d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case GLSL_SAMPLER_DIM_1D:
2120c234d0b25f622a7bdd3c40bc72fdbd59d8494c7cEric Anholt      inst->tex_target = (sampler_type->sampler_array)
21210a86d766ef0d98abd3373609a637bf137203e994Ian Romanick	 ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
2122d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
2123d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case GLSL_SAMPLER_DIM_2D:
2124c234d0b25f622a7bdd3c40bc72fdbd59d8494c7cEric Anholt      inst->tex_target = (sampler_type->sampler_array)
21250a86d766ef0d98abd3373609a637bf137203e994Ian Romanick	 ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
2126d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
2127d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case GLSL_SAMPLER_DIM_3D:
2128d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      inst->tex_target = TEXTURE_3D_INDEX;
2129d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
2130d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   case GLSL_SAMPLER_DIM_CUBE:
2131d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      inst->tex_target = TEXTURE_CUBE_INDEX;
2132d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      break;
21330bf63733e54b47daf9f50c32a1fca4039c82def2Ian Romanick   case GLSL_SAMPLER_DIM_RECT:
21340bf63733e54b47daf9f50c32a1fca4039c82def2Ian Romanick      inst->tex_target = TEXTURE_RECT_INDEX;
21350bf63733e54b47daf9f50c32a1fca4039c82def2Ian Romanick      break;
213668772031e6242aa78864dc9c7c1a607aec5ee7b9Ian Romanick   case GLSL_SAMPLER_DIM_BUF:
213768772031e6242aa78864dc9c7c1a607aec5ee7b9Ian Romanick      assert(!"FINISHME: Implement ARB_texture_buffer_object");
213868772031e6242aa78864dc9c7c1a607aec5ee7b9Ian Romanick      break;
2139d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   default:
214068772031e6242aa78864dc9c7c1a607aec5ee7b9Ian Romanick      assert(!"Should not get here.");
2141d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   }
2142d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2143d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   this->result = result_src;
214484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
214584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
214684771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
214784771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_return *ir)
214884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
21497b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   if (ir->get_value()) {
21507b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir_to_mesa_dst_reg l;
21517b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      int i;
21527b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
215335220fc5981045331b4f048f0fc2e1371a0673edEric Anholt      assert(current_function);
215435220fc5981045331b4f048f0fc2e1371a0673edEric Anholt
21557b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir->get_value()->accept(this);
21567b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      ir_to_mesa_src_reg r = this->result;
215784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
21587b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      l = ir_to_mesa_dst_reg_from_src(current_function->return_reg);
21597b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
21607b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      for (i = 0; i < type_size(current_function->sig->return_type); i++) {
21617b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 ir_to_mesa_emit_op1(ir, OPCODE_MOV, l, r);
21627b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 l.index++;
21637b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 r.index++;
21647b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      }
21657b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   }
21667b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
21677b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   ir_to_mesa_emit_op0(ir, OPCODE_RET);
216884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
216984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
217016efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunkevoid
217116efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunkeir_to_mesa_visitor::visit(ir_discard *ir)
217216efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke{
21739b075cb9fa9eb6a95d0816283ef01ae72dafa680Eric Anholt   struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
21749b075cb9fa9eb6a95d0816283ef01ae72dafa680Eric Anholt
2175ead2ea89f42b40edc56ddf8c6ce1df4efdcefe2aMarek Olšák   if (ir->condition) {
2176ead2ea89f42b40edc56ddf8c6ce1df4efdcefe2aMarek Olšák      ir->condition->accept(this);
2177ead2ea89f42b40edc56ddf8c6ce1df4efdcefe2aMarek Olšák      this->result.negate = ~this->result.negate;
2178ead2ea89f42b40edc56ddf8c6ce1df4efdcefe2aMarek Olšák      ir_to_mesa_emit_op1(ir, OPCODE_KIL, ir_to_mesa_undef_dst, this->result);
2179ead2ea89f42b40edc56ddf8c6ce1df4efdcefe2aMarek Olšák   } else {
2180ead2ea89f42b40edc56ddf8c6ce1df4efdcefe2aMarek Olšák      ir_to_mesa_emit_op0(ir, OPCODE_KIL_NV);
2181ead2ea89f42b40edc56ddf8c6ce1df4efdcefe2aMarek Olšák   }
218216efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke
21839b075cb9fa9eb6a95d0816283ef01ae72dafa680Eric Anholt   fp->UsesKill = GL_TRUE;
218416efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke}
218584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
218684771df82ed2ed8718013795089edd38cf5bd84dEric Anholtvoid
218784771df82ed2ed8718013795089edd38cf5bd84dEric Anholtir_to_mesa_visitor::visit(ir_if *ir)
218884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
2189854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt   ir_to_mesa_instruction *cond_inst, *if_inst, *else_inst = NULL;
2190cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt   ir_to_mesa_instruction *prev_inst;
2191cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt
2192cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt   prev_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
2193c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2194c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   ir->condition->accept(this);
21950161515c395c44233529c8d51f823b60050bc7baEric Anholt   assert(this->result.file != PROGRAM_UNDEFINED);
2196c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
21976d3a2c97f4a78e85545286e0e126cd3a27bd1cbdLuca Barbieri   if (this->options->EmitCondCodes) {
2198854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt      cond_inst = (ir_to_mesa_instruction *)this->instructions.get_tail();
2199cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt
2200cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt      /* See if we actually generated any instruction for generating
2201cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt       * the condition.  If not, then cook up a move to a temp so we
2202cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt       * have something to set cond_update on.
2203cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt       */
2204cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt      if (cond_inst == prev_inst) {
2205cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt	 ir_to_mesa_src_reg temp = get_temp(glsl_type::bool_type);
2206cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt	 cond_inst = ir_to_mesa_emit_op1(ir->condition, OPCODE_MOV,
2207cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt					 ir_to_mesa_dst_reg_from_src(temp),
2208cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt					 result);
2209cbe52c8012659abe5d81cf1180659820e704d290Eric Anholt      }
2210854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt      cond_inst->cond_update = GL_TRUE;
2211854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt
2212021222c6a872ca2eef770ebadb8754f659775204Eric Anholt      if_inst = ir_to_mesa_emit_op0(ir->condition, OPCODE_IF);
2213854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt      if_inst->dst_reg.cond_mask = COND_NE;
2214854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt   } else {
2215854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt      if_inst = ir_to_mesa_emit_op1(ir->condition,
2216854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt				    OPCODE_IF, ir_to_mesa_undef_dst,
2217854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt				    this->result);
2218854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt   }
2219c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2220c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   this->instructions.push_tail(if_inst);
2221c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2222c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   visit_exec_list(&ir->then_instructions, this);
2223c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2224c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   if (!ir->else_instructions.is_empty()) {
2225021222c6a872ca2eef770ebadb8754f659775204Eric Anholt      else_inst = ir_to_mesa_emit_op0(ir->condition, OPCODE_ELSE);
22260a52e8b691cecfeec27717c3289763226d5f1bdaEric Anholt      visit_exec_list(&ir->else_instructions, this);
2227c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   }
2228c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
22290161515c395c44233529c8d51f823b60050bc7baEric Anholt   if_inst = ir_to_mesa_emit_op1(ir->condition, OPCODE_ENDIF,
22300161515c395c44233529c8d51f823b60050bc7baEric Anholt				 ir_to_mesa_undef_dst, ir_to_mesa_undef);
223184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
223284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
2233ae252d3613d10a051657c4ca6db27409f7cf40aeEric Anholtir_to_mesa_visitor::ir_to_mesa_visitor()
2234ae252d3613d10a051657c4ca6db27409f7cf40aeEric Anholt{
22350161515c395c44233529c8d51f823b60050bc7baEric Anholt   result.file = PROGRAM_UNDEFINED;
2236ae252d3613d10a051657c4ca6db27409f7cf40aeEric Anholt   next_temp = 1;
22377b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   next_signature_id = 1;
22387b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   current_function = NULL;
2239abc6d7e0b4b04c75129d24c3cb6f021b92cd46f6Eric Anholt   mem_ctx = talloc_new(NULL);
2240ae252d3613d10a051657c4ca6db27409f7cf40aeEric Anholt}
2241ae252d3613d10a051657c4ca6db27409f7cf40aeEric Anholt
2242fe1918c71c3e387939cef9359d4b31ebc5c11a17Eric Anholtir_to_mesa_visitor::~ir_to_mesa_visitor()
2243fe1918c71c3e387939cef9359d4b31ebc5c11a17Eric Anholt{
2244abc6d7e0b4b04c75129d24c3cb6f021b92cd46f6Eric Anholt   talloc_free(mem_ctx);
2245fe1918c71c3e387939cef9359d4b31ebc5c11a17Eric Anholt}
2246fe1918c71c3e387939cef9359d4b31ebc5c11a17Eric Anholt
224784771df82ed2ed8718013795089edd38cf5bd84dEric Anholtstatic struct prog_src_register
224884771df82ed2ed8718013795089edd38cf5bd84dEric Anholtmesa_src_reg_from_ir_src_reg(ir_to_mesa_src_reg reg)
224984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
225084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   struct prog_src_register mesa_reg;
225184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
225284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   mesa_reg.File = reg.file;
225350fd99d1723a6c7f3bd2dedffeeadf7d5e33b83bBrian Paul   assert(reg.index < (1 << INST_INDEX_BITS));
225484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   mesa_reg.Index = reg.index;
225534195832669f0eb7c4a80997cc524f8d10319307Eric Anholt   mesa_reg.Swizzle = reg.swizzle;
2256f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt   mesa_reg.RelAddr = reg.reladdr != NULL;
2257ea6b34cce4471d6239201101a3b24db17eaae870Eric Anholt   mesa_reg.Negate = reg.negate;
2258285ff93819724b9a858984dc8c30858784a5ee5bEric Anholt   mesa_reg.Abs = 0;
2259b10bb527eaf39378da25dd4ad21b1c68ceaa1e2dEric Anholt   mesa_reg.HasIndex2 = GL_FALSE;
2260405546882a010885d342b0b40392de0da289374eVinson Lee   mesa_reg.RelAddr2 = 0;
2261405546882a010885d342b0b40392de0da289374eVinson Lee   mesa_reg.Index2 = 0;
226284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
226384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   return mesa_reg;
226484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt}
226584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
2266c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholtstatic void
22677b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholtset_branchtargets(ir_to_mesa_visitor *v,
22687b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt		  struct prog_instruction *mesa_instructions,
2269c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt		  int num_instructions)
2270c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt{
2271e2a358348b143a163c065d82c7375e6a94e98f2aKenneth Graunke   int if_count = 0, loop_count = 0;
227264fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   int *if_stack, *loop_stack;
227364fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   int if_stack_pos = 0, loop_stack_pos = 0;
227464fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt   int i, j;
2275c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2276c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   for (i = 0; i < num_instructions; i++) {
227764fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      switch (mesa_instructions[i].Opcode) {
227864fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      case OPCODE_IF:
2279c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 if_count++;
228064fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 break;
228164fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      case OPCODE_BGNLOOP:
228264fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 loop_count++;
228364fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 break;
228464fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      case OPCODE_BRK:
228564fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      case OPCODE_CONT:
228664fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 mesa_instructions[i].BranchTarget = -1;
228764fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 break;
228864fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      default:
228964fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 break;
229064fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      }
2291c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   }
2292c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
22933ef83d270b2c24867a0d020b81bdc6c54cb1c9b0Eric Anholt   if_stack = talloc_zero_array(v->mem_ctx, int, if_count);
22943ef83d270b2c24867a0d020b81bdc6c54cb1c9b0Eric Anholt   loop_stack = talloc_zero_array(v->mem_ctx, int, loop_count);
2295c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2296c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   for (i = 0; i < num_instructions; i++) {
2297c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      switch (mesa_instructions[i].Opcode) {
2298c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      case OPCODE_IF:
229964fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 if_stack[if_stack_pos] = i;
2300c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 if_stack_pos++;
2301c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 break;
2302c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      case OPCODE_ELSE:
230364fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 mesa_instructions[if_stack[if_stack_pos - 1]].BranchTarget = i;
230464fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 if_stack[if_stack_pos - 1] = i;
2305c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 break;
2306c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      case OPCODE_ENDIF:
230764fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 mesa_instructions[if_stack[if_stack_pos - 1]].BranchTarget = i;
2308c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 if_stack_pos--;
2309c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 break;
231064fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      case OPCODE_BGNLOOP:
231164fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 loop_stack[loop_stack_pos] = i;
231264fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 loop_stack_pos++;
231364fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 break;
231464fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      case OPCODE_ENDLOOP:
231564fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 loop_stack_pos--;
231664fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 /* Rewrite any breaks/conts at this nesting level (haven't
231764fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	  * already had a BranchTarget assigned) to point to the end
231864fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	  * of the loop.
231964fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	  */
232064fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 for (j = loop_stack[loop_stack_pos]; j < i; j++) {
232164fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	    if (mesa_instructions[j].Opcode == OPCODE_BRK ||
232264fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt		mesa_instructions[j].Opcode == OPCODE_CONT) {
232364fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	       if (mesa_instructions[j].BranchTarget == -1) {
232464fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt		  mesa_instructions[j].BranchTarget = i;
232564fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	       }
232664fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	    }
232764fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 }
232864fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 /* The loop ends point at each other. */
232964fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 mesa_instructions[i].BranchTarget = loop_stack[loop_stack_pos];
233064fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt	 mesa_instructions[loop_stack[loop_stack_pos]].BranchTarget = i;
23317b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 break;
23327b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      case OPCODE_CAL:
23337b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 foreach_iter(exec_list_iterator, iter, v->function_signatures) {
23347b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    function_entry *entry = (function_entry *)iter.get();
23357b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
23367b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    if (entry->sig_id == mesa_instructions[i].BranchTarget) {
23377b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	       mesa_instructions[i].BranchTarget = entry->inst;
23387b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	       break;
23397b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    }
23407b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 }
23417b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 break;
2342c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      default:
2343c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 break;
2344c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      }
2345c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   }
2346c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt}
2347c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2348c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholtstatic void
2349c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholtprint_program(struct prog_instruction *mesa_instructions,
2350c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	      ir_instruction **mesa_instruction_annotation,
2351c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	      int num_instructions)
2352c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt{
2353c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   ir_instruction *last_ir = NULL;
2354c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   int i;
2355748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt   int indent = 0;
2356c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2357c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   for (i = 0; i < num_instructions; i++) {
2358c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      struct prog_instruction *mesa_inst = mesa_instructions + i;
2359c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      ir_instruction *ir = mesa_instruction_annotation[i];
2360c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2361748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt      fprintf(stdout, "%3d: ", i);
2362748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt
236364fcbbca9ca8191b5131304af2026d0ed914b765Eric Anholt      if (last_ir != ir && ir) {
2364748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt	 int j;
2365748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt
2366748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt	 for (j = 0; j < indent; j++) {
2367748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt	    fprintf(stdout, " ");
2368748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt	 }
2369748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt	 ir->print();
2370c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 printf("\n");
2371c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt	 last_ir = ir;
2372748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt
2373748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt	 fprintf(stdout, "     "); /* line number spacing. */
2374c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      }
2375c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2376748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt      indent = _mesa_fprint_instruction_opt(stdout, mesa_inst, indent,
2377748c343f8bdbbc8c5f00403b790ad7130424c35fEric Anholt					    PROG_PRINT_DEBUG, NULL);
2378c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   }
2379c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt}
2380c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2381ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholtstatic void
2382ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholtcount_resources(struct gl_program *prog)
2383ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholt{
2384d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   unsigned int i;
2385d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2386d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   prog->SamplersUsed = 0;
2387ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholt
2388ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholt   for (i = 0; i < prog->NumInstructions; i++) {
2389ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholt      struct prog_instruction *inst = &prog->Instructions[i];
2390d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2391d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      if (_mesa_is_tex_instruction(inst->Opcode)) {
2392d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt	 prog->SamplerTargets[inst->TexSrcUnit] =
2393d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt	    (gl_texture_index)inst->TexSrcTarget;
2394d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt	 prog->SamplersUsed |= 1 << inst->TexSrcUnit;
2395d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt	 if (inst->TexShadow) {
2396d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt	    prog->ShadowSamplers |= 1 << inst->TexSrcUnit;
2397d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt	 }
2398d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      }
2399ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholt   }
2400d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt
2401d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt   _mesa_update_shader_textures_used(prog);
2402ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholt}
2403ffc845a50a69b48446f5e25e7b4485089231bbe7Eric Anholt
240499f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholtstruct uniform_sort {
240599f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   struct gl_uniform *u;
240699f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   int pos;
240799f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt};
240899f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt
240999f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt/* The shader_program->Uniforms list is almost sorted in increasing
241099f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt * uniform->{Frag,Vert}Pos locations, but not quite when there are
241199f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt * uniforms shared between targets.  We need to add parameters in
241299f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt * increasing order for the targets.
241399f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt */
241499f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholtstatic int
241599f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholtsort_uniforms(const void *a, const void *b)
241699f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt{
241799f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   struct uniform_sort *u1 = (struct uniform_sort *)a;
241899f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   struct uniform_sort *u2 = (struct uniform_sort *)b;
241999f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt
242099f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   return u1->pos - u2->pos;
242199f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt}
242299f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt
24230924ba0c3496160a134d37cec800f902ae805b9cEric Anholt/* Add the uniforms to the parameters.  The linker chose locations
24240924ba0c3496160a134d37cec800f902ae805b9cEric Anholt * in our parameters lists (which weren't created yet), which the
24250924ba0c3496160a134d37cec800f902ae805b9cEric Anholt * uniforms code will use to poke values into our parameters list
24260924ba0c3496160a134d37cec800f902ae805b9cEric Anholt * when uniforms are updated.
242785c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt */
242885c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholtstatic void
24290924ba0c3496160a134d37cec800f902ae805b9cEric Anholtadd_uniforms_to_parameters_list(struct gl_shader_program *shader_program,
24300924ba0c3496160a134d37cec800f902ae805b9cEric Anholt				struct gl_shader *shader,
24310924ba0c3496160a134d37cec800f902ae805b9cEric Anholt				struct gl_program *prog)
243285c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt{
243385c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt   unsigned int i;
243499f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   unsigned int next_sampler = 0, num_uniforms = 0;
243599f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   struct uniform_sort *sorted_uniforms;
243699f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt
243799f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   sorted_uniforms = talloc_array(NULL, struct uniform_sort,
243899f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt				  shader_program->Uniforms->NumUniforms);
243985c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt
24400924ba0c3496160a134d37cec800f902ae805b9cEric Anholt   for (i = 0; i < shader_program->Uniforms->NumUniforms; i++) {
24410924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      struct gl_uniform *uniform = shader_program->Uniforms->Uniforms + i;
24420924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      int parameter_index = -1;
24430924ba0c3496160a134d37cec800f902ae805b9cEric Anholt
24440924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      switch (shader->Type) {
24450924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      case GL_VERTEX_SHADER:
24460924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 parameter_index = uniform->VertPos;
24470924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 break;
24480924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      case GL_FRAGMENT_SHADER:
24490924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 parameter_index = uniform->FragPos;
24500924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 break;
24510924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      case GL_GEOMETRY_SHADER:
24520924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 parameter_index = uniform->GeomPos;
24530924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 break;
24540924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      }
24550924ba0c3496160a134d37cec800f902ae805b9cEric Anholt
24560924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      /* Only add uniforms used in our target. */
245799f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt      if (parameter_index != -1) {
245899f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt	 sorted_uniforms[num_uniforms].pos = parameter_index;
245999f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt	 sorted_uniforms[num_uniforms].u = uniform;
246099f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt	 num_uniforms++;
246199f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt      }
246299f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   }
246399f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt
246499f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   qsort(sorted_uniforms, num_uniforms, sizeof(struct uniform_sort),
246599f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt	 sort_uniforms);
246699f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt
246799f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   for (i = 0; i < num_uniforms; i++) {
246899f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt      struct gl_uniform *uniform = sorted_uniforms[i].u;
246999f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt      int parameter_index = sorted_uniforms[i].pos;
247099f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt      const glsl_type *type = uniform->Type;
247199f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt      unsigned int size;
24720924ba0c3496160a134d37cec800f902ae805b9cEric Anholt
24730924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      if (type->is_vector() ||
24740924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	  type->is_scalar()) {
24750924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 size = type->vector_elements;
24760924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      } else {
24770924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 size = type_size(type) * 4;
24780924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      }
24790924ba0c3496160a134d37cec800f902ae805b9cEric Anholt
2480aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt      gl_register_file file;
24810924ba0c3496160a134d37cec800f902ae805b9cEric Anholt      if (type->is_sampler() ||
24820924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	  (type->is_array() && type->fields.array->is_sampler())) {
2483aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 file = PROGRAM_SAMPLER;
2484aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt      } else {
2485aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 file = PROGRAM_UNIFORM;
2486aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt      }
248785c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt
2488aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt      GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1,
2489aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt						 uniform->Name);
24900924ba0c3496160a134d37cec800f902ae805b9cEric Anholt
2491aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt      if (index < 0) {
2492aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 index = _mesa_add_parameter(prog->Parameters, file,
2493aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt				     uniform->Name, size, type->gl_type,
2494aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt				     NULL, NULL, 0x0);
2495aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt
2496aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 /* Sampler uniform values are stored in prog->SamplerUnits,
2497aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	  * and the entry in that array is selected by this index we
2498aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	  * store in ParameterValues[].
2499aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	  */
2500aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 if (file == PROGRAM_SAMPLER) {
2501aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	    for (unsigned int j = 0; j < size / 4; j++)
2502aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	       prog->Parameters->ParameterValues[index + j][0] = next_sampler++;
2503aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 }
2504aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt
2505aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 /* The location chosen in the Parameters list here (returned
2506aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	  * from _mesa_add_uniform) has to match what the linker chose.
2507aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	  */
2508aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt	 if (index != parameter_index) {
2509500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt	    fail_link(shader_program, "Allocation of uniform `%s' to target "
2510500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt		      "failed (%d vs %d)\n",
2511500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt		      uniform->Name, index, parameter_index);
25120924ba0c3496160a134d37cec800f902ae805b9cEric Anholt	 }
251385c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt      }
251485c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt   }
251599f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt
251699f3c9caa39fbe9dfa7561c919202395720e9472Eric Anholt   talloc_free(sorted_uniforms);
251785c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt}
251885c978f38c819003b6447e8e4feb8b90bb352eeaEric Anholt
25192f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholtstatic void
2520f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergset_uniform_initializer(struct gl_context *ctx, void *mem_ctx,
25212f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt			struct gl_shader_program *shader_program,
25222f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt			const char *name, const glsl_type *type,
25232f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt			ir_constant *val)
25242f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt{
25252f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   if (type->is_record()) {
25262f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      ir_constant *field_constant;
25272f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25282f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      field_constant = (ir_constant *)val->components.get_head();
25292f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25302f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      for (unsigned int i = 0; i < type->length; i++) {
25312f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 const glsl_type *field_type = type->fields.structure[i].type;
25322f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 const char *field_name = talloc_asprintf(mem_ctx, "%s.%s", name,
25332f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt					    type->fields.structure[i].name);
25342f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 set_uniform_initializer(ctx, mem_ctx, shader_program, field_name,
25352f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt				 field_type, field_constant);
25362f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 field_constant = (ir_constant *)field_constant->next;
25372f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      }
25382f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      return;
25392f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   }
25402f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25412f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   int loc = _mesa_get_uniform_location(ctx, shader_program, name);
25422f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25432f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   if (loc == -1) {
2544500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt      fail_link(shader_program,
2545500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt		"Couldn't find uniform for initializer %s\n", name);
2546500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt      return;
25472f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   }
25482f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25492f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   for (unsigned int i = 0; i < (type->is_array() ? type->length : 1); i++) {
25502f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      ir_constant *element;
25512f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      const glsl_type *element_type;
25522f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      if (type->is_array()) {
25532f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 element = val->array_elements[i];
25542f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 element_type = type->fields.array;
25552f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      } else {
25562f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 element = val;
25572f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 element_type = type;
25582f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      }
25592f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25602f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      void *values;
25612f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25622f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      if (element_type->base_type == GLSL_TYPE_BOOL) {
25632f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 int *conv = talloc_array(mem_ctx, int, element_type->components());
25642f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 for (unsigned int j = 0; j < element_type->components(); j++) {
25652f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	    conv[j] = element->value.b[j];
25662f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 }
25672f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 values = (void *)conv;
25682f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 element_type = glsl_type::get_instance(GLSL_TYPE_INT,
25692f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt						element_type->vector_elements,
25702f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt						1);
25712f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      } else {
25722f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 values = &element->value;
25732f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      }
25742f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25752f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      if (element_type->is_matrix()) {
25762f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 _mesa_uniform_matrix(ctx, shader_program,
25772f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt			      element_type->matrix_columns,
25782f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt			      element_type->vector_elements,
25792f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt			      loc, 1, GL_FALSE, (GLfloat *)values);
25802f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 loc += element_type->matrix_columns;
25812f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      } else {
25822f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 _mesa_uniform(ctx, shader_program, loc, element_type->matrix_columns,
25832f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt		       values, element_type->gl_type);
25842f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 loc += type_size(element_type);
25852f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      }
25862f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   }
25872f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt}
25882f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25892f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholtstatic void
2590f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergset_uniform_initializers(struct gl_context *ctx,
25912f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt			 struct gl_shader_program *shader_program)
25922f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt{
25932f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   void *mem_ctx = NULL;
25942f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
25953322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick   for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
25962f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      struct gl_shader *shader = shader_program->_LinkedShaders[i];
25973322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick
25983322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick      if (shader == NULL)
25993322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick	 continue;
26003322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick
26012f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      foreach_iter(exec_list_iterator, iter, *shader->ir) {
26022f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 ir_instruction *ir = (ir_instruction *)iter.get();
26032f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 ir_variable *var = ir->as_variable();
26042f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
26052f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 if (!var || var->mode != ir_var_uniform || !var->constant_value)
26062f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	    continue;
26072f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
26082f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 if (!mem_ctx)
26092f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	    mem_ctx = talloc_new(NULL);
26102f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
26112f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt	 set_uniform_initializer(ctx, mem_ctx, shader_program, var->name,
26122f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt				 var->type, var->constant_value);
26132f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt      }
26142f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   }
26152f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
26162f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   talloc_free(mem_ctx);
26172f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt}
26182f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
2619f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul
2620f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul/**
2621f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * Convert a shader's GLSL IR into a Mesa gl_program.
2622f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul */
26236162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paulstatic struct gl_program *
26246162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paulget_mesa_program(struct gl_context *ctx,
26256162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul                 struct gl_shader_program *shader_program,
262695c08920ea3d040360e5cc51d8a852d21a0329eeEric Anholt		 struct gl_shader *shader)
262784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt{
262884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   ir_to_mesa_visitor v;
262984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   struct prog_instruction *mesa_instructions, *mesa_inst;
2630c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   ir_instruction **mesa_instruction_annotation;
2631c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   int i;
2632364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   struct gl_program *prog;
2633364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   GLenum target;
2634c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt   const char *target_string;
26357b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   GLboolean progress;
26366d3a2c97f4a78e85545286e0e126cd3a27bd1cbdLuca Barbieri   struct gl_shader_compiler_options *options =
26376d3a2c97f4a78e85545286e0e126cd3a27bd1cbdLuca Barbieri         &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
2638364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
2639364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   switch (shader->Type) {
2640c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt   case GL_VERTEX_SHADER:
2641c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt      target = GL_VERTEX_PROGRAM_ARB;
2642c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt      target_string = "vertex";
2643c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt      break;
2644c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt   case GL_FRAGMENT_SHADER:
2645c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt      target = GL_FRAGMENT_PROGRAM_ARB;
2646c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt      target_string = "fragment";
2647c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt      break;
2648903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul   case GL_GEOMETRY_SHADER:
2649903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul      target = GL_GEOMETRY_PROGRAM_NV;
2650903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul      target_string = "geometry";
2651903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul      break;
2652c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt   default:
2653c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt      assert(!"should not be reached");
26544841c0a15adcc722e67d7d246987cd686d3f7a17José Fonseca      return NULL;
2655364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   }
265684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
26571124e5a3cbba839ffd968742bfa3295c8de5498cEric Anholt   validate_ir_tree(shader->ir);
26581124e5a3cbba839ffd968742bfa3295c8de5498cEric Anholt
2659859fd56245c1d725cacab17a34793d41ea14e867Eric Anholt   prog = ctx->Driver.NewProgram(ctx, target, shader_program->Name);
2660364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   if (!prog)
2661364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt      return NULL;
2662364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   prog->Parameters = _mesa_new_parameter_list();
2663364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   prog->Varying = _mesa_new_parameter_list();
2664364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   prog->Attributes = _mesa_new_parameter_list();
2665364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   v.ctx = ctx;
2666364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   v.prog = prog;
2667aa452e20bff9aea2ecb994c9f7b413b0726a04f3Eric Anholt   v.shader_program = shader_program;
26686d3a2c97f4a78e85545286e0e126cd3a27bd1cbdLuca Barbieri   v.options = options;
2669364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
26700924ba0c3496160a134d37cec800f902ae805b9cEric Anholt   add_uniforms_to_parameters_list(shader_program, shader, prog);
26710924ba0c3496160a134d37cec800f902ae805b9cEric Anholt
26727b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /* Emit Mesa IR for main(). */
267316b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholt   visit_exec_list(shader->ir, &v);
2674021222c6a872ca2eef770ebadb8754f659775204Eric Anholt   v.ir_to_mesa_emit_op0(NULL, OPCODE_END);
267584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
26767b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   /* Now emit bodies for any functions that were used. */
26777b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   do {
26787b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      progress = GL_FALSE;
26797b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
26807b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      foreach_iter(exec_list_iterator, iter, v.function_signatures) {
26817b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 function_entry *entry = (function_entry *)iter.get();
26827b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
26837b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 if (!entry->bgn_inst) {
26847b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    v.current_function = entry;
26857b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
26867b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    entry->bgn_inst = v.ir_to_mesa_emit_op0(NULL, OPCODE_BGNSUB);
26877b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    entry->bgn_inst->function = entry;
26887b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
26897b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    visit_exec_list(&entry->sig->body, &v);
26907b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
2691806cb9f9528e3c55c157d7e8bbb751b769b6fcb7Eric Anholt	    ir_to_mesa_instruction *last;
2692806cb9f9528e3c55c157d7e8bbb751b769b6fcb7Eric Anholt	    last = (ir_to_mesa_instruction *)v.instructions.get_tail();
2693806cb9f9528e3c55c157d7e8bbb751b769b6fcb7Eric Anholt	    if (last->op != OPCODE_RET)
2694806cb9f9528e3c55c157d7e8bbb751b769b6fcb7Eric Anholt	       v.ir_to_mesa_emit_op0(NULL, OPCODE_RET);
2695806cb9f9528e3c55c157d7e8bbb751b769b6fcb7Eric Anholt
269640f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	    ir_to_mesa_instruction *end;
269740f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	    end = v.ir_to_mesa_emit_op0(NULL, OPCODE_ENDSUB);
269840f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	    end->function = entry;
269940f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt
27007b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	    progress = GL_TRUE;
27017b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 }
27027b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt      }
27037b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   } while (progress);
27047b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
2705364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   prog->NumTemporaries = v.next_temp;
2706364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
270784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   int num_instructions = 0;
270884771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   foreach_iter(exec_list_iterator, iter, v.instructions) {
270984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      num_instructions++;
271084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
271184771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
271284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   mesa_instructions =
271384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      (struct prog_instruction *)calloc(num_instructions,
271484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt					sizeof(*mesa_instructions));
2715850c659044d081c53713800cacf8d518fae6cd70Eric Anholt   mesa_instruction_annotation = talloc_array(v.mem_ctx, ir_instruction *,
2716364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt					      num_instructions);
271784771df82ed2ed8718013795089edd38cf5bd84dEric Anholt
2718f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul   /* Convert ir_mesa_instructions into prog_instructions.
2719f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul    */
272084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   mesa_inst = mesa_instructions;
2721c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt   i = 0;
272284771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   foreach_iter(exec_list_iterator, iter, v.instructions) {
2723f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul      const ir_to_mesa_instruction *inst = (ir_to_mesa_instruction *)iter.get();
2724b7abce770fe9bb09a6f435d35c1a4afd134fa855Eric Anholt
272584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      mesa_inst->Opcode = inst->op;
2726854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt      mesa_inst->CondUpdate = inst->cond_update;
2727ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt      if (inst->saturate)
2728ac89a90401f08df945248fcc96da59ba0e2bbfa9Eric Anholt	 mesa_inst->SaturateMode = SATURATE_ZERO_ONE;
272984771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      mesa_inst->DstReg.File = inst->dst_reg.file;
273084771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      mesa_inst->DstReg.Index = inst->dst_reg.index;
2731854fd66cbb569cb3d4768196f4c680eff489733eEric Anholt      mesa_inst->DstReg.CondMask = inst->dst_reg.cond_mask;
273212f654c63bc42d353e258cde989d9114cdde26c6Eric Anholt      mesa_inst->DstReg.WriteMask = inst->dst_reg.writemask;
2733f8a2b65bc9bf3dfb4a4aa6fe1c0ea65f78a01922Eric Anholt      mesa_inst->DstReg.RelAddr = inst->dst_reg.reladdr != NULL;
273484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      mesa_inst->SrcReg[0] = mesa_src_reg_from_ir_src_reg(inst->src_reg[0]);
273584771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      mesa_inst->SrcReg[1] = mesa_src_reg_from_ir_src_reg(inst->src_reg[1]);
273684771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      mesa_inst->SrcReg[2] = mesa_src_reg_from_ir_src_reg(inst->src_reg[2]);
2737d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      mesa_inst->TexSrcUnit = inst->sampler;
2738d4f7e660dd81e05b0829c1b70663b3959fd78f47Eric Anholt      mesa_inst->TexSrcTarget = inst->tex_target;
2739b61f4241f314144d3290085cda5db1959d8960a2Eric Anholt      mesa_inst->TexShadow = inst->tex_shadow;
2740c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      mesa_instruction_annotation[i] = inst->ir;
2741aaee40e107cf07a12c8e373d8bb910254f4ba30bEric Anholt
27425755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák      /* Set IndirectRegisterFiles. */
27435755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák      if (mesa_inst->DstReg.RelAddr)
27445755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák         prog->IndirectRegisterFiles |= 1 << mesa_inst->DstReg.File;
27455755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák
2746f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul      /* Update program's bitmask of indirectly accessed register files */
27475755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák      for (unsigned src = 0; src < 3; src++)
27485755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák         if (mesa_inst->SrcReg[src].RelAddr)
27495755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák            prog->IndirectRegisterFiles |= 1 << mesa_inst->SrcReg[src].File;
27505755d1d6a7ff68c7d690d67c4cd64ef8e01ec2edMarek Olšák
27516d3a2c97f4a78e85545286e0e126cd3a27bd1cbdLuca Barbieri      if (options->EmitNoIfs && mesa_inst->Opcode == OPCODE_IF) {
2752500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt	 fail_link(shader_program, "Couldn't flatten if statement\n");
275395c08920ea3d040360e5cc51d8a852d21a0329eeEric Anholt      }
275495c08920ea3d040360e5cc51d8a852d21a0329eeEric Anholt
275540f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt      switch (mesa_inst->Opcode) {
275640f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt      case OPCODE_BGNSUB:
27577b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 inst->function->inst = i;
275840f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	 mesa_inst->Comment = strdup(inst->function->sig->function_name());
275940f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	 break;
276040f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt      case OPCODE_ENDSUB:
276140f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	 mesa_inst->Comment = strdup(inst->function->sig->function_name());
276240f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	 break;
276340f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt      case OPCODE_CAL:
27647b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt	 mesa_inst->BranchTarget = inst->function->sig_id; /* rewritten later */
276540f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	 break;
276640f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt      case OPCODE_ARL:
2767d64343f1ae84979bd154475badf11af8a9bfc2ebEric Anholt	 prog->NumAddressRegs = 1;
276840f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	 break;
276940f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt      default:
277040f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt	 break;
277140f57c2becbb2cee7cfb6d6ed49dc1db57987e9aEric Anholt      }
27727b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt
277384771df82ed2ed8718013795089edd38cf5bd84dEric Anholt      mesa_inst++;
2774c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt      i++;
27756162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul
27766162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul      if (!shader_program->LinkStatus)
27776162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         break;
27786162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul   }
27796162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul
27806162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul   if (!shader_program->LinkStatus) {
27816162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul      free(mesa_instructions);
27826162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul      _mesa_reference_program(ctx, &shader->Program, NULL);
27836162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul      return NULL;
278484771df82ed2ed8718013795089edd38cf5bd84dEric Anholt   }
2785c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
27867b130149427019ac9ae6d39b6871c14fb6ba2dadEric Anholt   set_branchtargets(&v, mesa_instructions, num_instructions);
2787925b49ff310bf0b307add7c34627cddf87e6a554Eric Anholt
2788c8d0a9f0065c321308be635529c95735f3beb68fEric Anholt   if (ctx->Shader.Flags & GLSL_DUMP) {
2789455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt      printf("\n");
2790455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt      printf("GLSL IR for linked %s program %d:\n", target_string,
2791455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt	     shader_program->Name);
2792455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt      _mesa_print_ir(shader->ir, NULL);
2793455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt      printf("\n");
2794455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt      printf("\n");
2795455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt      printf("Mesa IR for linked %s program %d:\n", target_string,
2796455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt	     shader_program->Name);
2797364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt      print_program(mesa_instructions, mesa_instruction_annotation,
2798364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt		    num_instructions);
2799364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   }
2800364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
2801364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   prog->Instructions = mesa_instructions;
2802364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   prog->NumInstructions = num_instructions;
2803364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
2804925b49ff310bf0b307add7c34627cddf87e6a554Eric Anholt   do_set_program_inouts(shader->ir, prog);
2805925b49ff310bf0b307add7c34627cddf87e6a554Eric Anholt   count_resources(prog);
2806925b49ff310bf0b307add7c34627cddf87e6a554Eric Anholt
280716b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholt   _mesa_reference_program(ctx, &shader->Program, prog);
2808364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
280928faa12dc2413d93c7f4778327a5e7c4c8f57c85Eric Anholt   if ((ctx->Shader.Flags & GLSL_NO_OPT) == 0) {
281028faa12dc2413d93c7f4778327a5e7c4c8f57c85Eric Anholt      _mesa_optimize_program(ctx, prog);
281128faa12dc2413d93c7f4778327a5e7c4c8f57c85Eric Anholt   }
281228faa12dc2413d93c7f4778327a5e7c4c8f57c85Eric Anholt
2813364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   return prog;
2814364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt}
2815364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
281616b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholtextern "C" {
2817f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul
2818f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul/**
2819f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * Called via ctx->Driver.CompilerShader().
2820f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * This is a no-op.
2821f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * XXX can we remove the ctx->Driver.CompileShader() hook?
2822f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul */
2823d19eecef54384c163af27a470496ed885a5a271bEric AnholtGLboolean
2824f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_ir_compile_shader(struct gl_context *ctx, struct gl_shader *shader)
2825d19eecef54384c163af27a470496ed885a5a271bEric Anholt{
2826d19eecef54384c163af27a470496ed885a5a271bEric Anholt   assert(shader->CompileStatus);
2827fc63e37b971b641dfdff000ba353c4810414c20eIan Romanick   (void) ctx;
2828d19eecef54384c163af27a470496ed885a5a271bEric Anholt
2829d19eecef54384c163af27a470496ed885a5a271bEric Anholt   return GL_TRUE;
2830d19eecef54384c163af27a470496ed885a5a271bEric Anholt}
2831d19eecef54384c163af27a470496ed885a5a271bEric Anholt
2832f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul
2833f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul/**
2834f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * Link a shader.
2835f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * Called via ctx->Driver.LinkShader()
2836f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * This actually involves converting GLSL IR into Mesa gl_programs with
2837f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * code lowering and other optimizations.
2838f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul */
2839d19eecef54384c163af27a470496ed885a5a271bEric AnholtGLboolean
2840f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_ir_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
2841d19eecef54384c163af27a470496ed885a5a271bEric Anholt{
2842d19eecef54384c163af27a470496ed885a5a271bEric Anholt   assert(prog->LinkStatus);
2843d19eecef54384c163af27a470496ed885a5a271bEric Anholt
28443322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
28453322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick      if (prog->_LinkedShaders[i] == NULL)
28463322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick	 continue;
28473322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick
2848d19eecef54384c163af27a470496ed885a5a271bEric Anholt      bool progress;
2849d19eecef54384c163af27a470496ed885a5a271bEric Anholt      exec_list *ir = prog->_LinkedShaders[i]->ir;
2850f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul      const struct gl_shader_compiler_options *options =
28516d3a2c97f4a78e85545286e0e126cd3a27bd1cbdLuca Barbieri            &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(prog->_LinkedShaders[i]->Type)];
2852d19eecef54384c163af27a470496ed885a5a271bEric Anholt
2853d19eecef54384c163af27a470496ed885a5a271bEric Anholt      do {
2854d19eecef54384c163af27a470496ed885a5a271bEric Anholt	 progress = false;
2855d19eecef54384c163af27a470496ed885a5a271bEric Anholt
2856d19eecef54384c163af27a470496ed885a5a271bEric Anholt	 /* Lowering */
2857d19eecef54384c163af27a470496ed885a5a271bEric Anholt	 do_mat_op_to_vec(ir);
2858c4285be9a5bd1adaa89050989374b95a9a601cdcIan Romanick	 lower_instructions(ir, (MOD_TO_FRACT | DIV_TO_MUL_RCP | EXP_TO_EXP2
2859c4285be9a5bd1adaa89050989374b95a9a601cdcIan Romanick				 | LOG_TO_LOG2
2860c4285be9a5bd1adaa89050989374b95a9a601cdcIan Romanick				 | ((options->EmitNoPow) ? POW_TO_EXP2 : 0)));
2861d19eecef54384c163af27a470496ed885a5a271bEric Anholt
286287708e8c90220cc1997cef9de9b394c04d952be9Luca Barbieri	 progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;
286387708e8c90220cc1997cef9de9b394c04d952be9Luca Barbieri
2864e591c4625cae63660c5000fbab366e40fe154ab0Luca Barbieri	 progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
2865d19eecef54384c163af27a470496ed885a5a271bEric Anholt
286611d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick	 progress = lower_quadop_vector(ir, true) || progress;
286711d6f1c69871d0b7edc28f639256460839fccd2dIan Romanick
2868940df10100d740ef27fa39026fd51c3199ed3d62Kenneth Graunke	 if (options->EmitNoIfs) {
2869940df10100d740ef27fa39026fd51c3199ed3d62Kenneth Graunke	    progress = lower_discard(ir) || progress;
2870d19eecef54384c163af27a470496ed885a5a271bEric Anholt	    progress = do_if_to_cond_assign(ir) || progress;
2871940df10100d740ef27fa39026fd51c3199ed3d62Kenneth Graunke	 }
2872d19eecef54384c163af27a470496ed885a5a271bEric Anholt
28732b70dbfe091af5ae7c788e16275e1af2cb1c284cIan Romanick	 if (options->EmitNoNoise)
28742b70dbfe091af5ae7c788e16275e1af2cb1c284cIan Romanick	    progress = lower_noise(ir) || progress;
28752b70dbfe091af5ae7c788e16275e1af2cb1c284cIan Romanick
2876a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	 /* If there are forms of indirect addressing that the driver
2877a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	  * cannot handle, perform the lowering pass.
2878a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	  */
2879a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	 if (options->EmitNoIndirectInput || options->EmitNoIndirectOutput
2880a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	     || options->EmitNoIndirectTemp || options->EmitNoIndirectUniform)
2881a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	   progress =
2882a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	     lower_variable_index_to_cond_assign(ir,
2883a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick						 options->EmitNoIndirectInput,
2884a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick						 options->EmitNoIndirectOutput,
2885a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick						 options->EmitNoIndirectTemp,
2886a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick						 options->EmitNoIndirectUniform)
2887a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick	     || progress;
2888a6ecd1c3724a78b76ab9e81ea39632f1279021f8Ian Romanick
2889d19eecef54384c163af27a470496ed885a5a271bEric Anholt	 progress = do_vec_index_to_cond_assign(ir) || progress;
2890d19eecef54384c163af27a470496ed885a5a271bEric Anholt      } while (progress);
2891d19eecef54384c163af27a470496ed885a5a271bEric Anholt
2892d19eecef54384c163af27a470496ed885a5a271bEric Anholt      validate_ir_tree(ir);
2893d19eecef54384c163af27a470496ed885a5a271bEric Anholt   }
2894d19eecef54384c163af27a470496ed885a5a271bEric Anholt
28953322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
2896d19eecef54384c163af27a470496ed885a5a271bEric Anholt      struct gl_program *linked_prog;
2897d19eecef54384c163af27a470496ed885a5a271bEric Anholt
28983322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick      if (prog->_LinkedShaders[i] == NULL)
28993322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick	 continue;
29003322fbaf3b5e305ce00c1d08c26965bb98e0cef0Ian Romanick
2901d19eecef54384c163af27a470496ed885a5a271bEric Anholt      linked_prog = get_mesa_program(ctx, prog, prog->_LinkedShaders[i]);
2902d19eecef54384c163af27a470496ed885a5a271bEric Anholt
29036162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul      if (linked_prog) {
29046162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         bool ok = true;
29056162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul
29066162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         switch (prog->_LinkedShaders[i]->Type) {
29076162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         case GL_VERTEX_SHADER:
29086162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul            _mesa_reference_vertprog(ctx, &prog->VertexProgram,
29096162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul                                     (struct gl_vertex_program *)linked_prog);
29106162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul            ok = ctx->Driver.ProgramStringNotify(ctx, GL_VERTEX_PROGRAM_ARB,
29116162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul                                                 linked_prog);
29126162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul            break;
29136162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         case GL_FRAGMENT_SHADER:
29146162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul            _mesa_reference_fragprog(ctx, &prog->FragmentProgram,
29156162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul                                     (struct gl_fragment_program *)linked_prog);
29166162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul            ok = ctx->Driver.ProgramStringNotify(ctx, GL_FRAGMENT_PROGRAM_ARB,
29176162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul                                                 linked_prog);
29186162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul            break;
2919903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul         case GL_GEOMETRY_SHADER:
2920903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul            _mesa_reference_geomprog(ctx, &prog->GeometryProgram,
2921903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul                                     (struct gl_geometry_program *)linked_prog);
2922903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul            ok = ctx->Driver.ProgramStringNotify(ctx, GL_GEOMETRY_PROGRAM_NV,
2923903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul                                                 linked_prog);
2924903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul            break;
29256162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         }
29266162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         if (!ok) {
29276162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul            return GL_FALSE;
29286162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul         }
2929d19eecef54384c163af27a470496ed885a5a271bEric Anholt      }
29306162773ea4b0e84c3ab9c9952fb5e838519c2564Brian Paul
29313cd233eb5714137dccb6218ad78005511bcc02bdEric Anholt      _mesa_reference_program(ctx, &linked_prog, NULL);
2932d19eecef54384c163af27a470496ed885a5a271bEric Anholt   }
2933d19eecef54384c163af27a470496ed885a5a271bEric Anholt
2934d19eecef54384c163af27a470496ed885a5a271bEric Anholt   return GL_TRUE;
2935d19eecef54384c163af27a470496ed885a5a271bEric Anholt}
293616b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholt
2937f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul
2938f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul/**
2939f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * Compile a GLSL shader.  Called via glCompileShader().
2940f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul */
294116b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholtvoid
2942f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader)
2943364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt{
29442462a536ea5c98867296905e3da127eba7c7bdffIan Romanick   struct _mesa_glsl_parse_state *state =
29452462a536ea5c98867296905e3da127eba7c7bdffIan Romanick      new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
29465e18b051c039564d1998818d08caf1bff3983630Ian Romanick
2947153eca98064252be4daad9cc27746f37c245b627Ian Romanick   const char *source = shader->Source;
2948a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke   /* Check if the user called glCompileShader without first calling
2949a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke    * glShaderSource.  This should fail to compile, but not raise a GL_ERROR.
2950a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke    */
2951a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke   if (source == NULL) {
2952a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke      shader->CompileStatus = GL_FALSE;
2953a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke      return;
2954a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke   }
2955a09a8ec12d76e1fb1583fa99cf9f48246c108d7bKenneth Graunke
295606143ea09411aa283ac3633bfbfa4326584cd952Ian Romanick   state->error = preprocess(state, &source, &state->info_log,
29577dcfc44b72f00ba5a38cb02123c80113440f0de9Kenneth Graunke			     &ctx->Extensions, ctx->API);
2958153eca98064252be4daad9cc27746f37c245b627Ian Romanick
29591b708d8f4dd1a853de8537e81e6d5bf8c9f2aed1Eric Anholt   if (ctx->Shader.Flags & GLSL_DUMP) {
29601b708d8f4dd1a853de8537e81e6d5bf8c9f2aed1Eric Anholt      printf("GLSL source for shader %d:\n", shader->Name);
29611b708d8f4dd1a853de8537e81e6d5bf8c9f2aed1Eric Anholt      printf("%s\n", shader->Source);
29621b708d8f4dd1a853de8537e81e6d5bf8c9f2aed1Eric Anholt   }
29631b708d8f4dd1a853de8537e81e6d5bf8c9f2aed1Eric Anholt
2964153eca98064252be4daad9cc27746f37c245b627Ian Romanick   if (!state->error) {
2965153eca98064252be4daad9cc27746f37c245b627Ian Romanick     _mesa_glsl_lexer_ctor(state, source);
2966153eca98064252be4daad9cc27746f37c245b627Ian Romanick     _mesa_glsl_parse(state);
2967153eca98064252be4daad9cc27746f37c245b627Ian Romanick     _mesa_glsl_lexer_dtor(state);
2968153eca98064252be4daad9cc27746f37c245b627Ian Romanick   }
2969364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
29704a6a4316846ead3ec12759c96ecc4b61491aad65Eric Anholt   talloc_free(shader->ir);
297116b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholt   shader->ir = new(shader) exec_list;
2972364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   if (!state->error && !state->translation_unit.is_empty())
297316b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholt      _mesa_ast_to_hir(shader->ir, state);
2974364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
297516b68b1952d0da14b9ce8306efa64988ce46b4b7Eric Anholt   if (!state->error && !shader->ir->is_empty()) {
2976ee7b2b3f44d09c2311887d3524e197b9738580a9Eric Anholt      validate_ir_tree(shader->ir);
2977ee7b2b3f44d09c2311887d3524e197b9738580a9Eric Anholt
29782f4fe151681a6f6afe1d452eece6cf4144f44e49Eric Anholt      /* Do some optimization at compile time to reduce shader IR size
29792f4fe151681a6f6afe1d452eece6cf4144f44e49Eric Anholt       * and reduce later work if the same shader is linked multiple times
29802f4fe151681a6f6afe1d452eece6cf4144f44e49Eric Anholt       */
2981e591c4625cae63660c5000fbab366e40fe154ab0Luca Barbieri      while (do_common_optimization(shader->ir, false, 32))
29822f4fe151681a6f6afe1d452eece6cf4144f44e49Eric Anholt	 ;
2983ee7b2b3f44d09c2311887d3524e197b9738580a9Eric Anholt
2984ee7b2b3f44d09c2311887d3524e197b9738580a9Eric Anholt      validate_ir_tree(shader->ir);
2985364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   }
2986364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
2987364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   shader->symbols = state->symbols;
2988364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
2989364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   shader->CompileStatus = !state->error;
2990364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   shader->InfoLog = state->info_log;
299125f51d3b9b8c36c41cd23d2797b6a06f6e27ff86Ian Romanick   shader->Version = state->language_version;
2992d5be2acae379783c4aa31243e0a88a9e67e6ca7eIan Romanick   memcpy(shader->builtins_to_link, state->builtins_to_link,
2993d5be2acae379783c4aa31243e0a88a9e67e6ca7eIan Romanick	  sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
2994d5be2acae379783c4aa31243e0a88a9e67e6ca7eIan Romanick   shader->num_builtins_to_link = state->num_builtins_to_link;
2995c5ca73e72c27b2e5d7fcf4662b9921ddb3a9627bEric Anholt
2996b42519108dc7ab104cf9ade65a508f54a0294406Eric Anholt   if (ctx->Shader.Flags & GLSL_LOG) {
2997b42519108dc7ab104cf9ade65a508f54a0294406Eric Anholt      _mesa_write_shader_to_file(shader);
2998b42519108dc7ab104cf9ade65a508f54a0294406Eric Anholt   }
2999b42519108dc7ab104cf9ade65a508f54a0294406Eric Anholt
30000df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt   if (ctx->Shader.Flags & GLSL_DUMP) {
30010df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      if (shader->CompileStatus) {
30020df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("GLSL IR for shader %d:\n", shader->Name);
30030df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 _mesa_print_ir(shader->ir, NULL);
30040df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("\n\n");
30050df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      } else {
30060df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("GLSL shader %d failed to compile.\n", shader->Name);
30070df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      }
30080df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      if (shader->InfoLog && shader->InfoLog[0] != 0) {
30090df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("GLSL shader %d info log:\n", shader->Name);
30100df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("%s\n", shader->InfoLog);
30110df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      }
3012455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt   }
3013455290e4281bf53ce2fe248a2adf5163563c44c8Eric Anholt
3014116f1d4f95d8eb0a82b272016590549632c865b3Kenneth Graunke   /* Retain any live IR, but trash the rest. */
30154a6a4316846ead3ec12759c96ecc4b61491aad65Eric Anholt   reparent_ir(shader->ir, shader->ir);
3016116f1d4f95d8eb0a82b272016590549632c865b3Kenneth Graunke
3017364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   talloc_free(state);
3018d19eecef54384c163af27a470496ed885a5a271bEric Anholt
3019d19eecef54384c163af27a470496ed885a5a271bEric Anholt   if (shader->CompileStatus) {
3020d19eecef54384c163af27a470496ed885a5a271bEric Anholt      if (!ctx->Driver.CompileShader(ctx, shader))
3021d19eecef54384c163af27a470496ed885a5a271bEric Anholt	 shader->CompileStatus = GL_FALSE;
3022d19eecef54384c163af27a470496ed885a5a271bEric Anholt   }
3023d19eecef54384c163af27a470496ed885a5a271bEric Anholt}
3024364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
3025f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul
3026f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul/**
3027f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul * Link a GLSL shader program.  Called via glLinkProgram().
3028f1c1ee11d34f4aa5975641a615c7fd2edb32e1c0Brian Paul */
3029364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholtvoid
3030f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_mesa_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
3031364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt{
3032364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   unsigned int i;
3033849e18153cd91d812f694b806a84008498860bc3Eric Anholt
3034364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   _mesa_clear_shader_program_data(ctx, prog);
3035364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
3036849e18153cd91d812f694b806a84008498860bc3Eric Anholt   prog->LinkStatus = GL_TRUE;
3037364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
3038364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   for (i = 0; i < prog->NumShaders; i++) {
3039849e18153cd91d812f694b806a84008498860bc3Eric Anholt      if (!prog->Shaders[i]->CompileStatus) {
3040500e7b75995460537b0e682e5bde4c32eb40b85cEric Anholt	 fail_link(prog, "linking with uncompiled shader");
3041849e18153cd91d812f694b806a84008498860bc3Eric Anholt	 prog->LinkStatus = GL_FALSE;
3042364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt      }
3043364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   }
3044364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
3045364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   prog->Varying = _mesa_new_parameter_list();
3046364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   _mesa_reference_vertprog(ctx, &prog->VertexProgram, NULL);
3047364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   _mesa_reference_fragprog(ctx, &prog->FragmentProgram, NULL);
3048903ead0b26e4fc55474b652adf9470247283e7aaBrian Paul   _mesa_reference_geomprog(ctx, &prog->GeometryProgram, NULL);
3049364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
3050849e18153cd91d812f694b806a84008498860bc3Eric Anholt   if (prog->LinkStatus) {
30515d0f430e8ed01db29d11d22e4b6c3760d8c39f8fEric Anholt      link_shaders(ctx, prog);
3052849e18153cd91d812f694b806a84008498860bc3Eric Anholt   }
3053364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
3054364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   if (prog->LinkStatus) {
30550df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      if (!ctx->Driver.LinkShader(ctx, prog)) {
3056d19eecef54384c163af27a470496ed885a5a271bEric Anholt	 prog->LinkStatus = GL_FALSE;
3057af2ef53a2701426d32382e861d8f238a449e9cd9Eric Anholt      }
3058af2ef53a2701426d32382e861d8f238a449e9cd9Eric Anholt   }
3059af2ef53a2701426d32382e861d8f238a449e9cd9Eric Anholt
30602f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt   set_uniform_initializers(ctx, prog);
30612f5bf20e44d509fa3afbe2cfbb9bb65347daea6aEric Anholt
3062af2ef53a2701426d32382e861d8f238a449e9cd9Eric Anholt   if (ctx->Shader.Flags & GLSL_DUMP) {
3063af2ef53a2701426d32382e861d8f238a449e9cd9Eric Anholt      if (!prog->LinkStatus) {
30640df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("GLSL shader program %d failed to link\n", prog->Name);
30650df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      }
30660df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt
30670df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      if (prog->InfoLog && prog->InfoLog[0] != 0) {
30680df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("GLSL shader program %d info log:\n", prog->Name);
30690df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt	 printf("%s\n", prog->InfoLog);
30700df61bdb669d03d9c25e49d5698f193deca3cf6dEric Anholt      }
3071364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt   }
3072364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt}
3073364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt
3074364fcd8ee1af39e215338fba59306a14dd81c2b2Eric Anholt} /* extern "C" */
3075