dis_sparc.c revision 702941cd97e30d600c2c1275823bc58231b2c361
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*
9e2cd38c103ae52a41b09823a11c9b5c059555f0eh * CDDL HEADER START
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The contents of this file are subject to the terms of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Common Development and Distribution License (the "License").
bb5e3b2f129cc39517b925419c22f69a378ec023eh * You may not use this file except in compliance with the License.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
bb5e3b2f129cc39517b925419c22f69a378ec023eh * or http://www.opensolaris.org/os/licensing.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * See the License for the specific language governing permissions
bb5e3b2f129cc39517b925419c22f69a378ec023eh * and limitations under the License.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * When distributing Covered Code, include this CDDL HEADER in each
bb5e3b2f129cc39517b925419c22f69a378ec023eh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If applicable, add the following below this CDDL HEADER, with the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * fields enclosed by brackets "[]" replaced with your own identifying
bb5e3b2f129cc39517b925419c22f69a378ec023eh * information: Portions Copyright [yyyy] [name of copyright owner]
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * CDDL HEADER END
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Use is subject to license terms.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Copyright 2007 Jason King. All rights reserved.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Use is subject to license terms.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The sparc disassembler is mostly straightforward, each instruction is
bb5e3b2f129cc39517b925419c22f69a378ec023eh * represented by an inst_t structure. The inst_t definitions are organized
bb5e3b2f129cc39517b925419c22f69a378ec023eh * into tables. The tables are correspond to the opcode maps documented in the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * various sparc architecture manuals. Each table defines the bit range of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instruction whose value act as an index into the array of instructions. A
bb5e3b2f129cc39517b925419c22f69a378ec023eh * table can also refer to another table if needed. Each table also contains
bb5e3b2f129cc39517b925419c22f69a378ec023eh * a function pointer of type format_fcn that knows how to output the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instructions in the table, as well as handle any synthetic instructions
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instructions, they sometimes renamed or just reused the same instruction to
bb5e3b2f129cc39517b925419c22f69a378ec023eh * do different operations (i.e. the sparcv8 coprocessor instructions). To
bb5e3b2f129cc39517b925419c22f69a378ec023eh * accommodate this, each table can define an overlay table. The overlay table
bb5e3b2f129cc39517b925419c22f69a378ec023eh * is a list of (table index, architecture, new instruction definition) values.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Traversal starts with the first table,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * get index value from the instruction
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * if an relevant overlay entry exists for this index,
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * grab the overlay definition
bb5e3b2f129cc39517b925419c22f69a378ec023eh * else
bb5e3b2f129cc39517b925419c22f69a378ec023eh * grab the definition from the array (corresponding to the index value)
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the entry is an instruction,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * call print function of instruction.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the entry is a pointer to another table
bb5e3b2f129cc39517b925419c22f69a378ec023eh * traverse the table
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If not valid,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * return an error
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * To keep dis happy, for sparc, instead of actually returning an error, if
bb5e3b2f129cc39517b925419c22f69a378ec023eh * the instruction cannot be disassembled, we instead merely place the value
bb5e3b2f129cc39517b925419c22f69a378ec023eh * of the instruction into the output buffer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
193974072f41a843678abf5f61979c748687e66bSherry Moore * Adding new instructions:
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * With the above information, it hopefully makes it clear how to add support
bb5e3b2f129cc39517b925419c22f69a378ec023eh * for decoding new instructions. Presumably, with new instructions will come
bb5e3b2f129cc39517b925419c22f69a378ec023eh * a new dissassembly mode (I.e. DIS_SPARC_V8, DIS_SPARC_V9, etc.).
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the dissassembled format does not correspond to one of the existing
bb5e3b2f129cc39517b925419c22f69a378ec023eh * formats, a new formatter will have to be written. The 'flags' value of
bb5e3b2f129cc39517b925419c22f69a378ec023eh * inst_t is intended to instruct the corresponding formatter about how to
bb5e3b2f129cc39517b925419c22f69a378ec023eh * output the instruction.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * If the corresponding entry in the correct table is currently unoccupied,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * simply replace the INVALID entry with the correct definition. The INST and
bb5e3b2f129cc39517b925419c22f69a378ec023eh * TABLE macros are suggested to be used for this. If there is already an
bb5e3b2f129cc39517b925419c22f69a378ec023eh * instruction defined, then the entry must be placed in an overlay table. If
bb5e3b2f129cc39517b925419c22f69a378ec023eh * no overlay table exists for the instruction table, one will need to be
bb5e3b2f129cc39517b925419c22f69a378ec023eh * created.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <libdisasm.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <stdlib.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <stdio.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <sys/types.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <sys/byteorder.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <string.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include "libdisasm_impl.h"
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include "dis_sparc.h"
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint32_t);
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic uint32_t dis_get_bits(uint32_t, int, int);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic void do_binary(uint32_t);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_handle_t *
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
bb5e3b2f129cc39517b925419c22f69a378ec023eh dis_read_f read_func)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023eh char *opt = NULL;
bb5e3b2f129cc39517b925419c22f69a378ec023eh char *opt2, *save, *end;
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh dis_handle_t *dhp;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI)) == 0) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh (void) dis_seterrno(E_DIS_INVALFLAG);
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (NULL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((dhp = dis_zalloc(sizeof (struct dis_handle))) == NULL) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh (void) dis_seterrno(E_DIS_NOMEM);
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (NULL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_lookup = lookup_func;
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_read = read_func;
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_flags = flags;
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_data = data;
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug = DIS_DEBUG_COMPAT;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh opt = getenv("_LIBDISASM_DEBUG");
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (opt == NULL)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (dhp);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh opt2 = strdup(opt);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (opt2 == NULL) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh dis_handle_destroy(dhp);
bb5e3b2f129cc39517b925419c22f69a378ec023eh (void) dis_seterrno(E_DIS_NOMEM);
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (NULL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh save = opt2;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh while (opt2 != NULL) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh end = strchr(opt2, ',');
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (end != 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh *end++ = '\0';
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (strcasecmp("synth-all", opt2) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug |= DIS_DEBUG_SYN_ALL;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (strcasecmp("compat", opt2) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug |= DIS_DEBUG_COMPAT;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (strcasecmp("synth-none", opt2) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (strcasecmp("binary", opt2) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug |= DIS_DEBUG_PRTBIN;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (strcasecmp("format", opt2) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug |= DIS_DEBUG_PRTFMT;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (strcasecmp("all", opt2) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug = DIS_DEBUG_ALL;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (strcasecmp("none", opt2) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_debug = DIS_DEBUG_NONE;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh opt2 = end;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh free(save);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (dhp);
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehvoid
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_handle_destroy(dis_handle_t *dhp)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh dis_free(dhp, sizeof (dis_handle_t));
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehvoid
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_set_data(dis_handle_t *dhp, void *data)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_data = data;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehvoid
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_flags_set(dis_handle_t *dhp, int f)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_flags |= f;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehvoid
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_flags_clear(dis_handle_t *dhp, int f)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_flags &= ~f;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/* ARGSUSED */
bb5e3b2f129cc39517b925419c22f69a378ec023ehint
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_max_instrlen(dis_handle_t *dhp)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (4);
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The dis_i386.c comment for this says it returns the previous instruction,
bb5e3b2f129cc39517b925419c22f69a378ec023eh * however, I'm fairly sure it's actually returning the _address_ of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * nth previous instruction.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh/* ARGSUSED */
bb5e3b2f129cc39517b925419c22f69a378ec023ehuint64_t
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (n <= 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (pc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (pc < n)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (pc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (pc - n*4);
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/* ARGSUSED */
bb5e3b2f129cc39517b925419c22f69a378ec023ehint
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_instrlen(dis_handle_t *dhp, uint64_t pc)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (4);
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehint
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh const table_t *tp = &initial_table;
bb5e3b2f129cc39517b925419c22f69a378ec023eh const inst_t *inp = NULL;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint32_t instr;
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint32_t idx = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
bb5e3b2f129cc39517b925419c22f69a378ec023eh sizeof (instr))
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (-1);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_buf = buf;
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_buflen = buflen;
bb5e3b2f129cc39517b925419c22f69a378ec023eh dhp->dh_addr = addr;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh buf[0] = '\0';
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* this allows sparc code to be tested on x86 */
bb5e3b2f129cc39517b925419c22f69a378ec023eh instr = BE_32(instr);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((dhp->dh_debug & DIS_DEBUG_PRTBIN) != 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh do_binary(instr);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* CONSTCOND */
bb5e3b2f129cc39517b925419c22f69a378ec023eh while (1) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
bb5e3b2f129cc39517b925419c22f69a378ec023eh inp = &tp->tbl_inp[idx];
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh inp = dis_get_overlay(dhp, tp, idx);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((inp->in_type == INST_NONE) ||
bb5e3b2f129cc39517b925419c22f69a378ec023eh ((inp->in_arch & dhp->dh_flags) == 0))
bb5e3b2f129cc39517b925419c22f69a378ec023eh goto error;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (inp->in_type == INST_TBL) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh tp = inp->in_data.in_tbl;
bb5e3b2f129cc39517b925419c22f69a378ec023eh continue;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh break;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (tp->tbl_fmt(dhp, instr, inp, idx) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (0);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eherror:
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh (void) snprintf(buf, buflen,
bb5e3b2f129cc39517b925419c22f69a378ec023eh ((dhp->dh_flags & DIS_OCTAL) != 0) ? "0%011lo" : "0x%08lx",
bb5e3b2f129cc39517b925419c22f69a378ec023eh instr);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (0);
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic uint32_t
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_get_bits(uint32_t instr, int offset, int length)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint32_t mask, val;
bb5e3b2f129cc39517b925419c22f69a378ec023eh int i;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh for (i = 0, mask = 0; i < length; ++i)
bb5e3b2f129cc39517b925419c22f69a378ec023eh mask |= (1UL << i);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh mask = mask << (offset - length + 1);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh val = instr & mask;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh val = val >> (offset - length + 1);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (val);
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic const inst_t *
bb5e3b2f129cc39517b925419c22f69a378ec023ehdis_get_overlay(dis_handle_t *dhp, const table_t *tp, uint32_t idx)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh const inst_t *ip = &tp->tbl_inp[idx];
bb5e3b2f129cc39517b925419c22f69a378ec023eh int i;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (tp->tbl_ovp == NULL)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (ip);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh for (i = 0; tp->tbl_ovp[i].ov_idx != -1; ++i) {
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (tp->tbl_ovp[i].ov_idx != idx)
bb5e3b2f129cc39517b925419c22f69a378ec023eh continue;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh continue;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh ip = &tp->tbl_ovp[i].ov_inst;
bb5e3b2f129cc39517b925419c22f69a378ec023eh break;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh return (ip);
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#if !defined(DIS_STANDALONE)
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic void
bb5e3b2f129cc39517b925419c22f69a378ec023ehdo_binary(uint32_t instr)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh (void) fprintf(stderr, "DISASM: ");
bb5e3b2f129cc39517b925419c22f69a378ec023eh prt_binary(instr, 32);
bb5e3b2f129cc39517b925419c22f69a378ec023eh (void) fprintf(stderr, "\n");
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif /* DIS_STANDALONE */
bb5e3b2f129cc39517b925419c22f69a378ec023eh