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