blob: 7d6f1783aed9eb69bb70988ab7b1656d93f0bd5a [file] [log] [blame]
Anders Waldenborg2bef1a62013-10-23 08:10:20 +00001/*===-- 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>
Bradley Smith95b3e162014-09-30 16:31:40 +000021#include <string.h>
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000022
23static void pprint(int pos, unsigned char *buf, int len, const char *disasm) {
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000024 int i;
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000025 printf("%04x: ", pos);
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000026 for (i = 0; i < 8; i++) {
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000027 if (i < len) {
28 printf("%02x ", buf[i]);
29 } else {
30 printf(" ");
31 }
32 }
33
34 printf(" %s\n", disasm);
35}
36
Bradley Smith95b3e162014-09-30 16:31:40 +000037static void do_disassemble(const char *triple, const char *features,
38 unsigned char *buf, int siz) {
39 LLVMDisasmContextRef D = LLVMCreateDisasmCPUFeatures(triple, "", features,
40 NULL, 0, NULL, NULL);
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000041 char outline[1024];
42 int pos;
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000043
44 if (!D) {
Bradley Smith95b3e162014-09-30 16:31:40 +000045 printf("ERROR: Couldn't create disassembler for triple %s\n", triple);
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000046 return;
47 }
48
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000049 pos = 0;
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000050 while (pos < siz) {
51 size_t l = LLVMDisasmInstruction(D, buf + pos, siz - pos, 0, outline,
52 sizeof(outline));
53 if (!l) {
54 pprint(pos, buf + pos, 1, "\t???");
55 pos++;
56 } else {
57 pprint(pos, buf + pos, l, outline);
58 pos += l;
59 }
60 }
61
62 LLVMDisasmDispose(D);
63}
64
65static void handle_line(char **tokens, int ntokens) {
66 unsigned char disbuf[128];
67 size_t disbuflen = 0;
Bradley Smith95b3e162014-09-30 16:31:40 +000068 const char *triple = tokens[0];
69 const char *features = tokens[1];
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000070 int i;
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000071
Bradley Smith95b3e162014-09-30 16:31:40 +000072 printf("triple: %s, features: %s\n", triple, features);
73 if (!strcmp(features, "NULL"))
74 features = "";
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000075
Bradley Smith95b3e162014-09-30 16:31:40 +000076 for (i = 2; i < ntokens; i++) {
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000077 disbuf[disbuflen++] = strtol(tokens[i], NULL, 16);
78 if (disbuflen >= sizeof(disbuf)) {
79 fprintf(stderr, "Warning: Too long line, truncating\n");
80 break;
81 }
82 }
Bradley Smith95b3e162014-09-30 16:31:40 +000083 do_disassemble(triple, features, disbuf, disbuflen);
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000084}
85
Benjamin Kramer2e130532016-02-05 13:31:14 +000086int llvm_disassemble(void) {
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000087 LLVMInitializeAllTargetInfos();
88 LLVMInitializeAllTargetMCs();
89 LLVMInitializeAllDisassemblers();
90
Benjamin Kramer2e130532016-02-05 13:31:14 +000091 llvm_tokenize_stdin(handle_line);
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000092
93 return 0;
94}