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.1KB

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