12bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg/*===-- disassemble.c - tool for testing libLLVM and llvm-c API -----------===*\
22bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*                                                                            *|
32bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*                     The LLVM Compiler Infrastructure                       *|
42bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*                                                                            *|
52bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|* This file is distributed under the University of Illinois Open Source      *|
62bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|* License. See LICENSE.TXT for details.                                      *|
72bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*                                                                            *|
82bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*===----------------------------------------------------------------------===*|
92bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*                                                                            *|
102bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|* This file implements the --disassemble command in llvm-c-test.             *|
112bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|* --disassemble reads lines from stdin, parses them as a triple and hex      *|
122bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*  machine code, and prints disassembly of the machine code.                 *|
132bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg|*                                                                            *|
142bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg\*===----------------------------------------------------------------------===*/
152bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
162bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg#include "llvm-c-test.h"
172bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg#include "llvm-c/Disassembler.h"
182bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg#include "llvm-c/Target.h"
192bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg#include <stdio.h>
202bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg#include <stdlib.h>
2137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include <string.h>
222bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
232bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborgstatic void pprint(int pos, unsigned char *buf, int len, const char *disasm) {
2490fd79a222652126ee7d85f3afb2cd85aa51b2f9NAKAMURA Takumi  int i;
252bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  printf("%04x:  ", pos);
2690fd79a222652126ee7d85f3afb2cd85aa51b2f9NAKAMURA Takumi  for (i = 0; i < 8; i++) {
272bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    if (i < len) {
282bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      printf("%02x ", buf[i]);
292bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    } else {
302bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      printf("   ");
312bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    }
322bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  }
332bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
342bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  printf("   %s\n", disasm);
352bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg}
362bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
3737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic void do_disassemble(const char *triple, const char *features,
3837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                           unsigned char *buf, int siz) {
3937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMDisasmContextRef D = LLVMCreateDisasmCPUFeatures(triple, "", features,
4037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                                       NULL, 0, NULL, NULL);
4190fd79a222652126ee7d85f3afb2cd85aa51b2f9NAKAMURA Takumi  char outline[1024];
4290fd79a222652126ee7d85f3afb2cd85aa51b2f9NAKAMURA Takumi  int pos;
432bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
442bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  if (!D) {
4537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    printf("ERROR: Couldn't create disassembler for triple %s\n", triple);
462bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    return;
472bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  }
482bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
4990fd79a222652126ee7d85f3afb2cd85aa51b2f9NAKAMURA Takumi  pos = 0;
502bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  while (pos < siz) {
512bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    size_t l = LLVMDisasmInstruction(D, buf + pos, siz - pos, 0, outline,
522bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg                                     sizeof(outline));
532bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    if (!l) {
542bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      pprint(pos, buf + pos, 1, "\t???");
552bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      pos++;
562bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    } else {
572bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      pprint(pos, buf + pos, l, outline);
582bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      pos += l;
592bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    }
602bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  }
612bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
622bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  LLVMDisasmDispose(D);
632bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg}
642bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
652bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborgstatic void handle_line(char **tokens, int ntokens) {
662bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  unsigned char disbuf[128];
672bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  size_t disbuflen = 0;
6837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const char *triple = tokens[0];
6937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const char *features = tokens[1];
7090fd79a222652126ee7d85f3afb2cd85aa51b2f9NAKAMURA Takumi  int i;
712bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
7237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  printf("triple: %s, features: %s\n", triple, features);
7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!strcmp(features, "NULL"))
7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    features = "";
752bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
7637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  for (i = 2; i < ntokens; i++) {
772bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    disbuf[disbuflen++] = strtol(tokens[i], NULL, 16);
782bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    if (disbuflen >= sizeof(disbuf)) {
792bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      fprintf(stderr, "Warning: Too long line, truncating\n");
802bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg      break;
812bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg    }
822bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  }
8337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  do_disassemble(triple, features, disbuf, disbuflen);
842bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg}
852bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
862bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborgint disassemble(void) {
872bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  LLVMInitializeAllTargetInfos();
882bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  LLVMInitializeAllTargetMCs();
892bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  LLVMInitializeAllDisassemblers();
902bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
912bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  tokenize_stdin(handle_line);
922bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg
932bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg  return 0;
942bef1a6b25d938210547cd0f5ba4a08abdad2583Anders Waldenborg}
95