summaryrefslogtreecommitdiff
path: root/tools/llvm-c-test/module.c
diff options
context:
space:
mode:
authorAnders Waldenborg <anders@0x63.nu>2013-10-23 08:10:20 +0000
committerAnders Waldenborg <anders@0x63.nu>2013-10-23 08:10:20 +0000
commit2bef1a6b25d938210547cd0f5ba4a08abdad2583 (patch)
tree3cfdde4e7dc5a2040f134392541dcaeb1cd37c01 /tools/llvm-c-test/module.c
parent9242b73286f050c53a26225b2a9acd14aeaa91da (diff)
downloadllvm-2bef1a6b25d938210547cd0f5ba4a08abdad2583.tar.gz
llvm-2bef1a6b25d938210547cd0f5ba4a08abdad2583.tar.bz2
llvm-2bef1a6b25d938210547cd0f5ba4a08abdad2583.tar.xz
Add llvm-c-test tool for testing llvm-c
This provides rudimentary testing of the llvm-c api. The following commands are implemented: * --module-dump Read bytecode from stdin - print ir * --module-list-functions Read bytecode from stdin - list summary of functions * --module-list-globals Read bytecode from stdin - list summary of globals * --targets-list List available targets * --object-list-sections Read object file from stdin - list sections * --object-list-symbols Read object file from stdin - list symbols (like nm) * --disassemble Read lines of triple, hex ascii machine code from stdin - print disassembly * --calc Read lines of name, rpn from stdin - print generated module ir Differential-Revision: http://llvm-reviews.chandlerc.com/D1776 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193233 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-c-test/module.c')
-rw-r--r--tools/llvm-c-test/module.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/tools/llvm-c-test/module.c b/tools/llvm-c-test/module.c
new file mode 100644
index 0000000000..50e6e9c6de
--- /dev/null
+++ b/tools/llvm-c-test/module.c
@@ -0,0 +1,112 @@
+/*===-- module.c - tool for testing libLLVM and llvm-c API ----------------===*\
+|* *|
+|* The LLVM Compiler Infrastructure *|
+|* *|
+|* This file is distributed under the University of Illinois Open Source *|
+|* License. See LICENSE.TXT for details. *|
+|* *|
+|*===----------------------------------------------------------------------===*|
+|* *|
+|* 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 "llvm-c/Core.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static LLVMModuleRef load_module(void) {
+ LLVMMemoryBufferRef MB;
+ LLVMModuleRef M;
+ char *msg = NULL;
+
+ if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
+ fprintf(stderr, "Error reading file: %s\n", msg);
+ exit(1);
+ }
+
+ if (LLVMParseBitcode(MB, &M, &msg)) {
+ fprintf(stderr, "Error parsing bitcode: %s\n", msg);
+ exit(1);
+ }
+
+ return M;
+}
+
+int module_dump(void) {
+ LLVMModuleRef M = load_module();
+
+ char *irstr = LLVMPrintModuleToString(M);
+ puts(irstr);
+ LLVMDisposeMessage(irstr);
+
+ LLVMDisposeModule(M);
+
+ return 0;
+}
+
+int module_list_functions(void) {
+ LLVMModuleRef M = load_module();
+ LLVMValueRef f;
+
+ f = LLVMGetFirstFunction(M);
+ while (f) {
+ if (LLVMIsDeclaration(f)) {
+ printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
+ } else {
+ unsigned nisn = 0;
+ unsigned nbb = 0;
+
+ printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
+ LLVMCountBasicBlocks(f));
+
+ for (LLVMBasicBlockRef bb = LLVMGetFirstBasicBlock(f); bb;
+ bb = LLVMGetNextBasicBlock(bb)) {
+ nbb++;
+ for (LLVMValueRef isn = LLVMGetFirstInstruction(bb); isn;
+ 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;
+}
+
+int module_list_globals(void) {
+ LLVMModuleRef M = load_module();
+ 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;
+}