1 /*===-- module.c - tool for testing libLLVM and llvm-c API ----------------===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file is distributed under the University of Illinois Open Source *|
6 |* License. See LICENSE.TXT for details. *|
8 |*===----------------------------------------------------------------------===*|
10 |* This file implements the --module-dump, --module-list-functions and *|
11 |* --module-list-globals commands in llvm-c-test. *|
13 \*===----------------------------------------------------------------------===*/
15 #include "llvm-c-test.h"
16 #include "llvm-c/BitReader.h"
17 #include "llvm-c/Core.h"
22 static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
23 char *CErr = LLVMGetDiagInfoDescription(DI);
24 fprintf(stderr, "Error with new bitcode parser: %s\n", CErr);
25 LLVMDisposeMessage(CErr);
29 static LLVMModuleRef load_module(bool Lazy, bool New) {
30 LLVMMemoryBufferRef MB;
34 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
35 fprintf(stderr, "Error reading file: %s\n", msg);
41 LLVMContextRef C = LLVMGetGlobalContext();
42 LLVMContextSetDiagnosticHandler(C, diagnosticHandler, NULL);
44 Ret = LLVMGetBitcodeModule2(MB, &M);
46 Ret = LLVMParseBitcode2(MB, &M);
49 Ret = LLVMGetBitcodeModule(MB, &M, &msg);
51 Ret = LLVMParseBitcode(MB, &M, &msg);
55 fprintf(stderr, "Error parsing bitcode: %s\n", msg);
56 LLVMDisposeMemoryBuffer(MB);
61 LLVMDisposeMemoryBuffer(MB);
66 int module_dump(bool Lazy, bool New) {
67 LLVMModuleRef M = load_module(Lazy, New);
69 char *irstr = LLVMPrintModuleToString(M);
71 LLVMDisposeMessage(irstr);
78 int module_list_functions(void) {
79 LLVMModuleRef M = load_module(false, false);
82 f = LLVMGetFirstFunction(M);
84 if (LLVMIsDeclaration(f)) {
85 printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
92 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
93 LLVMCountBasicBlocks(f));
95 for (bb = LLVMGetFirstBasicBlock(f); bb;
96 bb = LLVMGetNextBasicBlock(bb)) {
98 for (isn = LLVMGetFirstInstruction(bb); isn;
99 isn = LLVMGetNextInstruction(isn)) {
101 if (LLVMIsACallInst(isn)) {
102 LLVMValueRef callee =
103 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
104 printf(" calls: %s\n", LLVMGetValueName(callee));
108 printf(" #isn: %u\n", nisn);
109 printf(" #bb: %u\n\n", nbb);
111 f = LLVMGetNextFunction(f);
114 LLVMDisposeModule(M);
119 int module_list_globals(void) {
120 LLVMModuleRef M = load_module(false, false);
123 g = LLVMGetFirstGlobal(M);
125 LLVMTypeRef T = LLVMTypeOf(g);
126 char *s = LLVMPrintTypeToString(T);
128 printf("Global%s: %s %s\n",
129 LLVMIsDeclaration(g) ? "Declaration" : "Definition",
130 LLVMGetValueName(g), s);
132 LLVMDisposeMessage(s);
134 g = LLVMGetNextGlobal(g);
137 LLVMDisposeModule(M);