Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ir_print_visitor.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. "exp",
  83. "log",
  84. "f2i",
  85. "i2f",
  86. "u2f",
  87. "trunc",
  88. "ceil",
  89. "floor",
  90. "+",
  91. "-",
  92. "*",
  93. "/",
  94. "%",
  95. "<",
  96. ">",
  97. "<=",
  98. ">=",
  99. "==",
  100. "!=",
  101. "<<",
  102. ">>",
  103. "&",
  104. "^",
  105. "|",
  106. "&&",
  107. "^^",
  108. "||",
  109. "!",
  110. "dot",
  111. "min",
  112. "max",
  113. };
  114. printf("(expression ");
  115. assert((unsigned int)ir->operation <
  116. sizeof(operators) / sizeof(operators[0]));
  117. printf("%s", operators[ir->operation]);
  118. printf("(");
  119. if (ir->operands[0])
  120. ir->operands[0]->accept(this);
  121. printf(") ");
  122. printf("(");
  123. if (ir->operands[1])
  124. ir->operands[1]->accept(this);
  125. printf(")) ");
  126. }
  127. void ir_print_visitor::visit(ir_swizzle *ir)
  128. {
  129. const unsigned swiz[4] = {
  130. ir->mask.x,
  131. ir->mask.y,
  132. ir->mask.z,
  133. ir->mask.w,
  134. };
  135. printf("(swiz ");
  136. for (unsigned i = 0; i < ir->mask.num_components; i++) {
  137. printf("%c", "xyzw"[swiz[i]]);
  138. }
  139. printf(" ");
  140. ir->val->accept(this);
  141. printf(")");
  142. }
  143. void ir_print_visitor::visit(ir_dereference *ir)
  144. {
  145. deref_depth++;
  146. switch (ir->mode) {
  147. case ir_dereference::ir_reference_variable: {
  148. printf("(var_ref ");
  149. ir->var->accept(this);
  150. printf(") ");
  151. break;
  152. }
  153. case ir_dereference::ir_reference_array:
  154. printf("(array_ref ");
  155. ir->var->accept(this);
  156. ir->selector.array_index->accept(this);
  157. printf(") ");
  158. break;
  159. case ir_dereference::ir_reference_record:
  160. printf("(record_ref ");
  161. ir->var->accept(this);
  162. printf("(%s)) ", ir->selector.field);
  163. break;
  164. }
  165. deref_depth--;
  166. }
  167. void ir_print_visitor::visit(ir_assignment *ir)
  168. {
  169. printf("(assign (");
  170. if (ir->condition)
  171. ir->condition->accept(this);
  172. else
  173. printf("true");
  174. printf(") (");
  175. ir->lhs->accept(this);
  176. printf(") (");
  177. ir->rhs->accept(this);
  178. printf(") ");
  179. }
  180. void ir_print_visitor::visit(ir_constant *ir)
  181. {
  182. const glsl_type *const base_type = ir->type->get_base_type();
  183. printf("(constant (");
  184. print_type(base_type);
  185. printf(") ");
  186. printf("(%d) (", ir->type->components());
  187. for (unsigned i = 0; i < ir->type->components(); i++) {
  188. if (i != 0)
  189. printf(", ");
  190. switch (base_type->base_type) {
  191. case GLSL_TYPE_UINT: printf("%u", ir->value.u[i]); break;
  192. case GLSL_TYPE_INT: printf("%d", ir->value.i[i]); break;
  193. case GLSL_TYPE_FLOAT: printf("%f", ir->value.f[i]); break;
  194. case GLSL_TYPE_BOOL: printf("%d", ir->value.b[i]); break;
  195. default: assert(0);
  196. }
  197. }
  198. printf(")) ");
  199. }
  200. void
  201. ir_print_visitor::visit(ir_call *ir)
  202. {
  203. printf("(call (%s) ", ir->callee_name());
  204. foreach_iter(exec_list_iterator, iter, *ir) {
  205. ir_instruction *const inst = (ir_instruction *) iter.get();
  206. inst->accept(this);
  207. }
  208. }
  209. void
  210. ir_print_visitor::visit(ir_return *ir)
  211. {
  212. printf("(return");
  213. ir_rvalue *const value = ir->get_value();
  214. if (value) {
  215. printf(" ");
  216. value->accept(this);
  217. }
  218. printf(")");
  219. }