Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ir.h 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2010 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #pragma once
  25. #ifndef IR_H
  26. #define IR_H
  27. #include "list.h"
  28. #include "ir_visitor.h"
  29. struct ir_program {
  30. void *bong_hits;
  31. };
  32. /**
  33. * Base class of all IR instructions
  34. */
  35. class ir_instruction : public exec_node {
  36. public:
  37. const struct glsl_type *type;
  38. class ir_constant *constant_expression_value();
  39. virtual void accept(ir_visitor *) = 0;
  40. /**
  41. * \name IR instruction downcast functions
  42. *
  43. * These functions either cast the object to a derived class or return
  44. * \c NULL if the object's type does not match the specified derived class.
  45. * Additional downcast functions will be added as needed.
  46. */
  47. /*@{*/
  48. virtual class ir_variable * as_variable() { return NULL; }
  49. virtual class ir_function * as_function() { return NULL; }
  50. virtual class ir_dereference * as_dereference() { return NULL; }
  51. virtual class ir_rvalue * as_rvalue() { return NULL; }
  52. virtual class ir_label * as_label() { return NULL; }
  53. virtual class ir_loop * as_loop() { return NULL; }
  54. virtual class ir_assignment * as_assignment() { return NULL; }
  55. virtual class ir_call * as_call() { return NULL; }
  56. virtual class ir_return * as_return() { return NULL; }
  57. virtual class ir_if * as_if() { return NULL; }
  58. virtual class ir_swizzle * as_swizzle() { return NULL; }
  59. virtual class ir_constant * as_constant() { return NULL; }
  60. /*@}*/
  61. protected:
  62. ir_instruction()
  63. {
  64. /* empty */
  65. }
  66. };
  67. class ir_rvalue : public ir_instruction {
  68. public:
  69. virtual ir_rvalue * as_rvalue()
  70. {
  71. return this;
  72. }
  73. virtual bool is_lvalue()
  74. {
  75. return false;
  76. }
  77. protected:
  78. ir_rvalue()
  79. {
  80. /* empty */
  81. }
  82. };
  83. enum ir_variable_mode {
  84. ir_var_auto = 0,
  85. ir_var_uniform,
  86. ir_var_in,
  87. ir_var_out,
  88. ir_var_inout
  89. };
  90. enum ir_varaible_interpolation {
  91. ir_var_smooth = 0,
  92. ir_var_flat,
  93. ir_var_noperspective
  94. };
  95. class ir_variable : public ir_instruction {
  96. public:
  97. ir_variable(const struct glsl_type *, const char *);
  98. virtual ir_variable *as_variable()
  99. {
  100. return this;
  101. }
  102. virtual void accept(ir_visitor *v)
  103. {
  104. v->visit(this);
  105. }
  106. /**
  107. * Duplicate an IR variable
  108. *
  109. * \note
  110. * This will probably be made \c virtual and moved to the base class
  111. * eventually.
  112. */
  113. ir_variable *clone() const
  114. {
  115. ir_variable *var = new ir_variable(type, name);
  116. var->max_array_access = this->max_array_access;
  117. var->read_only = this->read_only;
  118. var->centroid = this->centroid;
  119. var->invariant = this->invariant;
  120. var->mode = this->mode;
  121. var->interpolation = this->interpolation;
  122. return var;
  123. }
  124. const char *name;
  125. /**
  126. * Highest element accessed with a constant expression array index
  127. *
  128. * Not used for non-array variables.
  129. */
  130. unsigned max_array_access;
  131. unsigned read_only:1;
  132. unsigned centroid:1;
  133. unsigned invariant:1;
  134. /** If the variable is initialized outside of the scope of the shader */
  135. unsigned shader_in:1;
  136. /**
  137. * If the variable value is later used outside of the scope of the shader.
  138. */
  139. unsigned shader_out:1;
  140. unsigned mode:3;
  141. unsigned interpolation:2;
  142. /**
  143. * Flag that the whole array is assignable
  144. *
  145. * In GLSL 1.20 and later whole arrays are assignable (and comparable for
  146. * equality). This flag enables this behavior.
  147. */
  148. unsigned array_lvalue:1;
  149. /**
  150. * Emit a warning if this variable is accessed.
  151. */
  152. const char *warn_extension;
  153. /**
  154. * Value assigned in the initializer of a variable declared "const"
  155. */
  156. ir_constant *constant_value;
  157. };
  158. /*@{*/
  159. /**
  160. * The representation of a function instance; may be the full definition or
  161. * simply a prototype.
  162. */
  163. class ir_function_signature : public ir_instruction {
  164. /* An ir_function_signature will be part of the list of signatures in
  165. * an ir_function.
  166. */
  167. public:
  168. ir_function_signature(const glsl_type *return_type);
  169. virtual void accept(ir_visitor *v)
  170. {
  171. v->visit(this);
  172. }
  173. /**
  174. * Get the name of the function for which this is a signature
  175. */
  176. const char *function_name() const;
  177. /**
  178. * Check whether the qualifiers match between this signature's parameters
  179. * and the supplied parameter list. If not, returns the name of the first
  180. * parameter with mismatched qualifiers (for use in error messages).
  181. */
  182. const char *qualifiers_match(exec_list *params);
  183. /**
  184. * Replace the current parameter list with the given one. This is useful
  185. * if the current information came from a prototype, and either has invalid
  186. * or missing parameter names.
  187. */
  188. void replace_parameters(exec_list *new_params);
  189. /**
  190. * Function return type.
  191. *
  192. * \note This discards the optional precision qualifier.
  193. */
  194. const struct glsl_type *return_type;
  195. /**
  196. * List of ir_variable of function parameters.
  197. *
  198. * This represents the storage. The paramaters passed in a particular
  199. * call will be in ir_call::actual_paramaters.
  200. */
  201. struct exec_list parameters;
  202. /** Whether or not this function has a body (which may be empty). */
  203. unsigned is_defined:1;
  204. /** Body of instructions in the function. */
  205. struct exec_list body;
  206. private:
  207. /** Function of which this signature is one overload. */
  208. class ir_function *function;
  209. friend class ir_function;
  210. };
  211. /**
  212. * Header for tracking multiple overloaded functions with the same name.
  213. * Contains a list of ir_function_signatures representing each of the
  214. * actual functions.
  215. */
  216. class ir_function : public ir_instruction {
  217. public:
  218. ir_function(const char *name);
  219. virtual ir_function *as_function()
  220. {
  221. return this;
  222. }
  223. virtual void accept(ir_visitor *v)
  224. {
  225. v->visit(this);
  226. }
  227. void add_signature(ir_function_signature *sig)
  228. {
  229. sig->function = this;
  230. signatures.push_tail(sig);
  231. }
  232. /**
  233. * Get an iterator for the set of function signatures
  234. */
  235. exec_list_iterator iterator()
  236. {
  237. return signatures.iterator();
  238. }
  239. /**
  240. * Find a signature that matches a set of actual parameters, taking implicit
  241. * conversions into account.
  242. */
  243. const ir_function_signature *matching_signature(exec_list *actual_param);
  244. /**
  245. * Find a signature that exactly matches a set of actual parameters without
  246. * any implicit type conversions.
  247. */
  248. ir_function_signature *exact_matching_signature(exec_list *actual_ps);
  249. /**
  250. * Name of the function.
  251. */
  252. const char *name;
  253. private:
  254. /**
  255. * List of ir_function_signature for each overloaded function with this name.
  256. */
  257. struct exec_list signatures;
  258. };
  259. inline const char *ir_function_signature::function_name() const
  260. {
  261. return function->name;
  262. }
  263. /*@}*/
  264. /**
  265. * IR instruction representing high-level if-statements
  266. */
  267. class ir_if : public ir_instruction {
  268. public:
  269. ir_if(ir_rvalue *condition)
  270. : condition(condition)
  271. {
  272. /* empty */
  273. }
  274. virtual ir_if *as_if()
  275. {
  276. return this;
  277. }
  278. virtual void accept(ir_visitor *v)
  279. {
  280. v->visit(this);
  281. }
  282. ir_rvalue *condition;
  283. /** List of ir_instruction for the body of the then branch */
  284. exec_list then_instructions;
  285. /** List of ir_instruction for the body of the else branch */
  286. exec_list else_instructions;
  287. };
  288. /**
  289. * IR instruction representing a high-level loop structure.
  290. */
  291. class ir_loop : public ir_instruction {
  292. public:
  293. ir_loop() : from(NULL), to(NULL), increment(NULL), counter(NULL)
  294. {
  295. /* empty */
  296. }
  297. virtual void accept(ir_visitor *v)
  298. {
  299. v->visit(this);
  300. }
  301. virtual ir_loop *as_loop()
  302. {
  303. return this;
  304. }
  305. /**
  306. * Get an iterator for the instructions of the loop body
  307. */
  308. exec_list_iterator iterator()
  309. {
  310. return body_instructions.iterator();
  311. }
  312. /** List of ir_instruction that make up the body of the loop. */
  313. exec_list body_instructions;
  314. /**
  315. * \name Loop counter and controls
  316. */
  317. /*@{*/
  318. ir_rvalue *from;
  319. ir_rvalue *to;
  320. ir_rvalue *increment;
  321. ir_variable *counter;
  322. /*@}*/
  323. };
  324. class ir_assignment : public ir_rvalue {
  325. public:
  326. ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
  327. virtual void accept(ir_visitor *v)
  328. {
  329. v->visit(this);
  330. }
  331. virtual ir_assignment * as_assignment()
  332. {
  333. return this;
  334. }
  335. /**
  336. * Left-hand side of the assignment.
  337. */
  338. ir_rvalue *lhs;
  339. /**
  340. * Value being assigned
  341. */
  342. ir_rvalue *rhs;
  343. /**
  344. * Optional condition for the assignment.
  345. */
  346. ir_rvalue *condition;
  347. };
  348. /* Update ir_expression::num_operands() and operator_strs when
  349. * updating this list.
  350. */
  351. enum ir_expression_operation {
  352. ir_unop_bit_not,
  353. ir_unop_logic_not,
  354. ir_unop_neg,
  355. ir_unop_abs,
  356. ir_unop_rcp,
  357. ir_unop_rsq,
  358. ir_unop_sqrt,
  359. ir_unop_exp,
  360. ir_unop_log,
  361. ir_unop_exp2,
  362. ir_unop_log2,
  363. ir_unop_f2i, /**< Float-to-integer conversion. */
  364. ir_unop_i2f, /**< Integer-to-float conversion. */
  365. ir_unop_f2b, /**< Float-to-boolean conversion */
  366. ir_unop_b2f, /**< Boolean-to-float conversion */
  367. ir_unop_i2b, /**< int-to-boolean conversion */
  368. ir_unop_b2i, /**< Boolean-to-int conversion */
  369. ir_unop_u2f, /**< Unsigned-to-float conversion. */
  370. /**
  371. * \name Unary floating-point rounding operations.
  372. */
  373. /*@{*/
  374. ir_unop_trunc,
  375. ir_unop_ceil,
  376. ir_unop_floor,
  377. /*@}*/
  378. ir_binop_add,
  379. ir_binop_sub,
  380. ir_binop_mul,
  381. ir_binop_div,
  382. ir_binop_mod,
  383. /**
  384. * \name Binary comparison operators
  385. */
  386. /*@{*/
  387. ir_binop_less,
  388. ir_binop_greater,
  389. ir_binop_lequal,
  390. ir_binop_gequal,
  391. ir_binop_equal,
  392. ir_binop_nequal,
  393. /*@}*/
  394. /**
  395. * \name Bit-wise binary operations.
  396. */
  397. /*@{*/
  398. ir_binop_lshift,
  399. ir_binop_rshift,
  400. ir_binop_bit_and,
  401. ir_binop_bit_xor,
  402. ir_binop_bit_or,
  403. /*@}*/
  404. ir_binop_logic_and,
  405. ir_binop_logic_xor,
  406. ir_binop_logic_or,
  407. ir_binop_dot,
  408. ir_binop_min,
  409. ir_binop_max,
  410. ir_binop_pow
  411. };
  412. class ir_expression : public ir_rvalue {
  413. public:
  414. ir_expression(int op, const struct glsl_type *type,
  415. ir_rvalue *, ir_rvalue *);
  416. static unsigned int get_num_operands(ir_expression_operation);
  417. unsigned int get_num_operands()
  418. {
  419. return get_num_operands(operation);
  420. }
  421. /**
  422. * Return a string representing this expression's operator.
  423. */
  424. const char *operator_string();
  425. /**
  426. * Do a reverse-lookup to translate the given string into an operator.
  427. */
  428. static ir_expression_operation get_operator(const char *);
  429. virtual void accept(ir_visitor *v)
  430. {
  431. v->visit(this);
  432. }
  433. ir_expression *clone();
  434. ir_expression_operation operation;
  435. ir_rvalue *operands[2];
  436. };
  437. /**
  438. * IR instruction representing a function call
  439. */
  440. class ir_call : public ir_rvalue {
  441. public:
  442. ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
  443. : callee(callee)
  444. {
  445. assert(callee->return_type != NULL);
  446. type = callee->return_type;
  447. actual_parameters->move_nodes_to(& this->actual_parameters);
  448. }
  449. virtual ir_call *as_call()
  450. {
  451. return this;
  452. }
  453. virtual void accept(ir_visitor *v)
  454. {
  455. v->visit(this);
  456. }
  457. /**
  458. * Get a generic ir_call object when an error occurs
  459. */
  460. static ir_call *get_error_instruction();
  461. /**
  462. * Get an iterator for the set of acutal parameters
  463. */
  464. exec_list_iterator iterator()
  465. {
  466. return actual_parameters.iterator();
  467. }
  468. /**
  469. * Get the name of the function being called.
  470. */
  471. const char *callee_name() const
  472. {
  473. return callee->function_name();
  474. }
  475. const ir_function_signature *get_callee()
  476. {
  477. return callee;
  478. }
  479. /**
  480. * Generates an inline version of the function before @ir,
  481. * returning the return value of the function.
  482. */
  483. ir_rvalue *generate_inline(ir_instruction *ir);
  484. private:
  485. ir_call()
  486. : callee(NULL)
  487. {
  488. /* empty */
  489. }
  490. const ir_function_signature *callee;
  491. /* List of ir_rvalue of paramaters passed in this call. */
  492. exec_list actual_parameters;
  493. };
  494. /**
  495. * \name Jump-like IR instructions.
  496. *
  497. * These include \c break, \c continue, \c return, and \c discard.
  498. */
  499. /*@{*/
  500. class ir_jump : public ir_instruction {
  501. protected:
  502. ir_jump()
  503. {
  504. /* empty */
  505. }
  506. };
  507. class ir_return : public ir_jump {
  508. public:
  509. ir_return()
  510. : value(NULL)
  511. {
  512. /* empty */
  513. }
  514. ir_return(ir_rvalue *value)
  515. : value(value)
  516. {
  517. /* empty */
  518. }
  519. virtual ir_return *as_return()
  520. {
  521. return this;
  522. }
  523. ir_rvalue *get_value() const
  524. {
  525. return value;
  526. }
  527. virtual void accept(ir_visitor *v)
  528. {
  529. v->visit(this);
  530. }
  531. private:
  532. ir_rvalue *value;
  533. };
  534. /**
  535. * Jump instructions used inside loops
  536. *
  537. * These include \c break and \c continue. The \c break within a loop is
  538. * different from the \c break within a switch-statement.
  539. *
  540. * \sa ir_switch_jump
  541. */
  542. class ir_loop_jump : public ir_jump {
  543. public:
  544. enum jump_mode {
  545. jump_break,
  546. jump_continue
  547. };
  548. ir_loop_jump(ir_loop *loop, jump_mode mode)
  549. : loop(loop), mode(mode)
  550. {
  551. /* empty */
  552. }
  553. virtual void accept(ir_visitor *v)
  554. {
  555. v->visit(this);
  556. }
  557. bool is_break() const
  558. {
  559. return mode == jump_break;
  560. }
  561. bool is_continue() const
  562. {
  563. return mode == jump_continue;
  564. }
  565. private:
  566. /** Loop containing this break instruction. */
  567. ir_loop *loop;
  568. /** Mode selector for the jump instruction. */
  569. enum jump_mode mode;
  570. };
  571. /*@}*/
  572. struct ir_swizzle_mask {
  573. unsigned x:2;
  574. unsigned y:2;
  575. unsigned z:2;
  576. unsigned w:2;
  577. /**
  578. * Number of components in the swizzle.
  579. */
  580. unsigned num_components:3;
  581. /**
  582. * Does the swizzle contain duplicate components?
  583. *
  584. * L-value swizzles cannot contain duplicate components.
  585. */
  586. unsigned has_duplicates:1;
  587. };
  588. class ir_swizzle : public ir_rvalue {
  589. public:
  590. ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
  591. unsigned count);
  592. ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask);
  593. virtual ir_swizzle *as_swizzle()
  594. {
  595. return this;
  596. }
  597. ir_swizzle *clone()
  598. {
  599. return new ir_swizzle(this->val, this->mask);
  600. }
  601. /**
  602. * Construct an ir_swizzle from the textual representation. Can fail.
  603. */
  604. static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
  605. virtual void accept(ir_visitor *v)
  606. {
  607. v->visit(this);
  608. }
  609. bool is_lvalue()
  610. {
  611. return val->is_lvalue() && !mask.has_duplicates;
  612. }
  613. ir_rvalue *val;
  614. ir_swizzle_mask mask;
  615. };
  616. class ir_dereference : public ir_rvalue {
  617. public:
  618. ir_dereference(struct ir_instruction *);
  619. ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
  620. ir_dereference(ir_instruction *variable, const char *field);
  621. virtual ir_dereference *as_dereference()
  622. {
  623. return this;
  624. }
  625. virtual void accept(ir_visitor *v)
  626. {
  627. v->visit(this);
  628. }
  629. bool is_lvalue();
  630. enum {
  631. ir_reference_variable,
  632. ir_reference_array,
  633. ir_reference_record
  634. } mode;
  635. /**
  636. * Object being dereferenced.
  637. *
  638. * Must be either an \c ir_variable or an \c ir_rvalue.
  639. */
  640. ir_instruction *var;
  641. union {
  642. ir_rvalue *array_index;
  643. const char *field;
  644. } selector;
  645. };
  646. class ir_constant : public ir_rvalue {
  647. public:
  648. ir_constant(const struct glsl_type *type, const void *data);
  649. ir_constant(bool b);
  650. ir_constant(unsigned int u);
  651. ir_constant(int i);
  652. ir_constant(float f);
  653. virtual ir_constant *as_constant()
  654. {
  655. return this;
  656. }
  657. virtual void accept(ir_visitor *v)
  658. {
  659. v->visit(this);
  660. }
  661. ir_constant *clone()
  662. {
  663. return new ir_constant(this->type, &this->value);
  664. }
  665. /**
  666. * Value of the constant.
  667. *
  668. * The field used to back the values supplied by the constant is determined
  669. * by the type associated with the \c ir_instruction. Constants may be
  670. * scalars, vectors, or matrices.
  671. */
  672. union {
  673. unsigned u[16];
  674. int i[16];
  675. float f[16];
  676. bool b[16];
  677. } value;
  678. };
  679. void
  680. visit_exec_list(exec_list *list, ir_visitor *visitor);
  681. extern void
  682. _mesa_glsl_initialize_variables(exec_list *instructions,
  683. struct _mesa_glsl_parse_state *state);
  684. extern void
  685. _mesa_glsl_initialize_functions(exec_list *instructions,
  686. struct _mesa_glsl_parse_state *state);
  687. #endif /* IR_H */