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(void) {
23 LLVMMemoryBufferRef MB;
27 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
28 fprintf(stderr, "Error reading file: %s\n", msg);
32 if (LLVMParseBitcode(MB, &M, &msg)) {
33 fprintf(stderr, "Error parsing bitcode: %s\n", msg);
40 int module_dump(void) {
41 LLVMModuleRef M = load_module();
43 char *irstr = LLVMPrintModuleToString(M);
45 LLVMDisposeMessage(irstr);
52 int module_list_functions(void) {
53 LLVMModuleRef M = load_module();
56 f = LLVMGetFirstFunction(M);
58 if (LLVMIsDeclaration(f)) {
59 printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
64 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
65 LLVMCountBasicBlocks(f));
67 for (LLVMBasicBlockRef bb = LLVMGetFirstBasicBlock(f); bb;
68 bb = LLVMGetNextBasicBlock(bb)) {
70 for (LLVMValueRef isn = LLVMGetFirstInstruction(bb); isn;
71 isn = LLVMGetNextInstruction(isn)) {
73 if (LLVMIsACallInst(isn)) {
75 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
76 printf(" calls: %s\n", LLVMGetValueName(callee));
80 printf(" #isn: %u\n", nisn);
81 printf(" #bb: %u\n\n", nbb);
83 f = LLVMGetNextFunction(f);
91 int module_list_globals(void) {
92 LLVMModuleRef M = load_module();
95 g = LLVMGetFirstGlobal(M);
97 LLVMTypeRef T = LLVMTypeOf(g);
98 char *s = LLVMPrintTypeToString(T);
100 printf("Global%s: %s %s\n",
101 LLVMIsDeclaration(g) ? "Declaration" : "Definition",
102 LLVMGetValueName(g), s);
104 LLVMDisposeMessage(s);
106 g = LLVMGetNextGlobal(g);
109 LLVMDisposeModule(M);