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_print_visitor.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include <cstdio>
  24. #include "ir_print_visitor.h"
  25. #include "glsl_types.h"
  26. static void
  27. print_type(const glsl_type *t)
  28. {
  29. if (t->base_type == GLSL_TYPE_ARRAY) {
  30. printf("array (");
  31. print_type(t->fields.array);
  32. printf(") (%u))", t->length);
  33. } else if (t->base_type == GLSL_TYPE_STRUCT) {
  34. printf("struct (%s %u ", t->name ? t->name : "@", t->length);
  35. printf("(FINISHME: structure fields go here) ");
  36. printf(")");
  37. } else {
  38. printf("%s", t->name);
  39. }
  40. }
  41. void ir_print_visitor::visit(ir_variable *ir)
  42. {
  43. if (deref_depth) {
  44. printf("(%s)", ir->name);
  45. } else {
  46. printf("(declare ");
  47. const char *const cent = (ir->centroid) ? "centroid " : "";
  48. const char *const inv = (ir->invariant) ? "invariant " : "";
  49. const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " };
  50. const char *const interp[] = { "", "flat", "noperspective" };
  51. printf("(%s%s%s%s) ",
  52. cent, inv, mode[ir->mode], interp[ir->interpolation]);
  53. printf("(");
  54. print_type(ir->type);
  55. printf(") ");
  56. printf("(%s)) ", ir->name);
  57. }
  58. }
  59. void ir_print_visitor::visit(ir_label *ir)
  60. {
  61. printf("\n(label %s)", ir->label);
  62. }
  63. void ir_print_visitor::visit(ir_function_signature *ir)
  64. {
  65. printf("%s:%d:\n", __func__, __LINE__);
  66. (void) ir;
  67. }
  68. void ir_print_visitor::visit(ir_function *ir)
  69. {
  70. printf("(function %s\n", ir->name);
  71. printf(")\n");
  72. }
  73. void ir_print_visitor::visit(ir_expression *ir)
  74. {
  75. static const char *const operators[] = {
  76. "~",
  77. "!",
  78. "-",
  79. "abs",
  80. "rcp",
  81. "rsq",
  82. "sqrt",
  83. "exp",
  84. "log",
  85. "exp2",
  86. "log2",
  87. "f2i",
  88. "i2f",
  89. "f2b",
  90. "b2f",
  91. "i2b",
  92. "b2i",
  93. "u2f",
  94. "trunc",
  95. "ceil",
  96. "floor",
  97. "+",
  98. "-",
  99. "*",
  100. "/",
  101. "%",
  102. "<",
  103. ">",
  104. "<=",
  105. ">=",
  106. "==",
  107. "!=",
  108. "<<",
  109. ">>",
  110. "&",
  111. "^",
  112. "|",
  113. "&&",
  114. "^^",
  115. "||",
  116. "dot",
  117. "min",
  118. "max",
  119. "pow",
  120. };
  121. printf("(expression ");
  122. assert((unsigned int)ir->operation <
  123. sizeof(operators) / sizeof(operators[0]));
  124. printf("%s", operators[ir->operation]);
  125. printf("(");
  126. if (ir->operands[0])
  127. ir->operands[0]->accept(this);
  128. printf(") ");
  129. printf("(");
  130. if (ir->operands[1])
  131. ir->operands[1]->accept(this);
  132. printf(")) ");
  133. }
  134. void ir_print_visitor::visit(ir_swizzle *ir)
  135. {
  136. const unsigned swiz[4] = {
  137. ir->mask.x,
  138. ir->mask.y,
  139. ir->mask.z,
  140. ir->mask.w,
  141. };
  142. printf("(swiz ");
  143. for (unsigned i = 0; i < ir->mask.num_components; i++) {
  144. printf("%c", "xyzw"[swiz[i]]);
  145. }
  146. printf(" ");
  147. ir->val->accept(this);
  148. printf(")");
  149. }
  150. void ir_print_visitor::visit(ir_dereference *ir)
  151. {
  152. deref_depth++;
  153. switch (ir->mode) {
  154. case ir_dereference::ir_reference_variable: {
  155. printf("(var_ref ");
  156. ir->var->accept(this);
  157. printf(") ");
  158. break;
  159. }
  160. case ir_dereference::ir_reference_array:
  161. printf("(array_ref ");
  162. ir->var->accept(this);
  163. ir->selector.array_index->accept(this);
  164. printf(") ");
  165. break;
  166. case ir_dereference::ir_reference_record:
  167. printf("(record_ref ");
  168. ir->var->accept(this);
  169. printf("(%s)) ", ir->selector.field);
  170. break;
  171. }
  172. deref_depth--;
  173. }
  174. void ir_print_visitor::visit(ir_assignment *ir)
  175. {
  176. printf("(assign (");
  177. if (ir->condition)
  178. ir->condition->accept(this);
  179. else
  180. printf("true");
  181. printf(") (");
  182. ir->lhs->accept(this);
  183. printf(") (");
  184. ir->rhs->accept(this);
  185. printf(") ");
  186. }
  187. void ir_print_visitor::visit(ir_constant *ir)
  188. {
  189. const glsl_type *const base_type = ir->type->get_base_type();
  190. printf("(constant (");
  191. print_type(base_type);
  192. printf(") ");
  193. printf("(%d) (", ir->type->components());
  194. for (unsigned i = 0; i < ir->type->components(); i++) {
  195. if (i != 0)
  196. printf(", ");
  197. switch (base_type->base_type) {
  198. case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
  199. case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
  200. case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
  201. case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
  202. default: assert(0);
  203. }
  204. }
  205. printf(")) ");
  206. }
  207. void
  208. ir_print_visitor::visit(ir_call *ir)
  209. {
  210. printf("(call (%s) ", ir->callee_name());
  211. foreach_iter(exec_list_iterator, iter, *ir) {
  212. ir_instruction *const inst = (ir_instruction *) iter.get();
  213. inst->accept(this);
  214. }
  215. }
  216. void
  217. ir_print_visitor::visit(ir_return *ir)
  218. {
  219. printf("(return");
  220. ir_rvalue *const value = ir->get_value();
  221. if (value) {
  222. printf(" ");
  223. value->accept(this);
  224. }
  225. printf(")");
  226. }
  227. void
  228. ir_print_visitor::visit(ir_if *ir)
  229. {
  230. printf("(if ");
  231. ir->condition->accept(this);
  232. printf("(\n");
  233. foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
  234. ir_instruction *const inst = (ir_instruction *) iter.get();
  235. inst->accept(this);
  236. printf("\n");
  237. }
  238. printf(")\n");
  239. printf("(\n");
  240. foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
  241. ir_instruction *const inst = (ir_instruction *) iter.get();
  242. inst->accept(this);
  243. printf("\n");
  244. }
  245. printf("))\n");
  246. }
  247. void
  248. ir_print_visitor::visit(ir_loop *ir)
  249. {
  250. printf("(loop (");
  251. if (ir->counter != NULL)
  252. ir->counter->accept(this);
  253. printf(") (");
  254. if (ir->from != NULL)
  255. ir->from->accept(this);
  256. printf(") (");
  257. if (ir->to != NULL)
  258. ir->to->accept(this);
  259. printf(") (");
  260. if (ir->increment != NULL)
  261. ir->increment->accept(this);
  262. printf(") (\n");
  263. foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
  264. ir_instruction *const inst = (ir_instruction *) iter.get();
  265. inst->accept(this);
  266. printf("\n");
  267. }
  268. printf("))\n");
  269. }
  270. void
  271. ir_print_visitor::visit(ir_loop_jump *ir)
  272. {
  273. printf("%s", ir->is_break() ? "break" : "continue");
  274. }