]> git.saurik.com Git - apple/javascriptcore.git/blob - disassembler/udis86/udis86_syn-att.c
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / disassembler / udis86 / udis86_syn-att.c
1 /* udis86 - libudis86/syn-att.c
2 *
3 * Copyright (c) 2002-2009 Vivek Thampi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "config.h"
27
28 #if USE(UDIS86)
29
30 #include "udis86_types.h"
31 #include "udis86_extern.h"
32 #include "udis86_decode.h"
33 #include "udis86_itab.h"
34 #include "udis86_syn.h"
35
36 /* -----------------------------------------------------------------------------
37 * opr_cast() - Prints an operand cast.
38 * -----------------------------------------------------------------------------
39 */
40 static void
41 opr_cast(struct ud* u, struct ud_operand* op)
42 {
43 switch(op->size) {
44 case 16 : case 32 :
45 mkasm(u, "*"); break;
46 default: break;
47 }
48 }
49
50 /* -----------------------------------------------------------------------------
51 * gen_operand() - Generates assembly output for each operand.
52 * -----------------------------------------------------------------------------
53 */
54 static void
55 gen_operand(struct ud* u, struct ud_operand* op)
56 {
57 switch(op->type) {
58 case UD_OP_REG:
59 mkasm(u, "%%%s", ud_reg_tab[op->base - UD_R_AL]);
60 break;
61
62 case UD_OP_MEM:
63 if (u->br_far) opr_cast(u, op);
64 if (u->pfx_seg)
65 mkasm(u, "%%%s:", ud_reg_tab[u->pfx_seg - UD_R_AL]);
66 if (op->offset == 8) {
67 if (op->lval.sbyte < 0)
68 mkasm(u, "-0x%x", (-op->lval.sbyte) & 0xff);
69 else
70 mkasm(u, "0x%x", op->lval.sbyte);
71 }
72 else if (op->offset == 16) {
73 if (op->lval.sword < 0)
74 mkasm(u, "-0x%x", (-op->lval.sword) & 0xffff);
75 else
76 mkasm(u, "0x%x", op->lval.sword);
77 } else if (op->offset == 32) {
78 if (op->lval.sdword < 0)
79 mkasm(u, "-0x%x", (-op->lval.sdword) & 0xffffffff);
80 else
81 mkasm(u, "0x%x", op->lval.sdword);
82 } else if (op->offset == 64) {
83 if (op->lval.sdword < 0)
84 mkasm(u, "-0x" FMT64 "x", (uint64_t)-op->lval.sqword);
85 else
86 mkasm(u, "0x" FMT64 "x", (uint64_t)op->lval.sqword);
87 }
88
89 if (op->base)
90 mkasm(u, "(%%%s", ud_reg_tab[op->base - UD_R_AL]);
91 if (op->index) {
92 if (op->base)
93 mkasm(u, ",");
94 else mkasm(u, "(");
95 mkasm(u, "%%%s", ud_reg_tab[op->index - UD_R_AL]);
96 }
97 if (op->scale)
98 mkasm(u, ",%d", op->scale);
99 if (op->base || op->index)
100 mkasm(u, ")");
101 break;
102
103 case UD_OP_IMM: {
104 int64_t imm = 0;
105 uint64_t sext_mask = 0xffffffffffffffffull;
106 unsigned sext_size = op->size;
107
108 switch (op->size) {
109 case 8: imm = op->lval.sbyte; break;
110 case 16: imm = op->lval.sword; break;
111 case 32: imm = op->lval.sdword; break;
112 case 64: imm = op->lval.sqword; break;
113 }
114 if ( P_SEXT( u->itab_entry->prefix ) ) {
115 sext_size = u->operand[ 0 ].size;
116 if ( u->mnemonic == UD_Ipush )
117 /* push sign-extends to operand size */
118 sext_size = u->opr_mode;
119 }
120 if ( sext_size < 64 )
121 sext_mask = ( 1ull << sext_size ) - 1;
122 mkasm( u, "$0x" FMT64 "x", (uint64_t)(imm & sext_mask) );
123
124 break;
125 }
126
127 case UD_OP_JIMM:
128 switch (op->size) {
129 case 8:
130 mkasm(u, "0x" FMT64 "x", (uint64_t)(u->pc + op->lval.sbyte));
131 break;
132 case 16:
133 mkasm(u, "0x" FMT64 "x", (uint64_t)((u->pc + op->lval.sword) & 0xffff) );
134 break;
135 case 32:
136 if (u->dis_mode == 32)
137 mkasm(u, "0x" FMT64 "x", (uint64_t)((u->pc + op->lval.sdword) & 0xffffffff));
138 else
139 mkasm(u, "0x" FMT64 "x", (uint64_t)(u->pc + op->lval.sdword));
140 break;
141 default:break;
142 }
143 break;
144
145 case UD_OP_PTR:
146 switch (op->size) {
147 case 32:
148 mkasm(u, "$0x%x, $0x%x", op->lval.ptr.seg,
149 op->lval.ptr.off & 0xFFFF);
150 break;
151 case 48:
152 mkasm(u, "$0x%x, $0x%lx", op->lval.ptr.seg,
153 (unsigned long)op->lval.ptr.off);
154 break;
155 }
156 break;
157
158 default: return;
159 }
160 }
161
162 /* =============================================================================
163 * translates to AT&T syntax
164 * =============================================================================
165 */
166 extern void
167 ud_translate_att(struct ud *u)
168 {
169 int size = 0;
170
171 /* check if P_OSO prefix is used */
172 if (! P_OSO(u->itab_entry->prefix) && u->pfx_opr) {
173 switch (u->dis_mode) {
174 case 16:
175 mkasm(u, "o32 ");
176 break;
177 case 32:
178 case 64:
179 mkasm(u, "o16 ");
180 break;
181 }
182 }
183
184 /* check if P_ASO prefix was used */
185 if (! P_ASO(u->itab_entry->prefix) && u->pfx_adr) {
186 switch (u->dis_mode) {
187 case 16:
188 mkasm(u, "a32 ");
189 break;
190 case 32:
191 mkasm(u, "a16 ");
192 break;
193 case 64:
194 mkasm(u, "a32 ");
195 break;
196 }
197 }
198
199 if (u->pfx_lock)
200 mkasm(u, "lock ");
201 if (u->pfx_rep)
202 mkasm(u, "rep ");
203 if (u->pfx_repne)
204 mkasm(u, "repne ");
205
206 /* special instructions */
207 switch (u->mnemonic) {
208 case UD_Iretf:
209 mkasm(u, "lret ");
210 break;
211 case UD_Idb:
212 mkasm(u, ".byte 0x%x", u->operand[0].lval.ubyte);
213 return;
214 case UD_Ijmp:
215 case UD_Icall:
216 if (u->br_far) mkasm(u, "l");
217 mkasm(u, "%s", ud_lookup_mnemonic(u->mnemonic));
218 break;
219 case UD_Ibound:
220 case UD_Ienter:
221 if (u->operand[0].type != UD_NONE)
222 gen_operand(u, &u->operand[0]);
223 if (u->operand[1].type != UD_NONE) {
224 mkasm(u, ",");
225 gen_operand(u, &u->operand[1]);
226 }
227 return;
228 default:
229 mkasm(u, "%s", ud_lookup_mnemonic(u->mnemonic));
230 }
231
232 if (u->c1)
233 size = u->operand[0].size;
234 else if (u->c2)
235 size = u->operand[1].size;
236 else if (u->c3)
237 size = u->operand[2].size;
238
239 if (size == 8)
240 mkasm(u, "b");
241 else if (size == 16)
242 mkasm(u, "w");
243 else if (size == 64)
244 mkasm(u, "q");
245
246 mkasm(u, " ");
247
248 if (u->operand[2].type != UD_NONE) {
249 gen_operand(u, &u->operand[2]);
250 mkasm(u, ", ");
251 }
252
253 if (u->operand[1].type != UD_NONE) {
254 gen_operand(u, &u->operand[1]);
255 mkasm(u, ", ");
256 }
257
258 if (u->operand[0].type != UD_NONE)
259 gen_operand(u, &u->operand[0]);
260 }
261
262 #endif // USE(UDIS86)
263