Add a test for LLVMGetBitcodeModule.
[oota-llvm.git] / tools / llvm-c-test / module.c
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"
17 #include "llvm-c/Core.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 static LLVMModuleRef load_module(bool Lazy) {
23   LLVMMemoryBufferRef MB;
24   LLVMModuleRef M;
25   char *msg = NULL;
26
27   if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
28     fprintf(stderr, "Error reading file: %s\n", msg);
29     exit(1);
30   }
31
32   LLVMBool Ret;
33   if (Lazy)
34     Ret = LLVMGetBitcodeModule(MB, &M, &msg);
35   else
36     Ret = LLVMParseBitcode(MB, &M, &msg);
37
38   if (Ret) {
39     fprintf(stderr, "Error parsing bitcode: %s\n", msg);
40     LLVMDisposeMemoryBuffer(MB);
41     exit(1);
42   }
43
44   if (!Lazy)
45     LLVMDisposeMemoryBuffer(MB);
46
47   return M;
48 }
49
50 int module_dump(bool Lazy) {
51   LLVMModuleRef M = load_module(Lazy);
52
53   char *irstr = LLVMPrintModuleToString(M);
54   puts(irstr);
55   LLVMDisposeMessage(irstr);
56
57   LLVMDisposeModule(M);
58
59   return 0;
60 }
61
62 int module_list_functions(void) {
63   LLVMModuleRef M = load_module(false);
64   LLVMValueRef f;
65
66   f = LLVMGetFirstFunction(M);
67   while (f) {
68     if (LLVMIsDeclaration(f)) {
69       printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
70     } else {
71       LLVMBasicBlockRef bb;
72       LLVMValueRef isn;
73       unsigned nisn = 0;
74       unsigned nbb = 0;
75
76       printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
77              LLVMCountBasicBlocks(f));
78
79       for (bb = LLVMGetFirstBasicBlock(f); bb;
80            bb = LLVMGetNextBasicBlock(bb)) {
81         nbb++;
82         for (isn = LLVMGetFirstInstruction(bb); isn;
83              isn = LLVMGetNextInstruction(isn)) {
84           nisn++;
85           if (LLVMIsACallInst(isn)) {
86             LLVMValueRef callee =
87                 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
88             printf(" calls: %s\n", LLVMGetValueName(callee));
89           }
90         }
91       }
92       printf(" #isn: %u\n", nisn);
93       printf(" #bb: %u\n\n", nbb);
94     }
95     f = LLVMGetNextFunction(f);
96   }
97
98   LLVMDisposeModule(M);
99
100   return 0;
101 }
102
103 int module_list_globals(void) {
104   LLVMModuleRef M = load_module(false);
105   LLVMValueRef g;
106
107   g = LLVMGetFirstGlobal(M);
108   while (g) {
109     LLVMTypeRef T = LLVMTypeOf(g);
110     char *s = LLVMPrintTypeToString(T);
111
112     printf("Global%s: %s %s\n",
113            LLVMIsDeclaration(g) ? "Declaration" : "Definition",
114            LLVMGetValueName(g), s);
115
116     LLVMDisposeMessage(s);
117
118     g = LLVMGetNextGlobal(g);
119   }
120
121   LLVMDisposeModule(M);
122
123   return 0;
124 }