Merge pull request #90 from gizmo98/patch-2
[libretro-ppsspp.git] / ext / udis86 / udis86.c
blobf957de45ec9672b05d8629d88f31719589c716e5
1 /* udis86 - libudis86/udis86.c
3 * Copyright (c) 2002-2013 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.
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.
27 #include "udint.h"
28 #include "extern.h"
29 #include "decode.h"
31 #if !defined(__UD_STANDALONE__)
32 #include <string.h>
33 #endif /* !__UD_STANDALONE__ */
35 static void ud_inp_init(struct ud *u);
37 /* =============================================================================
38 * ud_init
39 * Initializes ud_t object.
40 * =============================================================================
42 extern void
43 ud_init(struct ud* u)
45 memset((void*)u, 0, sizeof(struct ud));
46 ud_set_mode(u, 16);
47 u->mnemonic = UD_Iinvalid;
48 ud_set_pc(u, 0);
49 #ifndef __UD_STANDALONE__
50 ud_set_input_file(u, stdin);
51 #endif /* __UD_STANDALONE__ */
53 ud_set_asm_buffer(u, u->asm_buf_int, sizeof(u->asm_buf_int));
57 /* =============================================================================
58 * ud_disassemble
59 * Disassembles one instruction and returns the number of
60 * bytes disassembled. A zero means end of disassembly.
61 * =============================================================================
63 extern unsigned int
64 ud_disassemble(struct ud* u)
66 int len;
67 if (u->inp_end) {
68 return 0;
70 if ((len = ud_decode(u)) > 0) {
71 if (u->translator != NULL) {
72 u->asm_buf[0] = '\0';
73 u->translator(u);
76 return len;
80 /* =============================================================================
81 * ud_set_mode() - Set Disassemly Mode.
82 * =============================================================================
84 extern void
85 ud_set_mode(struct ud* u, uint8_t m)
87 switch(m) {
88 case 16:
89 case 32:
90 case 64: u->dis_mode = m ; return;
91 default: u->dis_mode = 16; return;
95 /* =============================================================================
96 * ud_set_vendor() - Set vendor.
97 * =============================================================================
99 extern void
100 ud_set_vendor(struct ud* u, unsigned v)
102 switch(v) {
103 case UD_VENDOR_INTEL:
104 u->vendor = v;
105 break;
106 case UD_VENDOR_ANY:
107 u->vendor = v;
108 break;
109 default:
110 u->vendor = UD_VENDOR_AMD;
114 /* =============================================================================
115 * ud_set_pc() - Sets code origin.
116 * =============================================================================
118 extern void
119 ud_set_pc(struct ud* u, uint64_t o)
121 u->pc = o;
124 /* =============================================================================
125 * ud_set_syntax() - Sets the output syntax.
126 * =============================================================================
128 extern void
129 ud_set_syntax(struct ud* u, void (*t)(struct ud*))
131 u->translator = t;
134 /* =============================================================================
135 * ud_insn() - returns the disassembled instruction
136 * =============================================================================
138 const char*
139 ud_insn_asm(const struct ud* u)
141 return u->asm_buf;
144 /* =============================================================================
145 * ud_insn_offset() - Returns the offset.
146 * =============================================================================
148 uint64_t
149 ud_insn_off(const struct ud* u)
151 return u->insn_offset;
155 /* =============================================================================
156 * ud_insn_hex() - Returns hex form of disassembled instruction.
157 * =============================================================================
159 const char*
160 ud_insn_hex(struct ud* u)
162 u->insn_hexcode[0] = 0;
163 if (!u->error) {
164 unsigned int i;
165 const unsigned char *src_ptr = ud_insn_ptr(u);
166 char* src_hex;
167 src_hex = (char*) u->insn_hexcode;
168 /* for each byte used to decode instruction */
169 for (i = 0; i < ud_insn_len(u) && i < sizeof(u->insn_hexcode) / 2;
170 ++i, ++src_ptr) {
171 sprintf(src_hex, "%02x", *src_ptr & 0xFF);
172 src_hex += 2;
175 return u->insn_hexcode;
179 /* =============================================================================
180 * ud_insn_ptr
181 * Returns a pointer to buffer containing the bytes that were
182 * disassembled.
183 * =============================================================================
185 extern const uint8_t*
186 ud_insn_ptr(const struct ud* u)
188 return (u->inp_buf == NULL) ?
189 u->inp_sess : u->inp_buf + (u->inp_buf_index - u->inp_ctr);
193 /* =============================================================================
194 * ud_insn_len
195 * Returns the count of bytes disassembled.
196 * =============================================================================
198 extern unsigned int
199 ud_insn_len(const struct ud* u)
201 return (unsigned int)u->inp_ctr;
205 /* =============================================================================
206 * ud_insn_get_opr
207 * Return the operand struct representing the nth operand of
208 * the currently disassembled instruction. Returns NULL if
209 * there's no such operand.
210 * =============================================================================
212 const struct ud_operand*
213 ud_insn_opr(const struct ud *u, unsigned int n)
215 if (n > 3 || u->operand[n].type == UD_NONE) {
216 return NULL;
217 } else {
218 return &u->operand[n];
223 /* =============================================================================
224 * ud_opr_is_sreg
225 * Returns non-zero if the given operand is of a segment register type.
226 * =============================================================================
229 ud_opr_is_sreg(const struct ud_operand *opr)
231 return opr->type == UD_OP_REG &&
232 opr->base >= UD_R_ES &&
233 opr->base <= UD_R_GS;
237 /* =============================================================================
238 * ud_opr_is_sreg
239 * Returns non-zero if the given operand is of a general purpose
240 * register type.
241 * =============================================================================
244 ud_opr_is_gpr(const struct ud_operand *opr)
246 return opr->type == UD_OP_REG &&
247 opr->base >= UD_R_AL &&
248 opr->base <= UD_R_R15;
252 /* =============================================================================
253 * ud_set_user_opaque_data
254 * ud_get_user_opaque_data
255 * Get/set user opaqute data pointer
256 * =============================================================================
258 void
259 ud_set_user_opaque_data(struct ud * u, void* opaque)
261 u->user_opaque_data = opaque;
264 void*
265 ud_get_user_opaque_data(const struct ud *u)
267 return u->user_opaque_data;
271 /* =============================================================================
272 * ud_set_asm_buffer
273 * Allow the user to set an assembler output buffer. If `buf` is NULL,
274 * we switch back to the internal buffer.
275 * =============================================================================
277 void
278 ud_set_asm_buffer(struct ud *u, char *buf, size_t size)
280 if (buf == NULL) {
281 ud_set_asm_buffer(u, u->asm_buf_int, sizeof(u->asm_buf_int));
282 } else {
283 u->asm_buf = buf;
284 u->asm_buf_size = size;
289 /* =============================================================================
290 * ud_set_sym_resolver
291 * Set symbol resolver for relative targets used in the translation
292 * phase.
294 * The resolver is a function that takes a uint64_t address and returns a
295 * symbolic name for the that address. The function also takes a second
296 * argument pointing to an integer that the client can optionally set to a
297 * non-zero value for offsetted targets. (symbol+offset) The function may
298 * also return NULL, in which case the translator only prints the target
299 * address.
301 * The function pointer maybe NULL which resets symbol resolution.
302 * =============================================================================
304 void
305 ud_set_sym_resolver(struct ud *u, const char* (*resolver)(struct ud*,
306 uint64_t addr,
307 int64_t *offset))
309 u->sym_resolver = resolver;
313 /* =============================================================================
314 * ud_insn_mnemonic
315 * Return the current instruction mnemonic.
316 * =============================================================================
318 enum ud_mnemonic_code
319 ud_insn_mnemonic(const struct ud *u)
321 return u->mnemonic;
325 /* =============================================================================
326 * ud_lookup_mnemonic
327 * Looks up mnemonic code in the mnemonic string table.
328 * Returns NULL if the mnemonic code is invalid.
329 * =============================================================================
331 const char*
332 ud_lookup_mnemonic(enum ud_mnemonic_code c)
334 if (c < UD_MAX_MNEMONIC_CODE) {
335 return ud_mnemonics_str[c];
336 } else {
337 return NULL;
343 * ud_inp_init
344 * Initializes the input system.
346 static void
347 ud_inp_init(struct ud *u)
349 u->inp_hook = NULL;
350 u->inp_buf = NULL;
351 u->inp_buf_size = 0;
352 u->inp_buf_index = 0;
353 u->inp_curr = 0;
354 u->inp_ctr = 0;
355 u->inp_end = 0;
356 u->inp_peek = UD_EOI;
357 UD_NON_STANDALONE(u->inp_file = NULL);
361 /* =============================================================================
362 * ud_inp_set_hook
363 * Sets input hook.
364 * =============================================================================
366 void
367 ud_set_input_hook(register struct ud* u, int (*hook)(struct ud*))
369 ud_inp_init(u);
370 u->inp_hook = hook;
373 /* =============================================================================
374 * ud_inp_set_buffer
375 * Set buffer as input.
376 * =============================================================================
378 void
379 ud_set_input_buffer(register struct ud* u, const uint8_t* buf, size_t len)
381 ud_inp_init(u);
382 u->inp_buf = buf;
383 u->inp_buf_size = len;
384 u->inp_buf_index = 0;
388 #ifndef __UD_STANDALONE__
389 /* =============================================================================
390 * ud_input_set_file
391 * Set FILE as input.
392 * =============================================================================
394 static int
395 inp_file_hook(struct ud* u)
397 return fgetc(u->inp_file);
400 void
401 ud_set_input_file(register struct ud* u, FILE* f)
403 ud_inp_init(u);
404 u->inp_hook = inp_file_hook;
405 u->inp_file = f;
407 #endif /* __UD_STANDALONE__ */
410 /* =============================================================================
411 * ud_input_skip
412 * Skip n input bytes.
413 * ============================================================================
415 void
416 ud_input_skip(struct ud* u, size_t n)
418 if (u->inp_end) {
419 return;
421 if (u->inp_buf == NULL) {
422 while (n--) {
423 int c = u->inp_hook(u);
424 if (c == UD_EOI) {
425 goto eoi;
428 return;
429 } else {
430 if (n > u->inp_buf_size ||
431 u->inp_buf_index > u->inp_buf_size - n) {
432 u->inp_buf_index = u->inp_buf_size;
433 goto eoi;
435 u->inp_buf_index += n;
436 return;
438 eoi:
439 u->inp_end = 1;
440 UDERR(u, "cannot skip, eoi received\b");
441 return;
445 /* =============================================================================
446 * ud_input_end
447 * Returns non-zero on end-of-input.
448 * =============================================================================
451 ud_input_end(const struct ud *u)
453 return u->inp_end;
456 /* vim:set ts=2 sw=2 expandtab */