Add llvm-c-test tool for testing llvm-c
[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(void) {
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   if (LLVMParseBitcode(MB, &M, &msg)) {
33     fprintf(stderr, "Error parsing bitcode: %s\n", msg);
34     exit(1);
35   }
36
37   return M;
38 }
39
40 int module_dump(void) {
41   LLVMModuleRef M = load_module();
42
43   char *irstr = LLVMPrintModuleToString(M);
44   puts(irstr);
45   LLVMDisposeMessage(irstr);
46
47   LLVMDisposeModule(M);
48
49   return 0;
50 }
51
52 int module_list_functions(void) {
53   LLVMModuleRef M = load_module();
54   LLVMValueRef f;
55
56   f = LLVMGetFirstFunction(M);
57   while (f) {
58     if (LLVMIsDeclaration(f)) {
59       printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
60     } else {
61       unsigned nisn = 0;
62       unsigned nbb = 0;
63
64       printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
65              LLVMCountBasicBlocks(f));
66
67       for (LLVMBasicBlockRef bb = LLVMGetFirstBasicBlock(f); bb;
68            bb = LLVMGetNextBasicBlock(bb)) {
69         nbb++;
70         for (LLVMValueRef isn = LLVMGetFirstInstruction(bb); isn;
71              isn = LLVMGetNextInstruction(isn)) {
72           nisn++;
73           if (LLVMIsACallInst(isn)) {
74             LLVMValueRef callee =
75                 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
76             printf(" calls: %s\n", LLVMGetValueName(callee));
77           }
78         }
79       }
80       printf(" #isn: %u\n", nisn);
81       printf(" #bb: %u\n\n", nbb);
82     }
83     f = LLVMGetNextFunction(f);
84   }
85
86   LLVMDisposeModule(M);
87
88   return 0;
89 }
90
91 int module_list_globals(void) {
92   LLVMModuleRef M = load_module();
93   LLVMValueRef g;
94
95   g = LLVMGetFirstGlobal(M);
96   while (g) {
97     LLVMTypeRef T = LLVMTypeOf(g);
98     char *s = LLVMPrintTypeToString(T);
99
100     printf("Global%s: %s %s\n",
101            LLVMIsDeclaration(g) ? "Declaration" : "Definition",
102            LLVMGetValueName(g), s);
103
104     LLVMDisposeMessage(s);
105
106     g = LLVMGetNextGlobal(g);
107   }
108
109   LLVMDisposeModule(M);
110
111   return 0;
112 }