module.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*===-- module.c - tool for testing libLLVM and llvm-c API ----------------===*\
  2. |* *|
  3. |* The LLVM Compiler Infrastructure *|
  4. |* *|
  5. |* This file is distributed under the University of Illinois Open Source *|
  6. |* License. See LICENSE.TXT for details. *|
  7. |* *|
  8. |*===----------------------------------------------------------------------===*|
  9. |* *|
  10. |* This file implements the --module-dump, --module-list-functions and *|
  11. |* --module-list-globals commands in llvm-c-test. *|
  12. |* *|
  13. \*===----------------------------------------------------------------------===*/
  14. #include "llvm-c-test.h"
  15. #include "llvm-c/BitReader.h"
  16. #include "llvm-c/Core.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. static LLVMModuleRef load_module(void) {
  21. LLVMMemoryBufferRef MB;
  22. LLVMModuleRef M;
  23. char *msg = NULL;
  24. if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
  25. fprintf(stderr, "Error reading file: %s\n", msg);
  26. exit(1);
  27. }
  28. if (LLVMParseBitcode(MB, &M, &msg)) {
  29. fprintf(stderr, "Error parsing bitcode: %s\n", msg);
  30. LLVMDisposeMemoryBuffer(MB);
  31. exit(1);
  32. }
  33. LLVMDisposeMemoryBuffer(MB);
  34. return M;
  35. }
  36. int module_dump(void) {
  37. LLVMModuleRef M = load_module();
  38. char *irstr = LLVMPrintModuleToString(M);
  39. puts(irstr);
  40. LLVMDisposeMessage(irstr);
  41. LLVMDisposeModule(M);
  42. return 0;
  43. }
  44. int module_list_functions(void) {
  45. LLVMModuleRef M = load_module();
  46. LLVMValueRef f;
  47. f = LLVMGetFirstFunction(M);
  48. while (f) {
  49. if (LLVMIsDeclaration(f)) {
  50. printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
  51. } else {
  52. LLVMBasicBlockRef bb;
  53. LLVMValueRef isn;
  54. unsigned nisn = 0;
  55. unsigned nbb = 0;
  56. printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
  57. LLVMCountBasicBlocks(f));
  58. for (bb = LLVMGetFirstBasicBlock(f); bb;
  59. bb = LLVMGetNextBasicBlock(bb)) {
  60. nbb++;
  61. for (isn = LLVMGetFirstInstruction(bb); isn;
  62. isn = LLVMGetNextInstruction(isn)) {
  63. nisn++;
  64. if (LLVMIsACallInst(isn)) {
  65. LLVMValueRef callee =
  66. LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
  67. printf(" calls: %s\n", LLVMGetValueName(callee));
  68. }
  69. }
  70. }
  71. printf(" #isn: %u\n", nisn);
  72. printf(" #bb: %u\n\n", nbb);
  73. }
  74. f = LLVMGetNextFunction(f);
  75. }
  76. LLVMDisposeModule(M);
  77. return 0;
  78. }
  79. int module_list_globals(void) {
  80. LLVMModuleRef M = load_module();
  81. LLVMValueRef g;
  82. g = LLVMGetFirstGlobal(M);
  83. while (g) {
  84. LLVMTypeRef T = LLVMTypeOf(g);
  85. char *s = LLVMPrintTypeToString(T);
  86. printf("Global%s: %s %s\n",
  87. LLVMIsDeclaration(g) ? "Declaration" : "Definition",
  88. LLVMGetValueName(g), s);
  89. LLVMDisposeMessage(s);
  90. g = LLVMGetNextGlobal(g);
  91. }
  92. LLVMDisposeModule(M);
  93. return 0;
  94. }