blob: 180238664f6a492404b207a401687c33271bf1ca [file] [log] [blame]
Anders Waldenborgb932c662013-10-23 08:10:20 +00001/*===-- 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
22static 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
40int 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
52int 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 {
NAKAMURA Takumic4914692013-10-23 17:56:37 +000061 LLVMBasicBlockRef bb;
62 LLVMValueRef isn;
Anders Waldenborgb932c662013-10-23 08:10:20 +000063 unsigned nisn = 0;
64 unsigned nbb = 0;
65
66 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
67 LLVMCountBasicBlocks(f));
68
NAKAMURA Takumic4914692013-10-23 17:56:37 +000069 for (bb = LLVMGetFirstBasicBlock(f); bb;
Anders Waldenborgb932c662013-10-23 08:10:20 +000070 bb = LLVMGetNextBasicBlock(bb)) {
71 nbb++;
NAKAMURA Takumic4914692013-10-23 17:56:37 +000072 for (isn = LLVMGetFirstInstruction(bb); isn;
Anders Waldenborgb932c662013-10-23 08:10:20 +000073 isn = LLVMGetNextInstruction(isn)) {
74 nisn++;
75 if (LLVMIsACallInst(isn)) {
76 LLVMValueRef callee =
77 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
78 printf(" calls: %s\n", LLVMGetValueName(callee));
79 }
80 }
81 }
82 printf(" #isn: %u\n", nisn);
83 printf(" #bb: %u\n\n", nbb);
84 }
85 f = LLVMGetNextFunction(f);
86 }
87
88 LLVMDisposeModule(M);
89
90 return 0;
91}
92
93int module_list_globals(void) {
94 LLVMModuleRef M = load_module();
95 LLVMValueRef g;
96
97 g = LLVMGetFirstGlobal(M);
98 while (g) {
99 LLVMTypeRef T = LLVMTypeOf(g);
100 char *s = LLVMPrintTypeToString(T);
101
102 printf("Global%s: %s %s\n",
103 LLVMIsDeclaration(g) ? "Declaration" : "Definition",
104 LLVMGetValueName(g), s);
105
106 LLVMDisposeMessage(s);
107
108 g = LLVMGetNextGlobal(g);
109 }
110
111 LLVMDisposeModule(M);
112
113 return 0;
114}