gallivm: Use LLVM MC disassembler, instead of udis86.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_debug.cpp
1 /**************************************************************************
2 *
3 * Copyright 2009-2011 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <llvm-c/Core.h>
29 #include <llvm/Target/TargetMachine.h>
30 #include <llvm/Target/TargetRegistry.h>
31 #include <llvm/Target/TargetSelect.h>
32 #include <llvm/Target/TargetInstrInfo.h>
33 #include <llvm/Support/raw_ostream.h>
34 #include <llvm/Support/MemoryObject.h>
35 #include <llvm/System/Host.h>
36
37 #if HAVE_LLVM >= 0x0207
38 #include <llvm/MC/MCDisassembler.h>
39 #include <llvm/MC/MCAsmInfo.h>
40 #include <llvm/MC/MCInst.h>
41 #include <llvm/MC/MCInstPrinter.h>
42 #endif /* HAVE_LLVM >= 0x0207 */
43
44 #include "util/u_math.h"
45 #include "util/u_debug.h"
46
47 #include "lp_bld_debug.h"
48
49
50
51 /**
52 * Check alignment.
53 *
54 * It is important that this check is not implemented as a macro or inlined
55 * function, as the compiler assumptions in respect to alignment of global
56 * and stack variables would often make the check a no op, defeating the
57 * whole purpose of the exercise.
58 */
59 extern "C" boolean
60 lp_check_alignment(const void *ptr, unsigned alignment)
61 {
62 assert(util_is_power_of_two(alignment));
63 return ((uintptr_t)ptr & (alignment - 1)) == 0;
64 }
65
66
67 class raw_debug_ostream :
68 public llvm::raw_ostream
69 {
70 uint64_t pos;
71
72 void write_impl(const char *Ptr, size_t Size);
73 uint64_t current_pos() { return pos; }
74 uint64_t current_pos() const { return pos; }
75
76 #if HAVE_LLVM >= 0x207
77 uint64_t preferred_buffer_size() { return 512; }
78 #else
79 size_t preferred_buffer_size() { return 512; }
80 #endif
81 };
82
83
84 void
85 raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
86 {
87 if (Size > 0) {
88 char *lastPtr = (char *)&Ptr[Size];
89 char last = *lastPtr;
90 *lastPtr = 0;
91 _debug_printf("%*s", Size, Ptr);
92 *lastPtr = last;
93 pos += Size;
94 }
95 }
96
97
98 /**
99 * Same as LLVMDumpValue, but through our debugging channels.
100 */
101 extern "C" void
102 lp_debug_dump_value(LLVMValueRef value)
103 {
104 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
105 raw_debug_ostream os;
106 llvm::unwrap(value)->print(os);
107 os.flush();
108 #else
109 LLVMDumpValue(value);
110 #endif
111 }
112
113
114 /*
115 * MemoryObject wrapper around a buffer of memory, to be used by MC
116 * disassembler.
117 */
118 class BufferMemoryObject:
119 public llvm::MemoryObject
120 {
121 private:
122 const uint8_t *Bytes;
123 uint64_t Length;
124 public:
125 BufferMemoryObject(const uint8_t *bytes, uint64_t length) :
126 Bytes(bytes), Length(length)
127 {
128 }
129
130 uint64_t getBase() const
131 {
132 return 0;
133 }
134
135 uint64_t getExtent() const
136 {
137 return Length;
138 }
139
140 int readByte(uint64_t addr, uint8_t *byte) const
141 {
142 if (addr > getExtent())
143 return -1;
144 *byte = Bytes[addr];
145 return 0;
146 }
147 };
148
149
150 /*
151 * Disassemble a function, using the LLVM MC disassembler.
152 *
153 * See also:
154 * - http://blog.llvm.org/2010/01/x86-disassembler.html
155 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
156 */
157 extern "C" void
158 lp_disassemble(const void* func)
159 {
160 #if HAVE_LLVM >= 0x0207
161 using namespace llvm;
162
163 const uint8_t *bytes = (const uint8_t *)func;
164
165 /*
166 * Limit disassembly to this extent
167 */
168 const uint64_t extent = 0x10000;
169
170 uint64_t max_pc = 0;
171
172 /*
173 * Initialize all used objects.
174 */
175
176 std::string Triple = sys::getHostTriple();
177
178 std::string Error;
179 const Target *T = TargetRegistry::lookupTarget(Triple, Error);
180
181 #if HAVE_LLVM >= 0x0208
182 InitializeNativeTargetAsmPrinter();
183 #else
184 InitializeAllAsmPrinters();
185 #endif
186
187 InitializeAllDisassemblers();
188
189 OwningPtr<const MCAsmInfo> AsmInfo(T->createAsmInfo(Triple));
190
191 if (!AsmInfo) {
192 debug_printf("error: no assembly info for target %s\n", Triple.c_str());
193 return;
194 }
195
196 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler());
197 if (!DisAsm) {
198 debug_printf("error: no disassembler for target %s\n", Triple.c_str());
199 return;
200 }
201
202 raw_debug_ostream Out;
203
204 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
205 #if HAVE_LLVM >= 0x0208
206 OwningPtr<MCInstPrinter> Printer(
207 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo));
208 #else
209 OwningPtr<MCInstPrinter> Printer(
210 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, Out));
211 #endif
212 if (!Printer) {
213 debug_printf("error: no instruction printer for target %s\n", Triple.c_str());
214 return;
215 }
216
217 TargetMachine *TM = T->createTargetMachine(Triple, "");
218
219 const TargetInstrInfo *TII = TM->getInstrInfo();
220
221 /*
222 * Wrap the data in a MemoryObject
223 */
224 BufferMemoryObject memoryObject((const uint8_t *)bytes, extent);
225
226 uint64_t pc;
227 pc = 0;
228 while (true) {
229 MCInst Inst;
230 uint64_t Size;
231
232 /*
233 * Print address. We use addresses relative to the start of the function,
234 * so that between runs.
235 */
236
237 debug_printf("%6lu:\t", (unsigned long)pc);
238
239 if (!DisAsm->getInstruction(Inst, Size, memoryObject,
240 pc,
241 nulls())) {
242 debug_printf("invalid\n");
243 pc += 1;
244 }
245
246 /*
247 * Output the bytes in hexidecimal format.
248 */
249
250 if (0) {
251 unsigned i;
252 for (i = 0; i < Size; ++i) {
253 debug_printf("%02x ", ((const uint8_t*)bytes)[pc + i]);
254 }
255 for (; i < 16; ++i) {
256 debug_printf(" ");
257 }
258 }
259
260 /*
261 * Print the instruction.
262 */
263
264 #if HAVE_LLVM >= 0x208
265 Printer->printInst(&Inst, Out);
266 #else
267 Printer->printInst(&Inst);
268 #endif
269 Out.flush();
270
271 /*
272 * Advance.
273 */
274
275 pc += Size;
276
277 const TargetInstrDesc &TID = TII->get(Inst.getOpcode());
278
279 /*
280 * Keep track of forward jumps to a nearby address.
281 */
282
283 if (TID.isBranch()) {
284 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
285 const MCOperand &operand = Inst.getOperand(i);
286 if (operand.isImm()) {
287 uint64_t jump;
288
289 /*
290 * FIXME: Handle both relative and absolute addresses correctly.
291 * EDInstInfo actually has this info, but operandTypes and
292 * operandFlags enums are not exposed in the public interface.
293 */
294
295 if (1) {
296 /*
297 * PC relative addr.
298 */
299
300 jump = pc + operand.getImm();
301 } else {
302 /*
303 * Absolute addr.
304 */
305
306 jump = (uint64_t)operand.getImm();
307 }
308
309 /*
310 * Output the address relative to the function start, given
311 * that MC will print the addresses relative the current pc.
312 */
313 debug_printf("\t\t; %lu", (unsigned long)jump);
314
315 /*
316 * Ignore far jumps given it could be actually a tail return to
317 * a random address.
318 */
319
320 if (jump > max_pc &&
321 jump < extent) {
322 max_pc = jump;
323 }
324 }
325 }
326 }
327
328 debug_printf("\n");
329
330 /*
331 * Stop disassembling on return statements, if there is no record of a
332 * jump to a successive address.
333 */
334
335 if (TID.isReturn()) {
336 if (pc > max_pc) {
337 break;
338 }
339 }
340 }
341
342 /*
343 * Print GDB command, useful to verify output.
344 */
345
346 if (0) {
347 debug_printf("disassemble %p %p\n", bytes, bytes + pc);
348 }
349
350 debug_printf("\n");
351 #else
352 (void)func;
353 #endif
354 }
355