2013-10-23 10:10:20 +02:00
|
|
|
/*===-- module.c - tool for testing libLLVM and llvm-c API ----------------===*\
|
|
|
|
|* *|
|
2019-01-19 09:50:56 +01:00
|
|
|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|
|
|
|
|* Exceptions. *|
|
|
|
|
|* See https://llvm.org/LICENSE.txt for license information. *|
|
|
|
|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|
2013-10-23 10:10:20 +02:00
|
|
|
|* *|
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|* *|
|
|
|
|
|* This file implements the --module-dump, --module-list-functions and *|
|
|
|
|
|* --module-list-globals commands in llvm-c-test. *|
|
|
|
|
|* *|
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "llvm-c-test.h"
|
|
|
|
#include "llvm-c/BitReader.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-12-19 00:46:42 +01:00
|
|
|
static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
|
|
|
|
char *CErr = LLVMGetDiagInfoDescription(DI);
|
|
|
|
fprintf(stderr, "Error with new bitcode parser: %s\n", CErr);
|
|
|
|
LLVMDisposeMessage(CErr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
LLVMModuleRef llvm_load_module(bool Lazy, bool New) {
|
2013-10-23 10:10:20 +02:00
|
|
|
LLVMMemoryBufferRef MB;
|
|
|
|
LLVMModuleRef M;
|
|
|
|
char *msg = NULL;
|
|
|
|
|
|
|
|
if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
|
|
|
|
fprintf(stderr, "Error reading file: %s\n", msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-12-18 04:57:26 +01:00
|
|
|
LLVMBool Ret;
|
2015-12-19 00:46:42 +01:00
|
|
|
if (New) {
|
|
|
|
LLVMContextRef C = LLVMGetGlobalContext();
|
|
|
|
LLVMContextSetDiagnosticHandler(C, diagnosticHandler, NULL);
|
|
|
|
if (Lazy)
|
|
|
|
Ret = LLVMGetBitcodeModule2(MB, &M);
|
|
|
|
else
|
|
|
|
Ret = LLVMParseBitcode2(MB, &M);
|
|
|
|
} else {
|
|
|
|
if (Lazy)
|
|
|
|
Ret = LLVMGetBitcodeModule(MB, &M, &msg);
|
|
|
|
else
|
|
|
|
Ret = LLVMParseBitcode(MB, &M, &msg);
|
|
|
|
}
|
2015-12-18 04:57:26 +01:00
|
|
|
|
|
|
|
if (Ret) {
|
2013-10-23 10:10:20 +02:00
|
|
|
fprintf(stderr, "Error parsing bitcode: %s\n", msg);
|
2013-10-25 17:58:58 +02:00
|
|
|
LLVMDisposeMemoryBuffer(MB);
|
2013-10-23 10:10:20 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-12-18 04:57:26 +01:00
|
|
|
if (!Lazy)
|
|
|
|
LLVMDisposeMemoryBuffer(MB);
|
|
|
|
|
2013-10-23 10:10:20 +02:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
int llvm_module_dump(bool Lazy, bool New) {
|
|
|
|
LLVMModuleRef M = llvm_load_module(Lazy, New);
|
2013-10-23 10:10:20 +02:00
|
|
|
|
|
|
|
char *irstr = LLVMPrintModuleToString(M);
|
|
|
|
puts(irstr);
|
|
|
|
LLVMDisposeMessage(irstr);
|
|
|
|
|
|
|
|
LLVMDisposeModule(M);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
int llvm_module_list_functions(void) {
|
|
|
|
LLVMModuleRef M = llvm_load_module(false, false);
|
2013-10-23 10:10:20 +02:00
|
|
|
LLVMValueRef f;
|
|
|
|
|
|
|
|
f = LLVMGetFirstFunction(M);
|
|
|
|
while (f) {
|
|
|
|
if (LLVMIsDeclaration(f)) {
|
|
|
|
printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
|
|
|
|
} else {
|
2013-10-23 19:56:37 +02:00
|
|
|
LLVMBasicBlockRef bb;
|
|
|
|
LLVMValueRef isn;
|
2013-10-23 10:10:20 +02:00
|
|
|
unsigned nisn = 0;
|
|
|
|
unsigned nbb = 0;
|
|
|
|
|
|
|
|
printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
|
|
|
|
LLVMCountBasicBlocks(f));
|
|
|
|
|
2013-10-23 19:56:37 +02:00
|
|
|
for (bb = LLVMGetFirstBasicBlock(f); bb;
|
2013-10-23 10:10:20 +02:00
|
|
|
bb = LLVMGetNextBasicBlock(bb)) {
|
|
|
|
nbb++;
|
2013-10-23 19:56:37 +02:00
|
|
|
for (isn = LLVMGetFirstInstruction(bb); isn;
|
2013-10-23 10:10:20 +02:00
|
|
|
isn = LLVMGetNextInstruction(isn)) {
|
|
|
|
nisn++;
|
|
|
|
if (LLVMIsACallInst(isn)) {
|
|
|
|
LLVMValueRef callee =
|
|
|
|
LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
|
|
|
|
printf(" calls: %s\n", LLVMGetValueName(callee));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" #isn: %u\n", nisn);
|
|
|
|
printf(" #bb: %u\n\n", nbb);
|
|
|
|
}
|
|
|
|
f = LLVMGetNextFunction(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMDisposeModule(M);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
int llvm_module_list_globals(void) {
|
|
|
|
LLVMModuleRef M = llvm_load_module(false, false);
|
2013-10-23 10:10:20 +02:00
|
|
|
LLVMValueRef g;
|
|
|
|
|
|
|
|
g = LLVMGetFirstGlobal(M);
|
|
|
|
while (g) {
|
|
|
|
LLVMTypeRef T = LLVMTypeOf(g);
|
|
|
|
char *s = LLVMPrintTypeToString(T);
|
|
|
|
|
|
|
|
printf("Global%s: %s %s\n",
|
|
|
|
LLVMIsDeclaration(g) ? "Declaration" : "Definition",
|
|
|
|
LLVMGetValueName(g), s);
|
|
|
|
|
|
|
|
LLVMDisposeMessage(s);
|
|
|
|
|
|
|
|
g = LLVMGetNextGlobal(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMDisposeModule(M);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|