1 /**************************************************************************
3 * Copyright 2009-2011 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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>
36 #if HAVE_LLVM >= 0x0300
37 #include <llvm/Support/TargetRegistry.h>
38 #else /* HAVE_LLVM < 0x0300 */
39 #include <llvm/Target/TargetRegistry.h>
40 #endif /* HAVE_LLVM < 0x0300 */
42 #if HAVE_LLVM >= 0x0209
43 #include <llvm/Support/Host.h>
44 #else /* HAVE_LLVM < 0x0209 */
45 #include <llvm/System/Host.h>
46 #endif /* HAVE_LLVM < 0x0209 */
48 #if HAVE_LLVM >= 0x0207
49 #include <llvm/MC/MCDisassembler.h>
50 #include <llvm/MC/MCAsmInfo.h>
51 #include <llvm/MC/MCInst.h>
52 #include <llvm/MC/MCInstPrinter.h>
53 #endif /* HAVE_LLVM >= 0x0207 */
54 #if HAVE_LLVM >= 0x0301
55 #include <llvm/MC/MCRegisterInfo.h>
56 #endif /* HAVE_LLVM >= 0x0301 */
58 #include "util/u_math.h"
59 #include "util/u_debug.h"
61 #include "lp_bld_debug.h"
68 * It is important that this check is not implemented as a macro or inlined
69 * function, as the compiler assumptions in respect to alignment of global
70 * and stack variables would often make the check a no op, defeating the
71 * whole purpose of the exercise.
74 lp_check_alignment(const void *ptr, unsigned alignment)
76 assert(util_is_power_of_two(alignment));
77 return ((uintptr_t)ptr & (alignment - 1)) == 0;
81 class raw_debug_ostream :
82 public llvm::raw_ostream
88 raw_debug_ostream() : pos(0) { }
90 void write_impl(const char *Ptr, size_t Size);
92 #if HAVE_LLVM >= 0x207
93 uint64_t current_pos() const { return pos; }
94 size_t preferred_buffer_size() const { return 512; }
96 uint64_t current_pos() { return pos; }
97 size_t preferred_buffer_size() { return 512; }
103 raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
106 char *lastPtr = (char *)&Ptr[Size];
107 char last = *lastPtr;
109 _debug_printf("%*s", Size, Ptr);
117 * Same as LLVMDumpValue, but through our debugging channels.
120 lp_debug_dump_value(LLVMValueRef value)
122 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
123 raw_debug_ostream os;
124 llvm::unwrap(value)->print(os);
127 LLVMDumpValue(value);
132 #if HAVE_LLVM >= 0x0207
134 * MemoryObject wrapper around a buffer of memory, to be used by MC
137 class BufferMemoryObject:
138 public llvm::MemoryObject
141 const uint8_t *Bytes;
144 BufferMemoryObject(const uint8_t *bytes, uint64_t length) :
145 Bytes(bytes), Length(length)
149 uint64_t getBase() const
154 uint64_t getExtent() const
159 int readByte(uint64_t addr, uint8_t *byte) const
161 if (addr > getExtent())
167 #endif /* HAVE_LLVM >= 0x0207 */
171 * Disassemble a function, using the LLVM MC disassembler.
174 * - http://blog.llvm.org/2010/01/x86-disassembler.html
175 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
178 lp_disassemble(const void* func)
180 #if HAVE_LLVM >= 0x0207
181 using namespace llvm;
183 const uint8_t *bytes = (const uint8_t *)func;
186 * Limit disassembly to this extent
188 const uint64_t extent = 96 * 1024;
193 * Initialize all used objects.
196 #if HAVE_LLVM >= 0x0301
197 std::string Triple = sys::getDefaultTargetTriple();
199 std::string Triple = sys::getHostTriple();
203 const Target *T = TargetRegistry::lookupTarget(Triple, Error);
205 #if HAVE_LLVM >= 0x0300
206 OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(Triple));
208 OwningPtr<const MCAsmInfo> AsmInfo(T->createAsmInfo(Triple));
212 debug_printf("error: no assembly info for target %s\n", Triple.c_str());
216 #if HAVE_LLVM >= 0x0300
217 const MCSubtargetInfo *STI = T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), "");
218 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI));
220 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler());
223 debug_printf("error: no disassembler for target %s\n", Triple.c_str());
227 raw_debug_ostream Out;
229 #if HAVE_LLVM >= 0x0300
230 unsigned int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
232 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
235 #if HAVE_LLVM >= 0x0301
236 OwningPtr<const MCRegisterInfo> MRI(T->createMCRegInfo(Triple));
238 debug_printf("error: no register info for target %s\n", Triple.c_str());
242 OwningPtr<const MCInstrInfo> MII(T->createMCInstrInfo());
244 debug_printf("error: no instruction info for target %s\n", Triple.c_str());
249 #if HAVE_LLVM >= 0x0301
250 OwningPtr<MCInstPrinter> Printer(
251 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
252 #elif HAVE_LLVM == 0x0300
253 OwningPtr<MCInstPrinter> Printer(
254 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *STI));
255 #elif HAVE_LLVM >= 0x0208
256 OwningPtr<MCInstPrinter> Printer(
257 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo));
259 OwningPtr<MCInstPrinter> Printer(
260 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, Out));
263 debug_printf("error: no instruction printer for target %s\n", Triple.c_str());
267 #if HAVE_LLVM >= 0x0301
268 TargetOptions options;
270 options.JITEmitDebugInfo = true;
272 #if defined(PIPE_ARCH_X86)
273 options.StackAlignmentOverride = 4;
275 #if defined(DEBUG) || defined(PROFILE)
276 options.NoFramePointerElim = true;
278 TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "", options);
279 #elif HAVE_LLVM == 0x0300
280 TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "");
282 TargetMachine *TM = T->createTargetMachine(Triple, "");
285 const TargetInstrInfo *TII = TM->getInstrInfo();
288 * Wrap the data in a MemoryObject
290 BufferMemoryObject memoryObject((const uint8_t *)bytes, extent);
299 * Print address. We use addresses relative to the start of the function,
300 * so that between runs.
303 debug_printf("%6lu:\t", (unsigned long)pc);
305 if (!DisAsm->getInstruction(Inst, Size, memoryObject,
307 #if HAVE_LLVM >= 0x0300
312 debug_printf("invalid\n");
317 * Output the bytes in hexidecimal format.
322 for (i = 0; i < Size; ++i) {
323 debug_printf("%02x ", ((const uint8_t*)bytes)[pc + i]);
325 for (; i < 16; ++i) {
331 * Print the instruction.
334 #if HAVE_LLVM >= 0x0300
335 Printer->printInst(&Inst, Out, "");
336 #elif HAVE_LLVM >= 0x208
337 Printer->printInst(&Inst, Out);
339 Printer->printInst(&Inst);
349 #if HAVE_LLVM >= 0x0300
350 const MCInstrDesc &TID = TII->get(Inst.getOpcode());
352 const TargetInstrDesc &TID = TII->get(Inst.getOpcode());
356 * Keep track of forward jumps to a nearby address.
359 if (TID.isBranch()) {
360 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
361 const MCOperand &operand = Inst.getOperand(i);
362 if (operand.isImm()) {
366 * FIXME: Handle both relative and absolute addresses correctly.
367 * EDInstInfo actually has this info, but operandTypes and
368 * operandFlags enums are not exposed in the public interface.
376 jump = pc + operand.getImm();
382 jump = (uint64_t)operand.getImm();
386 * Output the address relative to the function start, given
387 * that MC will print the addresses relative the current pc.
389 debug_printf("\t\t; %lu", (unsigned long)jump);
392 * Ignore far jumps given it could be actually a tail return to
407 * Stop disassembling on return statements, if there is no record of a
408 * jump to a successive address.
411 if (TID.isReturn()) {
419 * Print GDB command, useful to verify output.
423 debug_printf("disassemble %p %p\n", bytes, bytes + pc);
427 #else /* HAVE_LLVM < 0x0207 */
429 #endif /* HAVE_LLVM < 0x0207 */