1/* LoongArch assembler/disassembler support.
2
3   Copyright (C) 2021-2022 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
27extern "C"
28{
29#endif
30
31  typedef uint32_t insn_t;
32
33  struct loongarch_opcode
34  {
35    const insn_t match;
36    const insn_t mask; /* High 1 byte is main opcode and it must be 0xf.  */
37#define LARCH_INSN_OPC(insn) ((insn & 0xf0000000) >> 28)
38    const char *const name;
39
40    /* ACTUAL PARAMETER:
41
42  // BNF with regular expression.
43args : token* end
44
45  // just few char separate 'iden'
46token : ','
47| '('
48| ')'
49| iden	     // maybe a label (include at least one alphabet),
50		      maybe a number, maybe a expr
51| regname
52
53regname : '$' iden
54
55iden : [a-zA-Z0-9\.\+\-]+
56
57end : '\0'
58
59
60FORMAT: A string to describe the format of actual parameter including
61bit field infomation.  For example, "r5:5,r0:5,sr10:16<<2" matches
62"$12,$13,12345" and "$4,$7,a_label".  That 'sr' means the instruction
63may need relocate. '10:16' means bit field of instruction.
64In a 'format', every 'escape's can be replaced to 'iden' or 'regname'
65acrroding to its meaning.  We fill all information needed by
66disassembing and assembing to 'format'.
67
68  // BNF with regular expression.
69format : escape (literal+ escape)* literal* end
70| (literal+ escape)* literal* end
71
72end : '\0'       // Get here means parse end.
73
74  // The intersection between any two among FIRST (end), FIRST
75  // (literal) and FIRST (escape) must be empty.
76  // So we can build a simple parser.
77literal : ','
78| '('
79| ')'
80
81  // Double '<'s means the real number is the immediate after shifting left.
82escape : esc_ch bit_field '<' '<' dec2
83| esc_ch bit_field
84| esc_ch    // for MACRO. non-macro format must indicate 'bit_field'
85
86  // '|' means to concatenate nonadjacent bit fields
87  // For example, "10:16|0:4" means
88  // "16 bits starting from the 10th bit concatenating with 4 bits
89  // starting from the 0th bit".
90  // This is to say "[25..10]||[3..0]" (little endian).
91b_field : dec2 ':' dec2
92| dec2 ':' dec2 '|' bit_field
93
94esc_ch : 's' 'r'   // signed immediate or label need relocate
95| 's'       // signed immediate no need relocate
96| 'u'       // unsigned immediate
97| 'l'       // label needed relocate
98| 'r'       // general purpose registers
99| 'f'       // FPU registers
100| 'v'       // 128 bit SIMD register
101| 'x'       // 256 bit SIMD register
102
103dec2 : [1-9][0-9]?
104| 0
105
106*/
107    const char *const format;
108
109    /* MACRO: Indicate how a macro instruction expand for assembling.
110       The main is to replace the '%num'(means the 'num'th 'escape' in
111       'format') in 'macro' string to get the real instruction.
112
113       Maybe need
114       */
115    const char *const macro;
116    const int *include;
117    const int *exclude;
118
119    const unsigned long pinfo;
120#define USELESS 0x0l
121  };
122
123  struct hash_control;
124
125  struct loongarch_ase
126  {
127    const int *enabled;
128    struct loongarch_opcode *const opcodes;
129    const int *include;
130    const int *exclude;
131
132    /* For disassemble to create main opcode hash table.  */
133    const struct loongarch_opcode *opc_htab[16];
134    unsigned char opc_htab_inited;
135
136    /* For GAS to create hash table.  */
137    struct htab *name_hash_entry;
138  };
139
140  extern int is_unsigned (const char *);
141  extern int is_signed (const char *);
142  extern int is_branch_label (const char *);
143
144  extern int loongarch_get_bit_field_width (const char *bit_field, char **end);
145  extern int32_t loongarch_decode_imm (const char *bit_field, insn_t insn,
146				       int si);
147
148#define MAX_ARG_NUM_PLUS_2 9
149
150  extern size_t loongarch_split_args_by_comma (char *args,
151					       const char *arg_strs[]);
152  extern char *loongarch_cat_splited_strs (const char *arg_strs[]);
153  extern insn_t loongarch_foreach_args (
154    const char *format, const char *arg_strs[],
155    int32_t (*helper) (char esc1, char esc2, const char *bit_field,
156		       const char *arg, void *context),
157    void *context);
158
159  extern int loongarch_check_format (const char *format);
160  extern int loongarch_check_macro (const char *format, const char *macro);
161
162  extern char *loongarch_expand_macro_with_format_map (
163    const char *format, const char *macro, const char *const arg_strs[],
164    const char *(*map) (char esc1, char esc2, const char *arg),
165    char *(*helper) (const char *const arg_strs[], void *context),
166    void *context, size_t len_str);
167  extern char *loongarch_expand_macro (
168    const char *macro, const char *const arg_strs[],
169    char *(*helper) (const char *const arg_strs[], void *context),
170    void *context, size_t len_str);
171  extern size_t loongarch_bits_imm_needed (int64_t imm, int si);
172
173  extern void loongarch_eliminate_adjacent_repeat_char (char *dest, char c);
174
175  extern int loongarch_parse_dis_options (const char *opts_in);
176  extern void loongarch_disassemble_one (
177    int64_t pc, insn_t insn,
178    int (*fprintf_func) (void *stream, const char *format, ...), void *stream);
179
180  extern const char *const loongarch_r_normal_name[32];
181  extern const char *const loongarch_r_lp64_name[32];
182  extern const char *const loongarch_r_lp64_name1[32];
183  extern const char *const loongarch_f_normal_name[32];
184  extern const char *const loongarch_f_lp64_name[32];
185  extern const char *const loongarch_f_lp64_name1[32];
186  extern const char *const loongarch_c_normal_name[8];
187  extern const char *const loongarch_cr_normal_name[4];
188  extern const char *const loongarch_v_normal_name[32];
189  extern const char *const loongarch_x_normal_name[32];
190
191  extern struct loongarch_ase loongarch_ASEs[];
192
193  extern struct loongarch_ASEs_option
194  {
195    struct opt_abi
196    {
197	    int elf_abi;
198    } abi;
199#define ase_abi abi.elf_abi
200
201    struct opt_isa
202    {
203	    int use_ilp32;
204	    int use_lp64;
205
206	    int use_soft_float;
207	    int use_single_float;
208	    int use_double_float;
209
210	    int use_lsx;
211	    int use_lasx;
212
213	    int use_la_local_with_abs;
214	    int use_la_global_with_pcrel;
215	    int use_la_global_with_abs;
216    } isa;
217#define ase_ilp32	isa.use_ilp32
218#define ase_lp64	isa.use_lp64
219
220#define ase_nf		isa.use_soft_float
221#define ase_sf		isa.use_single_float
222#define ase_df		isa.use_double_float
223
224#define ase_lsx		isa.use_lsx
225#define ase_lasx	isa.use_lasx
226
227#define ase_labs	isa.use_la_local_with_abs
228#define ase_gpcr	isa.use_la_global_with_pcrel
229#define ase_gabs	isa.use_la_global_with_abs
230
231  } LARCH_opts;
232
233  extern size_t loongarch_insn_length (insn_t insn);
234
235#ifdef __cplusplus
236}
237#endif
238
239#endif /* _LOONGARCH_H_ */
240