]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-cris.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gas / config / tc-cris.c
CommitLineData
3bcbcc3d 1/* tc-cris.c -- Assembler code for the CRIS CPU core.
a2c58332 2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3bcbcc3d
HPN
3
4 Contributed by Axis Communications AB, Lund, Sweden.
5 Originally written for GAS 1.38.1 by Mikael Asker.
08caf3f8 6 Updates, BFDizing, GNUifying and ELF support by Hans-Peter Nilsson.
3bcbcc3d
HPN
7
8 This file is part of GAS, the GNU Assembler.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
3bcbcc3d
HPN
13 any later version.
14
15 GAS 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.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the
4b4da160
NC
22 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
3bcbcc3d 24
3bcbcc3d 25#include "as.h"
3882b010 26#include "safe-ctype.h"
3bcbcc3d
HPN
27#include "subsegs.h"
28#include "opcode/cris.h"
fcdc20a4 29#include "dwarf2dbg.h"
3bcbcc3d
HPN
30
31/* Conventions used here:
32 Generally speaking, pointers to binutils types such as "fragS" and
33 "expressionS" get parameter and variable names ending in "P", such as
34 "fragP", to harmonize with the rest of the binutils code. Other
35 pointers get a "p" suffix, such as "bufp". Any function or type-name
36 that could clash with a current or future binutils or GAS function get
37 a "cris_" prefix. */
38
7b15d668
HPN
39#define SYNTAX_RELAX_REG_PREFIX "no_register_prefix"
40#define SYNTAX_ENFORCE_REG_PREFIX "register_prefix"
41#define SYNTAX_USER_SYM_LEADING_UNDERSCORE "leading_underscore"
42#define SYNTAX_USER_SYM_NO_LEADING_UNDERSCORE "no_leading_underscore"
43#define REGISTER_PREFIX_CHAR '$'
44
1c971160
HPN
45/* True for expressions where getting X_add_symbol and X_add_number is
46 enough to get the "base" and "offset"; no need to make_expr_symbol.
47 It's not enough to check if X_op_symbol is NULL; that misses unary
48 operations like O_uminus. */
49#define SIMPLE_EXPR(EXP) \
50 ((EXP)->X_op == O_constant || (EXP)->X_op == O_symbol)
51
08caf3f8
HPN
52/* Like in ":GOT", ":GOTOFF" etc. Other ports use '@', but that's in
53 line_separator_chars for CRIS, so we avoid it. */
cc99daad 54#define RELOC_SUFFIX_CHAR ':'
08caf3f8 55
3bcbcc3d
HPN
56/* This might be CRIS_INSN_NONE if we're assembling a prefix-insn only.
57 Note that some prefix-insns might be assembled as CRIS_INSN_NORMAL. */
58enum cris_insn_kind
59{
1048a9ba 60 CRIS_INSN_NORMAL, CRIS_INSN_NONE, CRIS_INSN_BRANCH, CRIS_INSN_MUL
3bcbcc3d
HPN
61};
62
63/* An instruction will have one of these prefixes.
64 Although the same bit-pattern, we handle BDAP with an immediate
65 expression (eventually quick or [pc+]) different from when we only have
66 register expressions. */
67enum prefix_kind
68{
69 PREFIX_NONE, PREFIX_BDAP_IMM, PREFIX_BDAP, PREFIX_BIAP, PREFIX_DIP,
70 PREFIX_PUSH
71};
72
73/* The prefix for an instruction. */
74struct cris_prefix
75{
47926f60
KH
76 enum prefix_kind kind;
77 int base_reg_number;
78 unsigned int opcode;
3bcbcc3d
HPN
79
80 /* There might be an expression to be evaluated, like I in [rN+I]. */
47926f60 81 expressionS expr;
3bcbcc3d
HPN
82
83 /* If there's an expression, we might need a relocation. Here's the
33eaf5de 84 type of what relocation to start relaxation with.
3bcbcc3d
HPN
85 The relocation is assumed to start immediately after the prefix insn,
86 so we don't provide an offset. */
87 enum bfd_reloc_code_real reloc;
88};
89
47926f60 90/* The description of the instruction being assembled. */
3bcbcc3d
HPN
91struct cris_instruction
92{
93 /* If CRIS_INSN_NONE, then this insn is of zero length. */
47926f60 94 enum cris_insn_kind insn_type;
3bcbcc3d
HPN
95
96 /* If a special register was mentioned, this is its description, else
47926f60 97 it is NULL. */
3bcbcc3d
HPN
98 const struct cris_spec_reg *spec_reg;
99
47926f60 100 unsigned int opcode;
3bcbcc3d
HPN
101
102 /* An insn may have at most one expression; theoretically there could be
47926f60
KH
103 another in its prefix (but I don't see how that could happen). */
104 expressionS expr;
3bcbcc3d
HPN
105
106 /* The expression might need a relocation. Here's one to start
107 relaxation with. */
47926f60 108 enum bfd_reloc_code_real reloc;
3bcbcc3d 109
08caf3f8 110 /* The size in bytes of an immediate expression, or zero if
3bcbcc3d 111 nonapplicable. */
47926f60 112 int imm_oprnd_size;
3bcbcc3d
HPN
113};
114
ae57792d
HPN
115enum cris_archs
116{
117 arch_cris_unknown,
118 arch_crisv0, arch_crisv3, arch_crisv8, arch_crisv10,
119 arch_cris_any_v0_v10, arch_crisv32, arch_cris_common_v10_v32
120};
121
17b9d67d 122static enum cris_archs cris_arch_from_string (const char **);
695a4822
HPN
123static int cris_insn_ver_valid_for_arch (enum cris_insn_version_usage,
124 enum cris_archs);
125
126static void cris_process_instruction (char *, struct cris_instruction *,
127 struct cris_prefix *);
128static int get_bwd_size_modifier (char **, int *);
129static int get_bw_size_modifier (char **, int *);
130static int get_gen_reg (char **, int *);
131static int get_spec_reg (char **, const struct cris_spec_reg **);
132static int get_sup_reg (char **, int *);
133static int get_autoinc_prefix_or_indir_op (char **, struct cris_prefix *,
134 int *, int *, int *,
135 expressionS *);
136static int get_3op_or_dip_prefix_op (char **, struct cris_prefix *);
137static int cris_get_expression (char **, expressionS *);
138static int get_flags (char **, int *);
139static void gen_bdap (int, expressionS *);
140static int branch_disp (int);
141static void gen_cond_branch_32 (char *, char *, fragS *, symbolS *, symbolS *,
142 long int);
143static void cris_number_to_imm (char *, long, int, fixS *, segT);
695a4822
HPN
144static void s_syntax (int);
145static void s_cris_file (int);
146static void s_cris_loc (int);
147static void s_cris_arch (int);
d2b52762 148static void s_cris_dtpoff (int);
7b15d668 149
08caf3f8 150/* Get ":GOT", ":GOTOFF", ":PLT" etc. suffixes. */
cc99daad
HPN
151static void cris_get_reloc_suffix (char **, bfd_reloc_code_real_type *,
152 expressionS *);
153static unsigned int cris_get_specified_reloc_size (bfd_reloc_code_real_type);
08caf3f8 154
7b15d668 155/* All the .syntax functions. */
695a4822
HPN
156static void cris_force_reg_prefix (void);
157static void cris_relax_reg_prefix (void);
158static void cris_sym_leading_underscore (void);
159static void cris_sym_no_leading_underscore (void);
160static char *cris_insn_first_word_frag (void);
7b15d668 161
3bcbcc3d 162/* Handle to the opcode hash table. */
629310ab 163static htab_t op_hash = NULL;
3bcbcc3d 164
399f703e
HPN
165/* If we target cris-axis-linux-gnu (as opposed to generic cris-axis-elf),
166 we default to no underscore and required register-prefixes. The
167 difference is in the default values. */
168#ifdef TE_LINUX
5b7c81bd 169#define DEFAULT_CRIS_AXIS_LINUX_GNU true
399f703e 170#else
5b7c81bd 171#define DEFAULT_CRIS_AXIS_LINUX_GNU false
399f703e
HPN
172#endif
173
7b15d668 174/* Whether we demand that registers have a `$' prefix. Default here. */
5b7c81bd 175static bool demand_register_prefix = DEFAULT_CRIS_AXIS_LINUX_GNU;
7b15d668
HPN
176
177/* Whether global user symbols have a leading underscore. Default here. */
5b7c81bd 178static bool symbols_have_leading_underscore
399f703e 179 = !DEFAULT_CRIS_AXIS_LINUX_GNU;
7b15d668 180
08caf3f8 181/* Whether or not we allow PIC, and expand to PIC-friendly constructs. */
5b7c81bd 182static bool pic = false;
08caf3f8 183
cc99daad 184/* Whether or not we allow TLS suffixes. For the moment, we always do. */
5b7c81bd 185static const bool tls = true;
cc99daad 186
ae57792d
HPN
187/* If we're configured for "cris", default to allow all v0..v10
188 instructions and register names. */
189#ifndef DEFAULT_CRIS_ARCH
190#define DEFAULT_CRIS_ARCH cris_any_v0_v10
191#endif
192
193/* No whitespace in the CONCAT2 parameter list. */
194static enum cris_archs cris_arch = XCONCAT2 (arch_,DEFAULT_CRIS_ARCH);
195
3bcbcc3d
HPN
196const pseudo_typeS md_pseudo_table[] =
197{
198 {"dword", cons, 4},
d2b52762 199 {"dtpoffd", s_cris_dtpoff, 4},
7b15d668 200 {"syntax", s_syntax, 0},
fcdc20a4
HPN
201 {"file", s_cris_file, 0},
202 {"loc", s_cris_loc, 0},
ae57792d 203 {"arch", s_cris_arch, 0},
3bcbcc3d
HPN
204 {NULL, 0, 0}
205};
206
207static int warn_for_branch_expansion = 0;
208
1048a9ba
HPN
209/* Whether to emit error when a MULS/MULU could be located last on a
210 cache-line. */
ae57792d
HPN
211static int err_for_dangerous_mul_placement
212 = (XCONCAT2 (arch_,DEFAULT_CRIS_ARCH) != arch_crisv32);
1048a9ba 213
3bcbcc3d
HPN
214const char cris_comment_chars[] = ";";
215
216/* This array holds the chars that only start a comment at the beginning of
217 a line. If the line seems to have the form '# 123 filename'
47926f60 218 .line and .file directives will appear in the pre-processed output. */
3bcbcc3d
HPN
219/* Note that input_file.c hand-checks for '#' at the beginning of the
220 first line of the input file. This is because the compiler outputs
47926f60
KH
221 #NO_APP at the beginning of its output. */
222/* Also note that slash-star will always start a comment. */
3bcbcc3d
HPN
223const char line_comment_chars[] = "#";
224const char line_separator_chars[] = "@";
225
226/* Now all floating point support is shut off. See md_atof. */
227const char EXP_CHARS[] = "";
228const char FLT_CHARS[] = "";
229
3bcbcc3d
HPN
230/* For CRIS, we encode the relax_substateTs (in e.g. fr_substate) as:
231 2 1 0
232 ---/ /--+-----------------+-----------------+-----------------+
233 | what state ? | how long ? |
234 ---/ /--+-----------------+-----------------+-----------------+
235
236 The "how long" bits are 00 = byte, 01 = word, 10 = dword (long).
3bcbcc3d
HPN
237 Not all lengths are legit for a given value of (what state).
238
239 Groups for CRIS address relaxing:
240
ae57792d 241 1. Bcc (pre-V32)
3bcbcc3d
HPN
242 length: byte, word, 10-byte expansion
243
244 2. BDAP
1048a9ba
HPN
245 length: byte, word, dword
246
247 3. MULS/MULU
248 Not really a relaxation (no infrastructure to get delay-slots
249 right), just an alignment and placement checker for the v10
ae57792d
HPN
250 multiply/cache-bug.
251
252 4. Bcc (V32 and later)
253 length: byte, word, 14-byte expansion
254
255 5. Bcc (V10+V32)
256 length: byte, word, error
257
258 6. BA (V32)
259 length: byte, word, dword
3bcbcc3d 260
ae57792d
HPN
261 7. LAPC (V32)
262 length: byte, dword
263 */
264
265#define STATE_COND_BRANCH (1)
3bcbcc3d 266#define STATE_BASE_PLUS_DISP_PREFIX (2)
1048a9ba 267#define STATE_MUL (3)
ae57792d
HPN
268#define STATE_COND_BRANCH_V32 (4)
269#define STATE_COND_BRANCH_COMMON (5)
270#define STATE_ABS_BRANCH_V32 (6)
271#define STATE_LAPC (7)
d2aa3f9f 272#define STATE_COND_BRANCH_PIC (8)
3bcbcc3d
HPN
273
274#define STATE_LENGTH_MASK (3)
275#define STATE_BYTE (0)
276#define STATE_WORD (1)
277#define STATE_DWORD (2)
278/* Symbol undefined. */
279#define STATE_UNDF (3)
280#define STATE_MAX_LENGTH (3)
281
2d2255b5 282/* These displacements are relative to the address following the opcode
3bcbcc3d
HPN
283 word of the instruction. The first letter is Byte, Word. The 2nd
284 letter is Forward, Backward. */
285
286#define BRANCH_BF ( 254)
287#define BRANCH_BB (-256)
ae57792d
HPN
288#define BRANCH_BF_V32 ( 252)
289#define BRANCH_BB_V32 (-258)
47926f60
KH
290#define BRANCH_WF (2 + 32767)
291#define BRANCH_WB (2 + -32768)
ae57792d
HPN
292#define BRANCH_WF_V32 (-2 + 32767)
293#define BRANCH_WB_V32 (-2 + -32768)
3bcbcc3d
HPN
294
295#define BDAP_BF ( 127)
296#define BDAP_BB (-128)
297#define BDAP_WF ( 32767)
298#define BDAP_WB (-32768)
299
300#define ENCODE_RELAX(what, length) (((what) << 2) + (length))
301
302const relax_typeS md_cris_relax_table[] =
303{
304 /* Error sentinel (0, 0). */
305 {1, 1, 0, 0},
306
307 /* Unused (0, 1). */
308 {1, 1, 0, 0},
309
310 /* Unused (0, 2). */
311 {1, 1, 0, 0},
312
313 /* Unused (0, 3). */
314 {1, 1, 0, 0},
315
316 /* Bcc o (1, 0). */
317 {BRANCH_BF, BRANCH_BB, 0, ENCODE_RELAX (1, 1)},
318
47926f60 319 /* Bcc [PC+] (1, 1). */
3bcbcc3d
HPN
320 {BRANCH_WF, BRANCH_WB, 2, ENCODE_RELAX (1, 2)},
321
322 /* BEXT/BWF, BA, JUMP (external), JUMP (always), Bnot_cc, JUMP (default)
323 (1, 2). */
324 {0, 0, 10, 0},
325
326 /* Unused (1, 3). */
327 {1, 1, 0, 0},
328
329 /* BDAP o (2, 0). */
330 {BDAP_BF, BDAP_BB, 0, ENCODE_RELAX (2, 1)},
331
332 /* BDAP.[bw] [PC+] (2, 1). */
333 {BDAP_WF, BDAP_WB, 2, ENCODE_RELAX (2, 2)},
334
335 /* BDAP.d [PC+] (2, 2). */
1048a9ba
HPN
336 {0, 0, 4, 0},
337
338 /* Unused (2, 3). */
ae57792d 339 {1, 1, 0, 0},
1048a9ba
HPN
340
341 /* MULS/MULU (3, 0). Positions (3, 1..3) are unused. */
ae57792d
HPN
342 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
343
344 /* V32: Bcc o (4, 0). */
345 {BRANCH_BF_V32, BRANCH_BB_V32, 0, ENCODE_RELAX (4, 1)},
346
347 /* V32: Bcc [PC+] (4, 1). */
348 {BRANCH_WF_V32, BRANCH_WB_V32, 2, ENCODE_RELAX (4, 2)},
349
350 /* V32: BA .+12; NOP; BA32 target; NOP; Bcc .-6 (4, 2). */
351 {0, 0, 12, 0},
352
353 /* Unused (4, 3). */
354 {1, 1, 0, 0},
355
356 /* COMMON: Bcc o (5, 0). The offsets are calculated as for v32. Code
357 should contain two nop insns (or four if offset size is large or
358 unknown) after every label. */
359 {BRANCH_BF_V32, BRANCH_BB_V32, 0, ENCODE_RELAX (5, 1)},
360
361 /* COMMON: Bcc [PC+] (5, 1). */
362 {BRANCH_WF_V32, BRANCH_WB_V32, 2, ENCODE_RELAX (5, 2)},
363
364 /* COMMON: FIXME: ???. Treat as error currently. */
365 {0, 0, 12, 0},
366
367 /* Unused (5, 3). */
368 {1, 1, 0, 0},
369
370 /* V32: BA o (6, 0). */
371 {BRANCH_BF_V32, BRANCH_BB_V32, 0, ENCODE_RELAX (6, 1)},
372
373 /* V32: BA.W (6, 1). */
374 {BRANCH_WF_V32, BRANCH_WB_V32, 2, ENCODE_RELAX (6, 2)},
375
376 /* V32: BA.D (6, 2). */
377 {0, 0, 4, 0},
378
379 /* Unused (6, 3). */
380 {1, 1, 0, 0},
381
382 /* LAPC: LAPCQ .+0..15*2,Rn (7, 0). */
383 {14*2, -1*2, 0, ENCODE_RELAX (7, 2)},
384
385 /* Unused (7, 1).
386 While there's a shorter sequence, e.g. LAPCQ + an ADDQ or SUBQ,
387 that would affect flags, so we can't do that as it wouldn't be a
388 proper insn expansion of LAPCQ. This row is associated with a
389 2-byte expansion, so it's unused rather than the next. */
390 {1, 1, 0, 0},
391
392 /* LAPC: LAPC.D (7, 2). */
393 {0, 0, 4, 0},
394
395 /* Unused (7, 3). */
d2aa3f9f
HPN
396 {1, 1, 0, 0},
397
398 /* PIC for pre-v32: Bcc o (8, 0). */
399 {BRANCH_BF, BRANCH_BB, 0, ENCODE_RELAX (STATE_COND_BRANCH_PIC, 1)},
400
401 /* Bcc [PC+] (8, 1). */
402 {BRANCH_WF, BRANCH_WB, 2, ENCODE_RELAX (STATE_COND_BRANCH_PIC, 2)},
403
404 /* 32-bit expansion, PIC (8, 2). */
405 {0, 0, 12, 0},
406
407 /* Unused (8, 3). */
ae57792d 408 {1, 1, 0, 0}
3bcbcc3d
HPN
409};
410
3bcbcc3d
HPN
411#undef BDAP_BF
412#undef BDAP_BB
413#undef BDAP_WF
414#undef BDAP_WB
415
ae57792d 416/* Target-specific multicharacter options, not const-declared. */
3bcbcc3d
HPN
417struct option md_longopts[] =
418{
7b15d668
HPN
419#define OPTION_NO_US (OPTION_MD_BASE + 0)
420 {"no-underscore", no_argument, NULL, OPTION_NO_US},
421#define OPTION_US (OPTION_MD_BASE + 1)
422 {"underscore", no_argument, NULL, OPTION_US},
ae57792d 423#define OPTION_PIC (OPTION_US + 1)
08caf3f8 424 {"pic", no_argument, NULL, OPTION_PIC},
ae57792d 425#define OPTION_MULBUG_ABORT_ON (OPTION_PIC + 1)
1048a9ba 426 {"mul-bug-abort", no_argument, NULL, OPTION_MULBUG_ABORT_ON},
ae57792d 427#define OPTION_MULBUG_ABORT_OFF (OPTION_MULBUG_ABORT_ON + 1)
1048a9ba 428 {"no-mul-bug-abort", no_argument, NULL, OPTION_MULBUG_ABORT_OFF},
ae57792d
HPN
429#define OPTION_ARCH (OPTION_MULBUG_ABORT_OFF + 1)
430 {"march", required_argument, NULL, OPTION_ARCH},
3bcbcc3d
HPN
431 {NULL, no_argument, NULL, 0}
432};
433
ae57792d 434/* Not const-declared. */
3bcbcc3d
HPN
435size_t md_longopts_size = sizeof (md_longopts);
436const char *md_shortopts = "hHN";
437
3bcbcc3d
HPN
438/* At first glance, this may seems wrong and should be 4 (ba + nop); but
439 since a short_jump must skip a *number* of long jumps, it must also be
440 a long jump. Here, we hope to make it a "ba [16bit_offs]" and a "nop"
441 for the delay slot and hope that the jump table at most needs
442 32767/4=8191 long-jumps. A branch is better than a jump, since it is
443 relative; we will not have a reloc to fix up somewhere.
444
445 Note that we can't add relocs, because relaxation uses these fixed
446 numbers, and md_create_short_jump is called after relaxation. */
447
2b4f075a 448int md_short_jump_size = 6;
ae57792d 449
d2aa3f9f
HPN
450/* The v32 version has a delay-slot, hence two bytes longer.
451 The pre-v32 PIC version uses a prefixed insn. */
ae57792d 452#define cris_any_v0_v10_long_jump_size 6
d2aa3f9f 453#define cris_any_v0_v10_long_jump_size_pic 8
ae57792d
HPN
454#define crisv32_long_jump_size 8
455
456int md_long_jump_size = XCONCAT2 (DEFAULT_CRIS_ARCH,_long_jump_size);
3bcbcc3d 457
7b15d668 458/* Report output format. Small changes in output format (like elf
08caf3f8
HPN
459 variants below) can happen until all options are parsed, but after
460 that, the output format must remain fixed. */
47926f60 461
3bcbcc3d 462const char *
695a4822 463cris_target_format (void)
3bcbcc3d
HPN
464{
465 switch (OUTPUT_FLAVOR)
466 {
467 case bfd_target_aout_flavour:
468 return "a.out-cris";
469
470 case bfd_target_elf_flavour:
7b15d668
HPN
471 if (symbols_have_leading_underscore)
472 return "elf32-us-cris";
3bcbcc3d
HPN
473 return "elf32-cris";
474
475 default:
476 abort ();
477 return NULL;
478 }
479}
480
ae57792d
HPN
481/* Return a bfd_mach_cris... value corresponding to the value of
482 cris_arch. */
483
484unsigned int
695a4822 485cris_mach (void)
ae57792d
HPN
486{
487 unsigned int retval = 0;
488
489 switch (cris_arch)
490 {
491 case arch_cris_common_v10_v32:
492 retval = bfd_mach_cris_v10_v32;
493 break;
494
495 case arch_crisv32:
496 retval = bfd_mach_cris_v32;
497 break;
498
499 case arch_crisv10:
500 case arch_cris_any_v0_v10:
501 retval = bfd_mach_cris_v0_v10;
502 break;
503
504 default:
505 BAD_CASE (cris_arch);
506 }
507
508 return retval;
509}
510
1c971160
HPN
511/* We need a port-specific relaxation function to cope with sym2 - sym1
512 relative expressions with both symbols in the same segment (but not
513 necessarily in the same frag as this insn), for example:
514 move.d [pc+sym2-(sym1-2)],r10
515 sym1:
516 The offset can be 8, 16 or 32 bits long. */
517
518long
695a4822
HPN
519cris_relax_frag (segT seg ATTRIBUTE_UNUSED, fragS *fragP,
520 long stretch ATTRIBUTE_UNUSED)
1c971160
HPN
521{
522 long growth;
523 offsetT aim = 0;
524 symbolS *symbolP;
525 const relax_typeS *this_type;
526 const relax_typeS *start_type;
527 relax_substateT next_state;
528 relax_substateT this_state;
529 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
530
531 /* We only have to cope with frags as prepared by
fb2fd3e1 532 md_estimate_size_before_relax. The dword cases may get here
1c971160
HPN
533 because of the different reasons that they aren't relaxable. */
534 switch (fragP->fr_subtype)
535 {
f6ce267c 536 case ENCODE_RELAX (STATE_COND_BRANCH_PIC, STATE_DWORD):
ae57792d
HPN
537 case ENCODE_RELAX (STATE_COND_BRANCH, STATE_DWORD):
538 case ENCODE_RELAX (STATE_COND_BRANCH_V32, STATE_DWORD):
539 case ENCODE_RELAX (STATE_COND_BRANCH_COMMON, STATE_DWORD):
540 case ENCODE_RELAX (STATE_ABS_BRANCH_V32, STATE_DWORD):
541 case ENCODE_RELAX (STATE_LAPC, STATE_DWORD):
1c971160
HPN
542 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD):
543 /* When we get to these states, the frag won't grow any more. */
544 return 0;
545
546 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_WORD):
547 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE):
548 if (fragP->fr_symbol == NULL
549 || S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
550 as_fatal (_("internal inconsistency problem in %s: fr_symbol %lx"),
551 __FUNCTION__, (long) fragP->fr_symbol);
552 symbolP = fragP->fr_symbol;
553 if (symbol_resolved_p (symbolP))
554 as_fatal (_("internal inconsistency problem in %s: resolved symbol"),
555 __FUNCTION__);
556 aim = S_GET_VALUE (symbolP);
557 break;
558
1048a9ba
HPN
559 case ENCODE_RELAX (STATE_MUL, STATE_BYTE):
560 /* Nothing to do here. */
561 return 0;
562
1c971160
HPN
563 default:
564 as_fatal (_("internal inconsistency problem in %s: fr_subtype %d"),
565 __FUNCTION__, fragP->fr_subtype);
566 }
567
568 /* The rest is stolen from relax_frag. There's no obvious way to
569 share the code, but fortunately no requirement to keep in sync as
570 long as fragP->fr_symbol does not have its segment changed. */
571
572 this_state = fragP->fr_subtype;
573 start_type = this_type = table + this_state;
574
575 if (aim < 0)
576 {
577 /* Look backwards. */
578 for (next_state = this_type->rlx_more; next_state;)
579 if (aim >= this_type->rlx_backward)
580 next_state = 0;
581 else
582 {
583 /* Grow to next state. */
584 this_state = next_state;
585 this_type = table + this_state;
586 next_state = this_type->rlx_more;
587 }
588 }
589 else
590 {
591 /* Look forwards. */
592 for (next_state = this_type->rlx_more; next_state;)
593 if (aim <= this_type->rlx_forward)
594 next_state = 0;
595 else
596 {
597 /* Grow to next state. */
598 this_state = next_state;
599 this_type = table + this_state;
600 next_state = this_type->rlx_more;
601 }
602 }
603
604 growth = this_type->rlx_length - start_type->rlx_length;
605 if (growth != 0)
606 fragP->fr_subtype = this_state;
607 return growth;
608}
609
3bcbcc3d
HPN
610/* Prepare machine-dependent frags for relaxation.
611
612 Called just before relaxation starts. Any symbol that is now undefined
613 will not become defined.
614
615 Return the correct fr_subtype in the frag.
616
617 Return the initial "guess for fr_var" to caller. The guess for fr_var
618 is *actually* the growth beyond fr_fix. Whatever we do to grow fr_fix
619 or fr_var contributes to our returned value.
620
621 Although it may not be explicit in the frag, pretend
47926f60 622 fr_var starts with a value. */
3bcbcc3d
HPN
623
624int
695a4822 625md_estimate_size_before_relax (fragS *fragP, segT segment_type)
3bcbcc3d 626{
47926f60 627 int old_fr_fix;
ae57792d
HPN
628 symbolS *symbolP = fragP->fr_symbol;
629
630#define HANDLE_RELAXABLE(state) \
631 case ENCODE_RELAX (state, STATE_UNDF): \
632 if (symbolP != NULL \
633 && S_GET_SEGMENT (symbolP) == segment_type \
634 && !S_IS_WEAK (symbolP)) \
635 /* The symbol lies in the same segment - a relaxable \
636 case. */ \
637 fragP->fr_subtype \
638 = ENCODE_RELAX (state, STATE_BYTE); \
639 else \
640 /* Unknown or not the same segment, so not relaxable. */ \
641 fragP->fr_subtype \
642 = ENCODE_RELAX (state, STATE_DWORD); \
643 fragP->fr_var \
644 = md_cris_relax_table[fragP->fr_subtype].rlx_length; \
645 break
3bcbcc3d
HPN
646
647 old_fr_fix = fragP->fr_fix;
648
649 switch (fragP->fr_subtype)
650 {
ae57792d
HPN
651 HANDLE_RELAXABLE (STATE_COND_BRANCH);
652 HANDLE_RELAXABLE (STATE_COND_BRANCH_V32);
653 HANDLE_RELAXABLE (STATE_COND_BRANCH_COMMON);
d2aa3f9f 654 HANDLE_RELAXABLE (STATE_COND_BRANCH_PIC);
ae57792d
HPN
655 HANDLE_RELAXABLE (STATE_ABS_BRANCH_V32);
656
657 case ENCODE_RELAX (STATE_LAPC, STATE_UNDF):
658 if (symbolP != NULL
659 && S_GET_SEGMENT (symbolP) == segment_type
660 && !S_IS_WEAK (symbolP))
661 {
662 /* The symbol lies in the same segment - a relaxable case.
663 Check if we currently have an odd offset; we can't code
664 that into the instruction. Relaxing presumably only cause
665 multiple-of-two changes, so we should only need to adjust
666 for that here. */
667 bfd_vma target_address
668 = (symbolP
669 ? S_GET_VALUE (symbolP)
670 : 0) + fragP->fr_offset;
671 bfd_vma var_part_offset = fragP->fr_fix;
672 bfd_vma address_of_var_part = fragP->fr_address + var_part_offset;
673 long offset = target_address - (address_of_var_part - 2);
674
675 fragP->fr_subtype
676 = (offset & 1)
677 ? ENCODE_RELAX (STATE_LAPC, STATE_DWORD)
678 : ENCODE_RELAX (STATE_LAPC, STATE_BYTE);
679 }
3bcbcc3d 680 else
c335d394
HPN
681 /* Unknown or not the same segment, so not relaxable. */
682 fragP->fr_subtype
ae57792d
HPN
683 = ENCODE_RELAX (STATE_LAPC, STATE_DWORD);
684 fragP->fr_var
685 = md_cris_relax_table[fragP->fr_subtype].rlx_length;
3bcbcc3d
HPN
686 break;
687
688 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_UNDF):
689 /* Note that we can not do anything sane with relaxing
690 [rX + a_known_symbol_in_text], it will have to be a 32-bit
691 value.
692
693 We could play tricks with managing a constant pool and make
08caf3f8
HPN
694 a_known_symbol_in_text a "bdap [pc + offset]" pointing there
695 (like the GOT for ELF shared libraries), but that's no use, it
696 would in general be no shorter or faster code, only more
697 complicated. */
3bcbcc3d 698
ae57792d 699 if (S_GET_SEGMENT (symbolP) != absolute_section)
3bcbcc3d
HPN
700 {
701 /* Go for dword if not absolute or same segment. */
702 fragP->fr_subtype
703 = ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD);
c335d394 704 fragP->fr_var = md_cris_relax_table[fragP->fr_subtype].rlx_length;
3bcbcc3d 705 }
1c971160
HPN
706 else if (!symbol_resolved_p (fragP->fr_symbol))
707 {
708 /* The symbol will eventually be completely resolved as an
709 absolute expression, but right now it depends on the result
710 of relaxation and we don't know anything else about the
711 value. We start relaxation with the assumption that it'll
712 fit in a byte. */
713 fragP->fr_subtype
714 = ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE);
715 fragP->fr_var = md_cris_relax_table[fragP->fr_subtype].rlx_length;
716 }
3bcbcc3d
HPN
717 else
718 {
719 /* Absolute expression. */
720 long int value;
ae57792d
HPN
721 value = (symbolP != NULL
722 ? S_GET_VALUE (symbolP) : 0) + fragP->fr_offset;
3bcbcc3d
HPN
723
724 if (value >= -128 && value <= 127)
725 {
726 /* Byte displacement. */
727 (fragP->fr_opcode)[0] = value;
728 }
729 else
730 {
731 /* Word or dword displacement. */
732 int pow2_of_size = 1;
733 char *writep;
734
735 if (value < -32768 || value > 32767)
736 {
737 /* Outside word range, make it a dword. */
738 pow2_of_size = 2;
739 }
740
741 /* Modify the byte-offset BDAP into a word or dword offset
742 BDAP. Or really, a BDAP rX,8bit into a
07e90ad5 743 BDAP.[wd] rX,[PC+] followed by a word or dword. */
3bcbcc3d
HPN
744 (fragP->fr_opcode)[0] = BDAP_PC_LOW + pow2_of_size * 16;
745
746 /* Keep the register number in the highest four bits. */
747 (fragP->fr_opcode)[1] &= 0xF0;
748 (fragP->fr_opcode)[1] |= BDAP_INCR_HIGH;
749
47926f60 750 /* It grew by two or four bytes. */
3bcbcc3d
HPN
751 fragP->fr_fix += 1 << pow2_of_size;
752 writep = fragP->fr_literal + old_fr_fix;
753 md_number_to_chars (writep, value, 1 << pow2_of_size);
754 }
755 frag_wane (fragP);
756 }
757 break;
758
ae57792d
HPN
759 case ENCODE_RELAX (STATE_COND_BRANCH, STATE_BYTE):
760 case ENCODE_RELAX (STATE_COND_BRANCH, STATE_WORD):
761 case ENCODE_RELAX (STATE_COND_BRANCH, STATE_DWORD):
d2aa3f9f
HPN
762 case ENCODE_RELAX (STATE_COND_BRANCH_PIC, STATE_BYTE):
763 case ENCODE_RELAX (STATE_COND_BRANCH_PIC, STATE_WORD):
764 case ENCODE_RELAX (STATE_COND_BRANCH_PIC, STATE_DWORD):
ae57792d
HPN
765 case ENCODE_RELAX (STATE_COND_BRANCH_V32, STATE_BYTE):
766 case ENCODE_RELAX (STATE_COND_BRANCH_V32, STATE_WORD):
767 case ENCODE_RELAX (STATE_COND_BRANCH_V32, STATE_DWORD):
768 case ENCODE_RELAX (STATE_COND_BRANCH_COMMON, STATE_BYTE):
769 case ENCODE_RELAX (STATE_COND_BRANCH_COMMON, STATE_WORD):
770 case ENCODE_RELAX (STATE_COND_BRANCH_COMMON, STATE_DWORD):
771 case ENCODE_RELAX (STATE_ABS_BRANCH_V32, STATE_BYTE):
772 case ENCODE_RELAX (STATE_ABS_BRANCH_V32, STATE_WORD):
773 case ENCODE_RELAX (STATE_ABS_BRANCH_V32, STATE_DWORD):
774 case ENCODE_RELAX (STATE_LAPC, STATE_BYTE):
775 case ENCODE_RELAX (STATE_LAPC, STATE_DWORD):
c335d394
HPN
776 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE):
777 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_WORD):
93c2a809
AM
778 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD):
779 /* When relaxing a section for the second time, we don't need to
c335d394
HPN
780 do anything except making sure that fr_var is set right. */
781 fragP->fr_var = md_cris_relax_table[fragP->fr_subtype].rlx_length;
93c2a809
AM
782 break;
783
1048a9ba
HPN
784 case ENCODE_RELAX (STATE_MUL, STATE_BYTE):
785 /* Nothing to do here. */
786 break;
787
3bcbcc3d
HPN
788 default:
789 BAD_CASE (fragP->fr_subtype);
790 }
791
792 return fragP->fr_var + (fragP->fr_fix - old_fr_fix);
793}
794
3bcbcc3d
HPN
795/* Perform post-processing of machine-dependent frags after relaxation.
796 Called after relaxation is finished.
797 In: Address of frag.
798 fr_type == rs_machine_dependent.
799 fr_subtype is what the address relaxed to.
800
801 Out: Any fixS:s and constants are set up.
802
803 The caller will turn the frag into a ".space 0". */
804
805void
695a4822
HPN
806md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
807 fragS *fragP)
3bcbcc3d 808{
47926f60 809 /* Pointer to first byte in variable-sized part of the frag. */
3bcbcc3d
HPN
810 char *var_partp;
811
812 /* Pointer to first opcode byte in frag. */
813 char *opcodep;
814
815 /* Used to check integrity of the relaxation.
816 One of 2 = long, 1 = word, or 0 = byte. */
66dbb2bb 817 int length_code ATTRIBUTE_UNUSED;
3bcbcc3d
HPN
818
819 /* Size in bytes of variable-sized part of frag. */
820 int var_part_size = 0;
821
822 /* This is part of *fragP. It contains all information about addresses
823 and offsets to varying parts. */
824 symbolS *symbolP;
825 unsigned long var_part_offset;
826
827 /* Where, in file space, is _var of *fragP? */
828 unsigned long address_of_var_part = 0;
829
830 /* Where, in file space, does addr point? */
831 unsigned long target_address;
832
833 know (fragP->fr_type == rs_machine_dependent);
834
835 length_code = fragP->fr_subtype & STATE_LENGTH_MASK;
836 know (length_code >= 0 && length_code < STATE_MAX_LENGTH);
837
838 var_part_offset = fragP->fr_fix;
839 var_partp = fragP->fr_literal + var_part_offset;
840 opcodep = fragP->fr_opcode;
841
842 symbolP = fragP->fr_symbol;
ac62c346 843 target_address = (symbolP ? S_GET_VALUE (symbolP) : 0) + fragP->fr_offset;
3bcbcc3d
HPN
844 address_of_var_part = fragP->fr_address + var_part_offset;
845
846 switch (fragP->fr_subtype)
47926f60 847 {
ae57792d 848 case ENCODE_RELAX (STATE_COND_BRANCH, STATE_BYTE):
d2aa3f9f 849 case ENCODE_RELAX (STATE_COND_BRANCH_PIC, STATE_BYTE):
ae57792d
HPN
850 case ENCODE_RELAX (STATE_COND_BRANCH_V32, STATE_BYTE):
851 case ENCODE_RELAX (STATE_COND_BRANCH_COMMON, STATE_BYTE):
852 case ENCODE_RELAX (STATE_ABS_BRANCH_V32, STATE_BYTE):
47926f60
KH
853 opcodep[0] = branch_disp ((target_address - address_of_var_part));
854 var_part_size = 0;
855 break;
3bcbcc3d 856
ae57792d 857 case ENCODE_RELAX (STATE_COND_BRANCH, STATE_WORD):
d2aa3f9f 858 case ENCODE_RELAX (STATE_COND_BRANCH_PIC, STATE_WORD):
ae57792d
HPN
859 case ENCODE_RELAX (STATE_COND_BRANCH_V32, STATE_WORD):
860 case ENCODE_RELAX (STATE_COND_BRANCH_COMMON, STATE_WORD):
861 case ENCODE_RELAX (STATE_ABS_BRANCH_V32, STATE_WORD):
47926f60
KH
862 /* We had a quick immediate branch, now turn it into a word one i.e. a
863 PC autoincrement. */
864 opcodep[0] = BRANCH_PC_LOW;
865 opcodep[1] &= 0xF0;
866 opcodep[1] |= BRANCH_INCR_HIGH;
867 md_number_to_chars (var_partp,
ae57792d
HPN
868 (long)
869 (target_address
870 - (address_of_var_part
871 + (cris_arch == arch_crisv32
872 || cris_arch == arch_cris_common_v10_v32
873 ? -2 : 2))),
47926f60
KH
874 2);
875 var_part_size = 2;
876 break;
877
ae57792d 878 case ENCODE_RELAX (STATE_COND_BRANCH, STATE_DWORD):
47926f60
KH
879 gen_cond_branch_32 (fragP->fr_opcode, var_partp, fragP,
880 fragP->fr_symbol, (symbolS *) NULL,
881 fragP->fr_offset);
882 /* Ten bytes added: a branch, nop and a jump. */
883 var_part_size = 2 + 2 + 4 + 2;
884 break;
3bcbcc3d 885
d2aa3f9f
HPN
886 case ENCODE_RELAX (STATE_COND_BRANCH_PIC, STATE_DWORD):
887 gen_cond_branch_32 (fragP->fr_opcode, var_partp, fragP,
888 fragP->fr_symbol, (symbolS *) NULL,
889 fragP->fr_offset);
890 /* Twelve bytes added: a branch, nop and a pic-branch-32. */
891 var_part_size = 2 + 2 + 4 + 2 + 2;
892 break;
893
ae57792d
HPN
894 case ENCODE_RELAX (STATE_COND_BRANCH_V32, STATE_DWORD):
895 gen_cond_branch_32 (fragP->fr_opcode, var_partp, fragP,
896 fragP->fr_symbol, (symbolS *) NULL,
897 fragP->fr_offset);
898 /* Twelve bytes added: a branch, nop and another branch and nop. */
899 var_part_size = 2 + 2 + 2 + 4 + 2;
900 break;
901
902 case ENCODE_RELAX (STATE_COND_BRANCH_COMMON, STATE_DWORD):
903 as_bad_where (fragP->fr_file, fragP->fr_line,
904 _("Relaxation to long branches for .arch common_v10_v32\
905 not implemented"));
906 /* Pretend we have twelve bytes for sake of quelling further
907 errors. */
908 var_part_size = 2 + 2 + 2 + 4 + 2;
909 break;
910
911 case ENCODE_RELAX (STATE_ABS_BRANCH_V32, STATE_DWORD):
912 /* We had a quick immediate branch or a word immediate ba. Now
913 turn it into a dword one. */
914 opcodep[0] = BA_DWORD_OPCODE & 255;
915 opcodep[1] = (BA_DWORD_OPCODE >> 8) & 255;
916 fix_new (fragP, var_partp - fragP->fr_literal, 4, symbolP,
917 fragP->fr_offset + 6, 1, BFD_RELOC_32_PCREL);
918 var_part_size = 4;
919 break;
920
921 case ENCODE_RELAX (STATE_LAPC, STATE_BYTE):
922 {
923 long offset = target_address - (address_of_var_part - 2);
924
925 /* This is mostly a sanity check; useful occurrences (if there
926 really are any) should have been caught in
927 md_estimate_size_before_relax. We can (at least
928 theoretically) stumble over invalid code with odd sizes and
929 .p2aligns within the code, so emit an error if that happens.
930 (The generic relaxation machinery is not fit to check this.) */
931
932 if (offset & 1)
933 as_bad_where (fragP->fr_file, fragP->fr_line,
934 _("Complicated LAPC target operand is not\
935 a multiple of two. Use LAPC.D"));
936
937 /* FIXME: This *is* a sanity check. Remove when done with. */
938 if (offset > 15*2 || offset < 0)
939 as_fatal (_("Internal error found in md_convert_frag: offset %ld.\
940 Please report this."),
941 offset);
942
943 opcodep[0] |= (offset / 2) & 0xf;
944 var_part_size = 0;
945 }
946 break;
947
948 case ENCODE_RELAX (STATE_LAPC, STATE_DWORD):
949 {
950 md_number_to_chars (opcodep,
951 LAPC_DWORD_OPCODE + (opcodep[1] & 0xf0) * 256,
952 2);
953 /* Remember that the reloc is against the position *after* the
954 relocated contents, so we need to adjust to the start of
955 the insn. */
956 fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
957 fragP->fr_offset + 6, 1, BFD_RELOC_32_PCREL);
958 var_part_size = 4;
959 }
960 break;
961
47926f60 962 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE):
1c971160
HPN
963 if (symbolP == NULL)
964 as_fatal (_("internal inconsistency in %s: bdapq no symbol"),
965 __FUNCTION__);
966 opcodep[0] = S_GET_VALUE (symbolP);
47926f60
KH
967 var_part_size = 0;
968 break;
969
970 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_WORD):
971 /* We had a BDAP 8-bit "quick immediate", now turn it into a 16-bit
972 one that uses PC autoincrement. */
973 opcodep[0] = BDAP_PC_LOW + (1 << 4);
974 opcodep[1] &= 0xF0;
975 opcodep[1] |= BDAP_INCR_HIGH;
1c971160
HPN
976 if (symbolP == NULL)
977 as_fatal (_("internal inconsistency in %s: bdap.w with no symbol"),
978 __FUNCTION__);
979 md_number_to_chars (var_partp, S_GET_VALUE (symbolP), 2);
47926f60
KH
980 var_part_size = 2;
981 break;
982
983 case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD):
984 /* We had a BDAP 16-bit "word", change the offset to a dword. */
985 opcodep[0] = BDAP_PC_LOW + (2 << 4);
986 opcodep[1] &= 0xF0;
987 opcodep[1] |= BDAP_INCR_HIGH;
988 if (fragP->fr_symbol == NULL)
989 md_number_to_chars (var_partp, fragP->fr_offset, 4);
990 else
991 fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
992 fragP->fr_offset, 0, BFD_RELOC_32);
993 var_part_size = 4;
994 break;
995
1048a9ba 996 case ENCODE_RELAX (STATE_MUL, STATE_BYTE):
708587a4 997 /* This is the only time we check position and alignment of the
1048a9ba
HPN
998 placement-tracking frag. */
999 if (sec->alignment_power < 2)
1000 as_bad_where (fragP->fr_file, fragP->fr_line,
1001 _("section alignment must be >= 4 bytes to check MULS/MULU safeness"));
1002 else
1003 {
1004 /* If the address after the MULS/MULU has alignment which is
1005 that of the section and may be that of a cache-size of the
1006 buggy versions, then the MULS/MULU can be placed badly. */
1007 if ((address_of_var_part
1008 & ((1 << sec->alignment_power) - 1) & 31) == 0)
1009 as_bad_where (fragP->fr_file, fragP->fr_line,
1010 _("dangerous MULS/MULU location; give it higher alignment"));
1011 }
1012 break;
1013
47926f60
KH
1014 default:
1015 BAD_CASE (fragP->fr_subtype);
1016 break;
1017 }
1018
1019 fragP->fr_fix += var_part_size;
3bcbcc3d
HPN
1020}
1021
1022/* Generate a short jump around a secondary jump table.
a07dbd48 1023 Also called from md_create_long_jump, when sufficient. */
3bcbcc3d 1024
a07dbd48
HPN
1025void
1026md_create_short_jump (char *storep, addressT from_addr, addressT to_addr,
695a4822
HPN
1027 fragS *fragP ATTRIBUTE_UNUSED,
1028 symbolS *to_symbol ATTRIBUTE_UNUSED)
3bcbcc3d
HPN
1029{
1030 long int distance;
1031
ae57792d
HPN
1032 /* See md_create_long_jump about the comment on the "+ 2". */
1033 long int max_minimal_minus_distance;
1034 long int max_minimal_plus_distance;
a07dbd48
HPN
1035 long int max_minus_distance;
1036 long int max_plus_distance;
ae57792d
HPN
1037 int nop_opcode;
1038
1039 if (cris_arch == arch_crisv32)
1040 {
1041 max_minimal_minus_distance = BRANCH_BB_V32 + 2;
1042 max_minimal_plus_distance = BRANCH_BF_V32 + 2;
a07dbd48
HPN
1043 max_minus_distance = BRANCH_WB_V32 + 2;
1044 max_plus_distance = BRANCH_WF_V32 + 2;
ae57792d
HPN
1045 nop_opcode = NOP_OPCODE_V32;
1046 }
a07dbd48
HPN
1047 else if (cris_arch == arch_cris_common_v10_v32)
1048 /* Bail out for compatibility mode. (It seems it can be implemented,
1049 perhaps with a 10-byte sequence: "move.d NNNN,$pc/$acr", "jump
1050 $acr", "nop"; but doesn't seem worth it at the moment.) */
1051 as_fatal (_("Out-of-range .word offset handling\
1052 is not implemented for .arch common_v10_v32"));
ae57792d
HPN
1053 else
1054 {
1055 max_minimal_minus_distance = BRANCH_BB + 2;
1056 max_minimal_plus_distance = BRANCH_BF + 2;
a07dbd48
HPN
1057 max_minus_distance = BRANCH_WB + 2;
1058 max_plus_distance = BRANCH_WF + 2;
ae57792d
HPN
1059 nop_opcode = NOP_OPCODE;
1060 }
1061
3bcbcc3d
HPN
1062 distance = to_addr - from_addr;
1063
ae57792d
HPN
1064 if (max_minimal_minus_distance <= distance
1065 && distance <= max_minimal_plus_distance)
3bcbcc3d
HPN
1066 {
1067 /* Create a "short" short jump: "BA distance - 2". */
47926f60 1068 storep[0] = branch_disp (distance - 2);
3bcbcc3d
HPN
1069 storep[1] = BA_QUICK_HIGH;
1070
1071 /* A nop for the delay slot. */
ae57792d 1072 md_number_to_chars (storep + 2, nop_opcode, 2);
3bcbcc3d
HPN
1073
1074 /* The extra word should be filled with something sane too. Make it
1075 a nop to keep disassembly sane. */
ae57792d 1076 md_number_to_chars (storep + 4, nop_opcode, 2);
3bcbcc3d 1077 }
a07dbd48
HPN
1078 else if (max_minus_distance <= distance
1079 && distance <= max_plus_distance)
3bcbcc3d
HPN
1080 {
1081 /* Make it a "long" short jump: "BA (PC+)". */
1082 md_number_to_chars (storep, BA_PC_INCR_OPCODE, 2);
1083
47926f60 1084 /* ".WORD distance - 4". */
ae57792d
HPN
1085 md_number_to_chars (storep + 2,
1086 (long) (distance - 4
1087 - (cris_arch == arch_crisv32
1088 ? -4 : 0)),
1089 2);
3bcbcc3d
HPN
1090
1091 /* A nop for the delay slot. */
ae57792d 1092 md_number_to_chars (storep + 4, nop_opcode, 2);
3bcbcc3d 1093 }
a07dbd48
HPN
1094 else
1095 as_bad_where (fragP->fr_file, fragP->fr_line,
1096 _(".word case-table handling failed: table too large"));
3bcbcc3d
HPN
1097}
1098
3bcbcc3d
HPN
1099/* Generate a long jump in a secondary jump table.
1100
1101 storep Where to store the jump instruction.
1102 from_addr Address of the jump instruction.
1103 to_addr Destination address of the jump.
1104 fragP Which frag the destination address operand
1105 lies in.
1106 to_symbol Destination symbol. */
1107
1108void
695a4822
HPN
1109md_create_long_jump (char *storep, addressT from_addr, addressT to_addr,
1110 fragS *fragP, symbolS *to_symbol)
3bcbcc3d
HPN
1111{
1112 long int distance;
1113
ae57792d
HPN
1114 /* FIXME: What's that "+ 3"? It comes from the magic numbers that
1115 used to be here, it's just translated to the limit macros used in
1116 the relax table. But why + 3? */
1117 long int max_short_minus_distance
1118 = cris_arch != arch_crisv32 ? BRANCH_WB + 3 : BRANCH_WB_V32 + 3;
1119
1120 long int max_short_plus_distance
1121 = cris_arch != arch_crisv32 ? BRANCH_WF + 3 : BRANCH_WF_V32 + 3;
1122
3bcbcc3d
HPN
1123 distance = to_addr - from_addr;
1124
ae57792d
HPN
1125 if (max_short_minus_distance <= distance
1126 && distance <= max_short_plus_distance)
44ed9ef2
AM
1127 {
1128 /* Then make it a "short" long jump. */
1129 md_create_short_jump (storep, from_addr, to_addr, fragP,
ae57792d 1130 to_symbol);
44ed9ef2
AM
1131 if (cris_arch == arch_crisv32)
1132 md_number_to_chars (storep + 6, NOP_OPCODE_V32, 2);
1133 else
1134 md_number_to_chars (storep + 6, NOP_OPCODE, 2);
1135 }
3bcbcc3d
HPN
1136 else
1137 {
ae57792d 1138 /* We have a "long" long jump: "JUMP [PC+]". If CRISv32, always
d2aa3f9f 1139 make it a BA. Else make it an "MOVE [PC=PC+N],P0" if we're supposed
ae57792d 1140 to emit PIC code. */
08caf3f8 1141 md_number_to_chars (storep,
ae57792d
HPN
1142 cris_arch == arch_crisv32
1143 ? BA_DWORD_OPCODE
d2aa3f9f
HPN
1144 : (pic ? MOVE_PC_INCR_OPCODE_PREFIX
1145 : JUMP_PC_INCR_OPCODE),
ae57792d 1146 2);
3bcbcc3d 1147
08caf3f8 1148 /* Follow with a ".DWORD to_addr", PC-relative for PIC. */
3bcbcc3d 1149 fix_new (fragP, storep + 2 - fragP->fr_literal, 4, to_symbol,
ae57792d
HPN
1150 cris_arch == arch_crisv32 ? 6 : 0,
1151 cris_arch == arch_crisv32 || pic ? 1 : 0,
1152 cris_arch == arch_crisv32 || pic
1153 ? BFD_RELOC_32_PCREL : BFD_RELOC_32);
1154
1155 /* Follow it with a "NOP" for CRISv32. */
1156 if (cris_arch == arch_crisv32)
1157 md_number_to_chars (storep + 6, NOP_OPCODE_V32, 2);
d2aa3f9f
HPN
1158 else if (pic)
1159 /* ...and the rest of the move-opcode for pre-v32 PIC. */
1160 md_number_to_chars (storep + 6, MOVE_PC_INCR_OPCODE_SUFFIX, 2);
3bcbcc3d
HPN
1161 }
1162}
1163
ed67db7a
HPN
1164/* Allocate space for the first piece of an insn, and mark it as the
1165 start of the insn for debug-format use. */
1166
1167static char *
695a4822 1168cris_insn_first_word_frag (void)
ed67db7a
HPN
1169{
1170 char *insnp = frag_more (2);
1171
1172 /* We need to mark the start of the insn by passing dwarf2_emit_insn
1173 the offset from the current fragment position. This must be done
1174 after the first fragment is created but before any other fragments
1175 (fixed or varying) are created. Note that the offset only
1176 corresponds to the "size" of the insn for a fixed-size,
1177 non-expanded insn. */
1178 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1179 dwarf2_emit_insn (2);
1180
1181 return insnp;
1182}
1183
3bcbcc3d 1184/* Port-specific assembler initialization. */
47926f60 1185
3bcbcc3d 1186void
695a4822 1187md_begin (void)
3bcbcc3d 1188{
3bcbcc3d
HPN
1189 int i = 0;
1190
47926f60 1191 /* Set up a hash table for the instructions. */
629310ab 1192 op_hash = str_htab_create ();
3bcbcc3d 1193
ae57792d
HPN
1194 /* Enable use of ".if ..asm.arch.cris.v32"
1195 and ".if ..asm.arch.cris.common_v10_v32" and a few others. */
1196 symbol_table_insert (symbol_new ("..asm.arch.cris.v32", absolute_section,
e01e1cee
AM
1197 &zero_address_frag,
1198 cris_arch == arch_crisv32));
ae57792d 1199 symbol_table_insert (symbol_new ("..asm.arch.cris.v10", absolute_section,
e01e1cee
AM
1200 &zero_address_frag,
1201 cris_arch == arch_crisv10));
ae57792d
HPN
1202 symbol_table_insert (symbol_new ("..asm.arch.cris.common_v10_v32",
1203 absolute_section,
e01e1cee
AM
1204 &zero_address_frag,
1205 cris_arch == arch_cris_common_v10_v32));
ae57792d
HPN
1206 symbol_table_insert (symbol_new ("..asm.arch.cris.any_v0_v10",
1207 absolute_section,
e01e1cee
AM
1208 &zero_address_frag,
1209 cris_arch == arch_cris_any_v0_v10));
ae57792d 1210
3bcbcc3d
HPN
1211 while (cris_opcodes[i].name != NULL)
1212 {
1213 const char *name = cris_opcodes[i].name;
ae57792d
HPN
1214
1215 if (! cris_insn_ver_valid_for_arch (cris_opcodes[i].applicable_version,
1216 cris_arch))
1217 {
1218 i++;
1219 continue;
1220 }
1221
fe0e921f
AM
1222 if (str_hash_insert (op_hash, name, &cris_opcodes[i], 0) != NULL)
1223 as_fatal (_("duplicate %s"), name);
1224
3bcbcc3d
HPN
1225 do
1226 {
1227 if (cris_opcodes[i].match & cris_opcodes[i].lose)
1228 as_fatal (_("Buggy opcode: `%s' \"%s\"\n"), cris_opcodes[i].name,
1229 cris_opcodes[i].args);
1230
1231 ++i;
47926f60
KH
1232 }
1233 while (cris_opcodes[i].name != NULL
1234 && strcmp (cris_opcodes[i].name, name) == 0);
3bcbcc3d
HPN
1235 }
1236}
1237
3bcbcc3d 1238/* Assemble a source line. */
47926f60 1239
3bcbcc3d 1240void
695a4822 1241md_assemble (char *str)
3bcbcc3d
HPN
1242{
1243 struct cris_instruction output_instruction;
1244 struct cris_prefix prefix;
1245 char *opcodep;
1246 char *p;
1247
1248 know (str);
1249
1250 /* Do the low-level grunt - assemble to bits and split up into a prefix
1251 and ordinary insn. */
1252 cris_process_instruction (str, &output_instruction, &prefix);
1253
1254 /* Handle any prefixes to the instruction. */
1255 switch (prefix.kind)
1256 {
1257 case PREFIX_NONE:
1258 break;
1259
1260 /* When the expression is unknown for a BDAP, it can need 0, 2 or 4
1261 extra bytes, so we handle it separately. */
1262 case PREFIX_BDAP_IMM:
cc99daad 1263 /* We only do it if the relocation is unspecified, i.e. not a PIC or TLS
08caf3f8
HPN
1264 relocation. */
1265 if (prefix.reloc == BFD_RELOC_NONE)
1266 {
1267 gen_bdap (prefix.base_reg_number, &prefix.expr);
1268 break;
1269 }
1270 /* Fall through. */
3bcbcc3d
HPN
1271 case PREFIX_BDAP:
1272 case PREFIX_BIAP:
1273 case PREFIX_DIP:
ed67db7a 1274 opcodep = cris_insn_first_word_frag ();
3bcbcc3d
HPN
1275
1276 /* Output the prefix opcode. */
1277 md_number_to_chars (opcodep, (long) prefix.opcode, 2);
1278
08caf3f8 1279 /* Having a specified reloc only happens for DIP and for BDAP with
cc99daad 1280 PIC or TLS operands, but it is ok to drop through here for the other
08caf3f8 1281 prefixes as they can have no relocs specified. */
3bcbcc3d
HPN
1282 if (prefix.reloc != BFD_RELOC_NONE)
1283 {
08caf3f8
HPN
1284 unsigned int relocsize
1285 = (prefix.kind == PREFIX_DIP
cc99daad 1286 ? 4 : cris_get_specified_reloc_size (prefix.reloc));
08caf3f8 1287
08caf3f8
HPN
1288 p = frag_more (relocsize);
1289 fix_new_exp (frag_now, (p - frag_now->fr_literal), relocsize,
3bcbcc3d
HPN
1290 &prefix.expr, 0, prefix.reloc);
1291 }
1292 break;
1293
1294 case PREFIX_PUSH:
ed67db7a 1295 opcodep = cris_insn_first_word_frag ();
3bcbcc3d
HPN
1296
1297 /* Output the prefix opcode. Being a "push", we add the negative
1298 size of the register to "sp". */
1299 if (output_instruction.spec_reg != NULL)
1300 {
47926f60 1301 /* Special register. */
3bcbcc3d
HPN
1302 opcodep[0] = -output_instruction.spec_reg->reg_size;
1303 }
1304 else
1305 {
47926f60 1306 /* General register. */
3bcbcc3d
HPN
1307 opcodep[0] = -4;
1308 }
1309 opcodep[1] = (REG_SP << 4) + (BDAP_QUICK_OPCODE >> 8);
1310 break;
1311
1312 default:
1313 BAD_CASE (prefix.kind);
1314 }
1315
1316 /* If we only had a prefix insn, we're done. */
1317 if (output_instruction.insn_type == CRIS_INSN_NONE)
1318 return;
1319
1320 /* Done with the prefix. Continue with the main instruction. */
ed67db7a
HPN
1321 if (prefix.kind == PREFIX_NONE)
1322 opcodep = cris_insn_first_word_frag ();
1323 else
1324 opcodep = frag_more (2);
3bcbcc3d
HPN
1325
1326 /* Output the instruction opcode. */
47926f60 1327 md_number_to_chars (opcodep, (long) (output_instruction.opcode), 2);
3bcbcc3d
HPN
1328
1329 /* Output the symbol-dependent instruction stuff. */
1330 if (output_instruction.insn_type == CRIS_INSN_BRANCH)
1331 {
1332 segT to_seg = absolute_section;
1333 int is_undefined = 0;
1334 int length_code;
1335
1336 if (output_instruction.expr.X_op != O_constant)
1337 {
1338 to_seg = S_GET_SEGMENT (output_instruction.expr.X_add_symbol);
1339
1340 if (to_seg == undefined_section)
1341 is_undefined = 1;
1342 }
1343
ae57792d
HPN
1344 if (to_seg == now_seg || is_undefined
1345 /* In CRISv32, there *is* a 32-bit absolute branch, so don't
1346 emit the 12-byte sequence for known symbols in other
1347 segments. */
1348 || (cris_arch == arch_crisv32
1349 && output_instruction.opcode == BA_QUICK_OPCODE))
3bcbcc3d 1350 {
08caf3f8
HPN
1351 /* Handle complex expressions. */
1352 valueT addvalue
1c971160
HPN
1353 = (SIMPLE_EXPR (&output_instruction.expr)
1354 ? output_instruction.expr.X_add_number
1355 : 0);
08caf3f8 1356 symbolS *sym
1c971160
HPN
1357 = (SIMPLE_EXPR (&output_instruction.expr)
1358 ? output_instruction.expr.X_add_symbol
1359 : make_expr_symbol (&output_instruction.expr));
08caf3f8 1360
ae57792d
HPN
1361 /* If is_undefined, the expression may still become now_seg.
1362 That case is handled by md_estimate_size_before_relax. */
1363 length_code = to_seg == now_seg ? STATE_BYTE : STATE_UNDF;
1364
d2aa3f9f
HPN
1365 /* Make room for max twelve bytes of variable length for v32 mode
1366 or PIC, ten for v10 and older. */
ae57792d
HPN
1367 frag_var (rs_machine_dependent,
1368 (cris_arch == arch_crisv32
d2aa3f9f
HPN
1369 || cris_arch == arch_cris_common_v10_v32
1370 || pic) ? 12 : 10, 0,
ae57792d
HPN
1371 ENCODE_RELAX (cris_arch == arch_crisv32
1372 ? (output_instruction.opcode
1373 == BA_QUICK_OPCODE
1374 ? STATE_ABS_BRANCH_V32
1375 : STATE_COND_BRANCH_V32)
1376 : (cris_arch == arch_cris_common_v10_v32
1377 ? STATE_COND_BRANCH_COMMON
d2aa3f9f
HPN
1378 : (pic ? STATE_COND_BRANCH_PIC
1379 : STATE_COND_BRANCH)),
ae57792d 1380 length_code),
08caf3f8 1381 sym, addvalue, opcodep);
3bcbcc3d
HPN
1382 }
1383 else
1384 {
1385 /* We have: to_seg != now_seg && to_seg != undefined_section.
1386 This means it is a branch to a known symbol in another
fb2fd3e1 1387 section, perhaps an absolute address. Emit a 32-bit branch. */
ae57792d
HPN
1388 char *cond_jump
1389 = frag_more ((cris_arch == arch_crisv32
d2aa3f9f
HPN
1390 || cris_arch == arch_cris_common_v10_v32
1391 || pic)
ae57792d 1392 ? 12 : 10);
08caf3f8 1393
08caf3f8 1394 gen_cond_branch_32 (opcodep, cond_jump, frag_now,
3bcbcc3d 1395 output_instruction.expr.X_add_symbol,
47926f60 1396 (symbolS *) NULL,
3bcbcc3d
HPN
1397 output_instruction.expr.X_add_number);
1398 }
1399 }
1048a9ba
HPN
1400 else if (output_instruction.insn_type == CRIS_INSN_MUL
1401 && err_for_dangerous_mul_placement)
1402 /* Create a frag which which we track the location of the mul insn
1403 (in the last two bytes before the mul-frag). */
1404 frag_variant (rs_machine_dependent, 0, 0,
1405 ENCODE_RELAX (STATE_MUL, STATE_BYTE),
1406 NULL, 0, opcodep);
3bcbcc3d
HPN
1407 else
1408 {
1409 if (output_instruction.imm_oprnd_size > 0)
1410 {
2d2255b5 1411 /* The instruction has an immediate operand. */
08caf3f8 1412 enum bfd_reloc_code_real reloc = BFD_RELOC_NONE;
3bcbcc3d
HPN
1413
1414 switch (output_instruction.imm_oprnd_size)
1415 {
1416 /* Any byte-size immediate constants are treated as
1417 word-size. FIXME: Thus overflow check does not work
1418 correctly. */
1419
1420 case 2:
08caf3f8
HPN
1421 /* Note that size-check for the explicit reloc has already
1422 been done when we get here. */
1423 if (output_instruction.reloc != BFD_RELOC_NONE)
1424 reloc = output_instruction.reloc;
1425 else
1426 reloc = BFD_RELOC_16;
3bcbcc3d
HPN
1427 break;
1428
1429 case 4:
08caf3f8
HPN
1430 /* Allow a relocation specified in the operand. */
1431 if (output_instruction.reloc != BFD_RELOC_NONE)
1432 reloc = output_instruction.reloc;
1433 else
1434 reloc = BFD_RELOC_32;
3bcbcc3d
HPN
1435 break;
1436
1437 default:
1438 BAD_CASE (output_instruction.imm_oprnd_size);
1439 }
1440
1441 p = frag_more (output_instruction.imm_oprnd_size);
1442 fix_new_exp (frag_now, (p - frag_now->fr_literal),
1443 output_instruction.imm_oprnd_size,
ae57792d
HPN
1444 &output_instruction.expr,
1445 reloc == BFD_RELOC_32_PCREL
1446 || reloc == BFD_RELOC_16_PCREL
1447 || reloc == BFD_RELOC_8_PCREL, reloc);
1448 }
1449 else if (output_instruction.reloc == BFD_RELOC_CRIS_LAPCQ_OFFSET
1450 && output_instruction.expr.X_md != 0)
1451 {
1452 /* Handle complex expressions. */
1453 valueT addvalue
1454 = (output_instruction.expr.X_op_symbol != NULL
1455 ? 0 : output_instruction.expr.X_add_number);
1456 symbolS *sym
1457 = (output_instruction.expr.X_op_symbol != NULL
1458 ? make_expr_symbol (&output_instruction.expr)
1459 : output_instruction.expr.X_add_symbol);
1460
1461 /* This is a relaxing construct, so we need a frag_var rather
1462 than the fix_new_exp call below. */
1463 frag_var (rs_machine_dependent,
1464 4, 0,
1465 ENCODE_RELAX (STATE_LAPC, STATE_UNDF),
1466 sym, addvalue, opcodep);
3bcbcc3d
HPN
1467 }
1468 else if (output_instruction.reloc != BFD_RELOC_NONE)
1469 {
1470 /* An immediate operand that has a relocation and needs to be
47926f60 1471 processed further. */
3bcbcc3d
HPN
1472
1473 /* It is important to use fix_new_exp here and everywhere else
1474 (and not fix_new), as fix_new_exp can handle "difference
1475 expressions" - where the expression contains a difference of
1476 two symbols in the same segment. */
1477 fix_new_exp (frag_now, (opcodep - frag_now->fr_literal), 2,
ae57792d
HPN
1478 &output_instruction.expr,
1479 output_instruction.reloc == BFD_RELOC_32_PCREL
1480 || output_instruction.reloc == BFD_RELOC_16_PCREL
1481 || output_instruction.reloc == BFD_RELOC_8_PCREL
1482 || (output_instruction.reloc
1483 == BFD_RELOC_CRIS_LAPCQ_OFFSET),
3bcbcc3d
HPN
1484 output_instruction.reloc);
1485 }
1486 }
1487}
1488
29cf29a2
HPN
1489/* Helper error-reporting function: calls as_bad for a format string
1490 for a single value and zeroes the offending value (zero assumed
1491 being a valid value) to avoid repeated error reports in later value
1492 checking. */
1493
1494static void
1495cris_bad (const char *format, offsetT *valp)
1496{
1497 /* We cast to long so the format string can assume that format. */
1498 as_bad (format, (long) *valp);
1499 *valp = 0;
1500}
1501
3bcbcc3d 1502/* Low level text-to-bits assembly. */
47926f60 1503
3bcbcc3d 1504static void
695a4822
HPN
1505cris_process_instruction (char *insn_text, struct cris_instruction *out_insnp,
1506 struct cris_prefix *prefixp)
3bcbcc3d 1507{
47926f60
KH
1508 char *s;
1509 char modified_char = 0;
1510 const char *args;
3bcbcc3d 1511 struct cris_opcode *instruction;
47926f60
KH
1512 char *operands;
1513 int match = 0;
1514 int mode;
1515 int regno;
1516 int size_bits;
3bcbcc3d
HPN
1517
1518 /* Reset these fields to a harmless state in case we need to return in
1519 error. */
1520 prefixp->kind = PREFIX_NONE;
1521 prefixp->reloc = BFD_RELOC_NONE;
ae57792d 1522 out_insnp->insn_type = CRIS_INSN_NONE;
3bcbcc3d
HPN
1523 out_insnp->imm_oprnd_size = 0;
1524
1525 /* Find the end of the opcode mnemonic. We assume (true in 2.9.1)
1526 that the caller has translated the opcode to lower-case, up to the
47926f60 1527 first non-letter. */
3882b010 1528 for (operands = insn_text; ISLOWER (*operands); ++operands)
3bcbcc3d
HPN
1529 ;
1530
1531 /* Terminate the opcode after letters, but save the character there if
1532 it was of significance. */
1533 switch (*operands)
1534 {
1535 case '\0':
1536 break;
1537
1538 case '.':
47926f60 1539 /* Put back the modified character later. */
3bcbcc3d 1540 modified_char = *operands;
47926f60 1541 /* Fall through. */
3bcbcc3d
HPN
1542
1543 case ' ':
47926f60
KH
1544 /* Consume the character after the mnemonic
1545 and replace it with '\0'. */
3bcbcc3d
HPN
1546 *operands++ = '\0';
1547 break;
1548
1549 default:
1550 as_bad (_("Unknown opcode: `%s'"), insn_text);
1551 return;
1552 }
1553
1554 /* Find the instruction. */
629310ab 1555 instruction = (struct cris_opcode *) str_hash_find (op_hash, insn_text);
3bcbcc3d
HPN
1556 if (instruction == NULL)
1557 {
1558 as_bad (_("Unknown opcode: `%s'"), insn_text);
1559 return;
1560 }
1561
1562 /* Put back the modified character. */
1563 switch (modified_char)
1564 {
1565 case 0:
1566 break;
1567
1568 default:
1569 *--operands = modified_char;
1570 }
1571
3bcbcc3d 1572 /* Try to match an opcode table slot. */
47926f60 1573 for (s = operands;;)
3bcbcc3d 1574 {
47926f60 1575 int imm_expr_found;
3bcbcc3d
HPN
1576
1577 /* Initialize *prefixp, perhaps after being modified for a
47926f60 1578 "near match". */
3bcbcc3d
HPN
1579 prefixp->kind = PREFIX_NONE;
1580 prefixp->reloc = BFD_RELOC_NONE;
1581
1582 /* Initialize *out_insnp. */
1583 memset (out_insnp, 0, sizeof (*out_insnp));
1584 out_insnp->opcode = instruction->match;
1585 out_insnp->reloc = BFD_RELOC_NONE;
1586 out_insnp->insn_type = CRIS_INSN_NORMAL;
1587 out_insnp->imm_oprnd_size = 0;
1588
1589 imm_expr_found = 0;
1590
1591 /* Build the opcode, checking as we go to make sure that the
1592 operands match. */
47926f60 1593 for (args = instruction->args;; ++args)
3bcbcc3d
HPN
1594 {
1595 switch (*args)
1596 {
1597 case '\0':
1598 /* If we've come to the end of arguments, we're done. */
1599 if (*s == '\0')
1600 match = 1;
1601 break;
1602
1603 case '!':
1604 /* Non-matcher character for disassembly.
1605 Ignore it here. */
1606 continue;
1607
ae57792d
HPN
1608 case '[':
1609 case ']':
3bcbcc3d
HPN
1610 case ',':
1611 case ' ':
1612 /* These must match exactly. */
1613 if (*s++ == *args)
1614 continue;
1615 break;
1616
ae57792d
HPN
1617 case 'A':
1618 /* "ACR", case-insensitive.
1619 Handle a sometimes-mandatory dollar sign as register
1620 prefix. */
1621 if (*s == REGISTER_PREFIX_CHAR)
1622 s++;
1623 else if (demand_register_prefix)
1624 break;
1625
1626 if ((*s++ != 'a' && s[-1] != 'A')
1627 || (*s++ != 'c' && s[-1] != 'C')
1628 || (*s++ != 'r' && s[-1] != 'R'))
1629 break;
1630 continue;
1631
3bcbcc3d
HPN
1632 case 'B':
1633 /* This is not really an operand, but causes a "BDAP
47926f60 1634 -size,SP" prefix to be output, for PUSH instructions. */
3bcbcc3d
HPN
1635 prefixp->kind = PREFIX_PUSH;
1636 continue;
1637
1638 case 'b':
1639 /* This letter marks an operand that should not be matched
1640 in the assembler. It is a branch with 16-bit
1641 displacement. The assembler will create them from the
1642 8-bit flavor when necessary. The assembler does not
1643 support the [rN+] operand, as the [r15+] that is
1644 generated for 16-bit displacements. */
1645 break;
1646
1647 case 'c':
1648 /* A 5-bit unsigned immediate in bits <4:0>. */
1649 if (! cris_get_expression (&s, &out_insnp->expr))
1650 break;
1651 else
1652 {
1653 if (out_insnp->expr.X_op == O_constant
1654 && (out_insnp->expr.X_add_number < 0
1655 || out_insnp->expr.X_add_number > 31))
29cf29a2
HPN
1656 cris_bad (_("Immediate value not in 5 bit unsigned range: %ld"),
1657 &out_insnp->expr.X_add_number);
3bcbcc3d
HPN
1658
1659 out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_5;
1660 continue;
1661 }
1662
1663 case 'C':
1664 /* A 4-bit unsigned immediate in bits <3:0>. */
1665 if (! cris_get_expression (&s, &out_insnp->expr))
1666 break;
1667 else
1668 {
1669 if (out_insnp->expr.X_op == O_constant
1670 && (out_insnp->expr.X_add_number < 0
1671 || out_insnp->expr.X_add_number > 15))
29cf29a2
HPN
1672 cris_bad (_("Immediate value not in 4 bit unsigned range: %ld"),
1673 &out_insnp->expr.X_add_number);
3bcbcc3d
HPN
1674
1675 out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_4;
1676 continue;
1677 }
1678
ae57792d
HPN
1679 /* For 'd', check for an optional ".d" or ".D" at the
1680 start of the operands, followed by a space character. */
1681 case 'd':
1682 if (modified_char == '.' && *s == '.')
1683 {
1684 if ((s[1] != 'd' && s[1] == 'D')
1685 || ! ISSPACE (s[2]))
1686 break;
1687 s += 2;
1688 continue;
1689 }
1690 continue;
1691
3bcbcc3d
HPN
1692 case 'D':
1693 /* General register in bits <15:12> and <3:0>. */
1694 if (! get_gen_reg (&s, &regno))
1695 break;
1696 else
1697 {
1698 out_insnp->opcode |= regno /* << 0 */;
1699 out_insnp->opcode |= regno << 12;
1700 continue;
1701 }
1702
1703 case 'f':
1704 /* Flags from the condition code register. */
1705 {
1706 int flags = 0;
1707
1708 if (! get_flags (&s, &flags))
1709 break;
1710
47926f60 1711 out_insnp->opcode |= ((flags & 0xf0) << 8) | (flags & 0xf);
3bcbcc3d
HPN
1712 continue;
1713 }
1714
1715 case 'i':
1716 /* A 6-bit signed immediate in bits <5:0>. */
1717 if (! cris_get_expression (&s, &out_insnp->expr))
1718 break;
1719 else
1720 {
1721 if (out_insnp->expr.X_op == O_constant
1722 && (out_insnp->expr.X_add_number < -32
1723 || out_insnp->expr.X_add_number > 31))
29cf29a2
HPN
1724 cris_bad (_("Immediate value not in 6 bit range: %ld"),
1725 &out_insnp->expr.X_add_number);
1726
3bcbcc3d
HPN
1727 out_insnp->reloc = BFD_RELOC_CRIS_SIGNED_6;
1728 continue;
1729 }
1730
1731 case 'I':
1732 /* A 6-bit unsigned immediate in bits <5:0>. */
1733 if (! cris_get_expression (&s, &out_insnp->expr))
1734 break;
1735 else
1736 {
1737 if (out_insnp->expr.X_op == O_constant
1738 && (out_insnp->expr.X_add_number < 0
1739 || out_insnp->expr.X_add_number > 63))
29cf29a2
HPN
1740 cris_bad (_("Immediate value not in 6 bit unsigned range: %ld"),
1741 &out_insnp->expr.X_add_number);
1742
3bcbcc3d
HPN
1743 out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_6;
1744 continue;
1745 }
1746
1747 case 'M':
1748 /* A size modifier, B, W or D, to be put in a bit position
1749 suitable for CLEAR instructions (i.e. reflecting a zero
1750 register). */
1751 if (! get_bwd_size_modifier (&s, &size_bits))
1752 break;
1753 else
1754 {
1755 switch (size_bits)
1756 {
1757 case 0:
1758 out_insnp->opcode |= 0 << 12;
1759 break;
1760
1761 case 1:
1762 out_insnp->opcode |= 4 << 12;
1763 break;
1764
1765 case 2:
1766 out_insnp->opcode |= 8 << 12;
1767 break;
1768 }
1769 continue;
1770 }
1771
1772 case 'm':
1773 /* A size modifier, B, W or D, to be put in bits <5:4>. */
ae57792d
HPN
1774 if (modified_char != '.'
1775 || ! get_bwd_size_modifier (&s, &size_bits))
3bcbcc3d
HPN
1776 break;
1777 else
1778 {
1779 out_insnp->opcode |= size_bits << 4;
1780 continue;
1781 }
1782
1783 case 'o':
1784 /* A branch expression. */
1785 if (! cris_get_expression (&s, &out_insnp->expr))
1786 break;
1787 else
1788 {
1789 out_insnp->insn_type = CRIS_INSN_BRANCH;
1790 continue;
1791 }
1792
ae57792d
HPN
1793 case 'Q':
1794 /* A 8-bit quick BDAP expression, "expr,R". */
1795 if (! cris_get_expression (&s, &out_insnp->expr))
1796 break;
1797
1798 if (*s != ',')
1799 break;
1800
1801 s++;
1802
1803 if (!get_gen_reg (&s, &regno))
1804 break;
1805
1806 out_insnp->opcode |= regno << 12;
1807 out_insnp->reloc = BFD_RELOC_CRIS_SIGNED_8;
1808 continue;
3739860c 1809
3bcbcc3d 1810 case 'O':
ae57792d 1811 /* A BDAP expression for any size, "expr,R". */
3bcbcc3d
HPN
1812 if (! cris_get_expression (&s, &prefixp->expr))
1813 break;
1814 else
1815 {
1816 if (*s != ',')
1817 break;
1818
1819 s++;
1820
1821 if (!get_gen_reg (&s, &prefixp->base_reg_number))
1822 break;
1823
1824 /* Since 'O' is used with an explicit bdap, we have no
47926f60 1825 "real" instruction. */
3bcbcc3d 1826 prefixp->kind = PREFIX_BDAP_IMM;
08caf3f8
HPN
1827 prefixp->opcode
1828 = BDAP_QUICK_OPCODE | (prefixp->base_reg_number << 12);
1829
3bcbcc3d
HPN
1830 out_insnp->insn_type = CRIS_INSN_NONE;
1831 continue;
1832 }
1833
1834 case 'P':
1835 /* Special register in bits <15:12>. */
1836 if (! get_spec_reg (&s, &out_insnp->spec_reg))
1837 break;
1838 else
1839 {
1840 /* Use of some special register names come with a
1841 specific warning. Note that we have no ".cpu type"
1842 pseudo yet, so some of this is just unused
1843 framework. */
1844 if (out_insnp->spec_reg->warning)
20203fb9 1845 as_warn ("%s", out_insnp->spec_reg->warning);
3bcbcc3d
HPN
1846 else if (out_insnp->spec_reg->applicable_version
1847 == cris_ver_warning)
1848 /* Others have a generic warning. */
1849 as_warn (_("Unimplemented register `%s' specified"),
1850 out_insnp->spec_reg->name);
1851
1852 out_insnp->opcode
1853 |= out_insnp->spec_reg->number << 12;
1854 continue;
1855 }
1856
1857 case 'p':
1858 /* This character is used in the disassembler to
1859 recognize a prefix instruction to fold into the
1860 addressing mode for the next instruction. It is
47926f60 1861 ignored here. */
3bcbcc3d
HPN
1862 continue;
1863
1864 case 'R':
1865 /* General register in bits <15:12>. */
1866 if (! get_gen_reg (&s, &regno))
1867 break;
1868 else
1869 {
1870 out_insnp->opcode |= regno << 12;
1871 continue;
1872 }
1873
1874 case 'r':
1875 /* General register in bits <3:0>. */
1876 if (! get_gen_reg (&s, &regno))
1877 break;
1878 else
1879 {
1880 out_insnp->opcode |= regno /* << 0 */;
1881 continue;
1882 }
1883
1884 case 'S':
1885 /* Source operand in bit <10> and a prefix; a 3-operand
1886 prefix. */
1887 if (! get_3op_or_dip_prefix_op (&s, prefixp))
1888 break;
1889 else
1890 continue;
1891
1892 case 's':
1893 /* Source operand in bits <10>, <3:0> and optionally a
1894 prefix; i.e. an indirect operand or an side-effect
ae57792d 1895 prefix (where valid). */
3bcbcc3d
HPN
1896 if (! get_autoinc_prefix_or_indir_op (&s, prefixp, &mode,
1897 &regno,
1898 &imm_expr_found,
1899 &out_insnp->expr))
1900 break;
1901 else
1902 {
1903 if (prefixp->kind != PREFIX_NONE)
1904 {
1905 /* A prefix, so it has the autoincrement bit
1906 set. */
1907 out_insnp->opcode |= (AUTOINCR_BIT << 8);
1908 }
1909 else
08caf3f8
HPN
1910 {
1911 /* No prefix. The "mode" variable contains bits like
1912 whether or not this is autoincrement mode. */
1913 out_insnp->opcode |= (mode << 10);
1914
cc99daad 1915 /* If there was a reloc specifier, then it was
08caf3f8
HPN
1916 attached to the prefix. Note that we can't check
1917 that the reloc size matches, since we don't have
1918 all the operands yet in all cases. */
1919 if (prefixp->reloc != BFD_RELOC_NONE)
1920 out_insnp->reloc = prefixp->reloc;
1921 }
3bcbcc3d
HPN
1922
1923 out_insnp->opcode |= regno /* << 0 */ ;
1924 continue;
1925 }
1926
ae57792d
HPN
1927 case 'N':
1928 case 'Y':
cc99daad
HPN
1929 /* Like 's', but immediate operand only. Also do not
1930 modify insn. There are no insns where an explicit reloc
ae57792d
HPN
1931 specifier makes sense. */
1932 if (cris_get_expression (&s, &out_insnp->expr))
1933 {
1934 imm_expr_found = 1;
1935 continue;
1936 }
1937 break;
1938
1939 case 'n':
1940 /* Like 'N', but PC-relative to the start of the insn.
1941 There might be a :PLT to request a PLT entry. */
1942 if (cris_get_expression (&s, &out_insnp->expr))
1943 {
1944 imm_expr_found = 1;
1945 out_insnp->reloc = BFD_RELOC_32_PCREL;
1946
1947 /* We have to adjust the expression, because that
1948 relocation is to the location *after* the
1949 relocation. So add 2 for the insn and 4 for the
1950 relocation. */
1951 out_insnp->expr.X_add_number += 6;
1952
cc99daad
HPN
1953 /* TLS specifiers do not make sense here. */
1954 if (pic && *s == RELOC_SUFFIX_CHAR)
1955 cris_get_reloc_suffix (&s, &out_insnp->reloc,
1956 &out_insnp->expr);
ae57792d
HPN
1957
1958 continue;
1959 }
1960 break;
1961
1962 case 'U':
1963 /* Maybe 'u', maybe 'n'. Only for LAPC/LAPCQ. */
1964 if (cris_get_expression (&s, &out_insnp->expr))
1965 {
1966 out_insnp->reloc = BFD_RELOC_CRIS_LAPCQ_OFFSET;
1967
1968 /* Define 1 as relaxing. */
1969 out_insnp->expr.X_md = 1;
1970 continue;
1971 }
1972 break;
1973
1974 case 'u':
1975 /* Four PC-relative bits in <3:0> representing <4:1>:0 of
1976 an offset relative to the beginning of the current
1977 insn. */
1978 if (cris_get_expression (&s, &out_insnp->expr))
1979 {
1980 out_insnp->reloc = BFD_RELOC_CRIS_LAPCQ_OFFSET;
1981
1982 /* Define 0 as non-relaxing. */
1983 out_insnp->expr.X_md = 0;
1984
1985 /* We have to adjust the expression, because that
1986 relocation is to the location *after* the
1987 insn. So add 2 for the insn. */
1988 out_insnp->expr.X_add_number += 2;
1989 continue;
1990 }
1991 break;
1992
3bcbcc3d
HPN
1993 case 'x':
1994 /* Rs.m in bits <15:12> and <5:4>. */
1995 if (! get_gen_reg (&s, &regno)
1996 || ! get_bwd_size_modifier (&s, &size_bits))
1997 break;
1998 else
1999 {
47926f60 2000 out_insnp->opcode |= (regno << 12) | (size_bits << 4);
3bcbcc3d
HPN
2001 continue;
2002 }
2003
2004 case 'y':
2005 /* Source operand in bits <10>, <3:0> and optionally a
2006 prefix; i.e. an indirect operand or an side-effect
2007 prefix.
2008
2009 The difference to 's' is that this does not allow an
81d4177b 2010 "immediate" expression. */
3bcbcc3d
HPN
2011 if (! get_autoinc_prefix_or_indir_op (&s, prefixp,
2012 &mode, &regno,
2013 &imm_expr_found,
2014 &out_insnp->expr)
2015 || imm_expr_found)
2016 break;
2017 else
2018 {
2019 if (prefixp->kind != PREFIX_NONE)
2020 {
2021 /* A prefix, and those matched here always have
2022 side-effects (see 's' case). */
2023 out_insnp->opcode |= (AUTOINCR_BIT << 8);
2024 }
2025 else
2026 {
2027 /* No prefix. The "mode" variable contains bits
2028 like whether or not this is autoincrement
2029 mode. */
2030 out_insnp->opcode |= (mode << 10);
2031 }
2032
2033 out_insnp->opcode |= regno /* << 0 */;
2034 continue;
2035 }
2036
2037 case 'z':
2038 /* Size modifier (B or W) in bit <4>. */
2039 if (! get_bw_size_modifier (&s, &size_bits))
2040 break;
2041 else
2042 {
2043 out_insnp->opcode |= size_bits << 4;
2044 continue;
2045 }
2046
ae57792d
HPN
2047 case 'T':
2048 if (cris_arch == arch_crisv32
2049 && get_sup_reg (&s, &regno))
2050 {
2051 out_insnp->opcode |= regno << 12;
2052 continue;
2053 }
2054 break;
2055
3bcbcc3d
HPN
2056 default:
2057 BAD_CASE (*args);
2058 }
2059
2060 /* We get here when we fail a match above or we found a
2061 complete match. Break out of this loop. */
2062 break;
2063 }
2064
2065 /* Was it a match or a miss? */
2066 if (match == 0)
2067 {
2068 /* If it's just that the args don't match, maybe the next
2069 item in the table is the same opcode but with
ae57792d
HPN
2070 matching operands. First skip any invalid ones. */
2071 while (instruction[1].name != NULL
2072 && strcmp (instruction->name, instruction[1].name) == 0
2073 && ! cris_insn_ver_valid_for_arch (instruction[1]
2074 .applicable_version,
2075 cris_arch))
2076 ++instruction;
2077
3bcbcc3d 2078 if (instruction[1].name != NULL
ae57792d
HPN
2079 && strcmp (instruction->name, instruction[1].name) == 0
2080 && cris_insn_ver_valid_for_arch (instruction[1]
2081 .applicable_version,
2082 cris_arch))
3bcbcc3d
HPN
2083 {
2084 /* Yep. Restart and try that one instead. */
2085 ++instruction;
2086 s = operands;
2087 continue;
2088 }
2089 else
2090 {
2091 /* We've come to the end of instructions with this
2092 opcode, so it must be an error. */
2093 as_bad (_("Illegal operands"));
ae57792d
HPN
2094
2095 /* As discard_rest_of_line, but without continuing to the
2096 next line. */
2097 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2098 input_line_pointer++;
3bcbcc3d
HPN
2099 return;
2100 }
2101 }
2102 else
2103 {
2104 /* We have a match. Check if there's anything more to do. */
2105 if (imm_expr_found)
2106 {
2107 /* There was an immediate mode operand, so we must check
2108 that it has an appropriate size. */
3bcbcc3d
HPN
2109 switch (instruction->imm_oprnd_size)
2110 {
2111 default:
2112 case SIZE_NONE:
2113 /* Shouldn't happen; this one does not have immediate
2114 operands with different sizes. */
2115 BAD_CASE (instruction->imm_oprnd_size);
2116 break;
2117
2118 case SIZE_FIX_32:
2119 out_insnp->imm_oprnd_size = 4;
2120 break;
2121
2122 case SIZE_SPEC_REG:
ae57792d
HPN
2123 if (cris_arch == arch_crisv32)
2124 /* All immediate loads of special registers are
2125 32-bit on CRISv32. */
2126 out_insnp->imm_oprnd_size = 4;
2127 else
2128 switch (out_insnp->spec_reg->reg_size)
2129 {
2130 case 1:
2131 if (out_insnp->expr.X_op == O_constant
2132 && (out_insnp->expr.X_add_number < -128
2133 || out_insnp->expr.X_add_number > 255))
29cf29a2
HPN
2134 cris_bad (_("Immediate value not in 8 bit range: %ld"),
2135 &out_insnp->expr.X_add_number);
ae57792d
HPN
2136 /* Fall through. */
2137 case 2:
2138 /* FIXME: We need an indicator in the instruction
2139 table to pass on, to indicate if we need to check
2140 overflow for a signed or unsigned number. */
2141 if (out_insnp->expr.X_op == O_constant
2142 && (out_insnp->expr.X_add_number < -32768
2143 || out_insnp->expr.X_add_number > 65535))
29cf29a2
HPN
2144 cris_bad (_("Immediate value not in 16 bit range: %ld"),
2145 &out_insnp->expr.X_add_number);
ae57792d
HPN
2146 out_insnp->imm_oprnd_size = 2;
2147 break;
2148
2149 case 4:
2150 out_insnp->imm_oprnd_size = 4;
2151 break;
2152
2153 default:
2154 BAD_CASE (out_insnp->spec_reg->reg_size);
2155 }
3bcbcc3d
HPN
2156 break;
2157
2158 case SIZE_FIELD:
ae57792d
HPN
2159 case SIZE_FIELD_SIGNED:
2160 case SIZE_FIELD_UNSIGNED:
3bcbcc3d
HPN
2161 switch (size_bits)
2162 {
ae57792d
HPN
2163 /* FIXME: Find way to pass un/signedness to
2164 caller, and set reloc type instead, postponing
2165 this check until cris_number_to_imm. That
2166 necessarily corrects the reloc type for the
2167 byte case, maybe requiring further changes. */
3bcbcc3d 2168 case 0:
ae57792d
HPN
2169 if (out_insnp->expr.X_op == O_constant)
2170 {
2171 if (instruction->imm_oprnd_size == SIZE_FIELD
2172 && (out_insnp->expr.X_add_number < -128
2173 || out_insnp->expr.X_add_number > 255))
29cf29a2
HPN
2174 cris_bad (_("Immediate value not in 8 bit range: %ld"),
2175 &out_insnp->expr.X_add_number);
ae57792d
HPN
2176 else if (instruction->imm_oprnd_size == SIZE_FIELD_SIGNED
2177 && (out_insnp->expr.X_add_number < -128
2178 || out_insnp->expr.X_add_number > 127))
29cf29a2
HPN
2179 cris_bad (_("Immediate value not in 8 bit signed range: %ld"),
2180 &out_insnp->expr.X_add_number);
ae57792d
HPN
2181 else if (instruction->imm_oprnd_size == SIZE_FIELD_UNSIGNED
2182 && (out_insnp->expr.X_add_number < 0
2183 || out_insnp->expr.X_add_number > 255))
29cf29a2
HPN
2184 cris_bad (_("Immediate value not in 8 bit unsigned range: %ld"),
2185 &out_insnp->expr.X_add_number);
ae57792d
HPN
2186 }
2187
47926f60 2188 /* Fall through. */
3bcbcc3d 2189 case 1:
ae57792d
HPN
2190 if (out_insnp->expr.X_op == O_constant)
2191 {
2192 if (instruction->imm_oprnd_size == SIZE_FIELD
2193 && (out_insnp->expr.X_add_number < -32768
2194 || out_insnp->expr.X_add_number > 65535))
29cf29a2
HPN
2195 cris_bad (_("Immediate value not in 16 bit range: %ld"),
2196 &out_insnp->expr.X_add_number);
ae57792d
HPN
2197 else if (instruction->imm_oprnd_size == SIZE_FIELD_SIGNED
2198 && (out_insnp->expr.X_add_number < -32768
2199 || out_insnp->expr.X_add_number > 32767))
29cf29a2
HPN
2200 cris_bad (_("Immediate value not in 16 bit signed range: %ld"),
2201 &out_insnp->expr.X_add_number);
ae57792d
HPN
2202 else if (instruction->imm_oprnd_size == SIZE_FIELD_UNSIGNED
2203 && (out_insnp->expr.X_add_number < 0
2204 || out_insnp->expr.X_add_number > 65535))
29cf29a2
HPN
2205 cris_bad (_("Immediate value not in 16 bit unsigned range: %ld"),
2206 &out_insnp->expr.X_add_number);
ae57792d 2207 }
3bcbcc3d
HPN
2208 out_insnp->imm_oprnd_size = 2;
2209 break;
2210
2211 case 2:
2212 out_insnp->imm_oprnd_size = 4;
2213 break;
2214
2215 default:
2216 BAD_CASE (out_insnp->spec_reg->reg_size);
2217 }
2218 }
08caf3f8
HPN
2219
2220 /* If there was a relocation specified for the immediate
cc99daad
HPN
2221 expression (i.e. it had a PIC or TLS modifier) check that the
2222 size of the relocation matches the size specified by
08caf3f8
HPN
2223 the opcode. */
2224 if (out_insnp->reloc != BFD_RELOC_NONE
cc99daad 2225 && (cris_get_specified_reloc_size (out_insnp->reloc)
08caf3f8 2226 != (unsigned int) out_insnp->imm_oprnd_size))
cc99daad
HPN
2227 as_bad (out_insnp->reloc == BFD_RELOC_CRIS_32_GD
2228 || out_insnp->reloc == BFD_RELOC_CRIS_32_TPREL
2229 || out_insnp->reloc == BFD_RELOC_CRIS_16_TPREL
5a00ea25 2230 || out_insnp->reloc == BFD_RELOC_CRIS_32_IE
cc99daad
HPN
2231 ? _("TLS relocation size does not match operand size")
2232 : _("PIC relocation size does not match operand size"));
3bcbcc3d 2233 }
1048a9ba
HPN
2234 else if (instruction->op == cris_muls_op
2235 || instruction->op == cris_mulu_op)
2236 out_insnp->insn_type = CRIS_INSN_MUL;
3bcbcc3d
HPN
2237 }
2238 break;
2239 }
2240}
2241
3bcbcc3d
HPN
2242/* Get a B, W, or D size modifier from the string pointed out by *cPP,
2243 which must point to a '.' in front of the modifier. On successful
2244 return, *cPP is advanced to the character following the size
2245 modifier, and is undefined otherwise.
2246
2247 cPP Pointer to pointer to string starting
2248 with the size modifier.
2249
2250 size_bitsp Pointer to variable to contain the size bits on
2251 successful return.
2252
2253 Return 1 iff a correct size modifier is found, else 0. */
2254
2255static int
695a4822 2256get_bwd_size_modifier (char **cPP, int *size_bitsp)
3bcbcc3d
HPN
2257{
2258 if (**cPP != '.')
2259 return 0;
2260 else
2261 {
47926f60 2262 /* Consume the '.'. */
3bcbcc3d
HPN
2263 (*cPP)++;
2264
2265 switch (**cPP)
2266 {
2267 case 'B':
2268 case 'b':
2269 *size_bitsp = 0;
2270 break;
2271
2272 case 'W':
2273 case 'w':
2274 *size_bitsp = 1;
2275 break;
2276
2277 case 'D':
2278 case 'd':
2279 *size_bitsp = 2;
2280 break;
2281
2282 default:
2283 return 0;
2284 }
2285
2286 /* Consume the size letter. */
2287 (*cPP)++;
2288 return 1;
2289 }
2290}
2291
3bcbcc3d
HPN
2292/* Get a B or W size modifier from the string pointed out by *cPP,
2293 which must point to a '.' in front of the modifier. On successful
2294 return, *cPP is advanced to the character following the size
2295 modifier, and is undefined otherwise.
2296
2297 cPP Pointer to pointer to string starting
2298 with the size modifier.
2299
2300 size_bitsp Pointer to variable to contain the size bits on
2301 successful return.
2302
2303 Return 1 iff a correct size modifier is found, else 0. */
2304
2305static int
695a4822 2306get_bw_size_modifier (char **cPP, int *size_bitsp)
3bcbcc3d
HPN
2307{
2308 if (**cPP != '.')
2309 return 0;
2310 else
2311 {
47926f60 2312 /* Consume the '.'. */
3bcbcc3d
HPN
2313 (*cPP)++;
2314
2315 switch (**cPP)
2316 {
2317 case 'B':
2318 case 'b':
2319 *size_bitsp = 0;
2320 break;
2321
2322 case 'W':
2323 case 'w':
2324 *size_bitsp = 1;
2325 break;
2326
2327 default:
2328 return 0;
2329 }
2330
2331 /* Consume the size letter. */
2332 (*cPP)++;
2333 return 1;
2334 }
2335}
2336
07e90ad5 2337/* Get a general register from the string pointed out by *cPP. The
3bcbcc3d
HPN
2338 variable *cPP is advanced to the character following the general
2339 register name on a successful return, and has its initial position
2340 otherwise.
2341
2342 cPP Pointer to pointer to string, beginning with a general
2343 register name.
2344
2345 regnop Pointer to int containing the register number.
2346
2347 Return 1 iff a correct general register designator is found,
2348 else 0. */
2349
2350static int
695a4822 2351get_gen_reg (char **cPP, int *regnop)
3bcbcc3d
HPN
2352{
2353 char *oldp;
2354 oldp = *cPP;
2355
7b15d668
HPN
2356 /* Handle a sometimes-mandatory dollar sign as register prefix. */
2357 if (**cPP == REGISTER_PREFIX_CHAR)
2358 (*cPP)++;
2359 else if (demand_register_prefix)
2360 return 0;
2361
3bcbcc3d
HPN
2362 switch (**cPP)
2363 {
2364 case 'P':
2365 case 'p':
2366 /* "P" as in "PC"? Consume the "P". */
2367 (*cPP)++;
2368
2369 if ((**cPP == 'C' || **cPP == 'c')
ae57792d
HPN
2370 && ! ISALNUM ((*cPP)[1])
2371 /* Here's a little twist: For v32 and the compatibility mode,
2372 we only recognize PC as a register number if there's '+]'
2373 after. We don't consume that, but the presence can only be
2374 valid after a register in a post-increment context, which
2375 is also the only valid context for PC as a register for
2376 v32. Not that it's used very often, but saying "MOVE.D
2377 [PC+],R5" should remain valid. It's not supported for
2378 jump-type insns or other insns with no [Rn+] mode, though. */
2379 && ((cris_arch != arch_crisv32
2380 && cris_arch != arch_cris_common_v10_v32)
2381 || ((*cPP)[1] == '+' && (*cPP)[2] == ']')))
3bcbcc3d
HPN
2382 {
2383 /* It's "PC": consume the "c" and we're done. */
2384 (*cPP)++;
2385 *regnop = REG_PC;
2386 return 1;
2387 }
2388 break;
2389
ae57792d
HPN
2390 /* Like with PC, we recognize ACR, but only if it's *not* followed
2391 by '+', and only for v32. */
2392 case 'A':
2393 case 'a':
2394 if (cris_arch != arch_crisv32
2395 || ((*cPP)[1] != 'c' && (*cPP)[1] != 'C')
2396 || ((*cPP)[2] != 'r' && (*cPP)[2] != 'R')
2397 || ISALNUM ((*cPP)[3])
2398 || (*cPP)[3] == '+')
2399 break;
2400 (*cPP) += 3;
2401 *regnop = 15;
2402 return 1;
2403
3bcbcc3d
HPN
2404 case 'R':
2405 case 'r':
47926f60 2406 /* Hopefully r[0-9] or r1[0-5]. Consume 'R' or 'r'. */
3bcbcc3d
HPN
2407 (*cPP)++;
2408
3882b010 2409 if (ISDIGIT (**cPP))
3bcbcc3d
HPN
2410 {
2411 /* It's r[0-9]. Consume and check the next digit. */
2412 *regnop = **cPP - '0';
2413 (*cPP)++;
2414
3882b010 2415 if (! ISALNUM (**cPP))
3bcbcc3d 2416 {
47926f60 2417 /* No more digits, we're done. */
3bcbcc3d
HPN
2418 return 1;
2419 }
2420 else
2421 {
2422 /* One more digit. Consume and add. */
47926f60 2423 *regnop = *regnop * 10 + (**cPP - '0');
3bcbcc3d
HPN
2424
2425 /* We need to check for a valid register number; Rn,
2426 0 <= n <= MAX_REG. */
2427 if (*regnop <= MAX_REG)
2428 {
2429 /* Consume second digit. */
2430 (*cPP)++;
2431 return 1;
2432 }
2433 }
2434 }
2435 break;
2436
2437 case 'S':
2438 case 's':
2439 /* "S" as in "SP"? Consume the "S". */
2440 (*cPP)++;
2441 if (**cPP == 'P' || **cPP == 'p')
2442 {
2443 /* It's "SP": consume the "p" and we're done. */
2444 (*cPP)++;
2445 *regnop = REG_SP;
2446 return 1;
2447 }
2448 break;
2449
2450 default:
2451 /* Just here to silence compilation warnings. */
2452 ;
2453 }
2454
2455 /* We get here if we fail. Restore the pointer. */
2456 *cPP = oldp;
2457 return 0;
2458}
2459
3bcbcc3d
HPN
2460/* Get a special register from the string pointed out by *cPP. The
2461 variable *cPP is advanced to the character following the special
2462 register name if one is found, and retains its original position
2463 otherwise.
2464
2465 cPP Pointer to pointer to string starting with a special register
2466 name.
2467
2468 sregpp Pointer to Pointer to struct spec_reg, where a pointer to the
2469 register description will be stored.
2470
2471 Return 1 iff a correct special register name is found. */
2472
2473static int
695a4822 2474get_spec_reg (char **cPP, const struct cris_spec_reg **sregpp)
3bcbcc3d
HPN
2475{
2476 char *s1;
2477 const char *s2;
7b15d668 2478 char *name_begin = *cPP;
3bcbcc3d
HPN
2479
2480 const struct cris_spec_reg *sregp;
2481
7b15d668
HPN
2482 /* Handle a sometimes-mandatory dollar sign as register prefix. */
2483 if (*name_begin == REGISTER_PREFIX_CHAR)
2484 name_begin++;
2485 else if (demand_register_prefix)
2486 return 0;
2487
3bcbcc3d 2488 /* Loop over all special registers. */
47926f60 2489 for (sregp = cris_spec_regs; sregp->name != NULL; sregp++)
3bcbcc3d 2490 {
3bcbcc3d 2491 /* Start over from beginning of the supposed name. */
7b15d668 2492 s1 = name_begin;
3bcbcc3d
HPN
2493 s2 = sregp->name;
2494
3882b010 2495 while (*s2 != '\0' && TOLOWER (*s1) == *s2)
3bcbcc3d
HPN
2496 {
2497 s1++;
2498 s2++;
2499 }
2500
2501 /* For a match, we must have consumed the name in the table, and we
2502 must be outside what could be part of a name. Assume here that a
47926f60 2503 test for alphanumerics is sufficient for a name test. */
ae57792d
HPN
2504 if (*s2 == 0 && ! ISALNUM (*s1)
2505 && cris_insn_ver_valid_for_arch (sregp->applicable_version,
2506 cris_arch))
3bcbcc3d 2507 {
47926f60 2508 /* We have a match. Update the pointer and be done. */
3bcbcc3d
HPN
2509 *cPP = s1;
2510 *sregpp = sregp;
2511 return 1;
2512 }
2513 }
2514
47926f60 2515 /* If we got here, we did not find any name. */
3bcbcc3d
HPN
2516 return 0;
2517}
2518
ae57792d
HPN
2519/* Get a support register from the string pointed out by *cPP. The
2520 variable *cPP is advanced to the character following the support-
2521 register name if one is found, and retains its original position
2522 otherwise.
2523
2524 cPP Pointer to pointer to string starting with a support-register
2525 name.
2526
2527 sregpp Pointer to int containing the register number.
2528
2529 Return 1 iff a correct support-register name is found. */
2530
2531static int
695a4822 2532get_sup_reg (char **cPP, int *regnop)
ae57792d
HPN
2533{
2534 char *s1;
2535 const char *s2;
2536 char *name_begin = *cPP;
2537
2538 const struct cris_support_reg *sregp;
2539
2540 /* Handle a sometimes-mandatory dollar sign as register prefix. */
2541 if (*name_begin == REGISTER_PREFIX_CHAR)
2542 name_begin++;
2543 else if (demand_register_prefix)
2544 return 0;
2545
2546 /* Loop over all support-registers. */
2547 for (sregp = cris_support_regs; sregp->name != NULL; sregp++)
2548 {
2549 /* Start over from beginning of the supposed name. */
2550 s1 = name_begin;
2551 s2 = sregp->name;
2552
2553 while (*s2 != '\0' && TOLOWER (*s1) == *s2)
2554 {
2555 s1++;
2556 s2++;
2557 }
2558
2559 /* For a match, we must have consumed the name in the table, and we
2560 must be outside what could be part of a name. Assume here that a
2561 test for alphanumerics is sufficient for a name test. */
2562 if (*s2 == 0 && ! ISALNUM (*s1))
2563 {
2564 /* We have a match. Update the pointer and be done. */
2565 *cPP = s1;
2566 *regnop = sregp->number;
2567 return 1;
2568 }
2569 }
2570
2571 /* If we got here, we did not find any name. */
2572 return 0;
2573}
2574
3bcbcc3d
HPN
2575/* Get an unprefixed or side-effect-prefix operand from the string pointed
2576 out by *cPP. The pointer *cPP is advanced to the character following
2577 the indirect operand if we have success, else it contains an undefined
2578 value.
2579
2580 cPP Pointer to pointer to string beginning with the first
2581 character of the supposed operand.
2582
2583 prefixp Pointer to structure containing an optional instruction
2584 prefix.
2585
2586 is_autoincp Pointer to int indicating the indirect or autoincrement
2587 bits.
2588
2589 src_regnop Pointer to int containing the source register number in
2590 the instruction.
2591
2592 imm_foundp Pointer to an int indicating if an immediate expression
2593 is found.
2594
2595 imm_exprP Pointer to a structure containing an immediate
2596 expression, if success and if *imm_foundp is nonzero.
2597
2598 Return 1 iff a correct indirect operand is found. */
2599
2600static int
695a4822
HPN
2601get_autoinc_prefix_or_indir_op (char **cPP, struct cris_prefix *prefixp,
2602 int *is_autoincp, int *src_regnop,
2603 int *imm_foundp, expressionS *imm_exprP)
3bcbcc3d
HPN
2604{
2605 /* Assume there was no immediate mode expression. */
2606 *imm_foundp = 0;
2607
2608 if (**cPP == '[')
2609 {
2610 /* So this operand is one of:
2611 Indirect: [rN]
2612 Autoincrement: [rN+]
2613 Indexed with assign: [rN=rM+rO.S]
2614 Offset with assign: [rN=rM+I], [rN=rM+[rO].s], [rN=rM+[rO+].s]
2615
2616 Either way, consume the '['. */
2617 (*cPP)++;
2618
2619 /* Get the rN register. */
2620 if (! get_gen_reg (cPP, src_regnop))
2621 /* If there was no register, then this cannot match. */
2622 return 0;
2623 else
2624 {
2625 /* We got the register, now check the next character. */
2626 switch (**cPP)
2627 {
2628 case ']':
2629 /* Indirect mode. We're done here. */
2630 prefixp->kind = PREFIX_NONE;
2631 *is_autoincp = 0;
2632 break;
2633
2634 case '+':
2635 /* This must be an auto-increment mode, if there's a
2636 match. */
2637 prefixp->kind = PREFIX_NONE;
2638 *is_autoincp = 1;
2639
2640 /* We consume this character and break out to check the
2641 closing ']'. */
2642 (*cPP)++;
2643 break;
2644
2645 case '=':
2646 /* This must be indexed with assign, or offset with assign
ae57792d
HPN
2647 to match. Not supported for crisv32 or in
2648 compatibility mode. */
2649 if (cris_arch == arch_crisv32
2650 || cris_arch == arch_cris_common_v10_v32)
2651 return 0;
2652
3bcbcc3d
HPN
2653 (*cPP)++;
2654
2655 /* Either way, the next thing must be a register. */
2656 if (! get_gen_reg (cPP, &prefixp->base_reg_number))
2657 /* No register, no match. */
2658 return 0;
2659 else
2660 {
2661 /* We've consumed "[rN=rM", so we must be looking at
2662 "+rO.s]" or "+I]", or "-I]", or "+[rO].s]" or
2663 "+[rO+].s]". */
2664 if (**cPP == '+')
2665 {
2666 int index_reg_number;
2667 (*cPP)++;
2668
2669 if (**cPP == '[')
2670 {
2671 int size_bits;
2672 /* This must be [rx=ry+[rz].s] or
2673 [rx=ry+[rz+].s] or no match. We must be
2674 looking at rz after consuming the '['. */
2675 (*cPP)++;
2676
2677 if (!get_gen_reg (cPP, &index_reg_number))
2678 return 0;
2679
2680 prefixp->kind = PREFIX_BDAP;
2681 prefixp->opcode
2682 = (BDAP_INDIR_OPCODE
2683 + (prefixp->base_reg_number << 12)
2684 + index_reg_number);
2685
2686 if (**cPP == '+')
2687 {
2688 /* We've seen "[rx=ry+[rz+" here, so now we
2689 know that there must be "].s]" left to
2690 check. */
2691 (*cPP)++;
2692 prefixp->opcode |= AUTOINCR_BIT << 8;
2693 }
2694
2695 /* If it wasn't autoincrement, we don't need to
2696 add anything. */
2697
2698 /* Check the next-to-last ']'. */
2699 if (**cPP != ']')
2700 return 0;
2701
2702 (*cPP)++;
2703
2704 /* Check the ".s" modifier. */
2705 if (! get_bwd_size_modifier (cPP, &size_bits))
2706 return 0;
2707
2708 prefixp->opcode |= size_bits << 4;
2709
2710 /* Now we got [rx=ry+[rz+].s or [rx=ry+[rz].s.
2711 We break out to check the final ']'. */
2712 break;
2713 }
ab3e48dc
KH
2714 /* It wasn't an indirection. Check if it's a
2715 register. */
2716 else if (get_gen_reg (cPP, &index_reg_number))
47926f60
KH
2717 {
2718 int size_bits;
2719
2720 /* Indexed with assign mode: "[rN+rM.S]". */
2721 prefixp->kind = PREFIX_BIAP;
2722 prefixp->opcode
2723 = (BIAP_OPCODE + (index_reg_number << 12)
2724 + prefixp->base_reg_number /* << 0 */);
2725
2726 if (! get_bwd_size_modifier (cPP, &size_bits))
2727 /* Size missing, this isn't a match. */
2728 return 0;
2729 else
3bcbcc3d 2730 {
47926f60 2731 /* Size found, break out to check the
3bcbcc3d 2732 final ']'. */
47926f60 2733 prefixp->opcode |= size_bits << 4;
3bcbcc3d
HPN
2734 break;
2735 }
47926f60
KH
2736 }
2737 /* Not a register. Then this must be "[rN+I]". */
2738 else if (cris_get_expression (cPP, &prefixp->expr))
2739 {
2740 /* We've got offset with assign mode. Fill
2741 in the blanks and break out to match the
2742 final ']'. */
2743 prefixp->kind = PREFIX_BDAP_IMM;
08caf3f8
HPN
2744
2745 /* We tentatively put an opcode corresponding to
2746 a 32-bit operand here, although it may be
cc99daad
HPN
2747 relaxed when there's no relocation
2748 specifier for the operand. */
08caf3f8
HPN
2749 prefixp->opcode
2750 = (BDAP_INDIR_OPCODE
2751 | (prefixp->base_reg_number << 12)
2752 | (AUTOINCR_BIT << 8)
2753 | (2 << 4)
2754 | REG_PC /* << 0 */);
2755
2756 /* This can have a PIC suffix, specifying reloc
2757 type to use. */
cc99daad 2758 if ((pic || tls) && **cPP == RELOC_SUFFIX_CHAR)
08caf3f8
HPN
2759 {
2760 unsigned int relocsize;
2761
cc99daad
HPN
2762 cris_get_reloc_suffix (cPP, &prefixp->reloc,
2763 &prefixp->expr);
08caf3f8
HPN
2764
2765 /* Tweak the size of the immediate operand
2766 in the prefix opcode if it isn't what we
2767 set. */
2768 relocsize
cc99daad 2769 = cris_get_specified_reloc_size (prefixp->reloc);
08caf3f8
HPN
2770 if (relocsize != 4)
2771 prefixp->opcode
2772 = ((prefixp->opcode & ~(3 << 4))
2773 | ((relocsize >> 1) << 4));
2774 }
47926f60
KH
2775 break;
2776 }
2777 else
2778 /* Neither register nor expression found, so
2779 this can't be a match. */
2780 return 0;
3bcbcc3d 2781 }
47926f60 2782 /* Not "[rN+" but perhaps "[rN-"? */
ab3e48dc 2783 else if (**cPP == '-')
47926f60
KH
2784 {
2785 /* We must have an offset with assign mode. */
2786 if (! cris_get_expression (cPP, &prefixp->expr))
2787 /* No expression, no match. */
2788 return 0;
2789 else
2790 {
2791 /* We've got offset with assign mode. Fill
2792 in the blanks and break out to match the
08caf3f8
HPN
2793 final ']'.
2794
cc99daad
HPN
2795 Note that we don't allow a relocation
2796 suffix for an operand with a minus
2797 sign. */
47926f60
KH
2798 prefixp->kind = PREFIX_BDAP_IMM;
2799 break;
2800 }
2801 }
2802 else
2803 /* Neither '+' nor '-' after "[rN=rM". Lose. */
2804 return 0;
3bcbcc3d
HPN
2805 }
2806 default:
2807 /* Neither ']' nor '+' nor '=' after "[rN". Lose. */
2808 return 0;
2809 }
2810 }
2811
2812 /* When we get here, we have a match and will just check the closing
2813 ']'. We can still fail though. */
2814 if (**cPP != ']')
2815 return 0;
2816 else
2817 {
2818 /* Don't forget to consume the final ']'.
2819 Then return in glory. */
2820 (*cPP)++;
2821 return 1;
2822 }
2823 }
47926f60 2824 /* No indirection. Perhaps a constant? */
ab3e48dc 2825 else if (cris_get_expression (cPP, imm_exprP))
47926f60
KH
2826 {
2827 /* Expression found, this is immediate mode. */
2828 prefixp->kind = PREFIX_NONE;
2829 *is_autoincp = 1;
2830 *src_regnop = REG_PC;
2831 *imm_foundp = 1;
08caf3f8
HPN
2832
2833 /* This can have a PIC suffix, specifying reloc type to use. The
2834 caller must check that the reloc size matches the operand size. */
cc99daad
HPN
2835 if ((pic || tls) && **cPP == RELOC_SUFFIX_CHAR)
2836 cris_get_reloc_suffix (cPP, &prefixp->reloc, imm_exprP);
08caf3f8 2837
47926f60
KH
2838 return 1;
2839 }
3bcbcc3d
HPN
2840
2841 /* No luck today. */
2842 return 0;
2843}
2844
3bcbcc3d
HPN
2845/* This function gets an indirect operand in a three-address operand
2846 combination from the string pointed out by *cPP. The pointer *cPP is
2847 advanced to the character following the indirect operand on success, or
2848 has an unspecified value on failure.
2849
2d2255b5 2850 cPP Pointer to pointer to string beginning
3bcbcc3d
HPN
2851 with the operand
2852
2853 prefixp Pointer to structure containing an
2854 instruction prefix
2855
2856 Returns 1 iff a correct indirect operand is found. */
2857
2858static int
695a4822 2859get_3op_or_dip_prefix_op (char **cPP, struct cris_prefix *prefixp)
3bcbcc3d 2860{
ab3e48dc
KH
2861 int reg_number;
2862
3bcbcc3d
HPN
2863 if (**cPP != '[')
2864 /* We must have a '[' or it's a clean failure. */
2865 return 0;
3bcbcc3d 2866
47926f60
KH
2867 /* Eat the first '['. */
2868 (*cPP)++;
2869
2870 if (**cPP == '[')
2871 {
2872 /* A second '[', so this must be double-indirect mode. */
3bcbcc3d 2873 (*cPP)++;
47926f60
KH
2874 prefixp->kind = PREFIX_DIP;
2875 prefixp->opcode = DIP_OPCODE;
3bcbcc3d 2876
47926f60
KH
2877 /* Get the register or fail entirely. */
2878 if (! get_gen_reg (cPP, &reg_number))
2879 return 0;
2880 else
3bcbcc3d 2881 {
47926f60
KH
2882 prefixp->opcode |= reg_number /* << 0 */ ;
2883 if (**cPP == '+')
2884 {
2885 /* Since we found a '+', this must be double-indirect
2886 autoincrement mode. */
2887 (*cPP)++;
2888 prefixp->opcode |= AUTOINCR_BIT << 8;
2889 }
2890
2891 /* There's nothing particular to do, if this was a
2892 double-indirect *without* autoincrement. */
2893 }
2894
2895 /* Check the first ']'. The second one is checked at the end. */
2896 if (**cPP != ']')
2897 return 0;
2898
2899 /* Eat the first ']', so we'll be looking at a second ']'. */
2900 (*cPP)++;
2901 }
2902 /* No second '['. Then we should have a register here, making
2903 it "[rN". */
2904 else if (get_gen_reg (cPP, &prefixp->base_reg_number))
2905 {
2906 /* This must be indexed or offset mode: "[rN+I]" or
2907 "[rN+rM.S]" or "[rN+[rM].S]" or "[rN+[rM+].S]". */
2908 if (**cPP == '+')
2909 {
47926f60
KH
2910 int index_reg_number;
2911
3bcbcc3d 2912 (*cPP)++;
3bcbcc3d 2913
47926f60 2914 if (**cPP == '[')
3bcbcc3d 2915 {
47926f60
KH
2916 /* This is "[rx+["... Expect a register next. */
2917 int size_bits;
2918 (*cPP)++;
2919
2920 if (!get_gen_reg (cPP, &index_reg_number))
2921 return 0;
2922
2923 prefixp->kind = PREFIX_BDAP;
2924 prefixp->opcode
2925 = (BDAP_INDIR_OPCODE
2926 + (prefixp->base_reg_number << 12)
2927 + index_reg_number);
2928
2929 /* We've seen "[rx+[ry", so check if this is
2930 autoincrement. */
3bcbcc3d
HPN
2931 if (**cPP == '+')
2932 {
47926f60 2933 /* Yep, now at "[rx+[ry+". */
3bcbcc3d
HPN
2934 (*cPP)++;
2935 prefixp->opcode |= AUTOINCR_BIT << 8;
2936 }
47926f60
KH
2937 /* If it wasn't autoincrement, we don't need to
2938 add anything. */
3bcbcc3d 2939
47926f60
KH
2940 /* Check a first closing ']': "[rx+[ry]" or
2941 "[rx+[ry+]". */
2942 if (**cPP != ']')
2943 return 0;
2944 (*cPP)++;
3bcbcc3d 2945
47926f60
KH
2946 /* Now expect a size modifier ".S". */
2947 if (! get_bwd_size_modifier (cPP, &size_bits))
2948 return 0;
3bcbcc3d 2949
47926f60
KH
2950 prefixp->opcode |= size_bits << 4;
2951
2952 /* Ok, all interesting stuff has been seen:
2953 "[rx+[ry+].S" or "[rx+[ry].S". We only need to
2954 expect a final ']', which we'll do in a common
2955 closing session. */
2956 }
2957 /* Seen "[rN+", but not a '[', so check if we have a
2958 register. */
2959 else if (get_gen_reg (cPP, &index_reg_number))
2960 {
2961 /* This is indexed mode: "[rN+rM.S]" or
2962 "[rN+rM.S+]". */
2963 int size_bits;
2964 prefixp->kind = PREFIX_BIAP;
2965 prefixp->opcode
2966 = (BIAP_OPCODE
2967 | prefixp->base_reg_number /* << 0 */
2968 | (index_reg_number << 12));
2969
07e90ad5 2970 /* Consume the ".S". */
47926f60
KH
2971 if (! get_bwd_size_modifier (cPP, &size_bits))
2972 /* Missing size, so fail. */
2973 return 0;
3bcbcc3d 2974 else
47926f60
KH
2975 /* Size found. Add that piece and drop down to
2976 the common checking of the closing ']'. */
2977 prefixp->opcode |= size_bits << 4;
2978 }
2979 /* Seen "[rN+", but not a '[' or a register, so then
ae57792d
HPN
2980 it must be a constant "I".
2981
2982 As a quality of implementation improvement, we check for a
2983 closing ']', like in an erroneous "[rN+]". If we don't,
2984 the expression parser will emit a confusing "bad
2985 expression" when it sees the ']', probably because it
2986 doesn't like seeing no expression. */
2987 else if (**cPP != ']' && cris_get_expression (cPP, &prefixp->expr))
47926f60
KH
2988 {
2989 /* Expression found, so fill in the bits of offset
2990 mode and drop down to check the closing ']'. */
2991 prefixp->kind = PREFIX_BDAP_IMM;
08caf3f8
HPN
2992
2993 /* We tentatively put an opcode corresponding to a 32-bit
2994 operand here, although it may be relaxed when there's no
2995 PIC specifier for the operand. */
2996 prefixp->opcode
2997 = (BDAP_INDIR_OPCODE
2998 | (prefixp->base_reg_number << 12)
2999 | (AUTOINCR_BIT << 8)
3000 | (2 << 4)
3001 | REG_PC /* << 0 */);
3002
3003 /* This can have a PIC suffix, specifying reloc type to use. */
cc99daad 3004 if ((pic || tls) && **cPP == RELOC_SUFFIX_CHAR)
08caf3f8
HPN
3005 {
3006 unsigned int relocsize;
3007
cc99daad 3008 cris_get_reloc_suffix (cPP, &prefixp->reloc, &prefixp->expr);
08caf3f8
HPN
3009
3010 /* Tweak the size of the immediate operand in the prefix
3011 opcode if it isn't what we set. */
cc99daad 3012 relocsize = cris_get_specified_reloc_size (prefixp->reloc);
08caf3f8
HPN
3013 if (relocsize != 4)
3014 prefixp->opcode
3015 = ((prefixp->opcode & ~(3 << 4))
3016 | ((relocsize >> 1) << 4));
3017 }
47926f60
KH
3018 }
3019 else
3020 /* Nothing valid here: lose. */
3021 return 0;
3022 }
3023 /* Seen "[rN" but no '+', so check if it's a '-'. */
3024 else if (**cPP == '-')
3025 {
3026 /* Yep, we must have offset mode. */
3027 if (! cris_get_expression (cPP, &prefixp->expr))
3028 /* No expression, so we lose. */
3029 return 0;
3030 else
3031 {
3032 /* Expression found to make this offset mode, so
3033 fill those bits and drop down to check the
08caf3f8
HPN
3034 closing ']'.
3035
3036 Note that we don't allow a PIC suffix for
3037 an operand with a minus sign like this. */
47926f60
KH
3038 prefixp->kind = PREFIX_BDAP_IMM;
3039 }
3040 }
3041 else
3042 {
3043 /* We've seen "[rN", but not '+' or '-'; rather a ']'.
3044 Hmm. Normally this is a simple indirect mode that we
3045 shouldn't match, but if we expect ']', then we have a
3046 zero offset, so it can be a three-address-operand,
3047 like "[rN],rO,rP", thus offset mode.
3048
3049 Don't eat the ']', that will be done in the closing
3050 ceremony. */
3051 prefixp->expr.X_op = O_constant;
3052 prefixp->expr.X_add_number = 0;
3053 prefixp->expr.X_add_symbol = NULL;
3054 prefixp->expr.X_op_symbol = NULL;
3055 prefixp->kind = PREFIX_BDAP_IMM;
3056 }
3057 }
3058 /* A '[', but no second '[', and no register. Check if we
3059 have an expression, making this "[I]" for a double-indirect
3060 prefix. */
3061 else if (cris_get_expression (cPP, &prefixp->expr))
3062 {
3063 /* Expression found, the so called absolute mode for a
3064 double-indirect prefix on PC. */
3065 prefixp->kind = PREFIX_DIP;
3066 prefixp->opcode = DIP_OPCODE | (AUTOINCR_BIT << 8) | REG_PC;
3067 prefixp->reloc = BFD_RELOC_32;
5a00ea25
HPN
3068
3069 /* For :GD and :IE, it makes sense to have TLS specifiers here. */
3070 if ((pic || tls) && **cPP == RELOC_SUFFIX_CHAR)
3071 cris_get_reloc_suffix (cPP, &prefixp->reloc, &prefixp->expr);
47926f60
KH
3072 }
3073 else
3074 /* Neither '[' nor register nor expression. We lose. */
3075 return 0;
3bcbcc3d
HPN
3076
3077 /* We get here as a closing ceremony to a successful match. We just
3078 need to check the closing ']'. */
3079 if (**cPP != ']')
3080 /* Oops. Close but no air-polluter. */
3081 return 0;
3082
3083 /* Don't forget to consume that ']', before returning in glory. */
3084 (*cPP)++;
3085 return 1;
3086}
3087
3bcbcc3d
HPN
3088/* Get an expression from the string pointed out by *cPP.
3089 The pointer *cPP is advanced to the character following the expression
3090 on a success, or retains its original value otherwise.
3091
3092 cPP Pointer to pointer to string beginning with the expression.
3093
3094 exprP Pointer to structure containing the expression.
3095
47926f60 3096 Return 1 iff a correct expression is found. */
3bcbcc3d
HPN
3097
3098static int
695a4822 3099cris_get_expression (char **cPP, expressionS *exprP)
3bcbcc3d
HPN
3100{
3101 char *saved_input_line_pointer;
3bcbcc3d
HPN
3102
3103 /* The "expression" function expects to find an expression at the
3104 global variable input_line_pointer, so we have to save it to give
3105 the impression that we don't fiddle with global variables. */
3106 saved_input_line_pointer = input_line_pointer;
3107 input_line_pointer = *cPP;
3108
ae57792d
HPN
3109 /* Avoid a common error, confusing addressing modes. Beware that the
3110 call to expression below does not signal that error; it treats []
3111 as parentheses, unless #define NEED_INDEX_OPERATOR in which case it
3112 gives them other confusing semantics rather than plain outlawing
3113 them, which is what we want. */
3114 if (*input_line_pointer == '[')
3115 {
3116 input_line_pointer = saved_input_line_pointer;
3117 return 0;
3118 }
3119
87975d2a 3120 expression (exprP);
3bcbcc3d
HPN
3121 if (exprP->X_op == O_illegal || exprP->X_op == O_absent)
3122 {
3123 input_line_pointer = saved_input_line_pointer;
3124 return 0;
3125 }
3126
3127 /* Everything seems to be fine, just restore the global
3128 input_line_pointer and say we're successful. */
3129 *cPP = input_line_pointer;
3130 input_line_pointer = saved_input_line_pointer;
3131 return 1;
3132}
3133
3bcbcc3d
HPN
3134/* Get a sequence of flag characters from *spp. The pointer *cPP is
3135 advanced to the character following the expression. The flag
3136 characters are consecutive, no commas or spaces.
3137
3138 cPP Pointer to pointer to string beginning with the expression.
3139
3140 flagp Pointer to int to return the flags expression.
3141
3142 Return 1 iff a correct flags expression is found. */
3143
3144static int
695a4822 3145get_flags (char **cPP, int *flagsp)
3bcbcc3d
HPN
3146{
3147 for (;;)
3148 {
3149 switch (**cPP)
3150 {
3151 case 'd':
3152 case 'D':
ae57792d
HPN
3153 if (! cris_insn_ver_valid_for_arch (cris_ver_v0_3,
3154 cris_arch))
3155 return 0;
3156 *flagsp |= 0x80;
3157 break;
3158
3bcbcc3d
HPN
3159 case 'm':
3160 case 'M':
ae57792d
HPN
3161 if (! cris_insn_ver_valid_for_arch (cris_ver_v8_10,
3162 cris_arch))
3163 return 0;
3bcbcc3d
HPN
3164 *flagsp |= 0x80;
3165 break;
3166
3167 case 'e':
3168 case 'E':
ae57792d
HPN
3169 if (! cris_insn_ver_valid_for_arch (cris_ver_v0_3,
3170 cris_arch))
3171 return 0;
3172 *flagsp |= 0x40;
3173 break;
3174
3bcbcc3d
HPN
3175 case 'b':
3176 case 'B':
ae57792d
HPN
3177 if (! cris_insn_ver_valid_for_arch (cris_ver_v8_10,
3178 cris_arch))
3179 return 0;
3180 *flagsp |= 0x40;
3181 break;
3182
3183 case 'p':
3184 case 'P':
3185 if (! cris_insn_ver_valid_for_arch (cris_ver_v32p,
3186 cris_arch))
3187 return 0;
3188 *flagsp |= 0x80;
3189 break;
3190
3191 case 'u':
3192 case 'U':
3193 if (! cris_insn_ver_valid_for_arch (cris_ver_v32p,
3194 cris_arch))
3195 return 0;
3bcbcc3d
HPN
3196 *flagsp |= 0x40;
3197 break;
3198
3199 case 'i':
3200 case 'I':
3201 *flagsp |= 0x20;
3202 break;
3203
3204 case 'x':
3205 case 'X':
3206 *flagsp |= 0x10;
3207 break;
3208
3209 case 'n':
3210 case 'N':
3211 *flagsp |= 0x8;
3212 break;
3213
3214 case 'z':
3215 case 'Z':
3216 *flagsp |= 0x4;
3217 break;
3218
3219 case 'v':
3220 case 'V':
3221 *flagsp |= 0x2;
3222 break;
3223
3224 case 'c':
3225 case 'C':
3226 *flagsp |= 1;
3227 break;
3228
3229 default:
3230 /* We consider this successful if we stop at a comma or
47926f60 3231 whitespace. Anything else, and we consider it a failure. */
3bcbcc3d
HPN
3232 if (**cPP != ','
3233 && **cPP != 0
3882b010 3234 && ! ISSPACE (**cPP))
3bcbcc3d
HPN
3235 return 0;
3236 else
3237 return 1;
3238 }
3239
3240 /* Don't forget to consume each flag character. */
3241 (*cPP)++;
3242 }
3243}
3244
3bcbcc3d 3245/* Generate code and fixes for a BDAP prefix.
ae57792d
HPN
3246 For v32, this handles ADDOQ because thankfully the opcodes are the
3247 same.
3bcbcc3d
HPN
3248
3249 base_regno Int containing the base register number.
3250
3251 exprP Pointer to structure containing the offset expression. */
3252
3253static void
695a4822 3254gen_bdap (int base_regno, expressionS *exprP)
3bcbcc3d
HPN
3255{
3256 unsigned int opcode;
3257 char *opcodep;
3258
3259 /* Put out the prefix opcode; assume quick immediate mode at first. */
3260 opcode = BDAP_QUICK_OPCODE | (base_regno << 12);
ed67db7a 3261 opcodep = cris_insn_first_word_frag ();
3bcbcc3d
HPN
3262 md_number_to_chars (opcodep, opcode, 2);
3263
3264 if (exprP->X_op == O_constant)
3265 {
3266 /* We have an absolute expression that we know the size of right
47926f60 3267 now. */
3bcbcc3d
HPN
3268 long int value;
3269 int size;
3270
3271 value = exprP->X_add_number;
3272 if (value < -32768 || value > 32767)
3273 /* Outside range for a "word", make it a dword. */
3274 size = 2;
3275 else
47926f60 3276 /* Assume "word" size. */
3bcbcc3d
HPN
3277 size = 1;
3278
3279 /* If this is a signed-byte value, we can fit it into the prefix
3280 insn itself. */
3281 if (value >= -128 && value <= 127)
3282 opcodep[0] = value;
3283 else
3284 {
3285 /* This is a word or dword displacement, which will be put in a
3286 word or dword after the prefix. */
3287 char *p;
3288
3289 opcodep[0] = BDAP_PC_LOW + (size << 4);
3290 opcodep[1] &= 0xF0;
3291 opcodep[1] |= BDAP_INCR_HIGH;
3292 p = frag_more (1 << size);
3293 md_number_to_chars (p, value, 1 << size);
3294 }
3295 }
3296 else
08caf3f8
HPN
3297 {
3298 /* Handle complex expressions. */
3299 valueT addvalue
1c971160 3300 = SIMPLE_EXPR (exprP) ? exprP->X_add_number : 0;
08caf3f8 3301 symbolS *sym
1c971160
HPN
3302 = (SIMPLE_EXPR (exprP)
3303 ? exprP->X_add_symbol : make_expr_symbol (exprP));
08caf3f8
HPN
3304
3305 /* The expression is not defined yet but may become absolute. We
3306 make it a relocation to be relaxed. */
3307 frag_var (rs_machine_dependent, 4, 0,
3308 ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_UNDF),
3309 sym, addvalue, opcodep);
3310 }
3bcbcc3d
HPN
3311}
3312
3bcbcc3d
HPN
3313/* Encode a branch displacement in the range -256..254 into the form used
3314 by CRIS conditional branch instructions.
3315
3316 offset The displacement value in bytes. */
3317
3318static int
695a4822 3319branch_disp (int offset)
3bcbcc3d
HPN
3320{
3321 int disp;
3322
ae57792d
HPN
3323 /* Adjust all short branch offsets here. */
3324 if (cris_arch == arch_crisv32 || cris_arch == arch_cris_common_v10_v32)
3325 offset += 2;
3326
3bcbcc3d
HPN
3327 disp = offset & 0xFE;
3328
3329 if (offset < 0)
3330 disp |= 1;
3331
3332 return disp;
3333}
3334
3bcbcc3d
HPN
3335/* Generate code and fixes for a 32-bit conditional branch instruction
3336 created by "extending" an existing 8-bit branch instruction.
3337
3338 opcodep Pointer to the word containing the original 8-bit branch
3339 instruction.
3340
3341 writep Pointer to "extension area" following the first instruction
3342 word.
3343
3344 fragP Pointer to the frag containing the instruction.
3345
3346 add_symP, Parts of the destination address expression.
3347 sub_symP,
3348 add_num. */
3349
3350static void
695a4822
HPN
3351gen_cond_branch_32 (char *opcodep, char *writep, fragS *fragP,
3352 symbolS *add_symP, symbolS *sub_symP, long int add_num)
3bcbcc3d 3353{
ae57792d
HPN
3354 int nop_opcode;
3355 int opc_offset;
3356 int branch_offset;
3357
3358 if (cris_arch == arch_crisv32)
3359 {
3360 nop_opcode = NOP_OPCODE_V32;
3361 opc_offset = 10;
3362 branch_offset = -2 - 8;
3363 }
d2aa3f9f
HPN
3364 else if (pic)
3365 {
3366 nop_opcode = NOP_OPCODE;
3367 opc_offset = 10;
3368 branch_offset = -2 - 8;
3369 }
ae57792d
HPN
3370 else
3371 {
3372 nop_opcode = NOP_OPCODE;
3373 opc_offset = 8;
3374 branch_offset = -2 - 6;
3375 }
3376
3377 /* We should never get here for compatibility mode. */
3378 if (cris_arch == arch_cris_common_v10_v32)
3379 as_fatal (_("Calling gen_cond_branch_32 for .arch common_v10_v32\n"));
3380
3bcbcc3d 3381 if (warn_for_branch_expansion)
08caf3f8
HPN
3382 as_warn_where (fragP->fr_file, fragP->fr_line,
3383 _("32-bit conditional branch generated"));
3bcbcc3d
HPN
3384
3385 /* Here, writep points to what will be opcodep + 2. First, we change
3386 the actual branch in opcodep[0] and opcodep[1], so that in the
3387 final insn, it will look like:
3388 opcodep+10: Bcc .-6
3389
3390 This means we don't have to worry about changing the opcode or
08caf3f8 3391 messing with the delay-slot instruction. So, we move it to last in
3bcbcc3d
HPN
3392 the "extended" branch, and just change the displacement. Admittedly,
3393 it's not the optimal extended construct, but we should get this
3394 rarely enough that it shouldn't matter. */
3395
ae57792d
HPN
3396 writep[opc_offset] = branch_disp (branch_offset);
3397 writep[opc_offset + 1] = opcodep[1];
3bcbcc3d
HPN
3398
3399 /* Then, we change the branch to an unconditional branch over the
3400 extended part, to the new location of the Bcc:
3401 opcodep: BA .+10
3402 opcodep+2: NOP
3403
3404 Note that these two writes are to currently different locations,
3405 merged later. */
3406
ae57792d 3407 md_number_to_chars (opcodep, BA_QUICK_OPCODE
d2aa3f9f
HPN
3408 + (cris_arch == arch_crisv32 ? 12 : (pic ? 10 : 8)),
3409 2);
ae57792d 3410 md_number_to_chars (writep, nop_opcode, 2);
3bcbcc3d
HPN
3411
3412 /* Then the extended thing, the 32-bit jump insn.
08caf3f8
HPN
3413 opcodep+4: JUMP [PC+]
3414 or, in the PIC case,
d2aa3f9f 3415 opcodep+4: MOVE [PC=PC+N],P0. */
3bcbcc3d 3416
08caf3f8 3417 md_number_to_chars (writep + 2,
ae57792d
HPN
3418 cris_arch == arch_crisv32
3419 ? BA_DWORD_OPCODE
d2aa3f9f
HPN
3420 : (pic ? MOVE_PC_INCR_OPCODE_PREFIX
3421 : JUMP_PC_INCR_OPCODE), 2);
3bcbcc3d
HPN
3422
3423 /* We have to fill in the actual value too.
3424 opcodep+6: .DWORD
3425 This is most probably an expression, but we can cope with an absolute
08caf3f8 3426 value too. FIXME: Testcase needed with and without pic. */
3bcbcc3d
HPN
3427
3428 if (add_symP == NULL && sub_symP == NULL)
08caf3f8
HPN
3429 {
3430 /* An absolute address. */
ae57792d 3431 if (pic || cris_arch == arch_crisv32)
08caf3f8
HPN
3432 fix_new (fragP, writep + 4 - fragP->fr_literal, 4,
3433 section_symbol (absolute_section),
ae57792d
HPN
3434 add_num
3435 + (cris_arch == arch_crisv32 ? 6 : 0),
3436 1, BFD_RELOC_32_PCREL);
08caf3f8
HPN
3437 else
3438 md_number_to_chars (writep + 4, add_num, 4);
3439 }
3bcbcc3d
HPN
3440 else
3441 {
08caf3f8
HPN
3442 if (sub_symP != NULL)
3443 as_bad_where (fragP->fr_file, fragP->fr_line,
3444 _("Complex expression not supported"));
3bcbcc3d 3445
08caf3f8 3446 /* Not absolute, we have to make it a frag for later evaluation. */
3bcbcc3d 3447 fix_new (fragP, writep + 4 - fragP->fr_literal, 4, add_symP,
ae57792d
HPN
3448 add_num + (cris_arch == arch_crisv32 ? 6 : 0),
3449 pic || cris_arch == arch_crisv32 ? 1 : 0,
3450 pic || cris_arch == arch_crisv32
3451 ? BFD_RELOC_32_PCREL : BFD_RELOC_32);
3bcbcc3d 3452 }
ae57792d
HPN
3453
3454 if (cris_arch == arch_crisv32)
3455 /* Follow it with a "NOP" for CRISv32. */
3456 md_number_to_chars (writep + 8, NOP_OPCODE_V32, 2);
d2aa3f9f
HPN
3457 else if (pic)
3458 /* ...and the rest of the move-opcode for pre-v32 PIC. */
3459 md_number_to_chars (writep + 8, MOVE_PC_INCR_OPCODE_SUFFIX, 2);
3bcbcc3d
HPN
3460}
3461
cc99daad
HPN
3462/* Get the size of an immediate-reloc in bytes. Only valid for
3463 specified relocs (TLS, PIC). */
08caf3f8
HPN
3464
3465static unsigned int
cc99daad 3466cris_get_specified_reloc_size (bfd_reloc_code_real_type reloc)
08caf3f8 3467{
cc99daad
HPN
3468 return
3469 reloc == BFD_RELOC_CRIS_16_GOTPLT
3470 || reloc == BFD_RELOC_CRIS_16_GOT
3471 || reloc == BFD_RELOC_CRIS_16_GOT_GD
3472 || reloc == BFD_RELOC_CRIS_16_DTPREL
3473 || reloc == BFD_RELOC_CRIS_16_GOT_TPREL
3474 || reloc == BFD_RELOC_CRIS_16_TPREL
08caf3f8
HPN
3475 ? 2 : 4;
3476}
3477
3478/* Store a reloc type at *RELOCP corresponding to the PIC suffix at *CPP.
3479 Adjust *EXPRP with any addend found after the PIC suffix. */
3480
3481static void
cc99daad
HPN
3482cris_get_reloc_suffix (char **cPP, bfd_reloc_code_real_type *relocp,
3483 expressionS *exprP)
08caf3f8
HPN
3484{
3485 char *s = *cPP;
3486 unsigned int i;
3487 expressionS const_expr;
3488
3489 const struct pic_suffixes_struct
3490 {
3491 const char *const suffix;
3492 unsigned int len;
3493 bfd_reloc_code_real_type reloc;
5b7c81bd
AM
3494 bool pic_p;
3495 bool tls_p;
08caf3f8
HPN
3496 } pic_suffixes[] =
3497 {
3498#undef PICMAP
5b7c81bd
AM
3499#define PICMAP(s, r) {s, sizeof (s) - 1, r, true, false}
3500#define PICTLSMAP(s, r) {s, sizeof (s) - 1, r, true, true}
3501#define TLSMAP(s, r) {s, sizeof (s) - 1, r, false, true}
08caf3f8
HPN
3502 /* Keep this in order with longest unambiguous prefix first. */
3503 PICMAP ("GOTPLT16", BFD_RELOC_CRIS_16_GOTPLT),
3504 PICMAP ("GOTPLT", BFD_RELOC_CRIS_32_GOTPLT),
3505 PICMAP ("PLTG", BFD_RELOC_CRIS_32_PLT_GOTREL),
3506 PICMAP ("PLT", BFD_RELOC_CRIS_32_PLT_PCREL),
3507 PICMAP ("GOTOFF", BFD_RELOC_CRIS_32_GOTREL),
3508 PICMAP ("GOT16", BFD_RELOC_CRIS_16_GOT),
cc99daad
HPN
3509 PICMAP ("GOT", BFD_RELOC_CRIS_32_GOT),
3510 PICTLSMAP ("GDGOTREL16", BFD_RELOC_CRIS_16_GOT_GD),
3511 PICTLSMAP ("GDGOTREL", BFD_RELOC_CRIS_32_GOT_GD),
3512 TLSMAP ("GD", BFD_RELOC_CRIS_32_GD),
3513 PICTLSMAP ("DTPREL16", BFD_RELOC_CRIS_16_DTPREL),
3514 PICTLSMAP ("DTPREL", BFD_RELOC_CRIS_32_DTPREL),
5a00ea25 3515 TLSMAP ("IE", BFD_RELOC_CRIS_32_IE),
cc99daad
HPN
3516 PICTLSMAP ("TPOFFGOT16", BFD_RELOC_CRIS_16_GOT_TPREL),
3517 PICTLSMAP ("TPOFFGOT", BFD_RELOC_CRIS_32_GOT_TPREL),
3518 TLSMAP ("TPOFF16", BFD_RELOC_CRIS_16_TPREL),
3519 TLSMAP ("TPOFF", BFD_RELOC_CRIS_32_TPREL)
08caf3f8
HPN
3520 };
3521
3522 /* We've already seen the ':', so consume it. */
3523 s++;
3524
3525 for (i = 0; i < sizeof (pic_suffixes)/sizeof (pic_suffixes[0]); i++)
3526 {
3527 if (strncmp (s, pic_suffixes[i].suffix, pic_suffixes[i].len) == 0
cc99daad
HPN
3528 && ! is_part_of_name (s[pic_suffixes[i].len])
3529 /* PIC and non-PIC relocations are exclusive. */
3530 && (pic != 0) == (pic_suffixes[i].pic_p != 0)
3531 /* But TLS can be active for non-TLS relocations too. */
3532 && (pic_suffixes[i].tls_p == 0 || tls))
08caf3f8
HPN
3533 {
3534 /* We have a match. Consume the suffix and set the relocation
3535 type. */
3536 s += pic_suffixes[i].len;
3537
3538 /* There can be a constant term appended. If so, we will add it
3539 to *EXPRP. */
3540 if (*s == '+' || *s == '-')
3541 {
3542 if (! cris_get_expression (&s, &const_expr))
3543 /* There was some kind of syntax error. Bail out. */
3544 break;
3545
3546 /* Allow complex expressions as the constant part. It still
b6ff326e 3547 has to be an assembly-time constant or there will be an
08caf3f8 3548 error emitting the reloc. This makes the PIC qualifiers
d551a338 3549 idempotent; foo:GOTOFF+32 == foo+32:GOTOFF. The former we
08caf3f8
HPN
3550 recognize here; the latter is parsed in the incoming
3551 expression. */
3552 exprP->X_add_symbol = make_expr_symbol (exprP);
3553 exprP->X_op = O_add;
3554 exprP->X_add_number = 0;
3555 exprP->X_op_symbol = make_expr_symbol (&const_expr);
3556 }
3557
3558 *relocp = pic_suffixes[i].reloc;
3559 *cPP = s;
3560 return;
3561 }
3562 }
3563
3564 /* No match. Don't consume anything; fall back and there will be a
3565 syntax error. */
3566}
3567
ae57792d 3568/* This *could* have been:
3bcbcc3d 3569
47926f60
KH
3570 Turn a string in input_line_pointer into a floating point constant
3571 of type TYPE, and store the appropriate bytes in *LITP. The number
3572 of LITTLENUMS emitted is stored in *SIZEP.
3bcbcc3d
HPN
3573
3574 type A character from FLTCHARS that describes what kind of
3575 floating-point number is wanted.
3576
3577 litp A pointer to an array that the result should be stored in.
3578
3579 sizep A pointer to an integer where the size of the result is stored.
3580
3581 But we don't support floating point constants in assembly code *at all*,
3582 since it's suboptimal and just opens up bug opportunities. GCC emits
3583 the bit patterns as hex. All we could do here is to emit what GCC
3584 would have done in the first place. *Nobody* writes floating-point
3585 code as assembly code, but if they do, they should be able enough to
3586 find out the correct bit patterns and use them. */
3587
6d4af3c2 3588const char *
695a4822
HPN
3589md_atof (int type ATTRIBUTE_UNUSED, char *litp ATTRIBUTE_UNUSED,
3590 int *sizep ATTRIBUTE_UNUSED)
3bcbcc3d
HPN
3591{
3592 /* FIXME: Is this function mentioned in the internals.texi manual? If
3593 not, add it. */
3594 return _("Bad call to md_atof () - floating point formats are not supported");
3595}
3596
3bcbcc3d
HPN
3597/* Turn a number as a fixS * into a series of bytes that represents the
3598 number on the target machine. The purpose of this procedure is the
3599 same as that of md_number_to_chars but this procedure is supposed to
3600 handle general bit field fixes and machine-dependent fixups.
3601
3602 bufp Pointer to an array where the result should be stored.
3603
3604 val The value to store.
3605
3606 n The number of bytes in "val" that should be stored.
3607
08caf3f8
HPN
3608 fixP The fix to be applied to the bit field starting at bufp.
3609
3610 seg The segment containing this number. */
3bcbcc3d
HPN
3611
3612static void
695a4822 3613cris_number_to_imm (char *bufp, long val, int n, fixS *fixP, segT seg)
3bcbcc3d
HPN
3614{
3615 segT sym_seg;
3616
3617 know (n <= 4);
3618 know (fixP);
3619
3620 /* We put the relative "vma" for the other segment for inter-segment
3621 relocations in the object data to stay binary "compatible" (with an
3622 uninteresting old version) for the relocation.
3623 Maybe delete some day. */
3624 if (fixP->fx_addsy
08caf3f8 3625 && (sym_seg = S_GET_SEGMENT (fixP->fx_addsy)) != seg)
3bcbcc3d
HPN
3626 val += sym_seg->vma;
3627
08caf3f8
HPN
3628 if (fixP->fx_addsy != NULL || fixP->fx_pcrel)
3629 switch (fixP->fx_r_type)
3630 {
3631 /* These must be fully resolved when getting here. */
08caf3f8
HPN
3632 case BFD_RELOC_16_PCREL:
3633 case BFD_RELOC_8_PCREL:
ae57792d 3634 as_bad_where (fixP->fx_file, fixP->fx_line,
08caf3f8
HPN
3635 _("PC-relative relocation must be trivially resolved"));
3636 default:
3637 ;
3638 }
3639
bfa1b75c
HPN
3640 /* Only use the computed value for old-arch binaries. For all
3641 others, where we're going to output a relocation, put 0 in the
3642 code. */
ae57792d
HPN
3643 if (cris_arch != arch_cris_any_v0_v10
3644 && (fixP->fx_addsy != NULL || fixP->fx_pcrel))
bfa1b75c 3645 val = 0;
ae57792d 3646
3bcbcc3d
HPN
3647 switch (fixP->fx_r_type)
3648 {
3649 /* Ditto here, we put the addend into the object code as
3650 well as the reloc addend. Keep it that way for now, to simplify
3651 regression tests on the object file contents. FIXME: Seems
3652 uninteresting now that we have a test suite. */
3653
cc99daad
HPN
3654 case BFD_RELOC_CRIS_32_GOT_GD:
3655 case BFD_RELOC_CRIS_16_GOT_GD:
3656 case BFD_RELOC_CRIS_32_GD:
5a00ea25 3657 case BFD_RELOC_CRIS_32_IE:
cc99daad
HPN
3658 case BFD_RELOC_CRIS_32_DTPREL:
3659 case BFD_RELOC_CRIS_16_DTPREL:
3660 case BFD_RELOC_CRIS_32_GOT_TPREL:
3661 case BFD_RELOC_CRIS_16_GOT_TPREL:
3662 case BFD_RELOC_CRIS_32_TPREL:
3663 case BFD_RELOC_CRIS_16_TPREL:
f3294356
HPN
3664#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3665 if (IS_ELF && fixP->fx_addsy != NULL)
3666 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3667#endif
3668 /* Fall through. */
3669
3670 case BFD_RELOC_CRIS_16_GOT:
3671 case BFD_RELOC_CRIS_32_GOT:
3672 case BFD_RELOC_CRIS_32_GOTREL:
3673 case BFD_RELOC_CRIS_16_GOTPLT:
3674 case BFD_RELOC_CRIS_32_GOTPLT:
3675 case BFD_RELOC_CRIS_32_PLT_GOTREL:
3676 case BFD_RELOC_CRIS_32_PLT_PCREL:
08caf3f8
HPN
3677 /* We don't want to put in any kind of non-zero bits in the data
3678 being relocated for these. */
bfa1b75c 3679 md_number_to_chars (bufp, 0, n);
08caf3f8
HPN
3680 break;
3681
08caf3f8 3682 case BFD_RELOC_32_PCREL:
bfa1b75c 3683 /* If this one isn't fully resolved, we don't want to put non-zero
ae57792d
HPN
3684 in the object. */
3685 if (fixP->fx_addsy != NULL || fixP->fx_pcrel)
bfa1b75c 3686 val = 0;
ae57792d
HPN
3687
3688 /* Fall through. */
3689 case BFD_RELOC_32:
3bcbcc3d
HPN
3690 /* No use having warnings here, since most hosts have a 32-bit type
3691 for "long" (which will probably change soon, now that I wrote
3692 this). */
3693 bufp[3] = (val >> 24) & 0xFF;
3694 bufp[2] = (val >> 16) & 0xFF;
3695 bufp[1] = (val >> 8) & 0xFF;
3696 bufp[0] = val & 0xFF;
3697 break;
3698
3699 /* FIXME: The 16 and 8-bit cases should have a way to check
3700 whether a signed or unsigned (or any signedness) number is
ae57792d 3701 accepted. */
3bcbcc3d
HPN
3702
3703 case BFD_RELOC_16:
08caf3f8 3704 case BFD_RELOC_16_PCREL:
3bcbcc3d 3705 if (val > 0xffff || val < -32768)
ae57792d
HPN
3706 as_bad_where (fixP->fx_file, fixP->fx_line,
3707 _("Value not in 16 bit range: %ld"), val);
bfa1b75c
HPN
3708 bufp[1] = (val >> 8) & 0xFF;
3709 bufp[0] = val & 0xFF;
ae57792d
HPN
3710 break;
3711
3712 case BFD_RELOC_CRIS_SIGNED_16:
3713 if (val > 32767 || val < -32768)
3714 as_bad_where (fixP->fx_file, fixP->fx_line,
3715 _("Value not in 16 bit signed range: %ld"), val);
bfa1b75c
HPN
3716 bufp[1] = (val >> 8) & 0xFF;
3717 bufp[0] = val & 0xFF;
3bcbcc3d
HPN
3718 break;
3719
3720 case BFD_RELOC_8:
08caf3f8 3721 case BFD_RELOC_8_PCREL:
3bcbcc3d 3722 if (val > 255 || val < -128)
ae57792d 3723 as_bad_where (fixP->fx_file, fixP->fx_line, _("Value not in 8 bit range: %ld"), val);
bfa1b75c 3724 bufp[0] = val & 0xFF;
ae57792d
HPN
3725 break;
3726
3727 case BFD_RELOC_CRIS_SIGNED_8:
3728 if (val > 127 || val < -128)
3729 as_bad_where (fixP->fx_file, fixP->fx_line,
3730 _("Value not in 8 bit signed range: %ld"), val);
bfa1b75c 3731 bufp[0] = val & 0xFF;
3bcbcc3d
HPN
3732 break;
3733
ae57792d
HPN
3734 case BFD_RELOC_CRIS_LAPCQ_OFFSET:
3735 /* FIXME: Test-cases for out-of-range values. Probably also need
3736 to use as_bad_where. */
3bcbcc3d
HPN
3737 case BFD_RELOC_CRIS_UNSIGNED_4:
3738 if (val > 15 || val < 0)
ae57792d
HPN
3739 as_bad_where (fixP->fx_file, fixP->fx_line,
3740 _("Value not in 4 bit unsigned range: %ld"), val);
bfa1b75c 3741 bufp[0] |= val & 0x0F;
3bcbcc3d
HPN
3742 break;
3743
3744 case BFD_RELOC_CRIS_UNSIGNED_5:
3745 if (val > 31 || val < 0)
ae57792d
HPN
3746 as_bad_where (fixP->fx_file, fixP->fx_line,
3747 _("Value not in 5 bit unsigned range: %ld"), val);
bfa1b75c 3748 bufp[0] |= val & 0x1F;
3bcbcc3d
HPN
3749 break;
3750
3751 case BFD_RELOC_CRIS_SIGNED_6:
3752 if (val > 31 || val < -32)
ae57792d
HPN
3753 as_bad_where (fixP->fx_file, fixP->fx_line,
3754 _("Value not in 6 bit range: %ld"), val);
bfa1b75c 3755 bufp[0] |= val & 0x3F;
3bcbcc3d
HPN
3756 break;
3757
3758 case BFD_RELOC_CRIS_UNSIGNED_6:
3759 if (val > 63 || val < 0)
ae57792d
HPN
3760 as_bad_where (fixP->fx_file, fixP->fx_line,
3761 _("Value not in 6 bit unsigned range: %ld"), val);
bfa1b75c 3762 bufp[0] |= val & 0x3F;
3bcbcc3d
HPN
3763 break;
3764
3765 case BFD_RELOC_CRIS_BDISP8:
bfa1b75c 3766 bufp[0] = branch_disp (val);
3bcbcc3d
HPN
3767 break;
3768
3769 case BFD_RELOC_NONE:
3770 /* May actually happen automatically. For example at broken
3771 words, if the word turns out not to be broken.
47926f60 3772 FIXME: When? Which testcase? */
3bcbcc3d
HPN
3773 if (! fixP->fx_addsy)
3774 md_number_to_chars (bufp, val, n);
3775 break;
3776
3777 case BFD_RELOC_VTABLE_INHERIT:
3778 /* This borrowed from tc-ppc.c on a whim. */
3779 if (fixP->fx_addsy
3780 && !S_IS_DEFINED (fixP->fx_addsy)
3781 && !S_IS_WEAK (fixP->fx_addsy))
3782 S_SET_WEAK (fixP->fx_addsy);
7b15d668
HPN
3783 /* Fall through. */
3784
3bcbcc3d 3785 case BFD_RELOC_VTABLE_ENTRY:
3bcbcc3d
HPN
3786 fixP->fx_done = 0;
3787 break;
3788
3789 default:
3790 BAD_CASE (fixP->fx_r_type);
3791 }
3792}
3793
3bcbcc3d
HPN
3794/* Processes machine-dependent command line options. Called once for
3795 each option on the command line that the machine-independent part of
3796 GAS does not understand. */
47926f60 3797
3bcbcc3d 3798int
17b9d67d 3799md_parse_option (int arg, const char *argp ATTRIBUTE_UNUSED)
3bcbcc3d
HPN
3800{
3801 switch (arg)
3802 {
3803 case 'H':
3804 case 'h':
7b15d668 3805 printf (_("Please use --help to see usage and options for this assembler.\n"));
3bcbcc3d 3806 md_show_usage (stdout);
7b15d668 3807 exit (EXIT_SUCCESS);
3bcbcc3d
HPN
3808
3809 case 'N':
3810 warn_for_branch_expansion = 1;
ae57792d 3811 break;
3bcbcc3d 3812
7b15d668 3813 case OPTION_NO_US:
5b7c81bd 3814 demand_register_prefix = true;
7b15d668
HPN
3815
3816 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
08caf3f8 3817 as_bad (_("--no-underscore is invalid with a.out format"));
7b15d668 3818 else
5b7c81bd 3819 symbols_have_leading_underscore = false;
ae57792d 3820 break;
7b15d668
HPN
3821
3822 case OPTION_US:
5b7c81bd
AM
3823 demand_register_prefix = false;
3824 symbols_have_leading_underscore = true;
ae57792d 3825 break;
7b15d668 3826
08caf3f8 3827 case OPTION_PIC:
0d7e0060
HPN
3828 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3829 as_bad (_("--pic is invalid for this object format"));
5b7c81bd 3830 pic = true;
d2aa3f9f
HPN
3831 if (cris_arch != arch_crisv32)
3832 md_long_jump_size = cris_any_v0_v10_long_jump_size_pic;
3833 else
3834 md_long_jump_size = crisv32_long_jump_size;
ae57792d
HPN
3835 break;
3836
3837 case OPTION_ARCH:
3838 {
17b9d67d 3839 const char *str = argp;
ae57792d
HPN
3840 enum cris_archs argarch = cris_arch_from_string (&str);
3841
3842 if (argarch == arch_cris_unknown)
3843 as_bad (_("invalid <arch> in --march=<arch>: %s"), argp);
3844 else
3845 cris_arch = argarch;
3846
3847 if (argarch == arch_crisv32)
3848 {
3849 err_for_dangerous_mul_placement = 0;
3850 md_long_jump_size = crisv32_long_jump_size;
3851 }
3852 else
d2aa3f9f
HPN
3853 {
3854 if (pic)
3855 md_long_jump_size = cris_any_v0_v10_long_jump_size_pic;
3856 else
3857 md_long_jump_size = cris_any_v0_v10_long_jump_size;
3858 }
ae57792d
HPN
3859 }
3860 break;
08caf3f8 3861
1048a9ba
HPN
3862 case OPTION_MULBUG_ABORT_OFF:
3863 err_for_dangerous_mul_placement = 0;
ae57792d 3864 break;
1048a9ba
HPN
3865
3866 case OPTION_MULBUG_ABORT_ON:
3867 err_for_dangerous_mul_placement = 1;
ae57792d 3868 break;
1048a9ba 3869
3bcbcc3d
HPN
3870 default:
3871 return 0;
47926f60 3872 }
ae57792d
HPN
3873
3874 return 1;
3bcbcc3d
HPN
3875}
3876
3877/* Round up a section size to the appropriate boundary. */
3878valueT
695a4822 3879md_section_align (segT segment, valueT size)
3bcbcc3d
HPN
3880{
3881 /* Round all sects to multiple of 4, except the bss section, which
3882 we'll round to word-size.
3883
3884 FIXME: Check if this really matters. All sections should be
3885 rounded up, and all sections should (optionally) be assumed to be
3886 dword-aligned, it's just that there is actual usage of linking to a
3887 multiple of two. */
3888 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
3889 {
3890 if (segment == bss_section)
3891 return (size + 1) & ~1;
3892 return (size + 3) & ~3;
3893 }
3894 else
3895 {
3896 /* FIXME: Is this wanted? It matches the testsuite, but that's not
3897 really a valid reason. */
3898 if (segment == text_section)
3899 return (size + 3) & ~3;
3900 }
3901
3902 return size;
3903}
3904
3bcbcc3d
HPN
3905/* Generate a machine-dependent relocation. */
3906arelent *
695a4822 3907tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixP)
3bcbcc3d
HPN
3908{
3909 arelent *relP;
3910 bfd_reloc_code_real_type code;
3911
3912 switch (fixP->fx_r_type)
3913 {
ae57792d
HPN
3914 case BFD_RELOC_CRIS_SIGNED_8:
3915 code = BFD_RELOC_8;
3916 break;
3917
3918 case BFD_RELOC_CRIS_SIGNED_16:
3919 code = BFD_RELOC_16;
3920 break;
3921
08caf3f8
HPN
3922 case BFD_RELOC_CRIS_16_GOT:
3923 case BFD_RELOC_CRIS_32_GOT:
3924 case BFD_RELOC_CRIS_16_GOTPLT:
3925 case BFD_RELOC_CRIS_32_GOTPLT:
3926 case BFD_RELOC_CRIS_32_GOTREL:
3927 case BFD_RELOC_CRIS_32_PLT_GOTREL:
3928 case BFD_RELOC_CRIS_32_PLT_PCREL:
3bcbcc3d 3929 case BFD_RELOC_32:
ae57792d 3930 case BFD_RELOC_32_PCREL:
3bcbcc3d
HPN
3931 case BFD_RELOC_16:
3932 case BFD_RELOC_8:
3933 case BFD_RELOC_VTABLE_INHERIT:
3934 case BFD_RELOC_VTABLE_ENTRY:
ae57792d
HPN
3935 case BFD_RELOC_CRIS_UNSIGNED_8:
3936 case BFD_RELOC_CRIS_UNSIGNED_16:
3937 case BFD_RELOC_CRIS_LAPCQ_OFFSET:
cc99daad
HPN
3938 case BFD_RELOC_CRIS_32_GOT_GD:
3939 case BFD_RELOC_CRIS_16_GOT_GD:
3940 case BFD_RELOC_CRIS_32_GD:
5a00ea25 3941 case BFD_RELOC_CRIS_32_IE:
cc99daad
HPN
3942 case BFD_RELOC_CRIS_32_DTPREL:
3943 case BFD_RELOC_CRIS_16_DTPREL:
3944 case BFD_RELOC_CRIS_32_GOT_TPREL:
3945 case BFD_RELOC_CRIS_16_GOT_TPREL:
3946 case BFD_RELOC_CRIS_32_TPREL:
3947 case BFD_RELOC_CRIS_16_TPREL:
3bcbcc3d
HPN
3948 code = fixP->fx_r_type;
3949 break;
3950 default:
3951 as_bad_where (fixP->fx_file, fixP->fx_line,
3952 _("Semantics error. This type of operand can not be relocated, it must be an assembly-time constant"));
3953 return 0;
3954 }
3955
add39d23 3956 relP = XNEW (arelent);
9c2799c2 3957 gas_assert (relP != 0);
add39d23 3958 relP->sym_ptr_ptr = XNEW (asymbol *);
3bcbcc3d
HPN
3959 *relP->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
3960 relP->address = fixP->fx_frag->fr_address + fixP->fx_where;
3961
ae57792d 3962 relP->addend = fixP->fx_offset;
3bcbcc3d
HPN
3963
3964 /* This is the standard place for KLUDGEs to work around bugs in
3965 bfd_install_relocation (first such note in the documentation
3966 appears with binutils-2.8).
3967
3968 That function bfd_install_relocation does the wrong thing with
3969 putting stuff into the addend of a reloc (it should stay out) for a
3970 weak symbol. The really bad thing is that it adds the
3971 "segment-relative offset" of the symbol into the reloc. In this
3972 case, the reloc should instead be relative to the symbol with no
3973 other offset than the assembly code shows; and since the symbol is
3974 weak, any local definition should be ignored until link time (or
3975 thereafter).
3976 To wit: weaksym+42 should be weaksym+42 in the reloc,
3977 not weaksym+(offset_from_segment_of_local_weaksym_definition)
3978
3979 To "work around" this, we subtract the segment-relative offset of
3980 "known" weak symbols. This evens out the extra offset.
3981
3982 That happens for a.out but not for ELF, since for ELF,
3983 bfd_install_relocation uses the "special function" field of the
3984 howto, and does not execute the code that needs to be undone. */
3985
3986 if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3987 && fixP->fx_addsy && S_IS_WEAK (fixP->fx_addsy)
3988 && ! bfd_is_und_section (S_GET_SEGMENT (fixP->fx_addsy)))
47926f60
KH
3989 {
3990 relP->addend -= S_GET_VALUE (fixP->fx_addsy);
3991 }
3bcbcc3d
HPN
3992
3993 relP->howto = bfd_reloc_type_lookup (stdoutput, code);
3994 if (! relP->howto)
3995 {
3996 const char *name;
3997
3998 name = S_GET_NAME (fixP->fx_addsy);
3999 if (name == NULL)
4000 name = _("<unknown>");
4001 as_fatal (_("Cannot generate relocation type for symbol %s, code %s"),
4002 name, bfd_get_reloc_code_name (code));
4003 }
4004
4005 return relP;
4006}
4007
3bcbcc3d 4008/* Machine-dependent usage-output. */
47926f60 4009
3bcbcc3d 4010void
695a4822 4011md_show_usage (FILE *stream)
3bcbcc3d 4012{
08caf3f8 4013 /* The messages are formatted to line up with the generic options. */
7b15d668
HPN
4014 fprintf (stream, _("CRIS-specific options:\n"));
4015 fprintf (stream, "%s",
4016 _(" -h, -H Don't execute, print this help text. Deprecated.\n"));
4017 fprintf (stream, "%s",
4018 _(" -N Warn when branches are expanded to jumps.\n"));
4019 fprintf (stream, "%s",
4020 _(" --underscore User symbols are normally prepended with underscore.\n"));
4021 fprintf (stream, "%s",
4022 _(" Registers will not need any prefix.\n"));
4023 fprintf (stream, "%s",
4024 _(" --no-underscore User symbols do not have any prefix.\n"));
4025 fprintf (stream, "%s",
4026 _(" Registers will require a `$'-prefix.\n"));
0d7e0060 4027#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
08caf3f8
HPN
4028 fprintf (stream, "%s",
4029 _(" --pic Enable generation of position-independent code.\n"));
0d7e0060 4030#endif
ae57792d
HPN
4031 fprintf (stream, "%s",
4032 _(" --march=<arch> Generate code for <arch>. Valid choices for <arch>\n\
4033 are v0_v10, v10, v32 and common_v10_v32.\n"));
3bcbcc3d
HPN
4034}
4035
3bcbcc3d 4036/* Apply a fixS (fixup of an instruction or data that we didn't have
47926f60 4037 enough info to complete immediately) to the data in a frag. */
3bcbcc3d 4038
94f592af 4039void
55cf6793 4040md_apply_fix (fixS *fixP, valueT *valP, segT seg)
3bcbcc3d 4041{
451a1fc5
HPN
4042 /* This assignment truncates upper bits if valueT is 64 bits (as with
4043 --enable-64-bit-bfd), which is fine here, though we cast to avoid
920e4177 4044 any compiler warnings. */
451a1fc5 4045 long val = (long) *valP;
3bcbcc3d
HPN
4046 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
4047
4048 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
4049 fixP->fx_done = 1;
4050
5bc11336
AM
4051 /* We can't actually support subtracting a symbol. */
4052 if (fixP->fx_subsy != (symbolS *) NULL)
4bf09429 4053 as_bad_subtract (fixP);
5bc11336
AM
4054
4055 /* This operand-type is scaled. */
4056 if (fixP->fx_r_type == BFD_RELOC_CRIS_LAPCQ_OFFSET)
4057 val /= 2;
4058 cris_number_to_imm (buf, val, fixP->fx_size, fixP, seg);
3bcbcc3d
HPN
4059}
4060
3bcbcc3d
HPN
4061/* All relocations are relative to the location just after the fixup;
4062 the address of the fixup plus its size. */
4063
4064long
695a4822 4065md_pcrel_from (fixS *fixP)
3bcbcc3d
HPN
4066{
4067 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
4068
4069 /* FIXME: We get here only at the end of assembly, when X in ".-X" is
08caf3f8
HPN
4070 still unknown. Since we don't have pc-relative relocations in a.out,
4071 this is invalid. What to do if anything for a.out, is to add
3bcbcc3d 4072 pc-relative relocations everywhere including the elinux program
08caf3f8
HPN
4073 loader. For ELF, allow straight-forward PC-relative relocations,
4074 which are always relative to the location after the relocation. */
4075 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
4076 || (fixP->fx_r_type != BFD_RELOC_8_PCREL
4077 && fixP->fx_r_type != BFD_RELOC_16_PCREL
ae57792d
HPN
4078 && fixP->fx_r_type != BFD_RELOC_32_PCREL
4079 && fixP->fx_r_type != BFD_RELOC_CRIS_LAPCQ_OFFSET))
08caf3f8
HPN
4080 as_bad_where (fixP->fx_file, fixP->fx_line,
4081 _("Invalid pc-relative relocation"));
3bcbcc3d
HPN
4082 return fixP->fx_size + addr;
4083}
4084
47926f60 4085/* We have no need to give defaults for symbol-values. */
3bcbcc3d 4086symbolS *
695a4822 4087md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
3bcbcc3d
HPN
4088{
4089 return 0;
4090}
4091
a161fe53
AM
4092/* If this function returns non-zero, it prevents the relocation
4093 against symbol(s) in the FIXP from being replaced with relocations
4094 against section symbols, and guarantees that a relocation will be
4095 emitted even when the value can be resolved locally. */
3bcbcc3d 4096int
695a4822 4097md_cris_force_relocation (struct fix *fixp)
3bcbcc3d 4098{
08caf3f8
HPN
4099 switch (fixp->fx_r_type)
4100 {
08caf3f8
HPN
4101 case BFD_RELOC_CRIS_16_GOT:
4102 case BFD_RELOC_CRIS_32_GOT:
4103 case BFD_RELOC_CRIS_16_GOTPLT:
4104 case BFD_RELOC_CRIS_32_GOTPLT:
4105 case BFD_RELOC_CRIS_32_GOTREL:
4106 case BFD_RELOC_CRIS_32_PLT_GOTREL:
4107 case BFD_RELOC_CRIS_32_PLT_PCREL:
4108 return 1;
4109 default:
4110 ;
4111 }
4112
ae6063d4 4113 return generic_force_reloc (fixp);
3bcbcc3d
HPN
4114}
4115
4116/* Check and emit error if broken-word handling has failed to fix up a
4117 case-table. This is called from write.c, after doing everything it
4118 knows about how to handle broken words. */
4119
4120void
695a4822 4121tc_cris_check_adjusted_broken_word (offsetT new_offset, struct broken_word *brokwP)
3bcbcc3d
HPN
4122{
4123 if (new_offset > 32767 || new_offset < -32768)
47926f60 4124 /* We really want a genuine error, not a warning, so make it one. */
3bcbcc3d
HPN
4125 as_bad_where (brokwP->frag->fr_file, brokwP->frag->fr_line,
4126 _("Adjusted signed .word (%ld) overflows: `switch'-statement too large."),
4127 (long) new_offset);
4128}
4129
7b15d668
HPN
4130/* Make a leading REGISTER_PREFIX_CHAR mandatory for all registers. */
4131
695a4822
HPN
4132static void
4133cris_force_reg_prefix (void)
7b15d668 4134{
5b7c81bd 4135 demand_register_prefix = true;
7b15d668
HPN
4136}
4137
4138/* Do not demand a leading REGISTER_PREFIX_CHAR for all registers. */
4139
695a4822
HPN
4140static void
4141cris_relax_reg_prefix (void)
7b15d668 4142{
5b7c81bd 4143 demand_register_prefix = false;
7b15d668
HPN
4144}
4145
4146/* Adjust for having a leading '_' on all user symbols. */
4147
695a4822
HPN
4148static void
4149cris_sym_leading_underscore (void)
7b15d668
HPN
4150{
4151 /* We can't really do anything more than assert that what the program
4152 thinks symbol starts with agrees with the command-line options, since
4153 the bfd is already created. */
4154
b34976b6 4155 if (!symbols_have_leading_underscore)
ed67db7a 4156 as_bad (_(".syntax %s requires command-line option `--underscore'"),
7b15d668
HPN
4157 SYNTAX_USER_SYM_LEADING_UNDERSCORE);
4158}
4159
4160/* Adjust for not having any particular prefix on user symbols. */
4161
695a4822 4162static void cris_sym_no_leading_underscore (void)
7b15d668 4163{
b34976b6 4164 if (symbols_have_leading_underscore)
ed67db7a 4165 as_bad (_(".syntax %s requires command-line option `--no-underscore'"),
7b15d668
HPN
4166 SYNTAX_USER_SYM_NO_LEADING_UNDERSCORE);
4167}
4168
4169/* Handle the .syntax pseudo, which takes an argument that decides what
4170 syntax the assembly code has. */
4171
4172static void
695a4822 4173s_syntax (int ignore ATTRIBUTE_UNUSED)
7b15d668
HPN
4174{
4175 static const struct syntaxes
4176 {
ae57792d 4177 const char *const operand;
695a4822 4178 void (*fn) (void);
4a1805b1 4179 } syntax_table[] =
7b15d668
HPN
4180 {{SYNTAX_ENFORCE_REG_PREFIX, cris_force_reg_prefix},
4181 {SYNTAX_RELAX_REG_PREFIX, cris_relax_reg_prefix},
4182 {SYNTAX_USER_SYM_LEADING_UNDERSCORE, cris_sym_leading_underscore},
4183 {SYNTAX_USER_SYM_NO_LEADING_UNDERSCORE, cris_sym_no_leading_underscore}};
4184
4185 const struct syntaxes *sp;
4186
4187 for (sp = syntax_table;
4188 sp < syntax_table + sizeof (syntax_table) / sizeof (syntax_table[0]);
4189 sp++)
4190 {
4191 if (strncmp (input_line_pointer, sp->operand,
4192 strlen (sp->operand)) == 0)
4193 {
bc805888 4194 (sp->fn) ();
7b15d668
HPN
4195
4196 input_line_pointer += strlen (sp->operand);
4197 demand_empty_rest_of_line ();
4198 return;
4199 }
4200 }
4201
4202 as_bad (_("Unknown .syntax operand"));
4203}
4204
fcdc20a4
HPN
4205/* Wrapper for dwarf2_directive_file to emit error if this is seen when
4206 not emitting ELF. */
4207
4208static void
695a4822 4209s_cris_file (int dummy)
fcdc20a4
HPN
4210{
4211 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
ed67db7a 4212 as_bad (_("Pseudodirective .file is only valid when generating ELF"));
fcdc20a4
HPN
4213 else
4214 dwarf2_directive_file (dummy);
4215}
4216
4217/* Wrapper for dwarf2_directive_loc to emit error if this is seen when not
4218 emitting ELF. */
4219
4220static void
695a4822 4221s_cris_loc (int dummy)
fcdc20a4
HPN
4222{
4223 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
ed67db7a 4224 as_bad (_("Pseudodirective .loc is only valid when generating ELF"));
fcdc20a4
HPN
4225 else
4226 dwarf2_directive_loc (dummy);
4227}
4228
d2b52762
HPN
4229/* Worker for .dtpoffd: generate a R_CRIS_32_DTPREL reloc, as for
4230 expr:DTPREL but for use in debug info. */
4231
4232static void
4233s_cris_dtpoff (int bytes)
4234{
4235 expressionS ex;
4236 char *p;
4237
4238 if (bytes != 4)
4239 as_fatal (_("internal inconsistency problem: %s called for %d bytes"),
4240 __FUNCTION__, bytes);
4241
4242 expression (&ex);
4243
4244 p = frag_more (bytes);
4245 md_number_to_chars (p, 0, bytes);
5b7c81bd 4246 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false,
d2b52762
HPN
4247 BFD_RELOC_CRIS_32_DTPREL);
4248
4249 demand_empty_rest_of_line ();
4250}
4251
4252
ae57792d
HPN
4253/* Translate a <arch> string (as common to --march=<arch> and .arch <arch>)
4254 into an enum. If the string *STR is recognized, *STR is updated to point
4255 to the end of the string. If the string is not recognized,
4256 arch_cris_unknown is returned. */
4257
4258static enum cris_archs
17b9d67d 4259cris_arch_from_string (const char **str)
ae57792d
HPN
4260{
4261 static const struct cris_arch_struct
4262 {
4263 const char *const name;
4264 enum cris_archs arch;
4265 } arch_table[] =
4266 /* Keep in order longest-first for choices where one is a prefix
4267 of another. */
4268 {{"v0_v10", arch_cris_any_v0_v10},
4269 {"v10", arch_crisv10},
4270 {"v32", arch_crisv32},
4271 {"common_v10_v32", arch_cris_common_v10_v32}};
4272
4273 const struct cris_arch_struct *ap;
4274
4275 for (ap = arch_table;
4276 ap < arch_table + sizeof (arch_table) / sizeof (arch_table[0]);
4277 ap++)
4278 {
4279 int len = strlen (ap->name);
4280
4281 if (strncmp (*str, ap->name, len) == 0
4282 && (str[0][len] == 0 || ISSPACE (str[0][len])))
4283 {
4284 *str += strlen (ap->name);
4285 return ap->arch;
4286 }
4287 }
4288
4289 return arch_cris_unknown;
4290}
4291
4292/* Return nonzero if architecture version ARCH matches version range in
4293 IVER. */
4294
4295static int
695a4822
HPN
4296cris_insn_ver_valid_for_arch (enum cris_insn_version_usage iver,
4297 enum cris_archs arch)
ae57792d
HPN
4298{
4299 switch (arch)
4300 {
4301 case arch_cris_any_v0_v10:
4302 return
4303 (iver == cris_ver_version_all
4304 || iver == cris_ver_warning
4305 || iver == cris_ver_v0_3
4306 || iver == cris_ver_v3p
4307 || iver == cris_ver_v0_10
4308 || iver == cris_ver_sim_v0_10
4309 || iver == cris_ver_v3_10
4310 || iver == cris_ver_v8
4311 || iver == cris_ver_v8p
4312 || iver == cris_ver_v8_10
4313 || iver == cris_ver_v10
4314 || iver == cris_ver_v10p);
3739860c 4315
ae57792d
HPN
4316 case arch_crisv32:
4317 return
4318 (iver == cris_ver_version_all
4319 || iver == cris_ver_v3p
4320 || iver == cris_ver_v8p
4321 || iver == cris_ver_v10p
4322 || iver == cris_ver_v32p);
4323
4324 case arch_cris_common_v10_v32:
4325 return
4326 (iver == cris_ver_version_all
4327 || iver == cris_ver_v3p
4328 || iver == cris_ver_v8p
4329 || iver == cris_ver_v10p);
4330
4331 case arch_crisv0:
4332 return
4333 (iver == cris_ver_version_all
4334 || iver == cris_ver_v0_3
4335 || iver == cris_ver_v0_10
4336 || iver == cris_ver_sim_v0_10);
4337
4338 case arch_crisv3:
4339 return
4340 (iver == cris_ver_version_all
4341 || iver == cris_ver_v0_3
4342 || iver == cris_ver_v3p
4343 || iver == cris_ver_v0_10
4344 || iver == cris_ver_sim_v0_10
4345 || iver == cris_ver_v3_10);
4346
4347 case arch_crisv8:
4348 return
4349 (iver == cris_ver_version_all
4350 || iver == cris_ver_v3p
4351 || iver == cris_ver_v0_10
4352 || iver == cris_ver_sim_v0_10
4353 || iver == cris_ver_v3_10
4354 || iver == cris_ver_v8
4355 || iver == cris_ver_v8p
4356 || iver == cris_ver_v8_10);
4357
4358 case arch_crisv10:
4359 return
4360 (iver == cris_ver_version_all
4361 || iver == cris_ver_v3p
4362 || iver == cris_ver_v0_10
4363 || iver == cris_ver_sim_v0_10
4364 || iver == cris_ver_v3_10
4365 || iver == cris_ver_v8p
4366 || iver == cris_ver_v8_10
4367 || iver == cris_ver_v10
4368 || iver == cris_ver_v10p);
4369
4370 default:
4371 BAD_CASE (arch);
4372 }
4373}
4374
4375/* Assert that the .arch ARCHCHOICE1 is compatible with the specified or
4376 default --march=<ARCHCHOICE2> option. */
4377
4378static void
695a4822 4379s_cris_arch (int dummy ATTRIBUTE_UNUSED)
ae57792d
HPN
4380{
4381 /* Right now we take the easy route and check for sameness. It's not
4382 obvious that allowing e.g. --march=v32 and .arch common_v0_v32
4383 would be more useful than confusing, implementation-wise and
4384 user-wise. */
4385
17b9d67d 4386 const char *str = input_line_pointer;
ae57792d
HPN
4387 enum cris_archs arch = cris_arch_from_string (&str);
4388
4389 if (arch == arch_cris_unknown)
4390 {
4391 as_bad (_("unknown operand to .arch"));
4392
4393 /* For this one, str does not reflect the end of the operand,
4394 since there was no matching arch. Skip it manually; skip
4395 things that can be part of a word (a name). */
4396 while (is_part_of_name (*str))
4397 str++;
4398 }
4399 else if (arch != cris_arch)
4400 as_bad (_(".arch <arch> requires a matching --march=... option"));
4401
17b9d67d 4402 input_line_pointer = (char *) str;
ae57792d
HPN
4403 demand_empty_rest_of_line ();
4404 return;
4405}
4406
3bcbcc3d
HPN
4407/*
4408 * Local variables:
4409 * eval: (c-set-style "gnu")
4410 * indent-tabs-mode: t
4411 * End:
4412 */