1 /*===-- disassemble.c - tool for testing libLLVM and llvm-c API -----------===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file is distributed under the University of Illinois Open Source *|
6 |* License. See LICENSE.TXT for details. *|
8 |*===----------------------------------------------------------------------===*|
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. *|
14 \*===----------------------------------------------------------------------===*/
16 #include "llvm-c-test.h"
17 #include "llvm-c/Disassembler.h"
18 #include "llvm-c/Target.h"
23 static void pprint(int pos, unsigned char *buf, int len, const char *disasm) {
25 printf("%04x: ", pos);
26 for (i = 0; i < 8; i++) {
28 printf("%02x ", buf[i]);
34 printf(" %s\n", disasm);
37 static void do_disassemble(const char *triple, const char *features,
38 unsigned char *buf, int siz) {
39 LLVMDisasmContextRef D = LLVMCreateDisasmCPUFeatures(triple, "", features,
45 printf("ERROR: Couldn't create disassembler for triple %s\n", triple);
51 size_t l = LLVMDisasmInstruction(D, buf + pos, siz - pos, 0, outline,
54 pprint(pos, buf + pos, 1, "\t???");
57 pprint(pos, buf + pos, l, outline);
65 static void handle_line(char **tokens, int ntokens) {
66 unsigned char disbuf[128];
68 const char *triple = tokens[0];
69 const char *features = tokens[1];
72 printf("triple: %s, features: %s\n", triple, features);
73 if (!strcmp(features, "NULL"))
76 for (i = 2; i < ntokens; i++) {
77 disbuf[disbuflen++] = strtol(tokens[i], NULL, 16);
78 if (disbuflen >= sizeof(disbuf)) {
79 fprintf(stderr, "Warning: Too long line, truncating\n");
83 do_disassemble(triple, features, disbuf, disbuflen);
86 int disassemble(void) {
87 LLVMInitializeAllTargetInfos();
88 LLVMInitializeAllTargetMCs();
89 LLVMInitializeAllDisassemblers();
91 tokenize_stdin(handle_line);