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 #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 */
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 */
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 */
57 #include "util/u_math.h"
58 #include "util/u_debug.h"
60 #include "lp_bld_debug.h"
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.
73 lp_check_alignment(const void *ptr
, unsigned alignment
)
75 assert(util_is_power_of_two(alignment
));
76 return ((uintptr_t)ptr
& (alignment
- 1)) == 0;
80 class raw_debug_ostream
:
81 public llvm::raw_ostream
85 void write_impl(const char *Ptr
, size_t Size
);
87 #if HAVE_LLVM >= 0x207
88 uint64_t current_pos() const { return pos
; }
89 size_t preferred_buffer_size() const { return 512; }
91 uint64_t current_pos() { return pos
; }
92 size_t preferred_buffer_size() { return 512; }
98 raw_debug_ostream::write_impl(const char *Ptr
, size_t Size
)
101 char *lastPtr
= (char *)&Ptr
[Size
];
102 char last
= *lastPtr
;
104 _debug_printf("%*s", Size
, Ptr
);
112 * Same as LLVMDumpValue, but through our debugging channels.
115 lp_debug_dump_value(LLVMValueRef value
)
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
);
122 LLVMDumpValue(value
);
127 #if HAVE_LLVM >= 0x0207
129 * MemoryObject wrapper around a buffer of memory, to be used by MC
132 class BufferMemoryObject
:
133 public llvm::MemoryObject
136 const uint8_t *Bytes
;
139 BufferMemoryObject(const uint8_t *bytes
, uint64_t length
) :
140 Bytes(bytes
), Length(length
)
144 uint64_t getBase() const
149 uint64_t getExtent() const
154 int readByte(uint64_t addr
, uint8_t *byte
) const
156 if (addr
> getExtent())
162 #endif /* HAVE_LLVM >= 0x0207 */
166 * Disassemble a function, using the LLVM MC disassembler.
169 * - http://blog.llvm.org/2010/01/x86-disassembler.html
170 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
173 lp_disassemble(const void* func
)
175 #if HAVE_LLVM >= 0x0207
176 using namespace llvm
;
178 const uint8_t *bytes
= (const uint8_t *)func
;
181 * Limit disassembly to this extent
183 const uint64_t extent
= 0x10000;
188 * Initialize all used objects.
191 #if HAVE_LLVM >= 0x0301
192 std::string Triple
= sys::getDefaultTargetTriple();
194 std::string Triple
= sys::getHostTriple();
198 const Target
*T
= TargetRegistry::lookupTarget(Triple
, Error
);
200 #if HAVE_LLVM >= 0x0208
201 InitializeNativeTargetAsmPrinter();
202 #elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
203 LLVMInitializeX86AsmPrinter();
204 #elif defined(PIPE_ARCH_ARM)
205 LLVMInitializeARMAsmPrinter();
206 #elif defined(PIPE_ARCH_PPC)
207 LLVMInitializePowerPCAsmPrinter();
210 #if HAVE_LLVM >= 0x0301
211 InitializeNativeTargetDisassembler();
212 #elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
213 LLVMInitializeX86Disassembler();
214 #elif defined(PIPE_ARCH_ARM)
215 LLVMInitializeARMDisassembler();
218 #if HAVE_LLVM >= 0x0300
219 OwningPtr
<const MCAsmInfo
> AsmInfo(T
->createMCAsmInfo(Triple
));
221 OwningPtr
<const MCAsmInfo
> AsmInfo(T
->createAsmInfo(Triple
));
225 debug_printf("error: no assembly info for target %s\n", Triple
.c_str());
229 #if HAVE_LLVM >= 0x0300
230 const MCSubtargetInfo
*STI
= T
->createMCSubtargetInfo(Triple
, sys::getHostCPUName(), "");
231 OwningPtr
<const MCDisassembler
> DisAsm(T
->createMCDisassembler(*STI
));
233 OwningPtr
<const MCDisassembler
> DisAsm(T
->createMCDisassembler());
236 debug_printf("error: no disassembler for target %s\n", Triple
.c_str());
240 raw_debug_ostream Out
;
242 #if HAVE_LLVM >= 0x0300
243 unsigned int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
245 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
248 #if HAVE_LLVM >= 0x0301
249 OwningPtr
<const MCRegisterInfo
> MRI(T
->createMCRegInfo(Triple
));
251 debug_printf("error: no register info for target %s\n", Triple
.c_str());
255 OwningPtr
<const MCInstrInfo
> MII(T
->createMCInstrInfo());
257 debug_printf("error: no instruction info for target %s\n", Triple
.c_str());
262 #if HAVE_LLVM >= 0x0301
263 OwningPtr
<MCInstPrinter
> Printer(
264 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *MII
, *MRI
, *STI
));
265 #elif HAVE_LLVM == 0x0300
266 OwningPtr
<MCInstPrinter
> Printer(
267 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *STI
));
268 #elif HAVE_LLVM >= 0x0208
269 OwningPtr
<MCInstPrinter
> Printer(
270 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
));
272 OwningPtr
<MCInstPrinter
> Printer(
273 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, Out
));
276 debug_printf("error: no instruction printer for target %s\n", Triple
.c_str());
280 #if HAVE_LLVM >= 0x0301
281 TargetOptions options
;
283 options
.JITEmitDebugInfo
= true;
285 #if defined(PIPE_ARCH_X86)
286 options
.StackAlignmentOverride
= 4;
288 #if defined(DEBUG) || defined(PROFILE)
289 options
.NoFramePointerElim
= true;
291 TargetMachine
*TM
= T
->createTargetMachine(Triple
, sys::getHostCPUName(), "", options
);
292 #elif HAVE_LLVM == 0x0300
293 TargetMachine
*TM
= T
->createTargetMachine(Triple
, sys::getHostCPUName(), "");
295 TargetMachine
*TM
= T
->createTargetMachine(Triple
, "");
298 const TargetInstrInfo
*TII
= TM
->getInstrInfo();
301 * Wrap the data in a MemoryObject
303 BufferMemoryObject
memoryObject((const uint8_t *)bytes
, extent
);
312 * Print address. We use addresses relative to the start of the function,
313 * so that between runs.
316 debug_printf("%6lu:\t", (unsigned long)pc
);
318 if (!DisAsm
->getInstruction(Inst
, Size
, memoryObject
,
320 #if HAVE_LLVM >= 0x0300
325 debug_printf("invalid\n");
330 * Output the bytes in hexidecimal format.
335 for (i
= 0; i
< Size
; ++i
) {
336 debug_printf("%02x ", ((const uint8_t*)bytes
)[pc
+ i
]);
338 for (; i
< 16; ++i
) {
344 * Print the instruction.
347 #if HAVE_LLVM >= 0x0300
348 Printer
->printInst(&Inst
, Out
, "");
349 #elif HAVE_LLVM >= 0x208
350 Printer
->printInst(&Inst
, Out
);
352 Printer
->printInst(&Inst
);
362 #if HAVE_LLVM >= 0x0300
363 const MCInstrDesc
&TID
= TII
->get(Inst
.getOpcode());
365 const TargetInstrDesc
&TID
= TII
->get(Inst
.getOpcode());
369 * Keep track of forward jumps to a nearby address.
372 if (TID
.isBranch()) {
373 for (unsigned i
= 0; i
< Inst
.getNumOperands(); ++i
) {
374 const MCOperand
&operand
= Inst
.getOperand(i
);
375 if (operand
.isImm()) {
379 * FIXME: Handle both relative and absolute addresses correctly.
380 * EDInstInfo actually has this info, but operandTypes and
381 * operandFlags enums are not exposed in the public interface.
389 jump
= pc
+ operand
.getImm();
395 jump
= (uint64_t)operand
.getImm();
399 * Output the address relative to the function start, given
400 * that MC will print the addresses relative the current pc.
402 debug_printf("\t\t; %lu", (unsigned long)jump
);
405 * Ignore far jumps given it could be actually a tail return to
420 * Stop disassembling on return statements, if there is no record of a
421 * jump to a successive address.
424 if (TID
.isReturn()) {
432 * Print GDB command, useful to verify output.
436 debug_printf("disassemble %p %p\n", bytes
, bytes
+ pc
);
440 #else /* HAVE_LLVM < 0x0207 */
442 #endif /* HAVE_LLVM < 0x0207 */