blob: 0f27337eb7c710057c78cba01131bdedbd001b2f [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
Rafael Espindola125592d2015-12-18 03:57:26 +000022static LLVMModuleRef load_module(bool Lazy) {
Anders Waldenborgb932c662013-10-23 08:10:20 +000023 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
Rafael Espindola125592d2015-12-18 03:57:26 +000032 LLVMBool Ret;
33 if (Lazy)
34 Ret = LLVMGetBitcodeModule(MB, &M, &msg);
35 else
36 Ret = LLVMParseBitcode(MB, &M, &msg);
37
38 if (Ret) {
Anders Waldenborgb932c662013-10-23 08:10:20 +000039 fprintf(stderr, "Error parsing bitcode: %s\n", msg);
Benjamin Kramer2daaea52013-10-25 15:58:58 +000040 LLVMDisposeMemoryBuffer(MB);
Anders Waldenborgb932c662013-10-23 08:10:20 +000041 exit(1);
42 }
43
Rafael Espindola125592d2015-12-18 03:57:26 +000044 if (!Lazy)
45 LLVMDisposeMemoryBuffer(MB);
46
Anders Waldenborgb932c662013-10-23 08:10:20 +000047 return M;
48}
49
Rafael Espindola125592d2015-12-18 03:57:26 +000050int module_dump(bool Lazy) {
51 LLVMModuleRef M = load_module(Lazy);
Anders Waldenborgb932c662013-10-23 08:10:20 +000052
53 char *irstr = LLVMPrintModuleToString(M);
54 puts(irstr);
55 LLVMDisposeMessage(irstr);
56
57 LLVMDisposeModule(M);
58
59 return 0;
60}
61
62int module_list_functions(void) {
Rafael Espindola125592d2015-12-18 03:57:26 +000063 LLVMModuleRef M = load_module(false);
Anders Waldenborgb932c662013-10-23 08:10:20 +000064 LLVMValueRef f;
65
66 f = LLVMGetFirstFunction(M);
67 while (f) {
68 if (LLVMIsDeclaration(f)) {
69 printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
70 } else {
NAKAMURA Takumic4914692013-10-23 17:56:37 +000071 LLVMBasicBlockRef bb;
72 LLVMValueRef isn;
Anders Waldenborgb932c662013-10-23 08:10:20 +000073 unsigned nisn = 0;
74 unsigned nbb = 0;
75
76 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
77 LLVMCountBasicBlocks(f));
78
NAKAMURA Takumic4914692013-10-23 17:56:37 +000079 for (bb = LLVMGetFirstBasicBlock(f); bb;
Anders Waldenborgb932c662013-10-23 08:10:20 +000080 bb = LLVMGetNextBasicBlock(bb)) {
81 nbb++;
NAKAMURA Takumic4914692013-10-23 17:56:37 +000082 for (isn = LLVMGetFirstInstruction(bb); isn;
Anders Waldenborgb932c662013-10-23 08:10:20 +000083 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
103int module_list_globals(void) {
Rafael Espindola125592d2015-12-18 03:57:26 +0000104 LLVMModuleRef M = load_module(false);
Anders Waldenborgb932c662013-10-23 08:10:20 +0000105 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}