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 LLVMModuleRef load_module(bool Lazy) {
23 LLVMMemoryBufferRef MB;
27 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
28 fprintf(stderr, "Error reading file: %s\n", msg);
34 Ret = LLVMGetBitcodeModule(MB, &M, &msg);
36 Ret = LLVMParseBitcode(MB, &M, &msg);
39 fprintf(stderr, "Error parsing bitcode: %s\n", msg);
40 LLVMDisposeMemoryBuffer(MB);
45 LLVMDisposeMemoryBuffer(MB);
50 int module_dump(bool Lazy) {
51 LLVMModuleRef M = load_module(Lazy);
53 char *irstr = LLVMPrintModuleToString(M);
55 LLVMDisposeMessage(irstr);
62 int module_list_functions(void) {
63 LLVMModuleRef M = load_module(false);
66 f = LLVMGetFirstFunction(M);
68 if (LLVMIsDeclaration(f)) {
69 printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
76 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
77 LLVMCountBasicBlocks(f));
79 for (bb = LLVMGetFirstBasicBlock(f); bb;
80 bb = LLVMGetNextBasicBlock(bb)) {
82 for (isn = LLVMGetFirstInstruction(bb); isn;
83 isn = LLVMGetNextInstruction(isn)) {
85 if (LLVMIsACallInst(isn)) {
87 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
88 printf(" calls: %s\n", LLVMGetValueName(callee));
92 printf(" #isn: %u\n", nisn);
93 printf(" #bb: %u\n\n", nbb);
95 f = LLVMGetNextFunction(f);
103 int module_list_globals(void) {
104 LLVMModuleRef M = load_module(false);
107 g = LLVMGetFirstGlobal(M);
109 LLVMTypeRef T = LLVMTypeOf(g);
110 char *s = LLVMPrintTypeToString(T);
112 printf("Global%s: %s %s\n",
113 LLVMIsDeclaration(g) ? "Declaration" : "Definition",
114 LLVMGetValueName(g), s);
116 LLVMDisposeMessage(s);
118 g = LLVMGetNextGlobal(g);
121 LLVMDisposeModule(M);