Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2009 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
29 #ifdef HAVE_UDIS86
30 #include <udis86.h>
31 #endif
32
33 #include "util/u_math.h"
34 #include "util/u_debug.h"
35 #include "lp_bld_debug.h"
36
37
38 /**
39 * Check alignment.
40 *
41 * It is important that this check is not implemented as a macro or inlined
42 * function, as the compiler assumptions in respect to alignment of global
43 * and stack variables would often make the check a no op, defeating the
44 * whole purpose of the exercise.
45 */
46 boolean
47 lp_check_alignment(const void *ptr, unsigned alignment)
48 {
49 assert(util_is_power_of_two(alignment));
50 return ((uintptr_t)ptr & (alignment - 1)) == 0;
51 }
52
53
54 void
55 lp_disassemble(const void* func)
56 {
57 #ifdef HAVE_UDIS86
58 ud_t ud_obj;
59 uint64_t max_jmp_pc;
60 uint inst_no;
61 boolean emit_addrs = TRUE, emit_line_nos = FALSE;
62
63 ud_init(&ud_obj);
64
65 ud_set_input_buffer(&ud_obj, (void*)func, 0xffff);
66
67 max_jmp_pc = (uint64_t) (uintptr_t) func;
68 ud_set_pc(&ud_obj, max_jmp_pc);
69
70 #ifdef PIPE_ARCH_X86
71 ud_set_mode(&ud_obj, 32);
72 #endif
73 #ifdef PIPE_ARCH_X86_64
74 ud_set_mode(&ud_obj, 64);
75 #endif
76
77 ud_set_syntax(&ud_obj, UD_SYN_ATT);
78
79 while (ud_disassemble(&ud_obj)) {
80
81 if (emit_addrs) {
82 #ifdef PIPE_ARCH_X86
83 debug_printf("0x%08lx:\t", (unsigned long)ud_insn_off(&ud_obj));
84 #endif
85 #ifdef PIPE_ARCH_X86_64
86 debug_printf("0x%016llx:\t", (unsigned long long)ud_insn_off(&ud_obj));
87 #endif
88 }
89 else if (emit_line_nos) {
90 debug_printf("%6d:\t", inst_no);
91 inst_no++;
92 }
93 #if 0
94 debug_printf("%-16s ", ud_insn_hex(&ud_obj));
95 #endif
96
97 debug_printf("%s\n", ud_insn_asm(&ud_obj));
98
99 if(ud_obj.mnemonic != UD_Icall) {
100 unsigned i;
101 for(i = 0; i < 3; ++i) {
102 const struct ud_operand *op = &ud_obj.operand[i];
103 if (op->type == UD_OP_JIMM){
104 uint64_t pc = ud_obj.pc;
105
106 switch (op->size) {
107 case 8:
108 pc += op->lval.sbyte;
109 break;
110 case 16:
111 pc += op->lval.sword;
112 break;
113 case 32:
114 pc += op->lval.sdword;
115 break;
116 default:
117 break;
118 }
119 if(pc > max_jmp_pc)
120 max_jmp_pc = pc;
121 }
122 }
123 }
124
125 if (ud_obj.mnemonic == UD_Iinvalid ||
126 (ud_insn_off(&ud_obj) >= max_jmp_pc &&
127 (ud_obj.mnemonic == UD_Iret ||
128 ud_obj.mnemonic == UD_Ijmp)))
129 break;
130 }
131
132 #if 0
133 /* Print GDB command, useful to verify udis86 output */
134 debug_printf("disassemble %p %p\n", func, (void*)(uintptr_t)ud_obj.pc);
135 #endif
136
137 debug_printf("\n");
138 #else
139 (void)func;
140 #endif
141 }