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