1 /* Disassembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
6 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
8 This file is part of the GNU Binutils and GDB, the GNU debugger.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 /* ??? The layout of this stuff is still work in progress.
33 For speed in assembly/disassembly, we use inline functions. That of course
34 will only work for GCC. When this stuff is finished, we can decide whether
35 to keep the inline functions (and only get the performance increase when
36 compiled with GCC), or switch to macros, or use something else.
39 /* Default text to print if an instruction isn't recognized. */
40 #define UNKNOWN_INSN_MSG "*unknown*"
42 /* FIXME: Machine generate. */
43 #ifndef CGEN_PCREL_OFFSET
44 #define CGEN_PCREL_OFFSET 0
47 static int print_insn PARAMS ((bfd_vma, disassemble_info *, char *, int));
49 static int extract_insn_normal
50 PARAMS ((const CGEN_INSN *, void *, cgen_insn_t, CGEN_FIELDS *));
51 static void print_insn_normal
52 PARAMS ((void *, const CGEN_INSN *, CGEN_FIELDS *, bfd_vma, int));
54 /* Default extraction routine.
56 ATTRS is a mask of the boolean attributes. We only need `unsigned',
57 but for generality we take a bitmask of all of them. */
60 extract_normal (buf_ctrl, insn_value, attrs, start, length, shift, total_length, valuep)
62 cgen_insn_t insn_value;
64 int start, length, shift, total_length;
71 value = ((insn_value >> (CGEN_BASE_INSN_BITSIZE - (start + length)))
72 & ((1 << length) - 1));
74 value = ((insn_value >> (total_length - (start + length)))
75 & ((1 << length) - 1));
77 if (! (attrs & CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED))
78 && (value & (1 << (length - 1))))
81 /* FIXME: unfinished */
84 /* This is backwards as we undo the effects of insert_normal. */
96 /* Default print handler. */
99 print_normal (dis_info, value, attrs, pc, length)
103 unsigned long pc; /* FIXME: should be bfd_vma */
106 disassemble_info *info = dis_info;
108 /* Print the operand as directed by the attributes. */
109 if (attrs & CGEN_ATTR_MASK (CGEN_OPERAND_FAKE))
110 ; /* nothing to do (??? at least not yet) */
111 else if (attrs & CGEN_ATTR_MASK (CGEN_OPERAND_PCREL_ADDR))
112 (*info->print_address_func) (pc + CGEN_PCREL_OFFSET + value, info);
113 /* ??? Not all cases of this are currently caught. */
114 else if (attrs & CGEN_ATTR_MASK (CGEN_OPERAND_ABS_ADDR))
115 /* FIXME: Why & 0xffffffff? */
116 (*info->print_address_func) ((bfd_vma) value & 0xffffffff, info);
117 else if (attrs & CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED))
118 (*info->fprintf_func) (info->stream, "0x%lx", value);
120 (*info->fprintf_func) (info->stream, "%ld", value);
123 /* Keyword print handler. */
126 print_keyword (dis_info, keyword_table, value, attrs)
128 CGEN_KEYWORD *keyword_table;
132 disassemble_info *info = dis_info;
133 const CGEN_KEYWORD_ENTRY *ke;
135 ke = cgen_keyword_lookup_value (keyword_table, value);
137 (*info->fprintf_func) (info->stream, "%s", ke->name);
139 (*info->fprintf_func) (info->stream, "???");
142 /* -- disassembler routines inserted here */
144 /* Default insn extractor.
146 The extracted fields are stored in DIS_FLDS.
147 BUF_CTRL is used to handle reading variable length insns (FIXME: not done).
148 Return the length of the insn in bits, or 0 if no match. */
151 extract_insn_normal (insn, buf_ctrl, insn_value, fields)
152 const CGEN_INSN *insn;
154 cgen_insn_t insn_value;
157 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
158 const unsigned char *syn;
160 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
162 CGEN_INIT_EXTRACT ();
164 for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
168 if (CGEN_SYNTAX_CHAR_P (*syn))
171 length = @arch@_cgen_extract_operand (CGEN_SYNTAX_FIELD (*syn),
172 buf_ctrl, insn_value, fields);
177 /* We recognized and successfully extracted this insn. */
178 return CGEN_INSN_BITSIZE (insn);
181 /* Default insn printer.
183 DIS_INFO is defined as `PTR' so the disassembler needn't know anything
184 about disassemble_info.
188 print_insn_normal (dis_info, insn, fields, pc, length)
190 const CGEN_INSN *insn;
195 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
196 disassemble_info *info = dis_info;
197 const unsigned char *syn;
201 for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
203 if (CGEN_SYNTAX_MNEMONIC_P (*syn))
205 (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn));
208 if (CGEN_SYNTAX_CHAR_P (*syn))
210 (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn));
214 /* We have an operand. */
215 @arch@_cgen_print_operand (CGEN_SYNTAX_FIELD (*syn), info,
216 fields, CGEN_INSN_ATTRS (insn), pc, length);
220 /* Default value for CGEN_PRINT_INSN.
221 Given BUFLEN bits (target byte order) read into BUF, look up the
222 insn in the instruction table and disassemble it.
224 The result is the size of the insn in bytes. */
226 #ifndef CGEN_PRINT_INSN
227 #define CGEN_PRINT_INSN print_insn
231 print_insn (pc, info, buf, buflen)
233 disassemble_info *info;
237 unsigned long insn_value;
238 const CGEN_INSN_LIST *insn_list;
246 insn_value = info->endian == BFD_ENDIAN_BIG ? bfd_getb16 (buf) : bfd_getl16 (buf);
249 insn_value = info->endian == BFD_ENDIAN_BIG ? bfd_getb32 (buf) : bfd_getl32 (buf);
255 /* The instructions are stored in hash lists.
256 Pick the first one and keep trying until we find the right one. */
258 insn_list = CGEN_DIS_LOOKUP_INSN (buf, insn_value);
259 while (insn_list != NULL)
261 const CGEN_INSN *insn = insn_list->insn;
265 #if 0 /* not needed as insn shouldn't be in hash lists if not supported */
266 /* Supported by this cpu? */
267 if (! @arch@_cgen_insn_supported (insn))
271 /* Basic bit mask must be correct. */
272 /* ??? May wish to allow target to defer this check until the extract
274 if ((insn_value & CGEN_INSN_MASK (insn)) == CGEN_INSN_VALUE (insn))
276 /* Printing is handled in two passes. The first pass parses the
277 machine insn and extracts the fields. The second pass prints
280 length = (*CGEN_EXTRACT_FN (insn)) (insn, NULL, insn_value, &fields);
283 (*CGEN_PRINT_FN (insn)) (info, insn, &fields, pc, length);
284 /* length is in bits, result is in bytes */
289 insn_list = CGEN_DIS_NEXT_INSN (insn_list);
296 Print one instruction from PC on INFO->STREAM.
297 Return the size of the instruction (in bytes). */
300 print_insn_@arch@ (pc, info)
302 disassemble_info *info;
304 char buffer[CGEN_MAX_INSN_SIZE];
306 static int initialized = 0;
307 static int current_mach = 0;
308 static int current_big_p = 0;
309 int mach = info->mach;
310 int big_p = info->endian == BFD_ENDIAN_BIG;
312 /* If we haven't initialized yet, or if we've switched cpu's, initialize. */
313 if (!initialized || mach != current_mach || big_p != current_big_p)
317 current_big_p = big_p;
318 @arch@_cgen_init_dis (mach, big_p ? CGEN_ENDIAN_BIG : CGEN_ENDIAN_LITTLE);
321 /* Read enough of the insn so we can look it up in the hash lists. */
323 status = (*info->read_memory_func) (pc, buffer, CGEN_BASE_INSN_SIZE, info);
326 (*info->memory_error_func) (status, pc, info);
330 /* We try to have as much common code as possible.
331 But at this point some targets need to take over. */
332 /* ??? Some targets may need a hook elsewhere. Try to avoid this,
333 but if not possible try to move this hook elsewhere rather than
335 length = CGEN_PRINT_INSN (pc, info, buffer, CGEN_BASE_INSN_BITSIZE);
339 (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG);
340 return CGEN_DEFAULT_INSN_SIZE;