llvm.org GIT mirror llvm / 2bef1a6
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 Anders Waldenborg 5 years ago
19 changed file(s) with 775 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 ; RUN: llvm-c-test --calc <%s | FileCheck %s
1
2 ; constant folding
3 test 100 200 +
4 ;CHECK: ModuleID = 'test'
5 ;CHECK: define i64 @test
6 ;CHECK: {
7 ;CHECK: ret i64 300
8 ;CHECK: }
9
10 arg1 0 @ 0 @ * 1 @ 1 @ * +
11 ;CHECK: ModuleID = 'arg1'
12 ;CHECK: getelementptr
13 ;CHECK: load
14 ;CHECK: ret
0 ; RUN: llvm-c-test --disassemble < %s | FileCheck %s
1
2
3 arm-linux-android 44 26 1f e5 0c 10 4b e2 02 20 81 e0
4 ;CHECK: triple: arm-linux-android
5 ;CHECK: ldr r2, [pc, #-1604]
6 ;CHECK: sub r1, r11, #12
7 ;CHECK: 02 20 81 e0
8 ;CHECK: add r2, r1, r2
9
10 x86_64-linux-unknown 48 83 c4 38 5b 5d 41 5c 41 5d 41 5e 41 5f c3
11 ;CHECK: triple: x86_64-linux-unknown
12 ;CHECK: addq $56, %rsp
13 ;CHECK: popq %rbx
14 ;CHECK: popq %rbp
15 ;CHECK: popq %r12
16 ;CHECK: popq %r13
17 ;CHECK: popq %r14
18 ;CHECK: popq %r15
19 ;CHECK: ret
20
21 i686-apple-darwin 0f b7 4c 24 0a e8 29 ce ff ff
22 ;CHECK: movzwl 10(%esp), %ecx
23 ;CHECK: calll -12759
24
25 i686-linux-unknown dd 44 24 04 d9 e1 c3
26 ;CHECK: fldl 4(%esp)
27 ;CHECK: fabs
28 ;CHECK: ret
0 ; RUN: llvm-as < %s | llvm-c-test --module-list-functions | FileCheck %s
1
2 define i32 @X() {
3 entry:
4 br label %l1
5
6 l1:
7 br label %l2
8
9 l2:
10 br label %l3
11
12 l3:
13 ret i32 1234
14 }
15 ;CHECK: FunctionDefinition: X [#bb=4]
16
17
18 define i32 @Z(i32 %a) {
19 entry:
20 %0 = tail call i32 @Y(i32 %a)
21 ret i32 %0
22 }
23
24 ;CHECK: FunctionDefinition: Z [#bb=1]
25 ;CHECK: calls: Y
26 ;CHECK: #isn: 2
27
28 declare i32 @Y(i32)
29 ;CHECK: FunctionDeclaration: Y
30
0 ; RUN: llvm-as < %s | llvm-c-test --module-list-globals | FileCheck %s
1
2 @foo = constant [7 x i8] c"foobar\00", align 1
3 ;CHECK: GlobalDefinition: foo [7 x i8]*
4
5 @bar = common global i32 0, align 4
6 ;CHECK: GlobalDefinition: bar i32*
0 targets = set(config.root.targets_to_build.split())
1 if not (targets & set(["X86", "ARM"])):
2 config.unsupported = True
231231 r"\bllvm-shlib\b",
232232 r"\bllvm-size\b",
233233 r"\bllvm-tblgen\b",
234 r"\bllvm-c-test\b",
234235 # Match llvmc but not -llvmc
235236 NOHYPHEN + r"\bllvmc\b",
236237 # Match lto but not -lto
3838
3939 add_llvm_tool_subdirectory(llvm-symbolizer)
4040
41 if( NOT MSVC )
42 add_llvm_tool_subdirectory(llvm-c-test)
43 else()
44 ignore_llvm_tool_subdirectory(llvm-c-test)
45 endif( NOT MSVC )
46
4147 add_llvm_tool_subdirectory(obj2yaml)
4248 add_llvm_tool_subdirectory(yaml2obj)
4349
3030 lli llvm-extract llvm-mc bugpoint llvm-bcanalyzer llvm-diff \
3131 macho-dump llvm-objdump llvm-readobj llvm-rtdyld \
3232 llvm-dwarfdump llvm-cov llvm-size llvm-stress llvm-mcmarkup \
33 llvm-symbolizer obj2yaml yaml2obj
33 llvm-symbolizer obj2yaml yaml2obj llvm-c-test
3434
3535 # If Intel JIT Events support is configured, build an extra tool to test it.
3636 ifeq ($(USE_INTEL_JITEVENTS), 1)
0 set(LLVM_LINK_COMPONENTS all)
1
2 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
3 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wstrict-prototypes")
4 endif ()
5
6 add_llvm_tool(llvm-c-test
7 calc.c
8 disassemble.c
9 helpers.c
10 include-all.c
11 main.c
12 module.c
13 object.c
14 targets.c
15 )
0 ##===- tools/llvm-c-test -----------------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 TOOLNAME = llvm-c-test
11
12 TOOL_NO_EXPORTS = 1
13 NO_INSTALL = 1
14
15
16 # If there is no shared lib, link all components...
17 ifneq ($(ENABLE_SHARED),1)
18 LINK_COMPONENTS = all
19 endif
20
21 include $(LEVEL)/Makefile.common
22
23 CFLAGS += -std=c99 -Wall -Wstrict-prototypes
24
25 # ...but if it is built - use it
26 ifeq ($(ENABLE_SHARED),1)
27 LIBS = -lLLVM-$(LLVMVersion)
28 endif
0 /*===-- calc.c - tool for testing libLLVM and llvm-c API ------------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file implements the --calc command in llvm-c-test. --calc reads lines *|
10 |* from stdin, parses them as a name and an expression in reverse polish *|
11 |* notation and prints a module with a function with the expression. *|
12 |* *|
13 \*===----------------------------------------------------------------------===*/
14
15 #include "llvm-c-test.h"
16 #include "llvm-c/Core.h"
17 #include
18 #include
19 #include
20 #include
21
22 typedef LLVMValueRef (*binop_func_t)(LLVMBuilderRef, LLVMValueRef LHS,
23 LLVMValueRef RHS, const char *Name);
24
25 static LLVMOpcode op_to_opcode(char op) {
26 switch (op) {
27 case '+': return LLVMAdd;
28 case '-': return LLVMSub;
29 case '*': return LLVMMul;
30 case '/': return LLVMSDiv;
31 case '&': return LLVMAnd;
32 case '|': return LLVMOr;
33 case '^': return LLVMXor;
34 }
35 assert(0 && "unknown operation");
36 return 0;
37 }
38
39 #define MAX_DEPTH 32
40
41 static LLVMValueRef build_from_tokens(char **tokens, int ntokens,
42 LLVMBuilderRef builder,
43 LLVMValueRef param) {
44 LLVMValueRef stack[MAX_DEPTH];
45 int depth = 0;
46
47 for (int i = 0; i < ntokens; i++) {
48 char tok = tokens[i][0];
49 switch (tok) {
50 case '+':
51 case '-':
52 case '*':
53 case '/':
54 case '&':
55 case '|':
56 case '^':
57 if (depth < 2) {
58 printf("stack underflow\n");
59 return NULL;
60 }
61
62 stack[depth - 2] = LLVMBuildBinOp(builder, op_to_opcode(tok),
63 stack[depth - 1], stack[depth - 2], "");
64 depth--;
65
66 break;
67
68 case '@':
69 if (depth < 1) {
70 printf("stack underflow\n");
71 return NULL;
72 }
73
74 LLVMValueRef off = LLVMBuildGEP(builder, param, &stack[depth - 1], 1, "");
75 stack[depth - 1] = LLVMBuildLoad(builder, off, "");
76
77 break;
78
79 default: {
80 char *end;
81 long val = strtol(tokens[i], &end, 0);
82 if (end[0] != '\0') {
83 printf("error parsing number\n");
84 return NULL;
85 }
86
87 if (depth >= MAX_DEPTH) {
88 printf("stack overflow\n");
89 return NULL;
90 }
91
92 stack[depth++] = LLVMConstInt(LLVMInt64Type(), val, 1);
93 break;
94 }
95 }
96 }
97
98 if (depth < 1) {
99 printf("stack underflow at return\n");
100 return NULL;
101 }
102
103 LLVMBuildRet(builder, stack[depth - 1]);
104
105 return stack[depth - 1];
106 }
107
108 static void handle_line(char **tokens, int ntokens) {
109 char *name = tokens[0];
110
111 LLVMModuleRef M = LLVMModuleCreateWithName(name);
112
113 LLVMTypeRef I64ty = LLVMInt64Type();
114 LLVMTypeRef I64Ptrty = LLVMPointerType(I64ty, 0);
115 LLVMTypeRef Fty = LLVMFunctionType(I64ty, &I64Ptrty, 1, 0);
116
117 LLVMValueRef F = LLVMAddFunction(M, name, Fty);
118 LLVMBuilderRef builder = LLVMCreateBuilder();
119 LLVMPositionBuilderAtEnd(builder, LLVMAppendBasicBlock(F, "entry"));
120
121 LLVMValueRef param;
122 LLVMGetParams(F, ¶m);
123 LLVMSetValueName(param, "in");
124
125 LLVMValueRef res = build_from_tokens(tokens + 1, ntokens - 1, builder, param);
126 if (res) {
127 char *irstr = LLVMPrintModuleToString(M);
128 puts(irstr);
129 LLVMDisposeMessage(irstr);
130 }
131
132 LLVMDisposeBuilder(builder);
133
134 LLVMDisposeModule(M);
135 }
136
137 int calc(void) {
138
139 tokenize_stdin(handle_line);
140
141 return 0;
142 }
0 /*===-- disassemble.c - tool for testing libLLVM and llvm-c API -----------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file implements the --disassemble command in llvm-c-test. *|
10 |* --disassemble reads lines from stdin, parses them as a triple and hex *|
11 |* machine code, and prints disassembly of the machine code. *|
12 |* *|
13 \*===----------------------------------------------------------------------===*/
14
15 #include "llvm-c-test.h"
16 #include "llvm-c/Disassembler.h"
17 #include "llvm-c/Target.h"
18 #include
19 #include
20
21 static void pprint(int pos, unsigned char *buf, int len, const char *disasm) {
22 printf("%04x: ", pos);
23 for (int i = 0; i < 8; i++) {
24 if (i < len) {
25 printf("%02x ", buf[i]);
26 } else {
27 printf(" ");
28 }
29 }
30
31 printf(" %s\n", disasm);
32 }
33
34 static void do_disassemble(const char *triple, unsigned char *buf, int siz) {
35 LLVMDisasmContextRef D = LLVMCreateDisasm(triple, NULL, 0, NULL, NULL);
36
37 if (!D) {
38 printf("ERROR: Couldn't create disassebler for triple %s\n", triple);
39 return;
40 }
41
42 char outline[1024];
43 int pos = 0;
44 while (pos < siz) {
45 size_t l = LLVMDisasmInstruction(D, buf + pos, siz - pos, 0, outline,
46 sizeof(outline));
47 if (!l) {
48 pprint(pos, buf + pos, 1, "\t???");
49 pos++;
50 } else {
51 pprint(pos, buf + pos, l, outline);
52 pos += l;
53 }
54 }
55
56 LLVMDisasmDispose(D);
57 }
58
59 static void handle_line(char **tokens, int ntokens) {
60 unsigned char disbuf[128];
61 size_t disbuflen = 0;
62 char *triple = tokens[0];
63
64 printf("triple: %s\n", triple);
65
66 for (int i = 1; i < ntokens; i++) {
67 disbuf[disbuflen++] = strtol(tokens[i], NULL, 16);
68 if (disbuflen >= sizeof(disbuf)) {
69 fprintf(stderr, "Warning: Too long line, truncating\n");
70 break;
71 }
72 }
73 do_disassemble(triple, disbuf, disbuflen);
74 }
75
76 int disassemble(void) {
77 LLVMInitializeAllTargetInfos();
78 LLVMInitializeAllTargetMCs();
79 LLVMInitializeAllDisassemblers();
80
81 tokenize_stdin(handle_line);
82
83 return 0;
84 }
0 /*===-- helpers.c - tool for testing libLLVM and llvm-c API ---------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* Helper functions *|
10 |* *|
11 \*===----------------------------------------------------------------------===*/
12
13 #include "llvm-c-test.h"
14 #include
15 #include
16
17 #define MAX_TOKENS 512
18 #define MAX_LINE_LEN 1024
19
20 void tokenize_stdin(void (*cb)(char **tokens, int ntokens)) {
21 char line[MAX_LINE_LEN];
22 char *tokbuf[MAX_TOKENS];
23
24 while (fgets(line, sizeof(line), stdin)) {
25 int c = 0;
26
27 if (line[0] == ';' || line[0] == '\n')
28 continue;
29
30 while (c < MAX_TOKENS) {
31 tokbuf[c] = strtok(c ? NULL : line, " \n");
32 if (!tokbuf[c])
33 break;
34 c++;
35 }
36 if (c)
37 cb(tokbuf, c);
38 }
39 }
0 /*===-- include-all.c - tool for testing libLLVM and llvm-c API -----------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file doesn't have any actual code. It just make sure that all *|
10 |* the llvm-c include files are good and doesn't generate any warnings *|
11 |* *|
12 \*===----------------------------------------------------------------------===*/
13
14 // FIXME: Autogenerate this list
15
16 #include "llvm-c/Analysis.h"
17 #include "llvm-c/BitReader.h"
18 #include "llvm-c/BitWriter.h"
19 #include "llvm-c/Core.h"
20 #include "llvm-c/Disassembler.h"
21 #include "llvm-c/ExecutionEngine.h"
22 #include "llvm-c/Initialization.h"
23 #include "llvm-c/LinkTimeOptimizer.h"
24 #include "llvm-c/Linker.h"
25 #include "llvm-c/Object.h"
26 #include "llvm-c/Target.h"
27 #include "llvm-c/TargetMachine.h"
28 #include "llvm-c/Transforms/IPO.h"
29 #include "llvm-c/Transforms/PassManagerBuilder.h"
30 #include "llvm-c/Transforms/Scalar.h"
31 #include "llvm-c/Transforms/Vectorize.h"
32 #include "llvm-c/lto.h"
0 /*===-- llvm-c-test.h - tool for testing libLLVM and llvm-c API -----------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* Header file for llvm-c-test *|
10 |* *|
11 \*===----------------------------------------------------------------------===*/
12 #ifndef LLVM_C_TEST_H
13 #define LLVM_C_TEST_H
14
15 // helpers.c
16 void tokenize_stdin(void (*cb)(char **tokens, int ntokens));
17
18 // module.c
19 int module_dump(void);
20 int module_list_functions(void);
21 int module_list_globals(void);
22
23 // calc.c
24 int calc(void);
25
26 // disassemble.c
27 int disassemble(void);
28
29 // object.c
30 int object_list_sections(void);
31 int object_list_symbols(void);
32
33 // targets.c
34 int targets_list(void);
35
36 #endif
0 /*===-- main.c - tool for testing libLLVM and llvm-c API ------------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* Main file for llvm-c-tests. "Parses" arguments and dispatches. *|
10 |* *|
11 \*===----------------------------------------------------------------------===*/
12
13 #include "llvm-c-test.h"
14 #include "llvm-c/BitReader.h"
15 #include "llvm-c/Core.h"
16 #include
17 #include
18 #include
19
20 static void print_usage(void) {
21 fprintf(stderr, "llvm-c-test command\n\n");
22 fprintf(stderr, " Commands:\n");
23 fprintf(stderr, " * --module-dump\n");
24 fprintf(stderr, " Read bytecode from stdin - print disassembly\n\n");
25 fprintf(stderr, " * --module-list-functions\n");
26 fprintf(stderr,
27 " Read bytecode from stdin - list summary of functions\n\n");
28 fprintf(stderr, " * --module-list-globals\n");
29 fprintf(stderr, " Read bytecode from stdin - list summary of globals\n\n");
30 fprintf(stderr, " * --targets-list\n");
31 fprintf(stderr, " List available targets\n\n");
32 fprintf(stderr, " * --object-list-sections\n");
33 fprintf(stderr, " Read object file form stdin - list sections\n\n");
34 fprintf(stderr, " * --object-list-symbols\n");
35 fprintf(stderr,
36 " Read object file form stdin - list symbols (like nm)\n\n");
37 fprintf(stderr, " * --disassemble\n");
38 fprintf(stderr, " Read lines of triple, hex ascii machine code from stdin "
39 "- print disassembly\n\n");
40 fprintf(stderr, " * --calc\n");
41 fprintf(
42 stderr,
43 " Read lines of name, rpn from stdin - print generated module\n\n");
44 }
45
46 int main(int argc, char **argv) {
47 LLVMPassRegistryRef pr = LLVMGetGlobalPassRegistry();
48
49 LLVMInitializeCore(pr);
50
51 if (argc == 2 && !strcmp(argv[1], "--module-dump")) {
52 return module_dump();
53 } else if (argc == 2 && !strcmp(argv[1], "--module-list-functions")) {
54 return module_list_functions();
55 } else if (argc == 2 && !strcmp(argv[1], "--module-list-globals")) {
56 return module_list_globals();
57 } else if (argc == 2 && !strcmp(argv[1], "--targets-list")) {
58 return targets_list();
59 } else if (argc == 2 && !strcmp(argv[1], "--object-list-sections")) {
60 return object_list_sections();
61 } else if (argc == 2 && !strcmp(argv[1], "--object-list-symbols")) {
62 return object_list_symbols();
63 } else if (argc == 2 && !strcmp(argv[1], "--disassemble")) {
64 return disassemble();
65 } else if (argc == 2 && !strcmp(argv[1], "--calc")) {
66 return calc();
67 } else {
68 print_usage();
69 }
70
71 return 1;
72 }
0 /*===-- module.c - tool for testing libLLVM and llvm-c API ----------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file implements the --module-dump, --module-list-functions and *|
10 |* --module-list-globals commands in llvm-c-test. *|
11 |* *|
12 \*===----------------------------------------------------------------------===*/
13
14 #include "llvm-c-test.h"
15 #include "llvm-c/BitReader.h"
16 #include "llvm-c/Core.h"
17 #include
18 #include
19 #include
20
21 static LLVMModuleRef load_module(void) {
22 LLVMMemoryBufferRef MB;
23 LLVMModuleRef M;
24 char *msg = NULL;
25
26 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
27 fprintf(stderr, "Error reading file: %s\n", msg);
28 exit(1);
29 }
30
31 if (LLVMParseBitcode(MB, &M, &msg)) {
32 fprintf(stderr, "Error parsing bitcode: %s\n", msg);
33 exit(1);
34 }
35
36 return M;
37 }
38
39 int module_dump(void) {
40 LLVMModuleRef M = load_module();
41
42 char *irstr = LLVMPrintModuleToString(M);
43 puts(irstr);
44 LLVMDisposeMessage(irstr);
45
46 LLVMDisposeModule(M);
47
48 return 0;
49 }
50
51 int module_list_functions(void) {
52 LLVMModuleRef M = load_module();
53 LLVMValueRef f;
54
55 f = LLVMGetFirstFunction(M);
56 while (f) {
57 if (LLVMIsDeclaration(f)) {
58 printf("FunctionDeclaration: %s\n", LLVMGetValueName(f));
59 } else {
60 unsigned nisn = 0;
61 unsigned nbb = 0;
62
63 printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f),
64 LLVMCountBasicBlocks(f));
65
66 for (LLVMBasicBlockRef bb = LLVMGetFirstBasicBlock(f); bb;
67 bb = LLVMGetNextBasicBlock(bb)) {
68 nbb++;
69 for (LLVMValueRef isn = LLVMGetFirstInstruction(bb); isn;
70 isn = LLVMGetNextInstruction(isn)) {
71 nisn++;
72 if (LLVMIsACallInst(isn)) {
73 LLVMValueRef callee =
74 LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1);
75 printf(" calls: %s\n", LLVMGetValueName(callee));
76 }
77 }
78 }
79 printf(" #isn: %u\n", nisn);
80 printf(" #bb: %u\n\n", nbb);
81 }
82 f = LLVMGetNextFunction(f);
83 }
84
85 LLVMDisposeModule(M);
86
87 return 0;
88 }
89
90 int module_list_globals(void) {
91 LLVMModuleRef M = load_module();
92 LLVMValueRef g;
93
94 g = LLVMGetFirstGlobal(M);
95 while (g) {
96 LLVMTypeRef T = LLVMTypeOf(g);
97 char *s = LLVMPrintTypeToString(T);
98
99 printf("Global%s: %s %s\n",
100 LLVMIsDeclaration(g) ? "Declaration" : "Definition",
101 LLVMGetValueName(g), s);
102
103 LLVMDisposeMessage(s);
104
105 g = LLVMGetNextGlobal(g);
106 }
107
108 LLVMDisposeModule(M);
109
110 return 0;
111 }
0 /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file implements the --object-list-sections and --object-list-symbols *|
10 |* commands in llvm-c-test. *|
11 |* *|
12 \*===----------------------------------------------------------------------===*/
13
14 #include "llvm-c-test.h"
15 #include "llvm-c/Object.h"
16 #include
17 #include
18
19 int object_list_sections(void) {
20 LLVMMemoryBufferRef MB;
21 LLVMObjectFileRef O;
22 char *msg = NULL;
23
24 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
25 fprintf(stderr, "Error reading file: %s\n", msg);
26 exit(1);
27 }
28
29 O = LLVMCreateObjectFile(MB);
30 if (!O) {
31 fprintf(stderr, "Error reading object\n");
32 exit(1);
33 }
34
35 LLVMSectionIteratorRef sect = LLVMGetSections(O);
36 while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
37 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
38 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
39
40 LLVMMoveToNextSection(sect);
41 }
42
43 LLVMDisposeSectionIterator(sect);
44
45 LLVMDisposeObjectFile(O);
46
47 return 0;
48 }
49
50 int object_list_symbols(void) {
51 LLVMMemoryBufferRef MB;
52 LLVMObjectFileRef O;
53 char *msg = NULL;
54
55 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
56 fprintf(stderr, "Error reading file: %s\n", msg);
57 exit(1);
58 }
59
60 O = LLVMCreateObjectFile(MB);
61 if (!O) {
62 fprintf(stderr, "Error reading object\n");
63 exit(1);
64 }
65
66 LLVMSectionIteratorRef sect = LLVMGetSections(O);
67 LLVMSymbolIteratorRef sym = LLVMGetSymbols(O);
68 while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
69
70 LLVMMoveToContainingSection(sect, sym);
71 printf("%s @0x%08" PRIx64 "/0x%08" PRIx64 " +%" PRIu64 " (%s)\n",
72 LLVMGetSymbolName(sym), LLVMGetSymbolAddress(sym),
73 LLVMGetSymbolFileOffset(sym), LLVMGetSymbolSize(sym),
74 LLVMGetSectionName(sect));
75
76 LLVMMoveToNextSymbol(sym);
77 }
78
79 LLVMDisposeSymbolIterator(sym);
80
81 LLVMDisposeObjectFile(O);
82
83 return 0;
84 }
0 /*===-- targets.c - tool for testing libLLVM and llvm-c API ---------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file implements the --targets command in llvm-c-test. *|
10 |* *|
11 \*===----------------------------------------------------------------------===*/
12
13 #include "llvm-c/TargetMachine.h"
14 #include
15
16 int targets_list(void) {
17 LLVMInitializeAllTargetInfos();
18 LLVMInitializeAllTargets();
19
20 for (LLVMTargetRef t = LLVMGetFirstTarget(); t; t = LLVMGetNextTarget(t)) {
21 printf("%s", LLVMGetTargetName(t));
22 if (LLVMTargetHasJIT(t))
23 printf(" (+jit)");
24 printf("\n - %s\n", LLVMGetTargetDescription(t));
25 }
26
27 return 0;
28 }