blob: c47b55d5029491f4c46bc01b16d99f53bd38358d [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"
Anders Waldenborgb932c662013-10-23 08:10:20 +000017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
Rafael Espindola2339ffe2015-12-18 23:46:42 +000021static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
22 char *CErr = LLVMGetDiagInfoDescription(DI);
23 fprintf(stderr, "Error with new bitcode parser: %s\n", CErr);
24 LLVMDisposeMessage(CErr);
25 exit(1);
26}
27
Benjamin Kramer9a3bd232016-02-05 13:31:14 +000028LLVMModuleRef llvm_load_module(bool Lazy, bool New) {
Anders Waldenborgb932c662013-10-23 08:10:20 +000029 LLVMMemoryBufferRef MB;
30 LLVMModuleRef M;
31 char *msg = NULL;
32
33 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
34 fprintf(stderr, "Error reading file: %s\n", msg);
35 exit(1);
36 }
37
Rafael Espindola125592d2015-12-18 03:57:26 +000038 LLVMBool Ret;
Rafael Espindola2339ffe2015-12-18 23:46:42 +000039 if (New) {
40 LLVMContextRef C = LLVMGetGlobalContext();
41 LLVMContextSetDiagnosticHandler(C, diagnosticHandler, NULL);
42 if (Lazy)
43 Ret = LLVMGetBitcodeModule2(MB, &M);
44 else
45 Ret = LLVMParseBitcode2(MB, &M);
46 } else {
47 if (Lazy)
48 Ret = LLVMGetBitcodeModule(MB, &M, &msg);
49 else
50 Ret = LLVMParseBitcode(MB, &M, &msg);
51 }
Rafael Espindola125592d2015-12-18 03:57:26 +000052
53 if (Ret) {
Anders Waldenborgb932c662013-10-23 08:10:20 +000054 fprintf(stderr, "Error parsing bitcode: %s\n", msg);
Benjamin Kramer2daaea52013-10-25 15:58:58 +000055 LLVMDisposeMemoryBuffer(MB);
Anders Waldenborgb932c662013-10-23 08:10:20 +000056 exit(1);
57 }
58
Rafael Espindola125592d2015-12-18 03:57:26 +000059 if (!Lazy)
60 LLVMDisposeMemoryBuffer(MB);
61
Anders Waldenborgb932c662013-10-23 08:10:20 +000062 return M;
63}
64
Benjamin Kramer9a3bd232016-02-05 13:31:14 +000065int llvm_module_dump(bool Lazy, bool New) {
66 LLVMModuleRef M = llvm_load_module(Lazy, New);
Anders Waldenborgb932c662013-10-23 08:10:20 +000067
68 char *irstr = LLVMPrintModuleToString(M);
69 puts(irstr);
70 LLVMDisposeMessage(irstr);
71
72 LLVMDisposeModule(M);
73
74 return 0;
75}
76
Benjamin Kramer9a3bd232016-02-05 13:31:14 +000077int llvm_module_list_functions(void) {
78 LLVMModuleRef M = llvm_load_module(false, false);
Anders Waldenborgb932c662013-10-23 08:10:20 +000079 LLVMValueRef f;
80
81 f = LLVMGetFirstFunction(M);
82 while (f) {
83 if (LLVMIsDeclaration(f)) {
84 printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
85 } else {
NAKAMURA Takumic4914692013-10-23 17:56:37 +000086 LLVMBasicBlockRef bb;
87 LLVMValueRef isn;
Anders Waldenborgb932c662013-10-23 08:10:20 +000088 unsigned nisn = 0;
89 unsigned nbb = 0;
90
91 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
92 LLVMCountBasicBlocks(f));
93
NAKAMURA Takumic4914692013-10-23 17:56:37 +000094 for (bb = LLVMGetFirstBasicBlock(f); bb;
Anders Waldenborgb932c662013-10-23 08:10:20 +000095 bb = LLVMGetNextBasicBlock(bb)) {
96 nbb++;
NAKAMURA Takumic4914692013-10-23 17:56:37 +000097 for (isn = LLVMGetFirstInstruction(bb); isn;
Anders Waldenborgb932c662013-10-23 08:10:20 +000098 isn = LLVMGetNextInstruction(isn)) {
99 nisn++;
100 if (LLVMIsACallInst(isn)) {
101 LLVMValueRef callee =
102 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
103 printf(" calls: %s\n", LLVMGetValueName(callee));
104 }
105 }
106 }
107 printf(" #isn: %u\n", nisn);
108 printf(" #bb: %u\n\n", nbb);
109 }
110 f = LLVMGetNextFunction(f);
111 }
112
113 LLVMDisposeModule(M);
114
115 return 0;
116}
117
Benjamin Kramer9a3bd232016-02-05 13:31:14 +0000118int llvm_module_list_globals(void) {
119 LLVMModuleRef M = llvm_load_module(false, false);
Anders Waldenborgb932c662013-10-23 08:10:20 +0000120 LLVMValueRef g;
121
122 g = LLVMGetFirstGlobal(M);
123 while (g) {
124 LLVMTypeRef T = LLVMTypeOf(g);
125 char *s = LLVMPrintTypeToString(T);
126
127 printf("Global%s: %s %s\n",
128 LLVMIsDeclaration(g) ? "Declaration" : "Definition",
129 LLVMGetValueName(g), s);
130
131 LLVMDisposeMessage(s);
132
133 g = LLVMGetNextGlobal(g);
134 }
135
136 LLVMDisposeModule(M);
137
138 return 0;
139}