xref: /aosp_15_r20/external/llvm/tools/llvm-c-test/module.c (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker /*===-- module.c - tool for testing libLLVM and llvm-c API ----------------===*\
2*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
3*9880d681SAndroid Build Coastguard Worker |*                     The LLVM Compiler Infrastructure                       *|
4*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
5*9880d681SAndroid Build Coastguard Worker |* This file is distributed under the University of Illinois Open Source      *|
6*9880d681SAndroid Build Coastguard Worker |* License. See LICENSE.TXT for details.                                      *|
7*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
8*9880d681SAndroid Build Coastguard Worker |*===----------------------------------------------------------------------===*|
9*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
10*9880d681SAndroid Build Coastguard Worker |* This file implements the --module-dump, --module-list-functions and        *|
11*9880d681SAndroid Build Coastguard Worker |* --module-list-globals commands in llvm-c-test.                             *|
12*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
13*9880d681SAndroid Build Coastguard Worker \*===----------------------------------------------------------------------===*/
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "llvm-c-test.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm-c/BitReader.h"
17*9880d681SAndroid Build Coastguard Worker #include <stdio.h>
18*9880d681SAndroid Build Coastguard Worker #include <stdlib.h>
19*9880d681SAndroid Build Coastguard Worker #include <string.h>
20*9880d681SAndroid Build Coastguard Worker 
diagnosticHandler(LLVMDiagnosticInfoRef DI,void * C)21*9880d681SAndroid Build Coastguard Worker static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
22*9880d681SAndroid Build Coastguard Worker   char *CErr = LLVMGetDiagInfoDescription(DI);
23*9880d681SAndroid Build Coastguard Worker   fprintf(stderr, "Error with new bitcode parser: %s\n", CErr);
24*9880d681SAndroid Build Coastguard Worker   LLVMDisposeMessage(CErr);
25*9880d681SAndroid Build Coastguard Worker   exit(1);
26*9880d681SAndroid Build Coastguard Worker }
27*9880d681SAndroid Build Coastguard Worker 
llvm_load_module(bool Lazy,bool New)28*9880d681SAndroid Build Coastguard Worker LLVMModuleRef llvm_load_module(bool Lazy, bool New) {
29*9880d681SAndroid Build Coastguard Worker   LLVMMemoryBufferRef MB;
30*9880d681SAndroid Build Coastguard Worker   LLVMModuleRef M;
31*9880d681SAndroid Build Coastguard Worker   char *msg = NULL;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker   if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
34*9880d681SAndroid Build Coastguard Worker     fprintf(stderr, "Error reading file: %s\n", msg);
35*9880d681SAndroid Build Coastguard Worker     exit(1);
36*9880d681SAndroid Build Coastguard Worker   }
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker   LLVMBool Ret;
39*9880d681SAndroid Build Coastguard Worker   if (New) {
40*9880d681SAndroid Build Coastguard Worker     LLVMContextRef C = LLVMGetGlobalContext();
41*9880d681SAndroid Build Coastguard Worker     LLVMContextSetDiagnosticHandler(C, diagnosticHandler, NULL);
42*9880d681SAndroid Build Coastguard Worker     if (Lazy)
43*9880d681SAndroid Build Coastguard Worker       Ret = LLVMGetBitcodeModule2(MB, &M);
44*9880d681SAndroid Build Coastguard Worker     else
45*9880d681SAndroid Build Coastguard Worker       Ret = LLVMParseBitcode2(MB, &M);
46*9880d681SAndroid Build Coastguard Worker   } else {
47*9880d681SAndroid Build Coastguard Worker     if (Lazy)
48*9880d681SAndroid Build Coastguard Worker       Ret = LLVMGetBitcodeModule(MB, &M, &msg);
49*9880d681SAndroid Build Coastguard Worker     else
50*9880d681SAndroid Build Coastguard Worker       Ret = LLVMParseBitcode(MB, &M, &msg);
51*9880d681SAndroid Build Coastguard Worker   }
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   if (Ret) {
54*9880d681SAndroid Build Coastguard Worker     fprintf(stderr, "Error parsing bitcode: %s\n", msg);
55*9880d681SAndroid Build Coastguard Worker     LLVMDisposeMemoryBuffer(MB);
56*9880d681SAndroid Build Coastguard Worker     exit(1);
57*9880d681SAndroid Build Coastguard Worker   }
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker   if (!Lazy)
60*9880d681SAndroid Build Coastguard Worker     LLVMDisposeMemoryBuffer(MB);
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker   return M;
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker 
llvm_module_dump(bool Lazy,bool New)65*9880d681SAndroid Build Coastguard Worker int llvm_module_dump(bool Lazy, bool New) {
66*9880d681SAndroid Build Coastguard Worker   LLVMModuleRef M = llvm_load_module(Lazy, New);
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   char *irstr = LLVMPrintModuleToString(M);
69*9880d681SAndroid Build Coastguard Worker   puts(irstr);
70*9880d681SAndroid Build Coastguard Worker   LLVMDisposeMessage(irstr);
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker   LLVMDisposeModule(M);
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker   return 0;
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker 
llvm_module_list_functions(void)77*9880d681SAndroid Build Coastguard Worker int llvm_module_list_functions(void) {
78*9880d681SAndroid Build Coastguard Worker   LLVMModuleRef M = llvm_load_module(false, false);
79*9880d681SAndroid Build Coastguard Worker   LLVMValueRef f;
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker   f = LLVMGetFirstFunction(M);
82*9880d681SAndroid Build Coastguard Worker   while (f) {
83*9880d681SAndroid Build Coastguard Worker     if (LLVMIsDeclaration(f)) {
84*9880d681SAndroid Build Coastguard Worker       printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
85*9880d681SAndroid Build Coastguard Worker     } else {
86*9880d681SAndroid Build Coastguard Worker       LLVMBasicBlockRef bb;
87*9880d681SAndroid Build Coastguard Worker       LLVMValueRef isn;
88*9880d681SAndroid Build Coastguard Worker       unsigned nisn = 0;
89*9880d681SAndroid Build Coastguard Worker       unsigned nbb = 0;
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker       printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
92*9880d681SAndroid Build Coastguard Worker              LLVMCountBasicBlocks(f));
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker       for (bb = LLVMGetFirstBasicBlock(f); bb;
95*9880d681SAndroid Build Coastguard Worker            bb = LLVMGetNextBasicBlock(bb)) {
96*9880d681SAndroid Build Coastguard Worker         nbb++;
97*9880d681SAndroid Build Coastguard Worker         for (isn = LLVMGetFirstInstruction(bb); isn;
98*9880d681SAndroid Build Coastguard Worker              isn = LLVMGetNextInstruction(isn)) {
99*9880d681SAndroid Build Coastguard Worker           nisn++;
100*9880d681SAndroid Build Coastguard Worker           if (LLVMIsACallInst(isn)) {
101*9880d681SAndroid Build Coastguard Worker             LLVMValueRef callee =
102*9880d681SAndroid Build Coastguard Worker                 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
103*9880d681SAndroid Build Coastguard Worker             printf(" calls: %s\n", LLVMGetValueName(callee));
104*9880d681SAndroid Build Coastguard Worker           }
105*9880d681SAndroid Build Coastguard Worker         }
106*9880d681SAndroid Build Coastguard Worker       }
107*9880d681SAndroid Build Coastguard Worker       printf(" #isn: %u\n", nisn);
108*9880d681SAndroid Build Coastguard Worker       printf(" #bb: %u\n\n", nbb);
109*9880d681SAndroid Build Coastguard Worker     }
110*9880d681SAndroid Build Coastguard Worker     f = LLVMGetNextFunction(f);
111*9880d681SAndroid Build Coastguard Worker   }
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker   LLVMDisposeModule(M);
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker   return 0;
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker 
llvm_module_list_globals(void)118*9880d681SAndroid Build Coastguard Worker int llvm_module_list_globals(void) {
119*9880d681SAndroid Build Coastguard Worker   LLVMModuleRef M = llvm_load_module(false, false);
120*9880d681SAndroid Build Coastguard Worker   LLVMValueRef g;
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   g = LLVMGetFirstGlobal(M);
123*9880d681SAndroid Build Coastguard Worker   while (g) {
124*9880d681SAndroid Build Coastguard Worker     LLVMTypeRef T = LLVMTypeOf(g);
125*9880d681SAndroid Build Coastguard Worker     char *s = LLVMPrintTypeToString(T);
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker     printf("Global%s: %s %s\n",
128*9880d681SAndroid Build Coastguard Worker            LLVMIsDeclaration(g) ? "Declaration" : "Definition",
129*9880d681SAndroid Build Coastguard Worker            LLVMGetValueName(g), s);
130*9880d681SAndroid Build Coastguard Worker 
131*9880d681SAndroid Build Coastguard Worker     LLVMDisposeMessage(s);
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker     g = LLVMGetNextGlobal(g);
134*9880d681SAndroid Build Coastguard Worker   }
135*9880d681SAndroid Build Coastguard Worker 
136*9880d681SAndroid Build Coastguard Worker   LLVMDisposeModule(M);
137*9880d681SAndroid Build Coastguard Worker 
138*9880d681SAndroid Build Coastguard Worker   return 0;
139*9880d681SAndroid Build Coastguard Worker }
140