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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "ir_print_visitor.h"
  24. #include "glsl_types.h"
  25. #include "glsl_parser_extras.h"
  26. static void print_type(const glsl_type *t);
  27. void
  28. ir_instruction::print(void) const
  29. {
  30. ir_instruction *deconsted = const_cast<ir_instruction *>(this);
  31. ir_print_visitor v;
  32. deconsted->accept(&v);
  33. }
  34. void
  35. _mesa_print_ir(exec_list *instructions,
  36. struct _mesa_glsl_parse_state *state)
  37. {
  38. for (unsigned i = 0; i < state->num_user_structures; i++) {
  39. const glsl_type *const s = state->user_structures[i];
  40. printf("(structure (%s) (%s@%p) (%u) (\n",
  41. s->name, s->name, s, s->length);
  42. for (unsigned j = 0; j < s->length; j++) {
  43. printf("\t((");
  44. print_type(s->fields.structure[j].type);
  45. printf(")(%s))\n", s->fields.structure[j].name);
  46. }
  47. printf(")\n");
  48. }
  49. printf("(\n");
  50. foreach_iter(exec_list_iterator, iter, *instructions) {
  51. ((ir_instruction *)iter.get())->print();
  52. printf("\n");
  53. }
  54. printf("\n)");
  55. }
  56. static void
  57. print_type(const glsl_type *t)
  58. {
  59. if (t->base_type == GLSL_TYPE_ARRAY) {
  60. printf("(array ");
  61. print_type(t->fields.array);
  62. printf(" %u)", t->length);
  63. } else if ((t->base_type == GLSL_TYPE_STRUCT)
  64. && (strncmp("gl_", t->name, 3) != 0)) {
  65. printf("%s@%p", t->name, t);
  66. } else {
  67. printf("%s", t->name);
  68. }
  69. }
  70. void ir_print_visitor::visit(ir_variable *ir)
  71. {
  72. printf("(declare ");
  73. const char *const cent = (ir->centroid) ? "centroid " : "";
  74. const char *const inv = (ir->invariant) ? "invariant " : "";
  75. const char *const mode[] = { "", "uniform ", "in ", "out ", "inout " };
  76. const char *const interp[] = { "", "flat", "noperspective" };
  77. printf("(%s%s%s%s) ",
  78. cent, inv, mode[ir->mode], interp[ir->interpolation]);
  79. print_type(ir->type);
  80. printf(" %s)", ir->name);
  81. }
  82. void ir_print_visitor::visit(ir_function_signature *ir)
  83. {
  84. printf("(signature ");
  85. print_type(ir->return_type);
  86. printf("\n (parameters\n");
  87. foreach_iter(exec_list_iterator, iter, ir->parameters) {
  88. ir_variable *const inst = (ir_variable *) iter.get();
  89. inst->accept(this);
  90. printf("\n");
  91. }
  92. printf(" )\n(");
  93. foreach_iter(exec_list_iterator, iter, ir->body) {
  94. ir_instruction *const inst = (ir_instruction *) iter.get();
  95. inst->accept(this);
  96. printf("\n");
  97. }
  98. printf("))\n");
  99. }
  100. void ir_print_visitor::visit(ir_function *ir)
  101. {
  102. printf("(function %s\n", ir->name);
  103. foreach_iter(exec_list_iterator, iter, *ir) {
  104. ir_function_signature *const sig = (ir_function_signature *) iter.get();
  105. sig->accept(this);
  106. printf("\n");
  107. }
  108. printf(")\n");
  109. }
  110. void ir_print_visitor::visit(ir_expression *ir)
  111. {
  112. printf("(expression ");
  113. print_type(ir->type);
  114. printf(" %s ", ir->operator_string());
  115. if (ir->operands[0])
  116. ir->operands[0]->accept(this);
  117. if (ir->operands[1])
  118. ir->operands[1]->accept(this);
  119. printf(") ");
  120. }
  121. void ir_print_visitor::visit(ir_texture *ir)
  122. {
  123. printf("(%s ", ir->opcode_string());
  124. ir->sampler->accept(this);
  125. printf(" ");
  126. ir->coordinate->accept(this);
  127. printf(" (%d %d %d) ", ir->offsets[0], ir->offsets[1], ir->offsets[2]);
  128. if (ir->op != ir_txf) {
  129. if (ir->projector)
  130. ir->projector->accept(this);
  131. else
  132. printf("1");
  133. if (ir->shadow_comparitor) {
  134. printf(" ");
  135. ir->shadow_comparitor->accept(this);
  136. } else {
  137. printf(" ()");
  138. }
  139. }
  140. printf(" ");
  141. switch (ir->op)
  142. {
  143. case ir_tex:
  144. break;
  145. case ir_txb:
  146. ir->lod_info.bias->accept(this);
  147. break;
  148. case ir_txl:
  149. case ir_txf:
  150. ir->lod_info.lod->accept(this);
  151. break;
  152. case ir_txd:
  153. printf("(");
  154. ir->lod_info.grad.dPdx->accept(this);
  155. printf(" ");
  156. ir->lod_info.grad.dPdy->accept(this);
  157. printf(")");
  158. break;
  159. };
  160. printf(")");
  161. }
  162. void ir_print_visitor::visit(ir_swizzle *ir)
  163. {
  164. const unsigned swiz[4] = {
  165. ir->mask.x,
  166. ir->mask.y,
  167. ir->mask.z,
  168. ir->mask.w,
  169. };
  170. printf("(swiz ");
  171. for (unsigned i = 0; i < ir->mask.num_components; i++) {
  172. printf("%c", "xyzw"[swiz[i]]);
  173. }
  174. printf(" ");
  175. ir->val->accept(this);
  176. printf(")");
  177. }
  178. void ir_print_visitor::visit(ir_dereference_variable *ir)
  179. {
  180. printf("(var_ref %s) ", ir->variable_referenced()->name);
  181. }
  182. void ir_print_visitor::visit(ir_dereference_array *ir)
  183. {
  184. printf("(array_ref ");
  185. ir->array->accept(this);
  186. ir->array_index->accept(this);
  187. printf(") ");
  188. }
  189. void ir_print_visitor::visit(ir_dereference_record *ir)
  190. {
  191. printf("(record_ref ");
  192. ir->record->accept(this);
  193. printf(" %s) ", ir->field);
  194. }
  195. void ir_print_visitor::visit(ir_assignment *ir)
  196. {
  197. printf("(assign ");
  198. if (ir->condition)
  199. ir->condition->accept(this);
  200. else
  201. printf("(constant bool (1))");
  202. printf(" ");
  203. ir->lhs->accept(this);
  204. printf(" ");
  205. ir->rhs->accept(this);
  206. printf(") ");
  207. }
  208. void ir_print_visitor::visit(ir_constant *ir)
  209. {
  210. const glsl_type *const base_type = ir->type->get_base_type();
  211. printf("(constant ");
  212. print_type(ir->type);
  213. printf(" (");
  214. for (unsigned i = 0; i < ir->type->components(); i++) {
  215. if (i != 0)
  216. printf(", ");
  217. switch (base_type->base_type) {
  218. case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
  219. case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
  220. case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
  221. case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
  222. default: assert(0);
  223. }
  224. }
  225. printf(")) ");
  226. }
  227. void
  228. ir_print_visitor::visit(ir_call *ir)
  229. {
  230. printf("(call %s (", ir->callee_name());
  231. foreach_iter(exec_list_iterator, iter, *ir) {
  232. ir_instruction *const inst = (ir_instruction *) iter.get();
  233. inst->accept(this);
  234. }
  235. printf("))\n");
  236. }
  237. void
  238. ir_print_visitor::visit(ir_return *ir)
  239. {
  240. printf("(return");
  241. ir_rvalue *const value = ir->get_value();
  242. if (value) {
  243. printf(" ");
  244. value->accept(this);
  245. }
  246. printf(")");
  247. }
  248. void
  249. ir_print_visitor::visit(ir_if *ir)
  250. {
  251. printf("(if ");
  252. ir->condition->accept(this);
  253. printf("(\n");
  254. foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
  255. ir_instruction *const inst = (ir_instruction *) iter.get();
  256. inst->accept(this);
  257. printf("\n");
  258. }
  259. printf(")\n");
  260. printf("(\n");
  261. foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
  262. ir_instruction *const inst = (ir_instruction *) iter.get();
  263. inst->accept(this);
  264. printf("\n");
  265. }
  266. printf("))\n");
  267. }
  268. void
  269. ir_print_visitor::visit(ir_loop *ir)
  270. {
  271. printf("(loop (");
  272. if (ir->counter != NULL)
  273. ir->counter->accept(this);
  274. printf(") (");
  275. if (ir->from != NULL)
  276. ir->from->accept(this);
  277. printf(") (");
  278. if (ir->to != NULL)
  279. ir->to->accept(this);
  280. printf(") (");
  281. if (ir->increment != NULL)
  282. ir->increment->accept(this);
  283. printf(") (\n");
  284. foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
  285. ir_instruction *const inst = (ir_instruction *) iter.get();
  286. inst->accept(this);
  287. printf("\n");
  288. }
  289. printf("))\n");
  290. }
  291. void
  292. ir_print_visitor::visit(ir_loop_jump *ir)
  293. {
  294. printf("%s", ir->is_break() ? "break" : "continue");
  295. }