Add llvm-c-test tool for testing llvm-c
[oota-llvm.git] / tools / llvm-c-test / disassemble.c
1 /*===-- disassemble.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 --disassemble command in llvm-c-test.             *|
11 |* --disassemble reads lines from stdin, parses them as a triple and hex      *|
12 |*  machine code, and prints disassembly of the machine code.                 *|
13 |*                                                                            *|
14 \*===----------------------------------------------------------------------===*/
15
16 #include "llvm-c-test.h"
17 #include "llvm-c/Disassembler.h"
18 #include "llvm-c/Target.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 static void pprint(int pos, unsigned char *buf, int len, const char *disasm) {
23   printf("%04x:  ", pos);
24   for (int i = 0; i < 8; i++) {
25     if (i < len) {
26       printf("%02x ", buf[i]);
27     } else {
28       printf("   ");
29     }
30   }
31
32   printf("   %s\n", disasm);
33 }
34
35 static void do_disassemble(const char *triple, unsigned char *buf, int siz) {
36   LLVMDisasmContextRef D = LLVMCreateDisasm(triple, NULL, 0, NULL, NULL);
37
38   if (!D) {
39     printf("ERROR: Couldn't create disassebler for triple %s\n", triple);
40     return;
41   }
42
43   char outline[1024];
44   int pos = 0;
45   while (pos < siz) {
46     size_t l = LLVMDisasmInstruction(D, buf + pos, siz - pos, 0, outline,
47                                      sizeof(outline));
48     if (!l) {
49       pprint(pos, buf + pos, 1, "\t???");
50       pos++;
51     } else {
52       pprint(pos, buf + pos, l, outline);
53       pos += l;
54     }
55   }
56
57   LLVMDisasmDispose(D);
58 }
59
60 static void handle_line(char **tokens, int ntokens) {
61   unsigned char disbuf[128];
62   size_t disbuflen = 0;
63   char *triple = tokens[0];
64
65   printf("triple: %s\n", triple);
66
67   for (int i = 1; i < ntokens; i++) {
68     disbuf[disbuflen++] = strtol(tokens[i], NULL, 16);
69     if (disbuflen >= sizeof(disbuf)) {
70       fprintf(stderr, "Warning: Too long line, truncating\n");
71       break;
72     }
73   }
74   do_disassemble(triple, disbuf, disbuflen);
75 }
76
77 int disassemble(void) {
78   LLVMInitializeAllTargetInfos();
79   LLVMInitializeAllTargetMCs();
80   LLVMInitializeAllDisassemblers();
81
82   tokenize_stdin(handle_line);
83
84   return 0;
85 }