Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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_dereference * as_dereference() { return NULL; }
  50. virtual class ir_rvalue * as_rvalue() { return NULL; }
  51. /*@}*/
  52. protected:
  53. ir_instruction()
  54. {
  55. /* empty */
  56. }
  57. };
  58. class ir_rvalue : public ir_instruction {
  59. public:
  60. virtual ir_rvalue * as_rvalue()
  61. {
  62. return this;
  63. }
  64. virtual bool is_lvalue()
  65. {
  66. return false;
  67. }
  68. protected:
  69. ir_rvalue() : ir_instruction() { }
  70. };
  71. enum ir_variable_mode {
  72. ir_var_auto = 0,
  73. ir_var_uniform,
  74. ir_var_in,
  75. ir_var_out,
  76. ir_var_inout
  77. };
  78. enum ir_varaible_interpolation {
  79. ir_var_smooth = 0,
  80. ir_var_flat,
  81. ir_var_noperspective
  82. };
  83. class ir_variable : public ir_instruction {
  84. public:
  85. ir_variable(const struct glsl_type *, const char *);
  86. virtual ir_variable *as_variable()
  87. {
  88. return this;
  89. }
  90. virtual void accept(ir_visitor *v)
  91. {
  92. v->visit(this);
  93. }
  94. /**
  95. * Duplicate an IR variable
  96. *
  97. * \note
  98. * This will probably be made \c virtual and moved to the base class
  99. * eventually.
  100. */
  101. ir_variable *clone() const
  102. {
  103. ir_variable *var = new ir_variable(type, name);
  104. var->max_array_access = this->max_array_access;
  105. var->read_only = this->read_only;
  106. var->centroid = this->centroid;
  107. var->invariant = this->invariant;
  108. var->mode = this->mode;
  109. var->interpolation = this->interpolation;
  110. return var;
  111. }
  112. const char *name;
  113. /**
  114. * Highest element accessed with a constant expression array index
  115. *
  116. * Not used for non-array variables.
  117. */
  118. unsigned max_array_access;
  119. unsigned read_only:1;
  120. unsigned centroid:1;
  121. unsigned invariant:1;
  122. unsigned mode:3;
  123. unsigned interpolation:2;
  124. /**
  125. * Flag that the whole array is assignable
  126. *
  127. * In GLSL 1.20 and later whole arrays are assignable (and comparable for
  128. * equality). This flag enables this behavior.
  129. */
  130. unsigned array_lvalue:1;
  131. };
  132. class ir_label : public ir_instruction {
  133. public:
  134. ir_label(const char *label);
  135. virtual void accept(ir_visitor *v)
  136. {
  137. v->visit(this);
  138. }
  139. const char *label;
  140. };
  141. /*@{*/
  142. class ir_function_signature : public ir_instruction {
  143. public:
  144. ir_function_signature(const glsl_type *return_type);
  145. virtual void accept(ir_visitor *v)
  146. {
  147. v->visit(this);
  148. }
  149. /**
  150. * Get the name of the function for which this is a signature
  151. */
  152. const char *function_name() const;
  153. /**
  154. * Function return type.
  155. *
  156. * \note This discards the optional precision qualifier.
  157. */
  158. const struct glsl_type *return_type;
  159. /**
  160. * List of function parameters stored as ir_variable objects.
  161. */
  162. struct exec_list parameters;
  163. /**
  164. * Pointer to the label that begins the function definition.
  165. */
  166. ir_label *definition;
  167. private:
  168. /** Function of which this signature is one overload. */
  169. class ir_function *function;
  170. friend class ir_function;
  171. };
  172. /**
  173. * Header for tracking functions in the symbol table
  174. */
  175. class ir_function : public ir_instruction {
  176. public:
  177. ir_function(const char *name);
  178. virtual void accept(ir_visitor *v)
  179. {
  180. v->visit(this);
  181. }
  182. void add_signature(ir_function_signature *sig)
  183. {
  184. sig->function = this;
  185. signatures.push_tail(sig);
  186. }
  187. /**
  188. * Get an iterator for the set of function signatures
  189. */
  190. exec_list_iterator iterator()
  191. {
  192. return signatures.iterator();
  193. }
  194. /**
  195. * Find a signature that matches a set of actual parameters.
  196. */
  197. const ir_function_signature *matching_signature(exec_list *actual_param);
  198. /**
  199. * Name of the function.
  200. */
  201. const char *name;
  202. private:
  203. /**
  204. * Set of overloaded functions with this name.
  205. */
  206. struct exec_list signatures;
  207. };
  208. inline const char *ir_function_signature::function_name() const
  209. {
  210. return function->name;
  211. }
  212. /*@}*/
  213. /**
  214. * IR instruction representing high-level if-statements
  215. */
  216. class ir_if : public ir_instruction {
  217. public:
  218. ir_if(ir_rvalue *condition)
  219. : condition(condition)
  220. {
  221. /* empty */
  222. }
  223. virtual void accept(ir_visitor *v)
  224. {
  225. v->visit(this);
  226. }
  227. ir_rvalue *condition;
  228. exec_list then_instructions;
  229. exec_list else_instructions;
  230. };
  231. class ir_assignment : public ir_rvalue {
  232. public:
  233. ir_assignment(ir_rvalue *lhs, ir_rvalue *rhs, ir_rvalue *condition);
  234. virtual void accept(ir_visitor *v)
  235. {
  236. v->visit(this);
  237. }
  238. /**
  239. * Left-hand side of the assignment.
  240. */
  241. ir_rvalue *lhs;
  242. /**
  243. * Value being assigned
  244. */
  245. ir_rvalue *rhs;
  246. /**
  247. * Optional condition for the assignment.
  248. */
  249. ir_rvalue *condition;
  250. };
  251. /* Update ir_expression::num_operands() and ir_print_visitor.cpp when
  252. * updating this list.
  253. */
  254. enum ir_expression_operation {
  255. ir_unop_bit_not,
  256. ir_unop_logic_not,
  257. ir_unop_neg,
  258. ir_unop_abs,
  259. ir_unop_rcp,
  260. ir_unop_rsq,
  261. ir_unop_sqrt,
  262. ir_unop_exp,
  263. ir_unop_log,
  264. ir_unop_exp2,
  265. ir_unop_log2,
  266. ir_unop_f2i, /**< Float-to-integer conversion. */
  267. ir_unop_i2f, /**< Integer-to-float conversion. */
  268. ir_unop_f2b, /**< Float-to-boolean conversion */
  269. ir_unop_b2f, /**< Boolean-to-float conversion */
  270. ir_unop_i2b, /**< int-to-boolean conversion */
  271. ir_unop_b2i, /**< Boolean-to-int conversion */
  272. ir_unop_u2f, /**< Unsigned-to-float conversion. */
  273. /**
  274. * \name Unary floating-point rounding operations.
  275. */
  276. /*@{*/
  277. ir_unop_trunc,
  278. ir_unop_ceil,
  279. ir_unop_floor,
  280. /*@}*/
  281. ir_binop_add,
  282. ir_binop_sub,
  283. ir_binop_mul,
  284. ir_binop_div,
  285. ir_binop_mod,
  286. /**
  287. * \name Binary comparison operators
  288. */
  289. /*@{*/
  290. ir_binop_less,
  291. ir_binop_greater,
  292. ir_binop_lequal,
  293. ir_binop_gequal,
  294. ir_binop_equal,
  295. ir_binop_nequal,
  296. /*@}*/
  297. /**
  298. * \name Bit-wise binary operations.
  299. */
  300. /*@{*/
  301. ir_binop_lshift,
  302. ir_binop_rshift,
  303. ir_binop_bit_and,
  304. ir_binop_bit_xor,
  305. ir_binop_bit_or,
  306. /*@}*/
  307. ir_binop_logic_and,
  308. ir_binop_logic_xor,
  309. ir_binop_logic_or,
  310. ir_binop_dot,
  311. ir_binop_min,
  312. ir_binop_max,
  313. ir_binop_pow
  314. };
  315. class ir_expression : public ir_rvalue {
  316. public:
  317. ir_expression(int op, const struct glsl_type *type,
  318. ir_rvalue *, ir_rvalue *);
  319. unsigned int get_num_operands(void);
  320. virtual void accept(ir_visitor *v)
  321. {
  322. v->visit(this);
  323. }
  324. ir_expression_operation operation;
  325. ir_rvalue *operands[2];
  326. };
  327. /**
  328. * IR instruction representing a function call
  329. */
  330. class ir_call : public ir_rvalue {
  331. public:
  332. ir_call(const ir_function_signature *callee, exec_list *actual_parameters)
  333. : ir_rvalue(), callee(callee)
  334. {
  335. assert(callee->return_type != NULL);
  336. type = callee->return_type;
  337. actual_parameters->move_nodes_to(& this->actual_parameters);
  338. }
  339. virtual void accept(ir_visitor *v)
  340. {
  341. v->visit(this);
  342. }
  343. /**
  344. * Get a generic ir_call object when an error occurs
  345. */
  346. static ir_call *get_error_instruction();
  347. /**
  348. * Get an iterator for the set of acutal parameters
  349. */
  350. exec_list_iterator iterator()
  351. {
  352. return actual_parameters.iterator();
  353. }
  354. /**
  355. * Get the name of the function being called.
  356. */
  357. const char *callee_name() const
  358. {
  359. return callee->function_name();
  360. }
  361. private:
  362. ir_call()
  363. : ir_rvalue(), callee(NULL)
  364. {
  365. /* empty */
  366. }
  367. const ir_function_signature *callee;
  368. exec_list actual_parameters;
  369. };
  370. /**
  371. * \name Jump-like IR instructions.
  372. *
  373. * These include \c break, \c continue, \c return, and \c discard.
  374. */
  375. /*@{*/
  376. class ir_jump : public ir_instruction {
  377. protected:
  378. ir_jump()
  379. : ir_instruction()
  380. {
  381. /* empty */
  382. }
  383. };
  384. class ir_return : public ir_jump {
  385. public:
  386. ir_return()
  387. : value(NULL)
  388. {
  389. /* empty */
  390. }
  391. ir_return(ir_rvalue *value)
  392. : value(value)
  393. {
  394. /* empty */
  395. }
  396. ir_rvalue *get_value() const
  397. {
  398. return value;
  399. }
  400. virtual void accept(ir_visitor *v)
  401. {
  402. v->visit(this);
  403. }
  404. private:
  405. ir_rvalue *value;
  406. };
  407. /*@}*/
  408. struct ir_swizzle_mask {
  409. unsigned x:2;
  410. unsigned y:2;
  411. unsigned z:2;
  412. unsigned w:2;
  413. /**
  414. * Number of components in the swizzle.
  415. */
  416. unsigned num_components:3;
  417. /**
  418. * Does the swizzle contain duplicate components?
  419. *
  420. * L-value swizzles cannot contain duplicate components.
  421. */
  422. unsigned has_duplicates:1;
  423. };
  424. class ir_swizzle : public ir_rvalue {
  425. public:
  426. ir_swizzle(ir_rvalue *, unsigned x, unsigned y, unsigned z, unsigned w,
  427. unsigned count);
  428. /**
  429. * Construct an ir_swizzle from the textual representation. Can fail.
  430. */
  431. static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
  432. virtual void accept(ir_visitor *v)
  433. {
  434. v->visit(this);
  435. }
  436. bool is_lvalue()
  437. {
  438. return val->is_lvalue() && !mask.has_duplicates;
  439. }
  440. ir_rvalue *val;
  441. ir_swizzle_mask mask;
  442. };
  443. class ir_dereference : public ir_rvalue {
  444. public:
  445. ir_dereference(struct ir_instruction *);
  446. ir_dereference(ir_instruction *variable, ir_rvalue *array_index);
  447. virtual ir_dereference *as_dereference()
  448. {
  449. return this;
  450. }
  451. virtual void accept(ir_visitor *v)
  452. {
  453. v->visit(this);
  454. }
  455. bool is_lvalue();
  456. enum {
  457. ir_reference_variable,
  458. ir_reference_array,
  459. ir_reference_record
  460. } mode;
  461. /**
  462. * Object being dereferenced.
  463. *
  464. * Must be either an \c ir_variable or an \c ir_rvalue.
  465. */
  466. ir_instruction *var;
  467. union {
  468. ir_rvalue *array_index;
  469. const char *field;
  470. } selector;
  471. };
  472. class ir_constant : public ir_rvalue {
  473. public:
  474. ir_constant(const struct glsl_type *type, const void *data);
  475. ir_constant(bool b);
  476. ir_constant(unsigned int u);
  477. ir_constant(int i);
  478. ir_constant(float f);
  479. virtual void accept(ir_visitor *v)
  480. {
  481. v->visit(this);
  482. }
  483. /**
  484. * Value of the constant.
  485. *
  486. * The field used to back the values supplied by the constant is determined
  487. * by the type associated with the \c ir_instruction. Constants may be
  488. * scalars, vectors, or matrices.
  489. */
  490. union {
  491. unsigned u[16];
  492. int i[16];
  493. float f[16];
  494. bool b[16];
  495. } value;
  496. };
  497. extern void
  498. _mesa_glsl_initialize_variables(exec_list *instructions,
  499. struct _mesa_glsl_parse_state *state);
  500. extern void
  501. _mesa_glsl_initialize_functions(exec_list *instructions,
  502. struct _mesa_glsl_parse_state *state);
  503. #endif /* IR_H */