util: remove util_is_pot in favor of util_is_power_of_two
[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
61 ud_init(&ud_obj);
62
63 ud_set_input_buffer(&ud_obj, (void*)func, 0xffff);
64
65 max_jmp_pc = (uint64_t) (uintptr_t) func;
66 ud_set_pc(&ud_obj, max_jmp_pc);
67
68 #ifdef PIPE_ARCH_X86
69 ud_set_mode(&ud_obj, 32);
70 #endif
71 #ifdef PIPE_ARCH_X86_64
72 ud_set_mode(&ud_obj, 64);
73 #endif
74
75 ud_set_syntax(&ud_obj, UD_SYN_ATT);
76
77 while (ud_disassemble(&ud_obj)) {
78
79 #ifdef PIPE_ARCH_X86
80 debug_printf("0x%08lx:\t", (unsigned long)ud_insn_off(&ud_obj));
81 #endif
82 #ifdef PIPE_ARCH_X86_64
83 debug_printf("0x%016llx:\t", (unsigned long long)ud_insn_off(&ud_obj));
84 #endif
85
86 #if 0
87 debug_printf("%-16s ", ud_insn_hex(&ud_obj));
88 #endif
89
90 debug_printf("%s\n", ud_insn_asm(&ud_obj));
91
92 if(ud_obj.mnemonic != UD_Icall) {
93 unsigned i;
94 for(i = 0; i < 3; ++i) {
95 const struct ud_operand *op = &ud_obj.operand[i];
96 if (op->type == UD_OP_JIMM){
97 uint64_t pc = ud_obj.pc;
98
99 switch (op->size) {
100 case 8:
101 pc += op->lval.sbyte;
102 break;
103 case 16:
104 pc += op->lval.sword;
105 break;
106 case 32:
107 pc += op->lval.sdword;
108 break;
109 default:
110 break;
111 }
112 if(pc > max_jmp_pc)
113 max_jmp_pc = pc;
114 }
115 }
116 }
117
118 if ((ud_insn_off(&ud_obj) >= max_jmp_pc && ud_obj.mnemonic == UD_Iret) ||
119 ud_obj.mnemonic == UD_Iinvalid)
120 break;
121 }
122
123 #if 0
124 /* Print GDB command, useful to verify udis86 output */
125 debug_printf("disassemble %p %p\n", func, (void*)(uintptr_t)ud_obj.pc);
126 #endif
127
128 debug_printf("\n");
129 #else
130 (void)func;
131 #endif
132 }