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 **************************************************************************/
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>
36 #if HAVE_LLVM >= 0x0209
37 #include <llvm/Support/Host.h>
39 #include <llvm/System/Host.h>
42 #if HAVE_LLVM >= 0x0207
43 #include <llvm/MC/MCDisassembler.h>
44 #include <llvm/MC/MCAsmInfo.h>
45 #include <llvm/MC/MCInst.h>
46 #include <llvm/MC/MCInstPrinter.h>
47 #endif /* HAVE_LLVM >= 0x0207 */
49 #include "util/u_math.h"
50 #include "util/u_debug.h"
52 #include "lp_bld_debug.h"
59 * It is important that this check is not implemented as a macro or inlined
60 * function, as the compiler assumptions in respect to alignment of global
61 * and stack variables would often make the check a no op, defeating the
62 * whole purpose of the exercise.
65 lp_check_alignment(const void *ptr
, unsigned alignment
)
67 assert(util_is_power_of_two(alignment
));
68 return ((uintptr_t)ptr
& (alignment
- 1)) == 0;
72 class raw_debug_ostream
:
73 public llvm::raw_ostream
77 void write_impl(const char *Ptr
, size_t Size
);
78 uint64_t current_pos() { return pos
; }
79 uint64_t current_pos() const { return pos
; }
81 #if HAVE_LLVM >= 0x207
82 uint64_t preferred_buffer_size() { return 512; }
84 size_t preferred_buffer_size() { return 512; }
90 raw_debug_ostream::write_impl(const char *Ptr
, size_t Size
)
93 char *lastPtr
= (char *)&Ptr
[Size
];
96 _debug_printf("%*s", Size
, Ptr
);
104 * Same as LLVMDumpValue, but through our debugging channels.
107 lp_debug_dump_value(LLVMValueRef value
)
109 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
110 raw_debug_ostream os
;
111 llvm::unwrap(value
)->print(os
);
114 LLVMDumpValue(value
);
119 #if HAVE_LLVM >= 0x0207
121 * MemoryObject wrapper around a buffer of memory, to be used by MC
124 class BufferMemoryObject
:
125 public llvm::MemoryObject
128 const uint8_t *Bytes
;
131 BufferMemoryObject(const uint8_t *bytes
, uint64_t length
) :
132 Bytes(bytes
), Length(length
)
136 uint64_t getBase() const
141 uint64_t getExtent() const
146 int readByte(uint64_t addr
, uint8_t *byte
) const
148 if (addr
> getExtent())
154 #endif /* HAVE_LLVM >= 0x0207 */
158 * Disassemble a function, using the LLVM MC disassembler.
161 * - http://blog.llvm.org/2010/01/x86-disassembler.html
162 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
165 lp_disassemble(const void* func
)
167 #if HAVE_LLVM >= 0x0207
168 using namespace llvm
;
170 const uint8_t *bytes
= (const uint8_t *)func
;
173 * Limit disassembly to this extent
175 const uint64_t extent
= 0x10000;
180 * Initialize all used objects.
183 std::string Triple
= sys::getHostTriple();
186 const Target
*T
= TargetRegistry::lookupTarget(Triple
, Error
);
188 #if HAVE_LLVM >= 0x0208
189 InitializeNativeTargetAsmPrinter();
191 InitializeAllAsmPrinters();
194 InitializeAllDisassemblers();
196 OwningPtr
<const MCAsmInfo
> AsmInfo(T
->createAsmInfo(Triple
));
199 debug_printf("error: no assembly info for target %s\n", Triple
.c_str());
203 OwningPtr
<const MCDisassembler
> DisAsm(T
->createMCDisassembler());
205 debug_printf("error: no disassembler for target %s\n", Triple
.c_str());
209 raw_debug_ostream Out
;
211 #if HAVE_LLVM >= 0x0300
212 unsigned int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
214 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
216 #if HAVE_LLVM >= 0x0208
217 OwningPtr
<MCInstPrinter
> Printer(
218 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
));
220 OwningPtr
<MCInstPrinter
> Printer(
221 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, Out
));
224 debug_printf("error: no instruction printer for target %s\n", Triple
.c_str());
228 #if HAVE_LLVM >= 0x0300
229 TargetMachine
*TM
= T
->createTargetMachine(Triple
, sys::getHostCPUName(), "");
231 TargetMachine
*TM
= T
->createTargetMachine(Triple
, "");
234 const TargetInstrInfo
*TII
= TM
->getInstrInfo();
237 * Wrap the data in a MemoryObject
239 BufferMemoryObject
memoryObject((const uint8_t *)bytes
, extent
);
248 * Print address. We use addresses relative to the start of the function,
249 * so that between runs.
252 debug_printf("%6lu:\t", (unsigned long)pc
);
254 if (!DisAsm
->getInstruction(Inst
, Size
, memoryObject
,
257 debug_printf("invalid\n");
262 * Output the bytes in hexidecimal format.
267 for (i
= 0; i
< Size
; ++i
) {
268 debug_printf("%02x ", ((const uint8_t*)bytes
)[pc
+ i
]);
270 for (; i
< 16; ++i
) {
276 * Print the instruction.
279 #if HAVE_LLVM >= 0x208
280 Printer
->printInst(&Inst
, Out
);
282 Printer
->printInst(&Inst
);
292 const TargetInstrDesc
&TID
= TII
->get(Inst
.getOpcode());
295 * Keep track of forward jumps to a nearby address.
298 if (TID
.isBranch()) {
299 for (unsigned i
= 0; i
< Inst
.getNumOperands(); ++i
) {
300 const MCOperand
&operand
= Inst
.getOperand(i
);
301 if (operand
.isImm()) {
305 * FIXME: Handle both relative and absolute addresses correctly.
306 * EDInstInfo actually has this info, but operandTypes and
307 * operandFlags enums are not exposed in the public interface.
315 jump
= pc
+ operand
.getImm();
321 jump
= (uint64_t)operand
.getImm();
325 * Output the address relative to the function start, given
326 * that MC will print the addresses relative the current pc.
328 debug_printf("\t\t; %lu", (unsigned long)jump
);
331 * Ignore far jumps given it could be actually a tail return to
346 * Stop disassembling on return statements, if there is no record of a
347 * jump to a successive address.
350 if (TID
.isReturn()) {
358 * Print GDB command, useful to verify output.
362 debug_printf("disassemble %p %p\n", bytes
, bytes
+ pc
);
366 #else /* HAVE_LLVM < 0x0207 */
368 #endif /* HAVE_LLVM < 0x0207 */