1130803Smarcel/* Interface definition for configurable Xtensa ISA support.
2130803Smarcel   Copyright 2003 Free Software Foundation, Inc.
3130803Smarcel
4130803Smarcel   This file is part of BFD, the Binary File Descriptor library.
5130803Smarcel
6130803Smarcel   This program is free software; you can redistribute it and/or modify
7130803Smarcel   it under the terms of the GNU General Public License as published by
8130803Smarcel   the Free Software Foundation; either version 2 of the License, or
9130803Smarcel   (at your option) any later version.
10130803Smarcel
11130803Smarcel   This program is distributed in the hope that it will be useful,
12130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
13130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14130803Smarcel   GNU General Public License for more details.
15130803Smarcel
16130803Smarcel   You should have received a copy of the GNU General Public License
17130803Smarcel   along with this program; if not, write to the Free Software
18130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19130803Smarcel
20130803Smarcel#ifndef XTENSA_LIBISA_H
21130803Smarcel#define XTENSA_LIBISA_H
22130803Smarcel
23130803Smarcel/* Use the statically-linked version for the GNU tools.  */
24130803Smarcel#define STATIC_LIBISA 1
25130803Smarcel
26130803Smarcel#ifdef __cplusplus
27130803Smarcelextern "C" {
28130803Smarcel#endif
29130803Smarcel
30130803Smarcel#ifndef uint32
31130803Smarcel#define uint32 unsigned int
32130803Smarcel#endif
33130803Smarcel
34130803Smarcel/* This file defines the interface to the Xtensa ISA library.  This library
35130803Smarcel   contains most of the ISA-specific information for a particular Xtensa
36130803Smarcel   processor.  For example, the set of valid instructions, their opcode
37130803Smarcel   encodings and operand fields are all included here.  To support Xtensa's
38130803Smarcel   configurability and user-defined instruction extensions (i.e., TIE), the
39130803Smarcel   library is initialized by loading one or more dynamic libraries; only a
40130803Smarcel   small set of interface code is present in the statically-linked portion
41130803Smarcel   of the library.
42130803Smarcel
43130803Smarcel   This interface basically defines four abstract data types.
44130803Smarcel
45130803Smarcel   . an instruction buffer - for holding the raw instruction bits
46130803Smarcel   . ISA info - information about the ISA as a whole
47130803Smarcel   . opcode info - information about individual instructions
48130803Smarcel   . operand info - information about specific instruction operands
49130803Smarcel
50130803Smarcel   It would be nice to implement these as classes in C++, but the library is
51130803Smarcel   implemented in C to match the expectations of the GNU tools.
52130803Smarcel   Instead, the interface defines a set of functions to access each data
53130803Smarcel   type.  With the exception of the instruction buffer, the internal
54130803Smarcel   representations of the data structures are hidden.  All accesses must be
55130803Smarcel   made through the functions defined here.  */
56130803Smarcel
57130803Smarceltypedef void* xtensa_isa;
58130803Smarceltypedef void* xtensa_operand;
59130803Smarcel
60130803Smarcel
61130803Smarcel/* Opcodes are represented here using sequential integers beginning with 0.
62130803Smarcel   The specific value used for a particular opcode is only fixed for a
63130803Smarcel   particular instantiation of an xtensa_isa structure, so these values
64130803Smarcel   should only be used internally.  */
65130803Smarceltypedef int xtensa_opcode;
66130803Smarcel
67130803Smarcel/* Define a unique value for undefined opcodes ("static const int" doesn't
68130803Smarcel   seem to work for this because EGCS 1.0.3 on i686-Linux without -O won't
69130803Smarcel   allow it to be used as an initializer).  */
70130803Smarcel#define XTENSA_UNDEFINED -1
71130803Smarcel
72130803Smarcel
73130803Smarceltypedef int libisa_module_specifier;
74130803Smarcel
75130803Smarcelextern xtensa_isa xtensa_isa_init (void);
76130803Smarcel
77130803Smarcel
78130803Smarcel/* Instruction buffers.  */
79130803Smarcel
80130803Smarceltypedef uint32 xtensa_insnbuf_word;
81130803Smarceltypedef xtensa_insnbuf_word *xtensa_insnbuf;
82130803Smarcel
83130803Smarcel/* Get the size in words of the xtensa_insnbuf array.  */
84130803Smarcelextern int xtensa_insnbuf_size (xtensa_isa);
85130803Smarcel
86130803Smarcel/* Allocate (with malloc) an xtensa_insnbuf of the right size.  */
87130803Smarcelextern xtensa_insnbuf xtensa_insnbuf_alloc (xtensa_isa);
88130803Smarcel
89130803Smarcel/* Release (with free) an xtensa_insnbuf of the right size.  */
90130803Smarcelextern void xtensa_insnbuf_free (xtensa_insnbuf);
91130803Smarcel
92130803Smarcel/* Inward and outward conversion from memory images (byte streams) to our
93130803Smarcel   internal instruction representation.  */
94130803Smarcelextern void xtensa_insnbuf_to_chars (xtensa_isa, const xtensa_insnbuf,
95130803Smarcel				     char *);
96130803Smarcel
97130803Smarcelextern void xtensa_insnbuf_from_chars (xtensa_isa, xtensa_insnbuf,
98130803Smarcel				       const char *);
99130803Smarcel
100130803Smarcel
101130803Smarcel/* ISA information.  */
102130803Smarcel
103130803Smarcel/* Load the ISA information from a shared library.  If successful, this returns
104130803Smarcel   a value which identifies the ISA for use in subsequent calls to the ISA
105130803Smarcel   library; otherwise, it returns NULL.  Multiple ISAs can be loaded to support
106130803Smarcel   heterogeneous multiprocessor systems.  */
107130803Smarcelextern xtensa_isa xtensa_load_isa (libisa_module_specifier);
108130803Smarcel
109130803Smarcel/* Extend an existing set of ISA information by loading an additional shared
110130803Smarcel   library of ISA information.  This is primarily intended for loading TIE
111130803Smarcel   extensions.  If successful, the return value is non-zero.  */
112130803Smarcelextern int xtensa_extend_isa (xtensa_isa, libisa_module_specifier);
113130803Smarcel
114130803Smarcel/* The default ISA.  This variable is set automatically to the ISA most
115130803Smarcel   recently loaded and is provided as a convenience.  An exception is the GNU
116130803Smarcel   opcodes library, where there is a fixed interface that does not allow
117130803Smarcel   passing the ISA as a parameter and the ISA must be taken from this global
118130803Smarcel   variable.  (Note: Since this variable is just a convenience, it is not
119130803Smarcel   exported when libisa is built as a DLL, due to the hassle of dealing with
120130803Smarcel   declspecs.)  */
121130803Smarcelextern xtensa_isa xtensa_default_isa;
122130803Smarcel
123130803Smarcel
124130803Smarcel/* Deallocate an xtensa_isa structure.  */
125130803Smarcelextern void xtensa_isa_free (xtensa_isa);
126130803Smarcel
127130803Smarcel/* Get the maximum instruction size in bytes.  */
128130803Smarcelextern int xtensa_insn_maxlength (xtensa_isa);
129130803Smarcel
130130803Smarcel/* Get the total number of opcodes for this processor.  */
131130803Smarcelextern int xtensa_num_opcodes (xtensa_isa);
132130803Smarcel
133130803Smarcel/* Translate a mnemonic name to an opcode.  Returns XTENSA_UNDEFINED if
134130803Smarcel   the name is not a valid opcode mnemonic.  */
135130803Smarcelextern xtensa_opcode xtensa_opcode_lookup (xtensa_isa, const char *);
136130803Smarcel
137130803Smarcel/* Decode a binary instruction buffer.  Returns the opcode or
138130803Smarcel   XTENSA_UNDEFINED if the instruction is illegal.  */
139130803Smarcelextern xtensa_opcode xtensa_decode_insn (xtensa_isa, const xtensa_insnbuf);
140130803Smarcel
141130803Smarcel
142130803Smarcel/* Opcode information.  */
143130803Smarcel
144130803Smarcel/* Set the opcode field(s) in a binary instruction buffer.  The operand
145130803Smarcel   fields are set to zero.  */
146130803Smarcelextern void xtensa_encode_insn (xtensa_isa, xtensa_opcode, xtensa_insnbuf);
147130803Smarcel
148130803Smarcel/* Get the mnemonic name for an opcode.  */
149130803Smarcelextern const char * xtensa_opcode_name (xtensa_isa, xtensa_opcode);
150130803Smarcel
151130803Smarcel/* Find the length (in bytes) of an instruction.  */
152130803Smarcelextern int xtensa_insn_length (xtensa_isa, xtensa_opcode);
153130803Smarcel
154130803Smarcel/* Find the length of an instruction by looking only at the first byte.  */
155130803Smarcelextern int xtensa_insn_length_from_first_byte (xtensa_isa, char);
156130803Smarcel
157130803Smarcel/* Find the number of operands for an instruction.  */
158130803Smarcelextern int xtensa_num_operands (xtensa_isa, xtensa_opcode);
159130803Smarcel
160130803Smarcel/* Get the information about operand number "opnd" of a particular opcode.  */
161130803Smarcelextern xtensa_operand xtensa_get_operand (xtensa_isa, xtensa_opcode, int);
162130803Smarcel
163130803Smarcel/* Operand information.  */
164130803Smarcel
165130803Smarcel/* Find the kind of operand.  There are three possibilities:
166130803Smarcel   1) PC-relative immediates (e.g., "l", "L").  These can be identified with
167130803Smarcel      the xtensa_operand_isPCRelative function.
168130803Smarcel   2) non-PC-relative immediates ("i").
169130803Smarcel   3) register-file short names (e.g., "a", "b", "m" and others defined
170130803Smarcel      via TIE).  */
171130803Smarcelextern char * xtensa_operand_kind (xtensa_operand);
172130803Smarcel
173130803Smarcel/* Check if an operand is an input ('<'), output ('>'), or inout ('=')
174130803Smarcel   operand.  Note: The output operand of a conditional assignment
175130803Smarcel   (e.g., movnez) appears here as an inout ('=') even if it is declared
176130803Smarcel   in the TIE code as an output ('>'); this allows the compiler to
177130803Smarcel   properly handle register allocation for conditional assignments.  */
178130803Smarcelextern char xtensa_operand_inout (xtensa_operand);
179130803Smarcel
180130803Smarcel/* Get and set the raw (encoded) value of the field for the specified
181130803Smarcel   operand.  The "set" function does not check if the value fits in the
182130803Smarcel   field; that is done by the "encode" function below.  */
183130803Smarcelextern uint32 xtensa_operand_get_field (xtensa_operand, const xtensa_insnbuf);
184130803Smarcel
185130803Smarcelextern void xtensa_operand_set_field (xtensa_operand, xtensa_insnbuf, uint32);
186130803Smarcel
187130803Smarcel
188130803Smarcel/* Encode and decode operands.  The raw bits in the operand field
189130803Smarcel   may be encoded in a variety of different ways.  These functions hide the
190130803Smarcel   details of that encoding.  The encode function has a special return type
191130803Smarcel   (xtensa_encode_result) to indicate success or the reason for failure; the
192130803Smarcel   encoded value is returned through the argument pointer.  The decode function
193130803Smarcel   has no possibility of failure and returns the decoded value.  */
194130803Smarcel
195130803Smarceltypedef enum
196130803Smarcel{
197130803Smarcel  xtensa_encode_result_ok,
198130803Smarcel  xtensa_encode_result_align,
199130803Smarcel  xtensa_encode_result_not_in_table,
200130803Smarcel  xtensa_encode_result_too_low,
201130803Smarcel  xtensa_encode_result_too_high,
202130803Smarcel  xtensa_encode_result_not_ok,
203130803Smarcel  xtensa_encode_result_max = xtensa_encode_result_not_ok
204130803Smarcel} xtensa_encode_result;
205130803Smarcel
206130803Smarcelextern xtensa_encode_result xtensa_operand_encode (xtensa_operand, uint32 *);
207130803Smarcel
208130803Smarcelextern uint32 xtensa_operand_decode (xtensa_operand, uint32);
209130803Smarcel
210130803Smarcel
211130803Smarcel/* For PC-relative offset operands, the interpretation of the offset may vary
212130803Smarcel   between opcodes, e.g., is it relative to the current PC or that of the next
213130803Smarcel   instruction?  The following functions are defined to perform PC-relative
214130803Smarcel   relocations and to undo them (as in the disassembler).  The first function
215130803Smarcel   takes the desired address and the PC of the current instruction and returns
216130803Smarcel   the unencoded value to be stored in the offset field.  The second function
217130803Smarcel   takes the unencoded offset value and the current PC and returns the address.
218130803Smarcel   Note that these functions do not replace the encode/decode functions; the
219130803Smarcel   operands must be encoded/decoded separately.  */
220130803Smarcel
221130803Smarcelextern int xtensa_operand_isPCRelative (xtensa_operand);
222130803Smarcel
223130803Smarcelextern uint32 xtensa_operand_do_reloc (xtensa_operand, uint32, uint32);
224130803Smarcel
225130803Smarcelextern uint32 xtensa_operand_undo_reloc	(xtensa_operand, uint32, uint32);
226130803Smarcel
227130803Smarcel#ifdef __cplusplus
228130803Smarcel}
229130803Smarcel#endif
230130803Smarcel#endif /* XTENSA_LIBISA_H */
231