disasm.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "disasm.h"
  2. #include "compiler.h"
  3. #include "errors.h"
  4. #include "functions.h"
  5. #include "log.h"
  6. #include "parser.h"
  7. #include "sets.h"
  8. #include "shader_stage.h"
  9. #include "types.h"
  10. #include <assert.h>
  11. #include <inttypes.h>
  12. #include <stddef.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. /*static char *type_string(type_id type) {
  17. if (type == float_id) {
  18. return "float";
  19. }
  20. if (type == float2_id) {
  21. return "float2";
  22. }
  23. if (type == float3_id) {
  24. return "float3";
  25. }
  26. if (type == float4_id) {
  27. return "float4";
  28. }
  29. if (type == float4x4_id) {
  30. return "float4x4";
  31. }
  32. if (type == ray_type_id) {
  33. return "RayDesc";
  34. }
  35. if (type == bvh_type_id) {
  36. return "RaytracingAccelerationStructure";
  37. }
  38. if (type == tex2d_type_id) {
  39. return "Texture2D<float4>";
  40. }
  41. return get_name(get_type(type)->name);
  42. }*/
  43. static void write_functions(void) {
  44. for (function_id i = 0; get_function(i) != NULL; ++i) {
  45. function *f = get_function(i);
  46. if (f->block == NULL) {
  47. continue;
  48. }
  49. kong_log(LOG_LEVEL_INFO, "Function: %s", get_name(f->name));
  50. uint8_t *data = f->code.o;
  51. size_t size = f->code.size;
  52. size_t index = 0;
  53. while (index < size) {
  54. opcode *o = (opcode *)&data[index];
  55. switch (o->type) {
  56. case OPCODE_RETURN:
  57. kong_log(LOG_LEVEL_INFO, "RETURN $%zu", o->op_return.var.index);
  58. break;
  59. case OPCODE_LOAD_ACCESS_LIST: {
  60. char accesses[1024];
  61. int offset = 0;
  62. for (int i = 0; i < o->op_load_access_list.access_list_size; ++i) {
  63. switch (o->op_load_access_list.access_list[i].kind) {
  64. case ACCESS_ELEMENT:
  65. offset += sprintf(&accesses[offset], "$%" PRIu64, o->op_load_access_list.access_list[i].access_element.index.index);
  66. break;
  67. case ACCESS_MEMBER:
  68. offset += sprintf(&accesses[offset], "%s", get_name(o->op_load_access_list.access_list[i].access_member.name));
  69. break;
  70. case ACCESS_SWIZZLE: {
  71. swizzle swizzle = o->op_load_access_list.access_list[i].access_swizzle.swizzle;
  72. char swizzle_chars[4] = "xyzw";
  73. for (uint32_t swizzle_index = 0; swizzle_index < swizzle.size; ++swizzle_index) {
  74. offset += sprintf(&accesses[offset], "%c", swizzle_chars[swizzle.indices[swizzle_index]]);
  75. }
  76. break;
  77. }
  78. }
  79. if (i < o->op_load_access_list.access_list_size - 1) {
  80. offset += sprintf(&accesses[offset], ", ");
  81. }
  82. }
  83. kong_log(LOG_LEVEL_INFO, "$%zu = LOAD_ACCESS_LIST $%zu[%s]", o->op_load_access_list.to.index, o->op_load_access_list.from.index, accesses);
  84. break;
  85. }
  86. case OPCODE_CALL: {
  87. char parameters[256];
  88. int offset = 0;
  89. parameters[0] = 0;
  90. for (int i = 0; i < o->op_call.parameters_size; ++i) {
  91. offset += sprintf(&parameters[offset], "$%" PRIu64, o->op_call.parameters[i].index);
  92. if (i < o->op_call.parameters_size - 1) {
  93. offset += sprintf(&parameters[offset], ", ");
  94. }
  95. }
  96. kong_log(LOG_LEVEL_INFO, "$%zu = CALL %s(%s)", o->op_call.var.index, get_name(o->op_call.func), parameters);
  97. break;
  98. }
  99. case OPCODE_VAR:
  100. break;
  101. case OPCODE_NOT:
  102. break;
  103. case OPCODE_STORE_VARIABLE:
  104. kong_log(LOG_LEVEL_INFO, "$%zu = STORE_VARIABLE $%zu", o->op_store_var.to.index, o->op_store_var.from.index);
  105. break;
  106. case OPCODE_SUB_AND_STORE_VARIABLE:
  107. break;
  108. case OPCODE_ADD_AND_STORE_VARIABLE:
  109. break;
  110. case OPCODE_DIVIDE_AND_STORE_VARIABLE:
  111. break;
  112. case OPCODE_MULTIPLY_AND_STORE_VARIABLE:
  113. break;
  114. case OPCODE_STORE_ACCESS_LIST: {
  115. char accesses[1024];
  116. int offset = 0;
  117. for (int i = 0; i < o->op_store_access_list.access_list_size; ++i) {
  118. switch (o->op_store_access_list.access_list[i].kind) {
  119. case ACCESS_ELEMENT:
  120. offset += sprintf(&accesses[offset], "$%" PRIu64, o->op_store_access_list.access_list[i].access_element.index.index);
  121. break;
  122. case ACCESS_MEMBER:
  123. offset += sprintf(&accesses[offset], "%s", get_name(o->op_store_access_list.access_list[i].access_member.name));
  124. break;
  125. case ACCESS_SWIZZLE: {
  126. swizzle swizzle = o->op_store_access_list.access_list[i].access_swizzle.swizzle;
  127. char swizzle_chars[4] = "xyzw";
  128. for (uint32_t swizzle_index = 0; swizzle_index < swizzle.size; ++swizzle_index) {
  129. offset += sprintf(&accesses[offset], "%c", swizzle_chars[swizzle.indices[swizzle_index]]);
  130. }
  131. break;
  132. }
  133. }
  134. if (i < o->op_store_access_list.access_list_size - 1) {
  135. offset += sprintf(&accesses[offset], ", ");
  136. }
  137. }
  138. kong_log(LOG_LEVEL_INFO, "$%zu[%s] = STORE_ACCESS_LIST $%zu", o->op_store_access_list.to.index, accesses, o->op_store_access_list.from.index);
  139. break;
  140. }
  141. case OPCODE_SUB_AND_STORE_ACCESS_LIST:
  142. break;
  143. case OPCODE_ADD_AND_STORE_ACCESS_LIST:
  144. break;
  145. case OPCODE_DIVIDE_AND_STORE_ACCESS_LIST:
  146. break;
  147. case OPCODE_MULTIPLY_AND_STORE_ACCESS_LIST:
  148. break;
  149. case OPCODE_LOAD_FLOAT_CONSTANT:
  150. kong_log(LOG_LEVEL_INFO, "$%zu = LOAD_FLOAT_CONSTANT %f", o->op_load_float_constant.to.index, o->op_load_float_constant.number);
  151. break;
  152. case OPCODE_LOAD_INT_CONSTANT:
  153. kong_log(LOG_LEVEL_INFO, "$%zu = LOAD_INT_CONSTANT %i", o->op_load_int_constant.to.index, o->op_load_int_constant.number);
  154. break;
  155. case OPCODE_LOAD_BOOL_CONSTANT:
  156. kong_log(LOG_LEVEL_INFO, "$%zu = LOAD_BOOL_CONSTANT %i", o->op_load_bool_constant.to.index, o->op_load_bool_constant.boolean ? 1 : 0);
  157. break;
  158. case OPCODE_ADD:
  159. kong_log(LOG_LEVEL_INFO, "$%zu = ADD $%zu, $%zu", o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
  160. break;
  161. case OPCODE_SUB:
  162. kong_log(LOG_LEVEL_INFO, "$%zu = SUB $%zu, $%zu", o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
  163. break;
  164. case OPCODE_MULTIPLY:
  165. kong_log(LOG_LEVEL_INFO, "$%zu = MULTIPLY $%zu, $%zu", o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
  166. break;
  167. case OPCODE_DIVIDE:
  168. kong_log(LOG_LEVEL_INFO, "$%zu = DIVIDE $%zu, $%zu", o->op_binary.result.index, o->op_binary.left.index, o->op_binary.right.index);
  169. break;
  170. case OPCODE_MOD:
  171. break;
  172. case OPCODE_EQUALS:
  173. break;
  174. case OPCODE_NOT_EQUALS:
  175. break;
  176. case OPCODE_GREATER:
  177. break;
  178. case OPCODE_GREATER_EQUAL:
  179. break;
  180. case OPCODE_LESS:
  181. break;
  182. case OPCODE_LESS_EQUAL:
  183. break;
  184. case OPCODE_AND:
  185. break;
  186. case OPCODE_OR:
  187. break;
  188. case OPCODE_BITWISE_XOR:
  189. break;
  190. case OPCODE_BITWISE_AND:
  191. break;
  192. case OPCODE_BITWISE_OR:
  193. break;
  194. case OPCODE_LEFT_SHIFT:
  195. break;
  196. case OPCODE_RIGHT_SHIFT:
  197. break;
  198. case OPCODE_IF:
  199. break;
  200. case OPCODE_WHILE_START:
  201. break;
  202. case OPCODE_WHILE_CONDITION:
  203. break;
  204. case OPCODE_WHILE_END:
  205. break;
  206. case OPCODE_BLOCK_START:
  207. break;
  208. case OPCODE_BLOCK_END:
  209. break;
  210. default: {
  211. debug_context context = {0};
  212. error(context, "Unknown opcode");
  213. break;
  214. }
  215. }
  216. index += o->size;
  217. }
  218. kong_log(LOG_LEVEL_INFO, "");
  219. }
  220. }
  221. void disassemble(void) {
  222. write_functions();
  223. }