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_constant_expression.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file ir_constant_expression.cpp
  25. * Evaluate and process constant valued expressions
  26. *
  27. * In GLSL, constant valued expressions are used in several places. These
  28. * must be processed and evaluated very early in the compilation process.
  29. *
  30. * * Sizes of arrays
  31. * * Initializers for uniforms
  32. * * Initializers for \c const variables
  33. */
  34. #define NULL 0
  35. #include "ir.h"
  36. #include "ir_visitor.h"
  37. #include "glsl_types.h"
  38. /**
  39. * Visitor class for evaluating constant expressions
  40. */
  41. class ir_constant_visitor : public ir_visitor {
  42. public:
  43. ir_constant_visitor()
  44. : value(NULL)
  45. {
  46. /* empty */
  47. }
  48. virtual ~ir_constant_visitor()
  49. {
  50. /* empty */
  51. }
  52. /**
  53. * \name Visit methods
  54. *
  55. * As typical for the visitor pattern, there must be one \c visit method for
  56. * each concrete subclass of \c ir_instruction. Virtual base classes within
  57. * the hierarchy should not have \c visit methods.
  58. */
  59. /*@{*/
  60. virtual void visit(ir_variable *);
  61. virtual void visit(ir_label *);
  62. virtual void visit(ir_function_signature *);
  63. virtual void visit(ir_function *);
  64. virtual void visit(ir_expression *);
  65. virtual void visit(ir_swizzle *);
  66. virtual void visit(ir_dereference *);
  67. virtual void visit(ir_assignment *);
  68. virtual void visit(ir_constant *);
  69. virtual void visit(ir_call *);
  70. virtual void visit(ir_return *);
  71. virtual void visit(ir_if *);
  72. virtual void visit(ir_loop *);
  73. virtual void visit(ir_loop_jump *);
  74. /*@}*/
  75. /**
  76. * Value of the constant expression.
  77. *
  78. * \note
  79. * This field will be \c NULL if the expression is not constant valued.
  80. */
  81. /* FINIHSME: This cannot hold values for constant arrays or structures. */
  82. ir_constant *value;
  83. };
  84. ir_constant *
  85. ir_instruction::constant_expression_value()
  86. {
  87. ir_constant_visitor visitor;
  88. this->accept(& visitor);
  89. return visitor.value;
  90. }
  91. void
  92. ir_constant_visitor::visit(ir_variable *ir)
  93. {
  94. (void) ir;
  95. value = NULL;
  96. }
  97. void
  98. ir_constant_visitor::visit(ir_label *ir)
  99. {
  100. (void) ir;
  101. value = NULL;
  102. }
  103. void
  104. ir_constant_visitor::visit(ir_function_signature *ir)
  105. {
  106. (void) ir;
  107. value = NULL;
  108. }
  109. void
  110. ir_constant_visitor::visit(ir_function *ir)
  111. {
  112. (void) ir;
  113. value = NULL;
  114. }
  115. void
  116. ir_constant_visitor::visit(ir_expression *ir)
  117. {
  118. value = NULL;
  119. ir_constant *op[2];
  120. unsigned int operand, c;
  121. unsigned u[16];
  122. int i[16];
  123. float f[16];
  124. bool b[16];
  125. const glsl_type *type = NULL;
  126. for (operand = 0; operand < ir->get_num_operands(); operand++) {
  127. op[operand] = ir->operands[operand]->constant_expression_value();
  128. if (!op[operand])
  129. return;
  130. }
  131. switch (ir->operation) {
  132. case ir_unop_logic_not:
  133. type = ir->operands[0]->type;
  134. assert(type->base_type == GLSL_TYPE_BOOL);
  135. for (c = 0; c < ir->operands[0]->type->components(); c++)
  136. b[c] = !op[0]->value.b[c];
  137. break;
  138. case ir_unop_f2i:
  139. assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
  140. type = ir->type;
  141. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  142. i[c] = op[0]->value.f[c];
  143. }
  144. break;
  145. case ir_unop_i2f:
  146. assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
  147. op[0]->type->base_type == GLSL_TYPE_INT);
  148. type = ir->type;
  149. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  150. if (op[0]->type->base_type == GLSL_TYPE_INT)
  151. f[c] = op[0]->value.i[c];
  152. else
  153. f[c] = op[0]->value.u[c];
  154. }
  155. break;
  156. case ir_binop_add:
  157. if (ir->operands[0]->type == ir->operands[1]->type) {
  158. type = ir->operands[0]->type;
  159. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  160. switch (ir->operands[0]->type->base_type) {
  161. case GLSL_TYPE_UINT:
  162. u[c] = op[0]->value.u[c] + op[1]->value.u[c];
  163. break;
  164. case GLSL_TYPE_INT:
  165. i[c] = op[0]->value.i[c] + op[1]->value.i[c];
  166. break;
  167. case GLSL_TYPE_FLOAT:
  168. f[c] = op[0]->value.f[c] + op[1]->value.f[c];
  169. break;
  170. default:
  171. assert(0);
  172. }
  173. }
  174. }
  175. break;
  176. case ir_binop_sub:
  177. if (ir->operands[0]->type == ir->operands[1]->type) {
  178. type = ir->operands[0]->type;
  179. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  180. switch (ir->operands[0]->type->base_type) {
  181. case GLSL_TYPE_UINT:
  182. u[c] = op[0]->value.u[c] - op[1]->value.u[c];
  183. break;
  184. case GLSL_TYPE_INT:
  185. i[c] = op[0]->value.i[c] - op[1]->value.i[c];
  186. break;
  187. case GLSL_TYPE_FLOAT:
  188. f[c] = op[0]->value.f[c] - op[1]->value.f[c];
  189. break;
  190. default:
  191. assert(0);
  192. }
  193. }
  194. }
  195. break;
  196. case ir_binop_mul:
  197. if (ir->operands[0]->type == ir->operands[1]->type &&
  198. !ir->operands[0]->type->is_matrix()) {
  199. type = ir->operands[0]->type;
  200. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  201. switch (ir->operands[0]->type->base_type) {
  202. case GLSL_TYPE_UINT:
  203. u[c] = op[0]->value.u[c] * op[1]->value.u[c];
  204. break;
  205. case GLSL_TYPE_INT:
  206. i[c] = op[0]->value.i[c] * op[1]->value.i[c];
  207. break;
  208. case GLSL_TYPE_FLOAT:
  209. f[c] = op[0]->value.f[c] * op[1]->value.f[c];
  210. break;
  211. default:
  212. assert(0);
  213. }
  214. }
  215. }
  216. break;
  217. case ir_binop_div:
  218. if (ir->operands[0]->type == ir->operands[1]->type) {
  219. type = ir->operands[0]->type;
  220. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  221. switch (ir->operands[0]->type->base_type) {
  222. case GLSL_TYPE_UINT:
  223. u[c] = op[0]->value.u[c] / op[1]->value.u[c];
  224. break;
  225. case GLSL_TYPE_INT:
  226. i[c] = op[0]->value.i[c] / op[1]->value.i[c];
  227. break;
  228. case GLSL_TYPE_FLOAT:
  229. f[c] = op[0]->value.f[c] / op[1]->value.f[c];
  230. break;
  231. default:
  232. assert(0);
  233. }
  234. }
  235. }
  236. break;
  237. case ir_binop_logic_and:
  238. type = ir->operands[0]->type;
  239. assert(type->base_type == GLSL_TYPE_BOOL);
  240. for (c = 0; c < ir->operands[0]->type->components(); c++)
  241. b[c] = op[0]->value.b[c] && op[1]->value.b[c];
  242. break;
  243. case ir_binop_logic_xor:
  244. type = ir->operands[0]->type;
  245. assert(type->base_type == GLSL_TYPE_BOOL);
  246. for (c = 0; c < ir->operands[0]->type->components(); c++)
  247. b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
  248. break;
  249. case ir_binop_logic_or:
  250. type = ir->operands[0]->type;
  251. assert(type->base_type == GLSL_TYPE_BOOL);
  252. for (c = 0; c < ir->operands[0]->type->components(); c++)
  253. b[c] = op[0]->value.b[c] || op[1]->value.b[c];
  254. break;
  255. case ir_binop_less:
  256. type = glsl_type::bool_type;
  257. switch (ir->operands[0]->type->base_type) {
  258. case GLSL_TYPE_UINT:
  259. b[0] = op[0]->value.u[0] < op[1]->value.u[0];
  260. break;
  261. case GLSL_TYPE_INT:
  262. b[0] = op[0]->value.i[0] < op[1]->value.i[0];
  263. break;
  264. case GLSL_TYPE_FLOAT:
  265. b[0] = op[0]->value.f[0] < op[1]->value.f[0];
  266. break;
  267. default:
  268. assert(0);
  269. }
  270. break;
  271. case ir_binop_greater:
  272. type = glsl_type::bool_type;
  273. switch (ir->operands[0]->type->base_type) {
  274. case GLSL_TYPE_UINT:
  275. b[0] = op[0]->value.u[0] > op[1]->value.u[0];
  276. break;
  277. case GLSL_TYPE_INT:
  278. b[0] = op[0]->value.i[0] > op[1]->value.i[0];
  279. break;
  280. case GLSL_TYPE_FLOAT:
  281. b[0] = op[0]->value.f[0] > op[1]->value.f[0];
  282. break;
  283. default:
  284. assert(0);
  285. }
  286. break;
  287. case ir_binop_lequal:
  288. type = glsl_type::bool_type;
  289. switch (ir->operands[0]->type->base_type) {
  290. case GLSL_TYPE_UINT:
  291. b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
  292. break;
  293. case GLSL_TYPE_INT:
  294. b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
  295. break;
  296. case GLSL_TYPE_FLOAT:
  297. b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
  298. break;
  299. default:
  300. assert(0);
  301. }
  302. break;
  303. case ir_binop_gequal:
  304. type = glsl_type::bool_type;
  305. switch (ir->operands[0]->type->base_type) {
  306. case GLSL_TYPE_UINT:
  307. b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
  308. break;
  309. case GLSL_TYPE_INT:
  310. b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
  311. break;
  312. case GLSL_TYPE_FLOAT:
  313. b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
  314. break;
  315. default:
  316. assert(0);
  317. }
  318. break;
  319. case ir_binop_equal:
  320. if (ir->operands[0]->type == ir->operands[1]->type) {
  321. type = glsl_type::bool_type;
  322. b[0] = true;
  323. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  324. switch (ir->operands[0]->type->base_type) {
  325. case GLSL_TYPE_UINT:
  326. b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c];
  327. break;
  328. case GLSL_TYPE_INT:
  329. b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c];
  330. break;
  331. case GLSL_TYPE_FLOAT:
  332. b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c];
  333. break;
  334. case GLSL_TYPE_BOOL:
  335. b[0] = b[0] && op[0]->value.b[c] == op[1]->value.b[c];
  336. break;
  337. default:
  338. assert(0);
  339. }
  340. }
  341. }
  342. break;
  343. case ir_binop_nequal:
  344. if (ir->operands[0]->type == ir->operands[1]->type) {
  345. type = glsl_type::bool_type;
  346. b[0] = false;
  347. for (c = 0; c < ir->operands[0]->type->components(); c++) {
  348. switch (ir->operands[0]->type->base_type) {
  349. case GLSL_TYPE_UINT:
  350. b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c];
  351. break;
  352. case GLSL_TYPE_INT:
  353. b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c];
  354. break;
  355. case GLSL_TYPE_FLOAT:
  356. b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c];
  357. break;
  358. case GLSL_TYPE_BOOL:
  359. b[0] = b[0] || op[0]->value.b[c] != op[1]->value.b[c];
  360. break;
  361. default:
  362. assert(0);
  363. }
  364. }
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. if (type) {
  371. switch (type->base_type) {
  372. case GLSL_TYPE_UINT:
  373. value = new ir_constant(type, u);
  374. break;
  375. case GLSL_TYPE_INT:
  376. value = new ir_constant(type, i);
  377. break;
  378. case GLSL_TYPE_FLOAT:
  379. value = new ir_constant(type, f);
  380. break;
  381. case GLSL_TYPE_BOOL:
  382. value = new ir_constant(type, b);
  383. break;
  384. }
  385. }
  386. }
  387. void
  388. ir_constant_visitor::visit(ir_swizzle *ir)
  389. {
  390. (void) ir;
  391. value = NULL;
  392. }
  393. void
  394. ir_constant_visitor::visit(ir_dereference *ir)
  395. {
  396. value = NULL;
  397. if (ir->mode == ir_dereference::ir_reference_variable) {
  398. ir_variable *var = ir->var->as_variable();
  399. if (var && var->constant_value) {
  400. value = new ir_constant(ir->type, &var->constant_value->value);
  401. }
  402. }
  403. /* FINISHME: Other dereference modes. */
  404. }
  405. void
  406. ir_constant_visitor::visit(ir_assignment *ir)
  407. {
  408. (void) ir;
  409. value = NULL;
  410. }
  411. void
  412. ir_constant_visitor::visit(ir_constant *ir)
  413. {
  414. value = ir;
  415. }
  416. void
  417. ir_constant_visitor::visit(ir_call *ir)
  418. {
  419. (void) ir;
  420. value = NULL;
  421. }
  422. void
  423. ir_constant_visitor::visit(ir_return *ir)
  424. {
  425. (void) ir;
  426. value = NULL;
  427. }
  428. void
  429. ir_constant_visitor::visit(ir_if *ir)
  430. {
  431. (void) ir;
  432. value = NULL;
  433. }
  434. void
  435. ir_constant_visitor::visit(ir_loop *ir)
  436. {
  437. (void) ir;
  438. value = NULL;
  439. }
  440. void
  441. ir_constant_visitor::visit(ir_loop_jump *ir)
  442. {
  443. (void) ir;
  444. value = NULL;
  445. }