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);
34 LLVMDisposeMemoryBuffer(MB);
38 LLVMDisposeMemoryBuffer(MB);
42 int module_dump(void) {
43 LLVMModuleRef M = load_module();
45 char *irstr = LLVMPrintModuleToString(M);
47 LLVMDisposeMessage(irstr);
54 int module_list_functions(void) {
55 LLVMModuleRef M = load_module();
58 f = LLVMGetFirstFunction(M);
60 if (LLVMIsDeclaration(f)) {
61 printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
68 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
69 LLVMCountBasicBlocks(f));
71 for (bb = LLVMGetFirstBasicBlock(f); bb;
72 bb = LLVMGetNextBasicBlock(bb)) {
74 for (isn = LLVMGetFirstInstruction(bb); isn;
75 isn = LLVMGetNextInstruction(isn)) {
77 if (LLVMIsACallInst(isn)) {
79 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
80 printf(" calls: %s\n", LLVMGetValueName(callee));
84 printf(" #isn: %u\n", nisn);
85 printf(" #bb: %u\n\n", nbb);
87 f = LLVMGetNextFunction(f);
95 int module_list_globals(void) {
96 LLVMModuleRef M = load_module();
99 g = LLVMGetFirstGlobal(M);
101 LLVMTypeRef T = LLVMTypeOf(g);
102 char *s = LLVMPrintTypeToString(T);
104 printf("Global%s: %s %s\n",
105 LLVMIsDeclaration(g) ? "Declaration" : "Definition",
106 LLVMGetValueName(g), s);
108 LLVMDisposeMessage(s);
110 g = LLVMGetNextGlobal(g);
113 LLVMDisposeModule(M);