f358ff429e4f33cfcf8a3c095e722ce58f44807c
[binutils-gdb.git] / include / opcode / loongarch.h
1 /* LoongArch assembler/disassembler support.
2
3 Copyright (C) 2021-2023 Free Software Foundation, Inc.
4 Contributed by Loongson Ltd.
5
6 This file is part of GNU Binutils.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the license, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING3. If not,
20 see <http://www.gnu.org/licenses/>. */
21
22 #ifndef _LOONGARCH_H_
23 #define _LOONGARCH_H_
24 #include <stdint.h>
25
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30
31 #define LARCH_NOP 0x03400000
32 #define LARCH_B 0x50000000
33 /* BCEQZ/BCNEZ. */
34 #define LARCH_FLOAT_BRANCH 0x48000000
35 #define LARCH_BRANCH_OPCODE_MASK 0xfc000000
36 #define LARCH_BRANCH_INVERT_BIT 0x04000000
37 #define LARCH_FLOAT_BRANCH_INVERT_BIT 0x00000100
38
39 #define ENCODE_BRANCH16_IMM(x) (((x) >> 2) << 10)
40
41 #define OUT_OF_RANGE(value, bits, align) \
42 ((value) < (-(1 << ((bits) - 1) << align)) \
43 || (value) > ((((1 << ((bits) - 1)) - 1) << align)))
44
45 typedef uint32_t insn_t;
46
47 struct loongarch_opcode
48 {
49 const insn_t match;
50 const insn_t mask; /* High 1 byte is main opcode and it must be 0xf. */
51 #define LARCH_INSN_OPC(insn) ((insn & 0xf0000000) >> 28)
52 const char *const name;
53
54 /* ACTUAL PARAMETER:
55
56 // BNF with regular expression.
57 args : token* end
58
59 // just few char separate 'iden'
60 token : ','
61 | '('
62 | ')'
63 | iden // maybe a label (include at least one alphabet),
64 maybe a number, maybe a expr
65 | regname
66
67 regname : '$' iden
68
69 iden : [a-zA-Z0-9\.\+\-]+
70
71 end : '\0'
72
73
74 FORMAT: A string to describe the format of actual parameter including
75 bit field infomation. For example, "r5:5,r0:5,sr10:16<<2" matches
76 "$12,$13,12345" and "$4,$7,a_label". That 'sr' means the instruction
77 may need relocate. '10:16' means bit field of instruction.
78 In a 'format', every 'escape's can be replaced to 'iden' or 'regname'
79 acrroding to its meaning. We fill all information needed by
80 disassembing and assembing to 'format'.
81
82 // BNF with regular expression.
83 format : escape (literal+ escape)* literal* end
84 | (literal+ escape)* literal* end
85
86 end : '\0' // Get here means parse end.
87
88 // The intersection between any two among FIRST (end), FIRST
89 // (literal) and FIRST (escape) must be empty.
90 // So we can build a simple parser.
91 literal : ','
92 | '('
93 | ')'
94
95 // Double '<'s means the real number is the immediate after shifting left.
96 escape : esc_ch bit_field '<' '<' dec2
97 | esc_ch bit_field
98 | esc_ch // for MACRO. non-macro format must indicate 'bit_field'
99
100 // '|' means to concatenate nonadjacent bit fields
101 // For example, "10:16|0:4" means
102 // "16 bits starting from the 10th bit concatenating with 4 bits
103 // starting from the 0th bit".
104 // This is to say "[25..10]||[3..0]" (little endian).
105 b_field : dec2 ':' dec2
106 | dec2 ':' dec2 '|' bit_field
107
108 esc_ch : 's' 'r' // signed immediate or label need relocate
109 | 's' // signed immediate no need relocate
110 | 'u' // unsigned immediate
111 | 'l' // label needed relocate
112 | 'r' // general purpose registers
113 | 'f' // FPU registers
114 | 'v' // 128 bit SIMD register
115 | 'x' // 256 bit SIMD register
116
117 dec2 : [1-9][0-9]?
118 | 0
119
120 */
121 const char *const format;
122
123 /* MACRO: Indicate how a macro instruction expand for assembling.
124 The main is to replace the '%num'(means the 'num'th 'escape' in
125 'format') in 'macro' string to get the real instruction.
126
127 Maybe need
128 */
129 const char *const macro;
130 const int *include;
131 const int *exclude;
132
133 const unsigned long pinfo;
134 #define USELESS 0x0l
135 /* Instruction is a simple alias only for disassembler use. */
136 #define INSN_DIS_ALIAS 0x00000001l
137 };
138
139 struct hash_control;
140
141 struct loongarch_ase
142 {
143 const int *enabled;
144 struct loongarch_opcode *const opcodes;
145 const int *include;
146 const int *exclude;
147
148 /* For disassemble to create main opcode hash table. */
149 const struct loongarch_opcode *opc_htab[16];
150 unsigned char opc_htab_inited;
151
152 /* For GAS to create hash table. */
153 struct htab *name_hash_entry;
154 };
155
156 extern int is_unsigned (const char *);
157 extern int is_signed (const char *);
158 extern int is_branch_label (const char *);
159
160 extern int loongarch_get_bit_field_width (const char *bit_field, char **end);
161 extern int32_t loongarch_decode_imm (const char *bit_field, insn_t insn,
162 int si);
163
164 #define MAX_ARG_NUM_PLUS_2 9
165
166 extern size_t loongarch_split_args_by_comma (char *args,
167 const char *arg_strs[]);
168 extern char *loongarch_cat_splited_strs (const char *arg_strs[]);
169 extern insn_t loongarch_foreach_args (
170 const char *format, const char *arg_strs[],
171 int32_t (*helper) (char esc1, char esc2, const char *bit_field,
172 const char *arg, void *context),
173 void *context);
174
175 extern int loongarch_check_format (const char *format);
176 extern int loongarch_check_macro (const char *format, const char *macro);
177
178 extern char *loongarch_expand_macro_with_format_map (
179 const char *format, const char *macro, const char *const arg_strs[],
180 const char *(*map) (char esc1, char esc2, const char *arg),
181 char *(*helper) (const char *const arg_strs[], void *context),
182 void *context, size_t len_str);
183 extern char *loongarch_expand_macro (
184 const char *macro, const char *const arg_strs[],
185 char *(*helper) (const char *const arg_strs[], void *context),
186 void *context, size_t len_str);
187 extern size_t loongarch_bits_imm_needed (int64_t imm, int si);
188
189 extern void loongarch_eliminate_adjacent_repeat_char (char *dest, char c);
190
191 extern const char *const loongarch_r_normal_name[32];
192 extern const char *const loongarch_r_lp64_name[32];
193 extern const char *const loongarch_r_lp64_name_deprecated[32];
194 extern const char *const loongarch_f_normal_name[32];
195 extern const char *const loongarch_f_lp64_name[32];
196 extern const char *const loongarch_f_lp64_name_deprecated[32];
197 extern const char *const loongarch_fc_normal_name[4];
198 extern const char *const loongarch_fc_numeric_name[4];
199 extern const char *const loongarch_c_normal_name[8];
200 extern const char *const loongarch_cr_normal_name[4];
201 extern const char *const loongarch_v_normal_name[32];
202 extern const char *const loongarch_x_normal_name[32];
203
204 extern struct loongarch_ase loongarch_ASEs[];
205
206 extern struct loongarch_ASEs_option
207 {
208 struct opt_abi
209 {
210 int elf_abi;
211 } abi;
212 #define ase_abi abi.elf_abi
213
214 struct opt_isa
215 {
216 int use_ilp32;
217 int use_lp64;
218
219 int use_soft_float;
220 int use_single_float;
221 int use_double_float;
222
223 int use_lsx;
224 int use_lasx;
225
226 int use_lvz;
227 int use_lbt;
228
229 int use_la_local_with_abs;
230 int use_la_global_with_pcrel;
231 int use_la_global_with_abs;
232 } isa;
233 #define ase_ilp32 isa.use_ilp32
234 #define ase_lp64 isa.use_lp64
235
236 #define ase_nf isa.use_soft_float
237 #define ase_sf isa.use_single_float
238 #define ase_df isa.use_double_float
239
240 #define ase_lsx isa.use_lsx
241 #define ase_lasx isa.use_lasx
242
243 #define ase_lvz isa.use_lvz
244 #define ase_lbt isa.use_lbt
245
246 #define ase_labs isa.use_la_local_with_abs
247 #define ase_gpcr isa.use_la_global_with_pcrel
248 #define ase_gabs isa.use_la_global_with_abs
249
250 int relax;
251 int thin_add_sub;
252 } LARCH_opts;
253
254 extern size_t loongarch_insn_length (insn_t insn);
255
256 #ifdef __cplusplus
257 }
258 #endif
259
260 #endif /* _LOONGARCH_H_ */