Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 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 | |
| 15 | #include "llvm-c-test.h" |
| 16 | #include "llvm-c/BitReader.h" |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
Rafael Espindola | 2339ffe | 2015-12-18 23:46:42 +0000 | [diff] [blame] | 21 | static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) { |
| 22 | char *CErr = LLVMGetDiagInfoDescription(DI); |
| 23 | fprintf(stderr, "Error with new bitcode parser: %s\n", CErr); |
| 24 | LLVMDisposeMessage(CErr); |
| 25 | exit(1); |
| 26 | } |
| 27 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 28 | LLVMModuleRef llvm_load_module(bool Lazy, bool New) { |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 29 | LLVMMemoryBufferRef MB; |
| 30 | LLVMModuleRef M; |
| 31 | char *msg = NULL; |
| 32 | |
| 33 | if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { |
| 34 | fprintf(stderr, "Error reading file: %s\n", msg); |
| 35 | exit(1); |
| 36 | } |
| 37 | |
Rafael Espindola | 125592d | 2015-12-18 03:57:26 +0000 | [diff] [blame] | 38 | LLVMBool Ret; |
Rafael Espindola | 2339ffe | 2015-12-18 23:46:42 +0000 | [diff] [blame] | 39 | if (New) { |
| 40 | LLVMContextRef C = LLVMGetGlobalContext(); |
| 41 | LLVMContextSetDiagnosticHandler(C, diagnosticHandler, NULL); |
| 42 | if (Lazy) |
| 43 | Ret = LLVMGetBitcodeModule2(MB, &M); |
| 44 | else |
| 45 | Ret = LLVMParseBitcode2(MB, &M); |
| 46 | } else { |
| 47 | if (Lazy) |
| 48 | Ret = LLVMGetBitcodeModule(MB, &M, &msg); |
| 49 | else |
| 50 | Ret = LLVMParseBitcode(MB, &M, &msg); |
| 51 | } |
Rafael Espindola | 125592d | 2015-12-18 03:57:26 +0000 | [diff] [blame] | 52 | |
| 53 | if (Ret) { |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 54 | fprintf(stderr, "Error parsing bitcode: %s\n", msg); |
Benjamin Kramer | 2daaea5 | 2013-10-25 15:58:58 +0000 | [diff] [blame] | 55 | LLVMDisposeMemoryBuffer(MB); |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 56 | exit(1); |
| 57 | } |
| 58 | |
Rafael Espindola | 125592d | 2015-12-18 03:57:26 +0000 | [diff] [blame] | 59 | if (!Lazy) |
| 60 | LLVMDisposeMemoryBuffer(MB); |
| 61 | |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 62 | return M; |
| 63 | } |
| 64 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 65 | int llvm_module_dump(bool Lazy, bool New) { |
| 66 | LLVMModuleRef M = llvm_load_module(Lazy, New); |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 67 | |
| 68 | char *irstr = LLVMPrintModuleToString(M); |
| 69 | puts(irstr); |
| 70 | LLVMDisposeMessage(irstr); |
| 71 | |
| 72 | LLVMDisposeModule(M); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 77 | int llvm_module_list_functions(void) { |
| 78 | LLVMModuleRef M = llvm_load_module(false, false); |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 79 | LLVMValueRef f; |
| 80 | |
| 81 | f = LLVMGetFirstFunction(M); |
| 82 | while (f) { |
| 83 | if (LLVMIsDeclaration(f)) { |
| 84 | printf("FunctionDeclaration: %s\n", LLVMGetValueName(f)); |
| 85 | } else { |
NAKAMURA Takumi | c491469 | 2013-10-23 17:56:37 +0000 | [diff] [blame] | 86 | LLVMBasicBlockRef bb; |
| 87 | LLVMValueRef isn; |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 88 | unsigned nisn = 0; |
| 89 | unsigned nbb = 0; |
| 90 | |
| 91 | printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f), |
| 92 | LLVMCountBasicBlocks(f)); |
| 93 | |
NAKAMURA Takumi | c491469 | 2013-10-23 17:56:37 +0000 | [diff] [blame] | 94 | for (bb = LLVMGetFirstBasicBlock(f); bb; |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 95 | bb = LLVMGetNextBasicBlock(bb)) { |
| 96 | nbb++; |
NAKAMURA Takumi | c491469 | 2013-10-23 17:56:37 +0000 | [diff] [blame] | 97 | for (isn = LLVMGetFirstInstruction(bb); isn; |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 98 | isn = LLVMGetNextInstruction(isn)) { |
| 99 | nisn++; |
| 100 | if (LLVMIsACallInst(isn)) { |
| 101 | LLVMValueRef callee = |
| 102 | LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1); |
| 103 | printf(" calls: %s\n", LLVMGetValueName(callee)); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | printf(" #isn: %u\n", nisn); |
| 108 | printf(" #bb: %u\n\n", nbb); |
| 109 | } |
| 110 | f = LLVMGetNextFunction(f); |
| 111 | } |
| 112 | |
| 113 | LLVMDisposeModule(M); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Benjamin Kramer | 9a3bd23 | 2016-02-05 13:31:14 +0000 | [diff] [blame] | 118 | int llvm_module_list_globals(void) { |
| 119 | LLVMModuleRef M = llvm_load_module(false, false); |
Anders Waldenborg | b932c66 | 2013-10-23 08:10:20 +0000 | [diff] [blame] | 120 | LLVMValueRef g; |
| 121 | |
| 122 | g = LLVMGetFirstGlobal(M); |
| 123 | while (g) { |
| 124 | LLVMTypeRef T = LLVMTypeOf(g); |
| 125 | char *s = LLVMPrintTypeToString(T); |
| 126 | |
| 127 | printf("Global%s: %s %s\n", |
| 128 | LLVMIsDeclaration(g) ? "Declaration" : "Definition", |
| 129 | LLVMGetValueName(g), s); |
| 130 | |
| 131 | LLVMDisposeMessage(s); |
| 132 | |
| 133 | g = LLVMGetNextGlobal(g); |
| 134 | } |
| 135 | |
| 136 | LLVMDisposeModule(M); |
| 137 | |
| 138 | return 0; |
| 139 | } |