gallium: Unify defines of CHAN_[XYZW] in tgsi_exec.h
[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 <stddef.h>
29
30 #include <llvm-c/Core.h>
31 #include <llvm/Target/TargetMachine.h>
32 #include <llvm/Target/TargetInstrInfo.h>
33 #include <llvm/Support/raw_ostream.h>
34 #include <llvm/Support/MemoryObject.h>
35
36 #if HAVE_LLVM >= 0x0300
37 #include <llvm/Support/TargetRegistry.h>
38 #include <llvm/Support/TargetSelect.h>
39 #else /* HAVE_LLVM < 0x0300 */
40 #include <llvm/Target/TargetRegistry.h>
41 #include <llvm/Target/TargetSelect.h>
42 #endif /* HAVE_LLVM < 0x0300 */
43
44 #if HAVE_LLVM >= 0x0209
45 #include <llvm/Support/Host.h>
46 #else /* HAVE_LLVM < 0x0209 */
47 #include <llvm/System/Host.h>
48 #endif /* HAVE_LLVM < 0x0209 */
49
50 #if HAVE_LLVM >= 0x0207
51 #include <llvm/MC/MCDisassembler.h>
52 #include <llvm/MC/MCAsmInfo.h>
53 #include <llvm/MC/MCInst.h>
54 #include <llvm/MC/MCInstPrinter.h>
55 #endif /* HAVE_LLVM >= 0x0207 */
56
57 #include "util/u_math.h"
58 #include "util/u_debug.h"
59
60 #include "lp_bld_debug.h"
61
62
63
64 /**
65 * Check alignment.
66 *
67 * It is important that this check is not implemented as a macro or inlined
68 * function, as the compiler assumptions in respect to alignment of global
69 * and stack variables would often make the check a no op, defeating the
70 * whole purpose of the exercise.
71 */
72 extern "C" boolean
73 lp_check_alignment(const void *ptr, unsigned alignment)
74 {
75 assert(util_is_power_of_two(alignment));
76 return ((uintptr_t)ptr & (alignment - 1)) == 0;
77 }
78
79
80 class raw_debug_ostream :
81 public llvm::raw_ostream
82 {
83 uint64_t pos;
84
85 void write_impl(const char *Ptr, size_t Size);
86 uint64_t current_pos() { return pos; }
87 uint64_t current_pos() const { return pos; }
88
89 #if HAVE_LLVM >= 0x207
90 uint64_t preferred_buffer_size() { return 512; }
91 #else
92 size_t preferred_buffer_size() { return 512; }
93 #endif
94 };
95
96
97 void
98 raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
99 {
100 if (Size > 0) {
101 char *lastPtr = (char *)&Ptr[Size];
102 char last = *lastPtr;
103 *lastPtr = 0;
104 _debug_printf("%*s", Size, Ptr);
105 *lastPtr = last;
106 pos += Size;
107 }
108 }
109
110
111 /**
112 * Same as LLVMDumpValue, but through our debugging channels.
113 */
114 extern "C" void
115 lp_debug_dump_value(LLVMValueRef value)
116 {
117 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
118 raw_debug_ostream os;
119 llvm::unwrap(value)->print(os);
120 os.flush();
121 #else
122 LLVMDumpValue(value);
123 #endif
124 }
125
126
127 #if HAVE_LLVM >= 0x0207
128 /*
129 * MemoryObject wrapper around a buffer of memory, to be used by MC
130 * disassembler.
131 */
132 class BufferMemoryObject:
133 public llvm::MemoryObject
134 {
135 private:
136 const uint8_t *Bytes;
137 uint64_t Length;
138 public:
139 BufferMemoryObject(const uint8_t *bytes, uint64_t length) :
140 Bytes(bytes), Length(length)
141 {
142 }
143
144 uint64_t getBase() const
145 {
146 return 0;
147 }
148
149 uint64_t getExtent() const
150 {
151 return Length;
152 }
153
154 int readByte(uint64_t addr, uint8_t *byte) const
155 {
156 if (addr > getExtent())
157 return -1;
158 *byte = Bytes[addr];
159 return 0;
160 }
161 };
162 #endif /* HAVE_LLVM >= 0x0207 */
163
164
165 /*
166 * Disassemble a function, using the LLVM MC disassembler.
167 *
168 * See also:
169 * - http://blog.llvm.org/2010/01/x86-disassembler.html
170 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
171 */
172 extern "C" void
173 lp_disassemble(const void* func)
174 {
175 #if HAVE_LLVM >= 0x0207
176 using namespace llvm;
177
178 const uint8_t *bytes = (const uint8_t *)func;
179
180 /*
181 * Limit disassembly to this extent
182 */
183 const uint64_t extent = 0x10000;
184
185 uint64_t max_pc = 0;
186
187 /*
188 * Initialize all used objects.
189 */
190
191 #if HAVE_LLVM >= 0x0301
192 std::string Triple = sys::getDefaultTargetTriple();
193 #else
194 std::string Triple = sys::getHostTriple();
195 #endif
196
197 std::string Error;
198 const Target *T = TargetRegistry::lookupTarget(Triple, Error);
199
200 #if HAVE_LLVM >= 0x0208
201 InitializeNativeTargetAsmPrinter();
202 #else
203 InitializeAllAsmPrinters();
204 #endif
205
206 InitializeAllDisassemblers();
207
208 #if HAVE_LLVM >= 0x0300
209 OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(Triple));
210 #else
211 OwningPtr<const MCAsmInfo> AsmInfo(T->createAsmInfo(Triple));
212 #endif
213
214 if (!AsmInfo) {
215 debug_printf("error: no assembly info for target %s\n", Triple.c_str());
216 return;
217 }
218
219 #if HAVE_LLVM >= 0x0300
220 const MCSubtargetInfo *STI = T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), "");
221 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI));
222 #else
223 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler());
224 #endif
225 if (!DisAsm) {
226 debug_printf("error: no disassembler for target %s\n", Triple.c_str());
227 return;
228 }
229
230 raw_debug_ostream Out;
231
232 #if HAVE_LLVM >= 0x0300
233 unsigned int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
234 #else
235 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
236 #endif
237
238 #if HAVE_LLVM >= 0x0300
239 OwningPtr<MCInstPrinter> Printer(
240 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *STI));
241 #elif HAVE_LLVM >= 0x0208
242 OwningPtr<MCInstPrinter> Printer(
243 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo));
244 #else
245 OwningPtr<MCInstPrinter> Printer(
246 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, Out));
247 #endif
248 if (!Printer) {
249 debug_printf("error: no instruction printer for target %s\n", Triple.c_str());
250 return;
251 }
252
253 #if HAVE_LLVM >= 0x0301
254 TargetOptions options;
255 #if defined(DEBUG)
256 options.JITEmitDebugInfo = true;
257 #endif
258 #if defined(PIPE_ARCH_X86)
259 options.StackAlignmentOverride = 4;
260 #endif
261 #if defined(DEBUG) || defined(PROFILE)
262 options.NoFramePointerElim = true;
263 #endif
264 TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "", options);
265 #elif HAVE_LLVM == 0x0300
266 TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "");
267 #else
268 TargetMachine *TM = T->createTargetMachine(Triple, "");
269 #endif
270
271 const TargetInstrInfo *TII = TM->getInstrInfo();
272
273 /*
274 * Wrap the data in a MemoryObject
275 */
276 BufferMemoryObject memoryObject((const uint8_t *)bytes, extent);
277
278 uint64_t pc;
279 pc = 0;
280 while (true) {
281 MCInst Inst;
282 uint64_t Size;
283
284 /*
285 * Print address. We use addresses relative to the start of the function,
286 * so that between runs.
287 */
288
289 debug_printf("%6lu:\t", (unsigned long)pc);
290
291 if (!DisAsm->getInstruction(Inst, Size, memoryObject,
292 pc,
293 #if HAVE_LLVM >= 0x0300
294 nulls(), nulls())) {
295 #else
296 nulls())) {
297 #endif
298 debug_printf("invalid\n");
299 pc += 1;
300 }
301
302 /*
303 * Output the bytes in hexidecimal format.
304 */
305
306 if (0) {
307 unsigned i;
308 for (i = 0; i < Size; ++i) {
309 debug_printf("%02x ", ((const uint8_t*)bytes)[pc + i]);
310 }
311 for (; i < 16; ++i) {
312 debug_printf(" ");
313 }
314 }
315
316 /*
317 * Print the instruction.
318 */
319
320 #if HAVE_LLVM >= 0x0300
321 Printer->printInst(&Inst, Out, "");
322 #elif HAVE_LLVM >= 0x208
323 Printer->printInst(&Inst, Out);
324 #else
325 Printer->printInst(&Inst);
326 #endif
327 Out.flush();
328
329 /*
330 * Advance.
331 */
332
333 pc += Size;
334
335 #if HAVE_LLVM >= 0x0300
336 const MCInstrDesc &TID = TII->get(Inst.getOpcode());
337 #else
338 const TargetInstrDesc &TID = TII->get(Inst.getOpcode());
339 #endif
340
341 /*
342 * Keep track of forward jumps to a nearby address.
343 */
344
345 if (TID.isBranch()) {
346 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
347 const MCOperand &operand = Inst.getOperand(i);
348 if (operand.isImm()) {
349 uint64_t jump;
350
351 /*
352 * FIXME: Handle both relative and absolute addresses correctly.
353 * EDInstInfo actually has this info, but operandTypes and
354 * operandFlags enums are not exposed in the public interface.
355 */
356
357 if (1) {
358 /*
359 * PC relative addr.
360 */
361
362 jump = pc + operand.getImm();
363 } else {
364 /*
365 * Absolute addr.
366 */
367
368 jump = (uint64_t)operand.getImm();
369 }
370
371 /*
372 * Output the address relative to the function start, given
373 * that MC will print the addresses relative the current pc.
374 */
375 debug_printf("\t\t; %lu", (unsigned long)jump);
376
377 /*
378 * Ignore far jumps given it could be actually a tail return to
379 * a random address.
380 */
381
382 if (jump > max_pc &&
383 jump < extent) {
384 max_pc = jump;
385 }
386 }
387 }
388 }
389
390 debug_printf("\n");
391
392 /*
393 * Stop disassembling on return statements, if there is no record of a
394 * jump to a successive address.
395 */
396
397 if (TID.isReturn()) {
398 if (pc > max_pc) {
399 break;
400 }
401 }
402 }
403
404 /*
405 * Print GDB command, useful to verify output.
406 */
407
408 if (0) {
409 debug_printf("disassemble %p %p\n", bytes, bytes + pc);
410 }
411
412 debug_printf("\n");
413 #else /* HAVE_LLVM < 0x0207 */
414 (void)func;
415 #endif /* HAVE_LLVM < 0x0207 */
416 }
417