123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832 |
- /* -*- c++ -*- */
- /*
- * Copyright © 2010 Intel Corporation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
- #pragma once
- #ifndef IR_H
- #define IR_H
-
- #include "list.h"
- #include "ir_visitor.h"
-
- struct ir_program {
- void *bong_hits;
- };
-
- /**
- * Base class of all IR instructions
- */
- class ir_instruction : public exec_node {
- public:
- const struct glsl_type *type;
-
- class ir_constant *constant_expression_value();
- virtual void accept(ir_visitor *) = 0;
-
- /**
- * \name IR instruction downcast functions
- *
- * These functions either cast the object to a derived class or return
- * \c NULL if the object's type does not match the specified derived class.
- * Additional downcast functions will be added as needed.
- */
- /*@{*/
- virtual class ir_variable * as_variable() { return NULL; }
- virtual class ir_function * as_function() { return NULL; }
- virtual class ir_dereference * as_dereference() { return NULL; }
- virtual class ir_rvalue * as_rvalue() { return NULL; }
- virtual class ir_label * as_label() { return NULL; }
- virtual class ir_loop * as_loop() { return NULL; }
- virtual class ir_assignment * as_assignment() { return NULL; }
- virtual class ir_call * as_call() { return NULL; }
- virtual class ir_return * as_return() { return NULL; }
- virtual class ir_if * as_if() { return NULL; }
- virtual class ir_swizzle * as_swizzle() { return NULL; }
- virtual class ir_constant * as_constant() { return NULL; }
- /*@}*/
-
- protected:
- ir_instruction()
- {
- /* empty */
- }
- };
-
-
- class ir_rvalue : public ir_instruction {
- public:
- virtual ir_rvalue * as_rvalue()
- {
- return this;
- }
-
- virtual bool is_lvalue()
- {
- return false;
- }
-
- protected:
- ir_rvalue()
- {
- /* empty */
- }
- };
-
-
- enum ir_variable_mode {
- ir_var_auto = 0,
- ir_var_uniform,
- ir_var_in,
- ir_var_out,
- ir_var_inout
- };
-
- enum ir_varaible_interpolation {
- ir_var_smooth = 0,
- ir_var_flat,
- ir_var_noperspective
- };
-
-
- class ir_variable : public ir_instruction {
- public:
- ir_variable(const struct glsl_type *, const char *);
-
- virtual ir_variable *as_variable()
- {
- return this;
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- /**
- * Duplicate an IR variable
- *
- * \note
- * This will probably be made \c virtual and moved to the base class
- * eventually.
- */
- ir_variable *clone() const
- {
- ir_variable *var = new ir_variable(type, name);
-
- var->max_array_access = this->max_array_access;
- var->read_only = this->read_only;
- var->centroid = this->centroid;
- var->invariant = this->invariant;
- var->mode = this->mode;
- var->interpolation = this->interpolation;
-
- return var;
- }
-
- const char *name;
-
- /**
- * Highest element accessed with a constant expression array index
- *
- * Not used for non-array variables.
- */
- unsigned max_array_access;
-
- unsigned read_only:1;
- unsigned centroid:1;
- unsigned invariant:1;
- /** If the variable is initialized outside of the scope of the shader */
- unsigned shader_in:1;
- /**
- * If the variable value is later used outside of the scope of the shader.
- */
- unsigned shader_out:1;
-
- unsigned mode:3;
- unsigned interpolation:2;
-
- /**
- * Flag that the whole array is assignable
- *
- * In GLSL 1.20 and later whole arrays are assignable (and comparable for
- * equality). This flag enables this behavior.
- */
- unsigned array_lvalue:1;
-
- /**
- * Emit a warning if this variable is accessed.
- */
- const char *warn_extension;
-
- /**
- * Value assigned in the initializer of a variable declared "const"
- */
- ir_constant *constant_value;
- };
-
-
- /*@{*/
- /**
- * The representation of a function instance; may be the full definition or
- * simply a prototype.
- */
- class ir_function_signature : public ir_instruction {
- /* An ir_function_signature will be part of the list of signatures in
- * an ir_function.
- */
- public:
- ir_function_signature(const glsl_type *return_type);
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- /**
- * Get the name of the function for which this is a signature
- */
- const char *function_name() const;
-
- /**
- * Check whether the qualifiers match between this signature's parameters
- * and the supplied parameter list. If not, returns the name of the first
- * parameter with mismatched qualifiers (for use in error messages).
- */
- const char *qualifiers_match(exec_list *params);
-
- /**
- * Replace the current parameter list with the given one. This is useful
- * if the current information came from a prototype, and either has invalid
- * or missing parameter names.
- */
- void replace_parameters(exec_list *new_params);
-
- /**
- * Function return type.
- *
- * \note This discards the optional precision qualifier.
- */
- const struct glsl_type *return_type;
-
- /**
- * List of ir_variable of function parameters.
- *
- * This represents the storage. The paramaters passed in a particular
- * call will be in ir_call::actual_paramaters.
- */
- struct exec_list parameters;
-
- /** Whether or not this function has a body (which may be empty). */
- unsigned is_defined:1;
-
- /** Body of instructions in the function. */
- struct exec_list body;
-
- private:
- /** Function of which this signature is one overload. */
- class ir_function *function;
-
- friend class ir_function;
- };
-
-
- /**
- * Header for tracking multiple overloaded functions with the same name.
- * Contains a list of ir_function_signatures representing each of the
- * actual functions.
- */
- class ir_function : public ir_instruction {
- public:
- ir_function(const char *name);
-
- virtual ir_function *as_function()
- {
- return this;
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- void add_signature(ir_function_signature *sig)
- {
- sig->function = this;
- signatures.push_tail(sig);
- }
-
- /**
- * Get an iterator for the set of function signatures
- */
- exec_list_iterator iterator()
- {
- return signatures.iterator();
- }
-
- /**
- * Find a signature that matches a set of actual parameters, taking implicit
- * conversions into account.
- */
- const ir_function_signature *matching_signature(exec_list *actual_param);
-
- /**
- * Find a signature that exactly matches a set of actual parameters without
- * any implicit type conversions.
- */
- ir_function_signature *exact_matching_signature(exec_list *actual_ps);
-
- /**
- * Name of the function.
- */
- const char *name;
-
- private:
- /**
- * List of ir_function_signature for each overloaded function with this name.
- */
- struct exec_list signatures;
- };
-
- inline const char *ir_function_signature::function_name() const
- {
- return function->name;
- }
- /*@}*/
-
-
- /**
- * IR instruction representing high-level if-statements
- */
- class ir_if : public ir_instruction {
- public:
- ir_if(ir_rvalue *condition)
- : condition(condition)
- {
- /* empty */
- }
-
- virtual ir_if *as_if()
- {
- return this;
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- ir_rvalue *condition;
- /** List of ir_instruction for the body of the then branch */
- exec_list then_instructions;
- /** List of ir_instruction for the body of the else branch */
- exec_list else_instructions;
- };
-
-
- /**
- * IR instruction representing a high-level loop structure.
- */
- class ir_loop : public ir_instruction {
- public:
- ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
- {
- /* empty */
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- virtual ir_loop *as_loop()
- {
- return this;
- }
-
- /**
- * Get an iterator for the instructions of the loop body
- */
- exec_list_iterator iterator()
- {
- return body_instructions.iterator();
- }
-
- /** List of ir_instruction that make up the body of the loop. */
- exec_list body_instructions;
-
- /**
- * \name Loop counter and controls
- */
- /*@{*/
- ir_rvalue *from;
- ir_rvalue *to;
- ir_rvalue *increment;
- ir_variable *counter;
- /*@}*/
- };
-
-
- class ir_assignment : public ir_rvalue {
- public:
- ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- virtual ir_assignment * as_assignment()
- {
- return this;
- }
-
- /**
- * Left-hand side of the assignment.
- */
- ir_rvalue *lhs;
-
- /**
- * Value being assigned
- */
- ir_rvalue *rhs;
-
- /**
- * Optional condition for the assignment.
- */
- ir_rvalue *condition;
- };
-
- /* Update ir_expression::num_operands() and operator_strs when
- * updating this list.
- */
- enum ir_expression_operation {
- ir_unop_bit_not,
- ir_unop_logic_not,
- ir_unop_neg,
- ir_unop_abs,
- ir_unop_rcp,
- ir_unop_rsq,
- ir_unop_sqrt,
- ir_unop_exp,
- ir_unop_log,
- ir_unop_exp2,
- ir_unop_log2,
- ir_unop_f2i, /**< Float-to-integer conversion. */
- ir_unop_i2f, /**< Integer-to-float conversion. */
- ir_unop_f2b, /**< Float-to-boolean conversion */
- ir_unop_b2f, /**< Boolean-to-float conversion */
- ir_unop_i2b, /**< int-to-boolean conversion */
- ir_unop_b2i, /**< Boolean-to-int conversion */
- ir_unop_u2f, /**< Unsigned-to-float conversion. */
-
- /**
- * \name Unary floating-point rounding operations.
- */
- /*@{*/
- ir_unop_trunc,
- ir_unop_ceil,
- ir_unop_floor,
- /*@}*/
-
- ir_binop_add,
- ir_binop_sub,
- ir_binop_mul,
- ir_binop_div,
- ir_binop_mod,
-
- /**
- * \name Binary comparison operators
- */
- /*@{*/
- ir_binop_less,
- ir_binop_greater,
- ir_binop_lequal,
- ir_binop_gequal,
- ir_binop_equal,
- ir_binop_nequal,
- /*@}*/
-
- /**
- * \name Bit-wise binary operations.
- */
- /*@{*/
- ir_binop_lshift,
- ir_binop_rshift,
- ir_binop_bit_and,
- ir_binop_bit_xor,
- ir_binop_bit_or,
- /*@}*/
-
- ir_binop_logic_and,
- ir_binop_logic_xor,
- ir_binop_logic_or,
-
- ir_binop_dot,
- ir_binop_min,
- ir_binop_max,
-
- ir_binop_pow
- };
-
- class ir_expression : public ir_rvalue {
- public:
- ir_expression(int op, const struct glsl_type *type,
- ir_rvalue *, ir_rvalue *);
-
- static unsigned int get_num_operands(ir_expression_operation);
- unsigned int get_num_operands()
- {
- return get_num_operands(operation);
- }
-
- /**
- * Return a string representing this expression's operator.
- */
- const char *operator_string();
-
- /**
- * Do a reverse-lookup to translate the given string into an operator.
- */
- static ir_expression_operation get_operator(const char *);
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- ir_expression *clone();
-
- ir_expression_operation operation;
- ir_rvalue *operands[2];
- };
-
-
- /**
- * IR instruction representing a function call
- */
- class ir_call : public ir_rvalue {
- public:
- ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
- : callee(callee)
- {
- assert(callee->return_type != NULL);
- type = callee->return_type;
- actual_parameters->move_nodes_to(& this->actual_parameters);
- }
-
- virtual ir_call *as_call()
- {
- return this;
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- /**
- * Get a generic ir_call object when an error occurs
- */
- static ir_call *get_error_instruction();
-
- /**
- * Get an iterator for the set of acutal parameters
- */
- exec_list_iterator iterator()
- {
- return actual_parameters.iterator();
- }
-
- /**
- * Get the name of the function being called.
- */
- const char *callee_name() const
- {
- return callee->function_name();
- }
-
- const ir_function_signature *get_callee()
- {
- return callee;
- }
-
- /**
- * Generates an inline version of the function before @ir,
- * returning the return value of the function.
- */
- ir_rvalue *generate_inline(ir_instruction *ir);
-
- private:
- ir_call()
- : callee(NULL)
- {
- /* empty */
- }
-
- const ir_function_signature *callee;
-
- /* List of ir_rvalue of paramaters passed in this call. */
- exec_list actual_parameters;
- };
-
-
- /**
- * \name Jump-like IR instructions.
- *
- * These include \c break, \c continue, \c return, and \c discard.
- */
- /*@{*/
- class ir_jump : public ir_instruction {
- protected:
- ir_jump()
- {
- /* empty */
- }
- };
-
- class ir_return : public ir_jump {
- public:
- ir_return()
- : value(NULL)
- {
- /* empty */
- }
-
- ir_return(ir_rvalue *value)
- : value(value)
- {
- /* empty */
- }
-
- virtual ir_return *as_return()
- {
- return this;
- }
-
- ir_rvalue *get_value() const
- {
- return value;
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- private:
- ir_rvalue *value;
- };
-
-
- /**
- * Jump instructions used inside loops
- *
- * These include \c break and \c continue. The \c break within a loop is
- * different from the \c break within a switch-statement.
- *
- * \sa ir_switch_jump
- */
- class ir_loop_jump : public ir_jump {
- public:
- enum jump_mode {
- jump_break,
- jump_continue
- };
-
- ir_loop_jump(ir_loop *loop, jump_mode mode)
- : loop(loop), mode(mode)
- {
- /* empty */
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- bool is_break() const
- {
- return mode == jump_break;
- }
-
- bool is_continue() const
- {
- return mode == jump_continue;
- }
-
- private:
- /** Loop containing this break instruction. */
- ir_loop *loop;
-
- /** Mode selector for the jump instruction. */
- enum jump_mode mode;
- };
- /*@}*/
-
-
- struct ir_swizzle_mask {
- unsigned x:2;
- unsigned y:2;
- unsigned z:2;
- unsigned w:2;
-
- /**
- * Number of components in the swizzle.
- */
- unsigned num_components:3;
-
- /**
- * Does the swizzle contain duplicate components?
- *
- * L-value swizzles cannot contain duplicate components.
- */
- unsigned has_duplicates:1;
- };
-
-
- class ir_swizzle : public ir_rvalue {
- public:
- ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
- unsigned count);
- ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
-
- virtual ir_swizzle *as_swizzle()
- {
- return this;
- }
-
- ir_swizzle *clone()
- {
- return new ir_swizzle(this->val, this->mask);
- }
-
- /**
- * Construct an ir_swizzle from the textual representation. Can fail.
- */
- static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- bool is_lvalue()
- {
- return val->is_lvalue() && !mask.has_duplicates;
- }
-
- ir_rvalue *val;
- ir_swizzle_mask mask;
- };
-
-
- class ir_dereference : public ir_rvalue {
- public:
- ir_dereference(struct ir_instruction *);
-
- ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
-
- ir_dereference(ir_instruction *variable, const char *field);
-
- virtual ir_dereference *as_dereference()
- {
- return this;
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- bool is_lvalue();
-
- enum {
- ir_reference_variable,
- ir_reference_array,
- ir_reference_record
- } mode;
-
- /**
- * Object being dereferenced.
- *
- * Must be either an \c ir_variable or an \c ir_rvalue.
- */
- ir_instruction *var;
-
- union {
- ir_rvalue *array_index;
- const char *field;
- } selector;
- };
-
-
- class ir_constant : public ir_rvalue {
- public:
- ir_constant(const struct glsl_type *type, const void *data);
- ir_constant(bool b);
- ir_constant(unsigned int u);
- ir_constant(int i);
- ir_constant(float f);
-
- virtual ir_constant *as_constant()
- {
- return this;
- }
-
- virtual void accept(ir_visitor *v)
- {
- v->visit(this);
- }
-
- ir_constant *clone()
- {
- return new ir_constant(this->type, &this->value);
- }
-
- /**
- * Value of the constant.
- *
- * The field used to back the values supplied by the constant is determined
- * by the type associated with the \c ir_instruction. Constants may be
- * scalars, vectors, or matrices.
- */
- union {
- unsigned u[16];
- int i[16];
- float f[16];
- bool b[16];
- } value;
- };
-
- void
- visit_exec_list(exec_list *list, ir_visitor *visitor);
-
- extern void
- _mesa_glsl_initialize_variables(exec_list *instructions,
- struct _mesa_glsl_parse_state *state);
-
- extern void
- _mesa_glsl_initialize_functions(exec_list *instructions,
- struct _mesa_glsl_parse_state *state);
-
- #endif /* IR_H */
|