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