84f1a7589c009b98f7dab3ea6160ff1b56f7a43a
[external/binutils.git] / gas / config / tc-cris.c
1 /* tc-cris.c -- Assembler code for the CRIS CPU core.
2    Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
3
4    Contributed by Axis Communications AB, Lund, Sweden.
5    Originally written for GAS 1.38.1 by Mikael Asker.
6    Updates, BFDizing, GNUifying and ELF support by Hans-Peter Nilsson.
7
8    This file is part of GAS, the GNU Assembler.
9
10    GAS is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2, or (at your option)
13    any later version.
14
15    GAS is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GAS; see the file COPYING.  If not, write to the
22    Free Software Foundation, 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.  */
24
25 #include <stdio.h>
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 PIC_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
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 static void cris_process_instruction PARAMS ((char *,
117                                               struct cris_instruction *,
118                                               struct cris_prefix *));
119 static int get_bwd_size_modifier PARAMS ((char **, int *));
120 static int get_bw_size_modifier PARAMS ((char **, int *));
121 static int get_gen_reg PARAMS ((char **, int *));
122 static int get_spec_reg PARAMS ((char **,
123                                  const struct cris_spec_reg **));
124 static int get_autoinc_prefix_or_indir_op PARAMS ((char **,
125                                                    struct cris_prefix *,
126                                                    int *, int *, int *,
127                                                    expressionS *));
128 static int get_3op_or_dip_prefix_op PARAMS ((char **,
129                                              struct cris_prefix *));
130 static int cris_get_expression PARAMS ((char **, expressionS *));
131 static int get_flags PARAMS ((char **, int *));
132 static void gen_bdap PARAMS ((int, expressionS *));
133 static int branch_disp PARAMS ((int));
134 static void gen_cond_branch_32 PARAMS ((char *, char *, fragS *,
135                                         symbolS *, symbolS *, long int));
136 static void cris_number_to_imm PARAMS ((char *, long, int, fixS *, segT));
137 static void cris_create_short_jump PARAMS ((char *, addressT, addressT,
138                                             fragS *, symbolS *));
139 static void s_syntax PARAMS ((int));
140 static void s_cris_file PARAMS ((int));
141 static void s_cris_loc PARAMS ((int));
142
143 /* Get ":GOT", ":GOTOFF", ":PLT" etc. suffixes.  */
144 static void cris_get_pic_suffix PARAMS ((char **,
145                                          bfd_reloc_code_real_type *,
146                                          expressionS *));
147 static unsigned int cris_get_pic_reloc_size
148   PARAMS ((bfd_reloc_code_real_type));
149
150 /* All the .syntax functions.  */
151 static void cris_force_reg_prefix PARAMS ((void));
152 static void cris_relax_reg_prefix PARAMS ((void));
153 static void cris_sym_leading_underscore PARAMS ((void));
154 static void cris_sym_no_leading_underscore PARAMS ((void));
155 static char *cris_insn_first_word_frag PARAMS ((void));
156
157 /* Handle to the opcode hash table.  */
158 static struct hash_control *op_hash = NULL;
159
160 /* Whether we demand that registers have a `$' prefix.  Default here.  */
161 static bfd_boolean demand_register_prefix = FALSE;
162
163 /* Whether global user symbols have a leading underscore.  Default here.  */
164 static bfd_boolean symbols_have_leading_underscore = TRUE;
165
166 /* Whether or not we allow PIC, and expand to PIC-friendly constructs.  */
167 static bfd_boolean pic = FALSE;
168
169 const pseudo_typeS md_pseudo_table[] =
170 {
171   {"dword", cons, 4},
172   {"syntax", s_syntax, 0},
173   {"file", s_cris_file, 0},
174   {"loc", s_cris_loc, 0},
175   {NULL, 0, 0}
176 };
177
178 static int warn_for_branch_expansion = 0;
179
180 const char cris_comment_chars[] = ";";
181
182 /* This array holds the chars that only start a comment at the beginning of
183    a line.  If the line seems to have the form '# 123 filename'
184    .line and .file directives will appear in the pre-processed output.  */
185 /* Note that input_file.c hand-checks for '#' at the beginning of the
186    first line of the input file.  This is because the compiler outputs
187    #NO_APP at the beginning of its output.  */
188 /* Also note that slash-star will always start a comment.  */
189 const char line_comment_chars[] = "#";
190 const char line_separator_chars[] = "@";
191
192 /* Now all floating point support is shut off.  See md_atof.  */
193 const char EXP_CHARS[] = "";
194 const char FLT_CHARS[] = "";
195
196 /* For CRIS, we encode the relax_substateTs (in e.g. fr_substate) as:
197                        2                 1                 0
198       ---/ /--+-----------------+-----------------+-----------------+
199               |  what state ?   |            how long ?             |
200       ---/ /--+-----------------+-----------------+-----------------+
201
202    The "how long" bits are 00 = byte, 01 = word, 10 = dword (long).
203    This is a Un*x convention.
204    Not all lengths are legit for a given value of (what state).
205
206    Groups for CRIS address relaxing:
207
208    1. Bcc
209       length: byte, word, 10-byte expansion
210
211    2. BDAP
212       length: byte, word, dword  */
213
214 #define STATE_CONDITIONAL_BRANCH    (1)
215 #define STATE_BASE_PLUS_DISP_PREFIX (2)
216
217 #define STATE_LENGTH_MASK           (3)
218 #define STATE_BYTE                  (0)
219 #define STATE_WORD                  (1)
220 #define STATE_DWORD                 (2)
221 /* Symbol undefined.  */
222 #define STATE_UNDF                  (3)
223 #define STATE_MAX_LENGTH            (3)
224
225 /* These displacements are relative to the adress following the opcode
226    word of the instruction.  The first letter is Byte, Word.  The 2nd
227    letter is Forward, Backward.  */
228
229 #define BRANCH_BF ( 254)
230 #define BRANCH_BB (-256)
231 #define BRANCH_WF (2 +  32767)
232 #define BRANCH_WB (2 + -32768)
233
234 #define BDAP_BF   ( 127)
235 #define BDAP_BB   (-128)
236 #define BDAP_WF   ( 32767)
237 #define BDAP_WB   (-32768)
238
239 #define ENCODE_RELAX(what, length) (((what) << 2) + (length))
240
241 const relax_typeS md_cris_relax_table[] =
242 {
243   /* Error sentinel (0, 0).  */
244   {1,         1,         0,  0},
245
246   /* Unused (0, 1).  */
247   {1,         1,         0,  0},
248
249   /* Unused (0, 2).  */
250   {1,         1,         0,  0},
251
252   /* Unused (0, 3).  */
253   {1,         1,         0,  0},
254
255   /* Bcc o (1, 0).  */
256   {BRANCH_BF, BRANCH_BB, 0,  ENCODE_RELAX (1, 1)},
257
258   /* Bcc [PC+] (1, 1).  */
259   {BRANCH_WF, BRANCH_WB, 2,  ENCODE_RELAX (1, 2)},
260
261   /* BEXT/BWF, BA, JUMP (external), JUMP (always), Bnot_cc, JUMP (default)
262      (1, 2).  */
263   {0,         0,         10, 0},
264
265   /* Unused (1, 3).  */
266   {1,         1,         0,  0},
267
268   /* BDAP o (2, 0).  */
269   {BDAP_BF,   BDAP_BB,   0,  ENCODE_RELAX (2, 1)},
270
271   /* BDAP.[bw] [PC+] (2, 1).  */
272   {BDAP_WF,   BDAP_WB,   2,  ENCODE_RELAX (2, 2)},
273
274   /* BDAP.d [PC+] (2, 2).  */
275   {0,         0,         4,  0}
276 };
277
278 #undef BRANCH_BF
279 #undef BRANCH_BB
280 #undef BRANCH_WF
281 #undef BRANCH_WB
282 #undef BDAP_BF
283 #undef BDAP_BB
284 #undef BDAP_WF
285 #undef BDAP_WB
286
287 /* Target-specific multicharacter options, not const-declared at usage
288    in 2.9.1 and CVS of 2000-02-16.  */
289 struct option md_longopts[] =
290 {
291 #define OPTION_NO_US (OPTION_MD_BASE + 0)
292   {"no-underscore", no_argument, NULL, OPTION_NO_US},
293 #define OPTION_US (OPTION_MD_BASE + 1)
294   {"underscore", no_argument, NULL, OPTION_US},
295 #define OPTION_PIC (OPTION_MD_BASE + 2)
296   {"pic", no_argument, NULL, OPTION_PIC},
297   {NULL, no_argument, NULL, 0}
298 };
299
300 /* Not const-declared at usage in 2.9.1.  */
301 size_t md_longopts_size = sizeof (md_longopts);
302 const char *md_shortopts = "hHN";
303
304 /* At first glance, this may seems wrong and should be 4 (ba + nop); but
305    since a short_jump must skip a *number* of long jumps, it must also be
306    a long jump.  Here, we hope to make it a "ba [16bit_offs]" and a "nop"
307    for the delay slot and hope that the jump table at most needs
308    32767/4=8191 long-jumps.  A branch is better than a jump, since it is
309    relative; we will not have a reloc to fix up somewhere.
310
311    Note that we can't add relocs, because relaxation uses these fixed
312    numbers, and md_create_short_jump is called after relaxation.  */
313
314 const int md_short_jump_size = 6;
315 const int md_long_jump_size = 6;
316
317 /* Report output format.  Small changes in output format (like elf
318    variants below) can happen until all options are parsed, but after
319    that, the output format must remain fixed.  */
320
321 const char *
322 cris_target_format ()
323 {
324   switch (OUTPUT_FLAVOR)
325     {
326     case bfd_target_aout_flavour:
327       return "a.out-cris";
328
329     case bfd_target_elf_flavour:
330       if (symbols_have_leading_underscore)
331         return "elf32-us-cris";
332       return "elf32-cris";
333
334     default:
335       abort ();
336       return NULL;
337     }
338 }
339
340 /* We need a port-specific relaxation function to cope with sym2 - sym1
341    relative expressions with both symbols in the same segment (but not
342    necessarily in the same frag as this insn), for example:
343      move.d [pc+sym2-(sym1-2)],r10
344     sym1:
345    The offset can be 8, 16 or 32 bits long.  */
346
347 long
348 cris_relax_frag (seg, fragP, stretch)
349      segT seg ATTRIBUTE_UNUSED;
350      fragS *fragP;
351      long stretch ATTRIBUTE_UNUSED;
352 {
353   long growth;
354   offsetT aim = 0;
355   symbolS *symbolP;
356   const relax_typeS *this_type;
357   const relax_typeS *start_type;
358   relax_substateT next_state;
359   relax_substateT this_state;
360   const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
361
362   /* We only have to cope with frags as prepared by
363      md_estimate_size_before_relax.  The dword cases may get here
364      because of the different reasons that they aren't relaxable.  */
365   switch (fragP->fr_subtype)
366     {
367     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_DWORD):
368     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD):
369       /* When we get to these states, the frag won't grow any more.  */
370       return 0;
371
372     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_WORD):
373     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE):
374       if (fragP->fr_symbol == NULL
375           || S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
376         as_fatal (_("internal inconsistency problem in %s: fr_symbol %lx"),
377                   __FUNCTION__, (long) fragP->fr_symbol);
378       symbolP = fragP->fr_symbol;
379       if (symbol_resolved_p (symbolP))
380         as_fatal (_("internal inconsistency problem in %s: resolved symbol"),
381                   __FUNCTION__);
382       aim = S_GET_VALUE (symbolP);
383       break;
384
385     default:
386       as_fatal (_("internal inconsistency problem in %s: fr_subtype %d"),
387                   __FUNCTION__, fragP->fr_subtype);
388     }
389
390   /* The rest is stolen from relax_frag.  There's no obvious way to
391      share the code, but fortunately no requirement to keep in sync as
392      long as fragP->fr_symbol does not have its segment changed.  */
393
394   this_state = fragP->fr_subtype;
395   start_type = this_type = table + this_state;
396
397   if (aim < 0)
398     {
399       /* Look backwards.  */
400       for (next_state = this_type->rlx_more; next_state;)
401         if (aim >= this_type->rlx_backward)
402           next_state = 0;
403         else
404           {
405             /* Grow to next state.  */
406             this_state = next_state;
407             this_type = table + this_state;
408             next_state = this_type->rlx_more;
409           }
410     }
411   else
412     {
413       /* Look forwards.  */
414       for (next_state = this_type->rlx_more; next_state;)
415         if (aim <= this_type->rlx_forward)
416           next_state = 0;
417         else
418           {
419             /* Grow to next state.  */
420             this_state = next_state;
421             this_type = table + this_state;
422             next_state = this_type->rlx_more;
423           }
424     }
425
426   growth = this_type->rlx_length - start_type->rlx_length;
427   if (growth != 0)
428     fragP->fr_subtype = this_state;
429   return growth;
430 }
431
432 /* Prepare machine-dependent frags for relaxation.
433
434    Called just before relaxation starts. Any symbol that is now undefined
435    will not become defined.
436
437    Return the correct fr_subtype in the frag.
438
439    Return the initial "guess for fr_var" to caller.  The guess for fr_var
440    is *actually* the growth beyond fr_fix. Whatever we do to grow fr_fix
441    or fr_var contributes to our returned value.
442
443    Although it may not be explicit in the frag, pretend
444    fr_var starts with a value.  */
445
446 int
447 md_estimate_size_before_relax (fragP, segment_type)
448      fragS *fragP;
449      /* The segment is either N_DATA or N_TEXT.  */
450      segT segment_type;
451 {
452   int old_fr_fix;
453
454   old_fr_fix = fragP->fr_fix;
455
456   switch (fragP->fr_subtype)
457     {
458     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF):
459       if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
460         /* The symbol lies in the same segment - a relaxable case.  */
461         fragP->fr_subtype
462           = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE);
463       else
464         /* Unknown or not the same segment, so not relaxable.  */
465         fragP->fr_subtype
466           = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_DWORD);
467       fragP->fr_var = md_cris_relax_table[fragP->fr_subtype].rlx_length;
468       break;
469
470     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_UNDF):
471       /* Note that we can not do anything sane with relaxing
472          [rX + a_known_symbol_in_text], it will have to be a 32-bit
473          value.
474
475          We could play tricks with managing a constant pool and make
476          a_known_symbol_in_text a "bdap [pc + offset]" pointing there
477          (like the GOT for ELF shared libraries), but that's no use, it
478          would in general be no shorter or faster code, only more
479          complicated.  */
480
481       if (S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
482         {
483           /* Go for dword if not absolute or same segment.  */
484           fragP->fr_subtype
485             = ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD);
486           fragP->fr_var = md_cris_relax_table[fragP->fr_subtype].rlx_length;
487         }
488       else if (!symbol_resolved_p (fragP->fr_symbol))
489         {
490           /* The symbol will eventually be completely resolved as an
491              absolute expression, but right now it depends on the result
492              of relaxation and we don't know anything else about the
493              value.  We start relaxation with the assumption that it'll
494              fit in a byte.  */
495           fragP->fr_subtype
496             = ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE);
497           fragP->fr_var = md_cris_relax_table[fragP->fr_subtype].rlx_length;
498         }
499       else
500         {
501           /* Absolute expression.  */
502           long int value;
503           value = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
504
505           if (value >= -128 && value <= 127)
506             {
507               /* Byte displacement.  */
508               (fragP->fr_opcode)[0] = value;
509             }
510           else
511             {
512               /* Word or dword displacement.  */
513               int pow2_of_size = 1;
514               char *writep;
515
516               if (value < -32768 || value > 32767)
517                 {
518                   /* Outside word range, make it a dword.  */
519                   pow2_of_size = 2;
520                 }
521
522               /* Modify the byte-offset BDAP into a word or dword offset
523                  BDAP.  Or really, a BDAP rX,8bit into a
524                  BDAP.[wd] rX,[PC+] followed by a word or dword.  */
525               (fragP->fr_opcode)[0] = BDAP_PC_LOW + pow2_of_size * 16;
526
527               /* Keep the register number in the highest four bits.  */
528               (fragP->fr_opcode)[1] &= 0xF0;
529               (fragP->fr_opcode)[1] |= BDAP_INCR_HIGH;
530
531               /* It grew by two or four bytes.  */
532               fragP->fr_fix += 1 << pow2_of_size;
533               writep = fragP->fr_literal + old_fr_fix;
534               md_number_to_chars (writep, value, 1 << pow2_of_size);
535             }
536           frag_wane (fragP);
537         }
538       break;
539
540     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
541     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
542     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_DWORD):
543     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE):
544     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_WORD):
545     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD):
546       /* When relaxing a section for the second time, we don't need to
547          do anything except making sure that fr_var is set right.  */
548       fragP->fr_var = md_cris_relax_table[fragP->fr_subtype].rlx_length;
549       break;
550
551     default:
552       BAD_CASE (fragP->fr_subtype);
553     }
554
555   return fragP->fr_var + (fragP->fr_fix - old_fr_fix);
556 }
557
558 /* Perform post-processing of machine-dependent frags after relaxation.
559    Called after relaxation is finished.
560    In:  Address of frag.
561         fr_type == rs_machine_dependent.
562         fr_subtype is what the address relaxed to.
563
564    Out: Any fixS:s and constants are set up.
565
566    The caller will turn the frag into a ".space 0".  */
567
568 void
569 md_convert_frag (abfd, sec, fragP)
570      bfd *abfd ATTRIBUTE_UNUSED;
571      segT sec ATTRIBUTE_UNUSED;
572      fragS *fragP;
573 {
574   /* Pointer to first byte in variable-sized part of the frag.  */
575   char *var_partp;
576
577   /* Pointer to first opcode byte in frag.  */
578   char *opcodep;
579
580   /* Used to check integrity of the relaxation.
581      One of 2 = long, 1 = word, or 0 = byte.  */
582   int length_code;
583
584   /* Size in bytes of variable-sized part of frag.  */
585   int var_part_size = 0;
586
587   /* This is part of *fragP.  It contains all information about addresses
588      and offsets to varying parts.  */
589   symbolS *symbolP;
590   unsigned long var_part_offset;
591
592   /* Where, in file space, is _var of *fragP?  */
593   unsigned long address_of_var_part = 0;
594
595   /* Where, in file space, does addr point?  */
596   unsigned long target_address;
597
598   know (fragP->fr_type == rs_machine_dependent);
599
600   length_code = fragP->fr_subtype & STATE_LENGTH_MASK;
601   know (length_code >= 0 && length_code < STATE_MAX_LENGTH);
602
603   var_part_offset = fragP->fr_fix;
604   var_partp = fragP->fr_literal + var_part_offset;
605   opcodep = fragP->fr_opcode;
606
607   symbolP = fragP->fr_symbol;
608   target_address = (symbolP ? S_GET_VALUE (symbolP) : 0) + fragP->fr_offset;
609   address_of_var_part = fragP->fr_address + var_part_offset;
610
611   switch (fragP->fr_subtype)
612     {
613     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
614       opcodep[0] = branch_disp ((target_address - address_of_var_part));
615       var_part_size = 0;
616       break;
617
618     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
619       /* We had a quick immediate branch, now turn it into a word one i.e. a
620          PC autoincrement.  */
621       opcodep[0] = BRANCH_PC_LOW;
622       opcodep[1] &= 0xF0;
623       opcodep[1] |= BRANCH_INCR_HIGH;
624       md_number_to_chars (var_partp,
625                           (long) (target_address - (address_of_var_part + 2)),
626                           2);
627       var_part_size = 2;
628       break;
629
630     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_DWORD):
631       gen_cond_branch_32 (fragP->fr_opcode, var_partp, fragP,
632                           fragP->fr_symbol, (symbolS *) NULL,
633                           fragP->fr_offset);
634       /* Ten bytes added: a branch, nop and a jump.  */
635       var_part_size = 2 + 2 + 4 + 2;
636       break;
637
638     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_BYTE):
639       if (symbolP == NULL)
640         as_fatal (_("internal inconsistency in %s: bdapq no symbol"),
641                     __FUNCTION__);
642       opcodep[0] = S_GET_VALUE (symbolP);
643       var_part_size = 0;
644       break;
645
646     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_WORD):
647       /* We had a BDAP 8-bit "quick immediate", now turn it into a 16-bit
648          one that uses PC autoincrement.  */
649       opcodep[0] = BDAP_PC_LOW + (1 << 4);
650       opcodep[1] &= 0xF0;
651       opcodep[1] |= BDAP_INCR_HIGH;
652       if (symbolP == NULL)
653         as_fatal (_("internal inconsistency in %s: bdap.w with no symbol"),
654                   __FUNCTION__);
655       md_number_to_chars (var_partp, S_GET_VALUE (symbolP), 2);
656       var_part_size = 2;
657       break;
658
659     case ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_DWORD):
660       /* We had a BDAP 16-bit "word", change the offset to a dword.  */
661       opcodep[0] = BDAP_PC_LOW + (2 << 4);
662       opcodep[1] &= 0xF0;
663       opcodep[1] |= BDAP_INCR_HIGH;
664       if (fragP->fr_symbol == NULL)
665         md_number_to_chars (var_partp, fragP->fr_offset, 4);
666       else
667         fix_new (fragP, var_partp - fragP->fr_literal, 4, fragP->fr_symbol,
668                  fragP->fr_offset, 0, BFD_RELOC_32);
669       var_part_size = 4;
670       break;
671
672     default:
673       BAD_CASE (fragP->fr_subtype);
674       break;
675     }
676
677   fragP->fr_fix += var_part_size;
678 }
679
680 /* Generate a short jump around a secondary jump table.
681    Used by md_create_long_jump.
682
683    This used to be md_create_short_jump, but is now called from
684    md_create_long_jump instead, when sufficient.
685    since the sizes of the jumps are the same.  It used to be brittle,
686    making possibilities for creating bad code.  */
687
688 static void
689 cris_create_short_jump (storep, from_addr, to_addr, fragP, to_symbol)
690      char *storep;
691      addressT from_addr;
692      addressT to_addr;
693      fragS *fragP ATTRIBUTE_UNUSED;
694      symbolS *to_symbol ATTRIBUTE_UNUSED;
695 {
696   long int distance;
697
698   distance = to_addr - from_addr;
699
700   if (-254 <= distance && distance <= 256)
701     {
702       /* Create a "short" short jump: "BA distance - 2".  */
703       storep[0] = branch_disp (distance - 2);
704       storep[1] = BA_QUICK_HIGH;
705
706       /* A nop for the delay slot.  */
707       md_number_to_chars (storep + 2, NOP_OPCODE, 2);
708
709       /* The extra word should be filled with something sane too.  Make it
710          a nop to keep disassembly sane.  */
711       md_number_to_chars (storep + 4, NOP_OPCODE, 2);
712     }
713   else
714     {
715       /* Make it a "long" short jump: "BA (PC+)".  */
716       md_number_to_chars (storep, BA_PC_INCR_OPCODE, 2);
717
718       /* ".WORD distance - 4".  */
719       md_number_to_chars (storep + 2, (long) (distance - 4), 2);
720
721       /* A nop for the delay slot.  */
722       md_number_to_chars (storep + 4, NOP_OPCODE, 2);
723     }
724 }
725
726 /* Generate a long jump in a secondary jump table.
727
728    storep  Where to store the jump instruction.
729    from_addr  Address of the jump instruction.
730    to_addr    Destination address of the jump.
731    fragP      Which frag the destination address operand
732               lies in.
733    to_symbol  Destination symbol.  */
734
735 void
736 md_create_long_jump (storep, from_addr, to_addr, fragP, to_symbol)
737      char *storep;
738      addressT from_addr;
739      addressT to_addr;
740      fragS *fragP;
741      symbolS *to_symbol;
742 {
743   long int distance;
744
745   distance = to_addr - from_addr;
746
747   if (-32763 <= distance && distance <= 32772)
748     {
749       /* Then make it a "short" long jump.  */
750       cris_create_short_jump (storep, from_addr, to_addr, fragP,
751                               to_symbol);
752     }
753   else
754     {
755       /* We have a "long" long jump: "JUMP [PC+]".
756          Make it an "ADD [PC+],PC" if we're supposed to emit PIC code.  */
757       md_number_to_chars (storep,
758                           pic ? ADD_PC_INCR_OPCODE : JUMP_PC_INCR_OPCODE, 2);
759
760       /* Follow with a ".DWORD to_addr", PC-relative for PIC.  */
761       fix_new (fragP, storep + 2 - fragP->fr_literal, 4, to_symbol,
762                0, pic ? 1 : 0, pic ? BFD_RELOC_32_PCREL : BFD_RELOC_32);
763     }
764 }
765
766 /* Allocate space for the first piece of an insn, and mark it as the
767    start of the insn for debug-format use.  */
768
769 static char *
770 cris_insn_first_word_frag ()
771 {
772   char *insnp = frag_more (2);
773
774   /* We need to mark the start of the insn by passing dwarf2_emit_insn
775      the offset from the current fragment position.  This must be done
776      after the first fragment is created but before any other fragments
777      (fixed or varying) are created.  Note that the offset only
778      corresponds to the "size" of the insn for a fixed-size,
779      non-expanded insn.  */
780   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
781     dwarf2_emit_insn (2);
782
783   return insnp;
784 }
785
786 /* Port-specific assembler initialization.  */
787
788 void
789 md_begin ()
790 {
791   const char *hashret = NULL;
792   int i = 0;
793
794   /* Set up a hash table for the instructions.  */
795   op_hash = hash_new ();
796   if (op_hash == NULL)
797     as_fatal (_("Virtual memory exhausted"));
798
799   while (cris_opcodes[i].name != NULL)
800     {
801       const char *name = cris_opcodes[i].name;
802       hashret = hash_insert (op_hash, name, (PTR) &cris_opcodes[i]);
803
804       if (hashret != NULL && *hashret != '\0')
805         as_fatal (_("Can't hash `%s': %s\n"), cris_opcodes[i].name,
806                   *hashret == 0 ? _("(unknown reason)") : hashret);
807       do
808         {
809           if (cris_opcodes[i].match & cris_opcodes[i].lose)
810             as_fatal (_("Buggy opcode: `%s' \"%s\"\n"), cris_opcodes[i].name,
811                       cris_opcodes[i].args);
812
813           ++i;
814         }
815       while (cris_opcodes[i].name != NULL
816              && strcmp (cris_opcodes[i].name, name) == 0);
817     }
818 }
819
820 /* Assemble a source line.  */
821
822 void
823 md_assemble (str)
824      char *str;
825 {
826   struct cris_instruction output_instruction;
827   struct cris_prefix prefix;
828   char *opcodep;
829   char *p;
830
831   know (str);
832
833   /* Do the low-level grunt - assemble to bits and split up into a prefix
834      and ordinary insn.  */
835   cris_process_instruction (str, &output_instruction, &prefix);
836
837   /* Handle any prefixes to the instruction.  */
838   switch (prefix.kind)
839     {
840     case PREFIX_NONE:
841       break;
842
843       /* When the expression is unknown for a BDAP, it can need 0, 2 or 4
844          extra bytes, so we handle it separately.  */
845     case PREFIX_BDAP_IMM:
846       /* We only do it if the relocation is unspecified, i.e. not a PIC
847          relocation.  */
848       if (prefix.reloc == BFD_RELOC_NONE)
849         {
850           gen_bdap (prefix.base_reg_number, &prefix.expr);
851           break;
852         }
853       /* Fall through.  */
854     case PREFIX_BDAP:
855     case PREFIX_BIAP:
856     case PREFIX_DIP:
857       opcodep = cris_insn_first_word_frag ();
858
859       /* Output the prefix opcode.  */
860       md_number_to_chars (opcodep, (long) prefix.opcode, 2);
861
862       /* Having a specified reloc only happens for DIP and for BDAP with
863          PIC operands, but it is ok to drop through here for the other
864          prefixes as they can have no relocs specified.  */
865       if (prefix.reloc != BFD_RELOC_NONE)
866         {
867           unsigned int relocsize
868             = (prefix.kind == PREFIX_DIP
869                ? 4 : cris_get_pic_reloc_size (prefix.reloc));
870
871           p = frag_more (relocsize);
872           fix_new_exp (frag_now, (p - frag_now->fr_literal), relocsize,
873                        &prefix.expr, 0, prefix.reloc);
874         }
875       break;
876
877     case PREFIX_PUSH:
878       opcodep = cris_insn_first_word_frag ();
879
880       /* Output the prefix opcode.  Being a "push", we add the negative
881          size of the register to "sp".  */
882       if (output_instruction.spec_reg != NULL)
883         {
884           /* Special register.  */
885           opcodep[0] = -output_instruction.spec_reg->reg_size;
886         }
887       else
888         {
889           /* General register.  */
890           opcodep[0] = -4;
891         }
892       opcodep[1] = (REG_SP << 4) + (BDAP_QUICK_OPCODE >> 8);
893       break;
894
895     default:
896       BAD_CASE (prefix.kind);
897     }
898
899   /* If we only had a prefix insn, we're done.  */
900   if (output_instruction.insn_type == CRIS_INSN_NONE)
901     return;
902
903   /* Done with the prefix.  Continue with the main instruction.  */
904   if (prefix.kind == PREFIX_NONE)
905     opcodep = cris_insn_first_word_frag ();
906   else
907     opcodep = frag_more (2);
908
909   /* Output the instruction opcode.  */
910   md_number_to_chars (opcodep, (long) (output_instruction.opcode), 2);
911
912   /* Output the symbol-dependent instruction stuff.  */
913   if (output_instruction.insn_type == CRIS_INSN_BRANCH)
914     {
915       segT to_seg = absolute_section;
916       int is_undefined = 0;
917       int length_code;
918
919       if (output_instruction.expr.X_op != O_constant)
920         {
921           to_seg = S_GET_SEGMENT (output_instruction.expr.X_add_symbol);
922
923           if (to_seg == undefined_section)
924             is_undefined = 1;
925         }
926
927       if (to_seg == now_seg || is_undefined)
928         {
929           /* Handle complex expressions.  */
930           valueT addvalue
931             = (SIMPLE_EXPR (&output_instruction.expr)
932                ? output_instruction.expr.X_add_number
933                : 0);
934           symbolS *sym
935             = (SIMPLE_EXPR (&output_instruction.expr)
936                ? output_instruction.expr.X_add_symbol
937                : make_expr_symbol (&output_instruction.expr));
938
939           /* If is_undefined, then the expression may BECOME now_seg.  */
940           length_code = is_undefined ? STATE_UNDF : STATE_BYTE;
941
942           /* Make room for max ten bytes of variable length.  */
943           frag_var (rs_machine_dependent, 10, 0,
944                     ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code),
945                     sym, addvalue, opcodep);
946         }
947       else
948         {
949           /* We have: to_seg != now_seg && to_seg != undefined_section.
950              This means it is a branch to a known symbol in another
951              section, perhaps an absolute address.  Emit a 32-bit branch.  */
952           char *cond_jump = frag_more (10);
953
954           gen_cond_branch_32 (opcodep, cond_jump, frag_now,
955                               output_instruction.expr.X_add_symbol,
956                               (symbolS *) NULL,
957                               output_instruction.expr.X_add_number);
958         }
959     }
960   else
961     {
962       if (output_instruction.imm_oprnd_size > 0)
963         {
964           /* The intruction has an immediate operand.  */
965           enum bfd_reloc_code_real reloc = BFD_RELOC_NONE;
966
967           switch (output_instruction.imm_oprnd_size)
968             {
969               /* Any byte-size immediate constants are treated as
970                  word-size.  FIXME: Thus overflow check does not work
971                  correctly.  */
972
973             case 2:
974               /* Note that size-check for the explicit reloc has already
975                  been done when we get here.  */
976               if (output_instruction.reloc != BFD_RELOC_NONE)
977                 reloc = output_instruction.reloc;
978               else
979                 reloc = BFD_RELOC_16;
980               break;
981
982             case 4:
983               /* Allow a relocation specified in the operand.  */
984               if (output_instruction.reloc != BFD_RELOC_NONE)
985                 reloc = output_instruction.reloc;
986               else
987                 reloc = BFD_RELOC_32;
988               break;
989
990             default:
991               BAD_CASE (output_instruction.imm_oprnd_size);
992             }
993
994           p = frag_more (output_instruction.imm_oprnd_size);
995           fix_new_exp (frag_now, (p - frag_now->fr_literal),
996                        output_instruction.imm_oprnd_size,
997                        &output_instruction.expr, 0, reloc);
998         }
999       else if (output_instruction.reloc != BFD_RELOC_NONE)
1000         {
1001           /* An immediate operand that has a relocation and needs to be
1002              processed further.  */
1003
1004           /* It is important to use fix_new_exp here and everywhere else
1005              (and not fix_new), as fix_new_exp can handle "difference
1006              expressions" - where the expression contains a difference of
1007              two symbols in the same segment.  */
1008           fix_new_exp (frag_now, (opcodep - frag_now->fr_literal), 2,
1009                        &output_instruction.expr, 0,
1010                        output_instruction.reloc);
1011         }
1012     }
1013 }
1014
1015 /* Low level text-to-bits assembly.  */
1016
1017 static void
1018 cris_process_instruction (insn_text, out_insnp, prefixp)
1019      char *insn_text;
1020      struct cris_instruction *out_insnp;
1021      struct cris_prefix *prefixp;
1022 {
1023   char *s;
1024   char modified_char = 0;
1025   const char *args;
1026   struct cris_opcode *instruction;
1027   char *operands;
1028   int match = 0;
1029   int mode;
1030   int regno;
1031   int size_bits;
1032
1033   /* Reset these fields to a harmless state in case we need to return in
1034      error.  */
1035   prefixp->kind = PREFIX_NONE;
1036   prefixp->reloc = BFD_RELOC_NONE;
1037   out_insnp->insn_type = CRIS_INSN_NORMAL;
1038   out_insnp->imm_oprnd_size = 0;
1039
1040   /* Find the end of the opcode mnemonic.  We assume (true in 2.9.1)
1041      that the caller has translated the opcode to lower-case, up to the
1042      first non-letter.  */
1043   for (operands = insn_text; ISLOWER (*operands); ++operands)
1044     ;
1045
1046   /* Terminate the opcode after letters, but save the character there if
1047      it was of significance.  */
1048   switch (*operands)
1049     {
1050     case '\0':
1051       break;
1052
1053     case '.':
1054       /* Put back the modified character later.  */
1055       modified_char = *operands;
1056       /* Fall through.  */
1057
1058     case ' ':
1059       /* Consume the character after the mnemonic
1060          and replace it with '\0'.  */
1061       *operands++ = '\0';
1062       break;
1063
1064     default:
1065       as_bad (_("Unknown opcode: `%s'"), insn_text);
1066       return;
1067     }
1068
1069   /* Find the instruction.  */
1070   instruction = (struct cris_opcode *) hash_find (op_hash, insn_text);
1071   if (instruction == NULL)
1072     {
1073       as_bad (_("Unknown opcode: `%s'"), insn_text);
1074       return;
1075     }
1076
1077   /* Put back the modified character.  */
1078   switch (modified_char)
1079     {
1080     case 0:
1081       break;
1082
1083     default:
1084       *--operands = modified_char;
1085     }
1086
1087   /* Try to match an opcode table slot.  */
1088   for (s = operands;;)
1089     {
1090       int imm_expr_found;
1091
1092       /* Initialize *prefixp, perhaps after being modified for a
1093          "near match".  */
1094       prefixp->kind = PREFIX_NONE;
1095       prefixp->reloc = BFD_RELOC_NONE;
1096
1097       /* Initialize *out_insnp.  */
1098       memset (out_insnp, 0, sizeof (*out_insnp));
1099       out_insnp->opcode = instruction->match;
1100       out_insnp->reloc = BFD_RELOC_NONE;
1101       out_insnp->insn_type = CRIS_INSN_NORMAL;
1102       out_insnp->imm_oprnd_size = 0;
1103
1104       imm_expr_found = 0;
1105
1106       /* Build the opcode, checking as we go to make sure that the
1107          operands match.  */
1108       for (args = instruction->args;; ++args)
1109         {
1110           switch (*args)
1111             {
1112             case '\0':
1113               /* If we've come to the end of arguments, we're done.  */
1114               if (*s == '\0')
1115                 match = 1;
1116               break;
1117
1118             case '!':
1119               /* Non-matcher character for disassembly.
1120                  Ignore it here.  */
1121               continue;
1122
1123             case ',':
1124             case ' ':
1125               /* These must match exactly.  */
1126               if (*s++ == *args)
1127                 continue;
1128               break;
1129
1130             case 'B':
1131               /* This is not really an operand, but causes a "BDAP
1132                  -size,SP" prefix to be output, for PUSH instructions.  */
1133               prefixp->kind = PREFIX_PUSH;
1134               continue;
1135
1136             case 'b':
1137               /* This letter marks an operand that should not be matched
1138                  in the assembler. It is a branch with 16-bit
1139                  displacement.  The assembler will create them from the
1140                  8-bit flavor when necessary.  The assembler does not
1141                  support the [rN+] operand, as the [r15+] that is
1142                  generated for 16-bit displacements.  */
1143               break;
1144
1145             case 'c':
1146               /* A 5-bit unsigned immediate in bits <4:0>.  */
1147               if (! cris_get_expression (&s, &out_insnp->expr))
1148                 break;
1149               else
1150                 {
1151                   if (out_insnp->expr.X_op == O_constant
1152                       && (out_insnp->expr.X_add_number < 0
1153                           || out_insnp->expr.X_add_number > 31))
1154                     as_bad (_("Immediate value not in 5 bit unsigned range: %ld"),
1155                             out_insnp->expr.X_add_number);
1156
1157                   out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_5;
1158                   continue;
1159                 }
1160
1161             case 'C':
1162               /* A 4-bit unsigned immediate in bits <3:0>.  */
1163               if (! cris_get_expression (&s, &out_insnp->expr))
1164                 break;
1165               else
1166                 {
1167                   if (out_insnp->expr.X_op == O_constant
1168                       && (out_insnp->expr.X_add_number < 0
1169                           || out_insnp->expr.X_add_number > 15))
1170                     as_bad (_("Immediate value not in 4 bit unsigned range: %ld"),
1171                             out_insnp->expr.X_add_number);
1172
1173                   out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_4;
1174                   continue;
1175                 }
1176
1177             case 'D':
1178               /* General register in bits <15:12> and <3:0>.  */
1179               if (! get_gen_reg (&s, &regno))
1180                 break;
1181               else
1182                 {
1183                   out_insnp->opcode |= regno /* << 0 */;
1184                   out_insnp->opcode |= regno << 12;
1185                   continue;
1186                 }
1187
1188             case 'f':
1189               /* Flags from the condition code register.  */
1190               {
1191                 int flags = 0;
1192
1193                 if (! get_flags (&s, &flags))
1194                   break;
1195
1196                 out_insnp->opcode |= ((flags & 0xf0) << 8) | (flags & 0xf);
1197                 continue;
1198               }
1199
1200             case 'i':
1201               /* A 6-bit signed immediate in bits <5:0>.  */
1202               if (! cris_get_expression (&s, &out_insnp->expr))
1203                 break;
1204               else
1205                 {
1206                   if (out_insnp->expr.X_op == O_constant
1207                       && (out_insnp->expr.X_add_number < -32
1208                           || out_insnp->expr.X_add_number > 31))
1209                     as_bad (_("Immediate value not in 6 bit range: %ld"),
1210                             out_insnp->expr.X_add_number);
1211                   out_insnp->reloc = BFD_RELOC_CRIS_SIGNED_6;
1212                   continue;
1213                 }
1214
1215             case 'I':
1216               /* A 6-bit unsigned immediate in bits <5:0>.  */
1217               if (! cris_get_expression (&s, &out_insnp->expr))
1218                 break;
1219               else
1220                 {
1221                   if (out_insnp->expr.X_op == O_constant
1222                       && (out_insnp->expr.X_add_number < 0
1223                           || out_insnp->expr.X_add_number > 63))
1224                     as_bad (_("Immediate value not in 6 bit unsigned range: %ld"),
1225                             out_insnp->expr.X_add_number);
1226                   out_insnp->reloc = BFD_RELOC_CRIS_UNSIGNED_6;
1227                   continue;
1228                 }
1229
1230             case 'M':
1231               /* A size modifier, B, W or D, to be put in a bit position
1232                  suitable for CLEAR instructions (i.e. reflecting a zero
1233                  register).  */
1234               if (! get_bwd_size_modifier (&s, &size_bits))
1235                 break;
1236               else
1237                 {
1238                   switch (size_bits)
1239                     {
1240                     case 0:
1241                       out_insnp->opcode |= 0 << 12;
1242                       break;
1243
1244                     case 1:
1245                       out_insnp->opcode |= 4 << 12;
1246                       break;
1247
1248                     case 2:
1249                       out_insnp->opcode |= 8 << 12;
1250                       break;
1251                     }
1252                   continue;
1253                 }
1254
1255             case 'm':
1256               /* A size modifier, B, W or D, to be put in bits <5:4>.  */
1257               if (! get_bwd_size_modifier (&s, &size_bits))
1258                 break;
1259               else
1260                 {
1261                   out_insnp->opcode |= size_bits << 4;
1262                   continue;
1263                 }
1264
1265             case 'o':
1266               /* A branch expression.  */
1267               if (! cris_get_expression (&s, &out_insnp->expr))
1268                 break;
1269               else
1270                 {
1271                   out_insnp->insn_type = CRIS_INSN_BRANCH;
1272                   continue;
1273                 }
1274
1275             case 'O':
1276               /* A BDAP expression for any size, "expr,r".  */
1277               if (! cris_get_expression (&s, &prefixp->expr))
1278                 break;
1279               else
1280                 {
1281                   if (*s != ',')
1282                     break;
1283
1284                   s++;
1285
1286                   if (!get_gen_reg (&s, &prefixp->base_reg_number))
1287                     break;
1288
1289                   /* Since 'O' is used with an explicit bdap, we have no
1290                      "real" instruction.  */
1291                   prefixp->kind = PREFIX_BDAP_IMM;
1292                   prefixp->opcode
1293                     = BDAP_QUICK_OPCODE | (prefixp->base_reg_number << 12);
1294
1295                   out_insnp->insn_type = CRIS_INSN_NONE;
1296                   continue;
1297                 }
1298
1299             case 'P':
1300               /* Special register in bits <15:12>.  */
1301               if (! get_spec_reg (&s, &out_insnp->spec_reg))
1302                 break;
1303               else
1304                 {
1305                   /* Use of some special register names come with a
1306                      specific warning.  Note that we have no ".cpu type"
1307                      pseudo yet, so some of this is just unused
1308                      framework.  */
1309                   if (out_insnp->spec_reg->warning)
1310                     as_warn (out_insnp->spec_reg->warning);
1311                   else if (out_insnp->spec_reg->applicable_version
1312                            == cris_ver_warning)
1313                     /* Others have a generic warning.  */
1314                     as_warn (_("Unimplemented register `%s' specified"),
1315                              out_insnp->spec_reg->name);
1316
1317                   out_insnp->opcode
1318                     |= out_insnp->spec_reg->number << 12;
1319                   continue;
1320                 }
1321
1322             case 'p':
1323               /* This character is used in the disassembler to
1324                  recognize a prefix instruction to fold into the
1325                  addressing mode for the next instruction.  It is
1326                  ignored here.  */
1327               continue;
1328
1329             case 'R':
1330               /* General register in bits <15:12>.  */
1331               if (! get_gen_reg (&s, &regno))
1332                 break;
1333               else
1334                 {
1335                   out_insnp->opcode |= regno << 12;
1336                   continue;
1337                 }
1338
1339             case 'r':
1340               /* General register in bits <3:0>.  */
1341               if (! get_gen_reg (&s, &regno))
1342                 break;
1343               else
1344                 {
1345                   out_insnp->opcode |= regno /* << 0 */;
1346                   continue;
1347                 }
1348
1349             case 'S':
1350               /* Source operand in bit <10> and a prefix; a 3-operand
1351                  prefix.  */
1352               if (! get_3op_or_dip_prefix_op (&s, prefixp))
1353                 break;
1354               else
1355                 continue;
1356
1357             case 's':
1358               /* Source operand in bits <10>, <3:0> and optionally a
1359                  prefix; i.e. an indirect operand or an side-effect
1360                  prefix.  */
1361               if (! get_autoinc_prefix_or_indir_op (&s, prefixp, &mode,
1362                                                     &regno,
1363                                                     &imm_expr_found,
1364                                                     &out_insnp->expr))
1365                 break;
1366               else
1367                 {
1368                   if (prefixp->kind != PREFIX_NONE)
1369                     {
1370                       /* A prefix, so it has the autoincrement bit
1371                          set.  */
1372                       out_insnp->opcode |= (AUTOINCR_BIT << 8);
1373                     }
1374                   else
1375                     {
1376                       /* No prefix.  The "mode" variable contains bits like
1377                          whether or not this is autoincrement mode.  */
1378                       out_insnp->opcode |= (mode << 10);
1379
1380                       /* If there was a PIC reloc specifier, then it was
1381                          attached to the prefix.  Note that we can't check
1382                          that the reloc size matches, since we don't have
1383                          all the operands yet in all cases.  */
1384                       if (prefixp->reloc != BFD_RELOC_NONE)
1385                         out_insnp->reloc = prefixp->reloc;
1386                     }
1387
1388                   out_insnp->opcode |= regno /* << 0 */ ;
1389                   continue;
1390                 }
1391
1392             case 'x':
1393               /* Rs.m in bits <15:12> and <5:4>.  */
1394               if (! get_gen_reg (&s, &regno)
1395                   || ! get_bwd_size_modifier (&s, &size_bits))
1396                 break;
1397               else
1398                 {
1399                   out_insnp->opcode |= (regno << 12) | (size_bits << 4);
1400                   continue;
1401                 }
1402
1403             case 'y':
1404               /* Source operand in bits <10>, <3:0> and optionally a
1405                  prefix; i.e. an indirect operand or an side-effect
1406                  prefix.
1407
1408                  The difference to 's' is that this does not allow an
1409                  "immediate" expression.  */
1410               if (! get_autoinc_prefix_or_indir_op (&s, prefixp,
1411                                                     &mode, &regno,
1412                                                     &imm_expr_found,
1413                                                     &out_insnp->expr)
1414                   || imm_expr_found)
1415                 break;
1416               else
1417                 {
1418                   if (prefixp->kind != PREFIX_NONE)
1419                     {
1420                       /* A prefix, and those matched here always have
1421                          side-effects (see 's' case).  */
1422                       out_insnp->opcode |= (AUTOINCR_BIT << 8);
1423                     }
1424                   else
1425                     {
1426                       /* No prefix.  The "mode" variable contains bits
1427                          like whether or not this is autoincrement
1428                          mode.  */
1429                       out_insnp->opcode |= (mode << 10);
1430                     }
1431
1432                   out_insnp->opcode |= regno /* << 0 */;
1433                   continue;
1434                 }
1435
1436             case 'z':
1437               /* Size modifier (B or W) in bit <4>.  */
1438               if (! get_bw_size_modifier (&s, &size_bits))
1439                 break;
1440               else
1441                 {
1442                   out_insnp->opcode |= size_bits << 4;
1443                   continue;
1444                 }
1445
1446             default:
1447               BAD_CASE (*args);
1448             }
1449
1450           /* We get here when we fail a match above or we found a
1451              complete match.  Break out of this loop.  */
1452           break;
1453         }
1454
1455       /* Was it a match or a miss?  */
1456       if (match == 0)
1457         {
1458           /* If it's just that the args don't match, maybe the next
1459              item in the table is the same opcode but with
1460              matching operands.  */
1461           if (instruction[1].name != NULL
1462               && ! strcmp (instruction->name, instruction[1].name))
1463             {
1464               /* Yep.  Restart and try that one instead.  */
1465               ++instruction;
1466               s = operands;
1467               continue;
1468             }
1469           else
1470             {
1471               /* We've come to the end of instructions with this
1472                  opcode, so it must be an error.  */
1473               as_bad (_("Illegal operands"));
1474               return;
1475             }
1476         }
1477       else
1478         {
1479           /* We have a match.  Check if there's anything more to do.  */
1480           if (imm_expr_found)
1481             {
1482               /* There was an immediate mode operand, so we must check
1483                  that it has an appropriate size.  */
1484               switch (instruction->imm_oprnd_size)
1485                 {
1486                 default:
1487                 case SIZE_NONE:
1488                   /* Shouldn't happen; this one does not have immediate
1489                      operands with different sizes.  */
1490                   BAD_CASE (instruction->imm_oprnd_size);
1491                   break;
1492
1493                 case SIZE_FIX_32:
1494                   out_insnp->imm_oprnd_size = 4;
1495                   break;
1496
1497                 case SIZE_SPEC_REG:
1498                   switch (out_insnp->spec_reg->reg_size)
1499                     {
1500                     case 1:
1501                       if (out_insnp->expr.X_op == O_constant
1502                           && (out_insnp->expr.X_add_number < -128
1503                               || out_insnp->expr.X_add_number > 255))
1504                         as_bad (_("Immediate value not in 8 bit range: %ld"),
1505                                 out_insnp->expr.X_add_number);
1506                       /* Fall through.  */
1507                     case 2:
1508                       /* FIXME:  We need an indicator in the instruction
1509                          table to pass on, to indicate if we need to check
1510                          overflow for a signed or unsigned number.  */
1511                       if (out_insnp->expr.X_op == O_constant
1512                           && (out_insnp->expr.X_add_number < -32768
1513                               || out_insnp->expr.X_add_number > 65535))
1514                         as_bad (_("Immediate value not in 16 bit range: %ld"),
1515                                 out_insnp->expr.X_add_number);
1516                       out_insnp->imm_oprnd_size = 2;
1517                       break;
1518
1519                     case 4:
1520                       out_insnp->imm_oprnd_size = 4;
1521                       break;
1522
1523                     default:
1524                       BAD_CASE (out_insnp->spec_reg->reg_size);
1525                     }
1526                   break;
1527
1528                 case SIZE_FIELD:
1529                   switch (size_bits)
1530                     {
1531                     case 0:
1532                       if (out_insnp->expr.X_op == O_constant
1533                           && (out_insnp->expr.X_add_number < -128
1534                               || out_insnp->expr.X_add_number > 255))
1535                         as_bad (_("Immediate value not in 8 bit range: %ld"),
1536                                 out_insnp->expr.X_add_number);
1537                       /* Fall through.  */
1538                     case 1:
1539                       if (out_insnp->expr.X_op == O_constant
1540                           && (out_insnp->expr.X_add_number < -32768
1541                               || out_insnp->expr.X_add_number > 65535))
1542                         as_bad (_("Immediate value not in 16 bit range: %ld"),
1543                                 out_insnp->expr.X_add_number);
1544                       out_insnp->imm_oprnd_size = 2;
1545                       break;
1546
1547                     case 2:
1548                       out_insnp->imm_oprnd_size = 4;
1549                       break;
1550
1551                     default:
1552                       BAD_CASE (out_insnp->spec_reg->reg_size);
1553                     }
1554                 }
1555
1556               /* If there was a relocation specified for the immediate
1557                  expression (i.e. it had a PIC modifier) check that the
1558                  size of the PIC relocation matches the size specified by
1559                  the opcode.  */
1560               if (out_insnp->reloc != BFD_RELOC_NONE
1561                   && (cris_get_pic_reloc_size (out_insnp->reloc)
1562                       != (unsigned int) out_insnp->imm_oprnd_size))
1563                 as_bad (_("PIC relocation size does not match operand size"));
1564             }
1565         }
1566       break;
1567     }
1568 }
1569
1570 /* Get a B, W, or D size modifier from the string pointed out by *cPP,
1571    which must point to a '.' in front of the modifier.  On successful
1572    return, *cPP is advanced to the character following the size
1573    modifier, and is undefined otherwise.
1574
1575    cPP          Pointer to pointer to string starting
1576                 with the size modifier.
1577
1578    size_bitsp   Pointer to variable to contain the size bits on
1579                 successful return.
1580
1581    Return 1 iff a correct size modifier is found, else 0.  */
1582
1583 static int
1584 get_bwd_size_modifier (cPP, size_bitsp)
1585      char **cPP;
1586      int *size_bitsp;
1587 {
1588   if (**cPP != '.')
1589     return 0;
1590   else
1591     {
1592       /* Consume the '.'.  */
1593       (*cPP)++;
1594
1595       switch (**cPP)
1596         {
1597         case 'B':
1598         case 'b':
1599           *size_bitsp = 0;
1600           break;
1601
1602         case 'W':
1603         case 'w':
1604           *size_bitsp = 1;
1605           break;
1606
1607         case 'D':
1608         case 'd':
1609           *size_bitsp = 2;
1610           break;
1611
1612         default:
1613           return 0;
1614         }
1615
1616       /* Consume the size letter.  */
1617       (*cPP)++;
1618       return 1;
1619     }
1620 }
1621
1622 /* Get a B or W size modifier from the string pointed out by *cPP,
1623    which must point to a '.' in front of the modifier.  On successful
1624    return, *cPP is advanced to the character following the size
1625    modifier, and is undefined otherwise.
1626
1627    cPP          Pointer to pointer to string starting
1628                 with the size modifier.
1629
1630    size_bitsp   Pointer to variable to contain the size bits on
1631                 successful return.
1632
1633    Return 1 iff a correct size modifier is found, else 0.  */
1634
1635 static int
1636 get_bw_size_modifier (cPP, size_bitsp)
1637      char **cPP;
1638      int *size_bitsp;
1639 {
1640   if (**cPP != '.')
1641     return 0;
1642   else
1643     {
1644       /* Consume the '.'.  */
1645       (*cPP)++;
1646
1647       switch (**cPP)
1648         {
1649         case 'B':
1650         case 'b':
1651           *size_bitsp = 0;
1652           break;
1653
1654         case 'W':
1655         case 'w':
1656           *size_bitsp = 1;
1657           break;
1658
1659         default:
1660           return 0;
1661         }
1662
1663       /* Consume the size letter.  */
1664       (*cPP)++;
1665       return 1;
1666     }
1667 }
1668
1669 /* Get a general register from the string pointed out by *cPP.  The
1670    variable *cPP is advanced to the character following the general
1671    register name on a successful return, and has its initial position
1672    otherwise.
1673
1674    cPP      Pointer to pointer to string, beginning with a general
1675             register name.
1676
1677    regnop   Pointer to int containing the register number.
1678
1679    Return 1 iff a correct general register designator is found,
1680             else 0.  */
1681
1682 static int
1683 get_gen_reg (cPP, regnop)
1684      char **cPP;
1685      int *regnop;
1686 {
1687   char *oldp;
1688   oldp = *cPP;
1689
1690   /* Handle a sometimes-mandatory dollar sign as register prefix.  */
1691   if (**cPP == REGISTER_PREFIX_CHAR)
1692     (*cPP)++;
1693   else if (demand_register_prefix)
1694     return 0;
1695
1696   switch (**cPP)
1697     {
1698     case 'P':
1699     case 'p':
1700       /* "P" as in "PC"?  Consume the "P".  */
1701       (*cPP)++;
1702
1703       if ((**cPP == 'C' || **cPP == 'c')
1704           && ! ISALNUM ((*cPP)[1]))
1705         {
1706           /* It's "PC": consume the "c" and we're done.  */
1707           (*cPP)++;
1708           *regnop = REG_PC;
1709           return 1;
1710         }
1711       break;
1712
1713     case 'R':
1714     case 'r':
1715       /* Hopefully r[0-9] or r1[0-5].  Consume 'R' or 'r'.  */
1716       (*cPP)++;
1717
1718       if (ISDIGIT (**cPP))
1719         {
1720           /* It's r[0-9].  Consume and check the next digit.  */
1721           *regnop = **cPP - '0';
1722           (*cPP)++;
1723
1724           if (! ISALNUM (**cPP))
1725             {
1726               /* No more digits, we're done.  */
1727               return 1;
1728             }
1729           else
1730             {
1731               /* One more digit.  Consume and add.  */
1732               *regnop = *regnop * 10 + (**cPP - '0');
1733
1734               /* We need to check for a valid register number; Rn,
1735                  0 <= n <= MAX_REG.  */
1736               if (*regnop <= MAX_REG)
1737                 {
1738                   /* Consume second digit.  */
1739                   (*cPP)++;
1740                   return 1;
1741                 }
1742             }
1743         }
1744       break;
1745
1746     case 'S':
1747     case 's':
1748       /* "S" as in "SP"?  Consume the "S".  */
1749       (*cPP)++;
1750       if (**cPP == 'P' || **cPP == 'p')
1751         {
1752           /* It's "SP": consume the "p" and we're done.  */
1753           (*cPP)++;
1754           *regnop = REG_SP;
1755           return 1;
1756         }
1757       break;
1758
1759     default:
1760       /* Just here to silence compilation warnings.  */
1761       ;
1762     }
1763
1764   /* We get here if we fail.  Restore the pointer.  */
1765   *cPP = oldp;
1766   return 0;
1767 }
1768
1769 /* Get a special register from the string pointed out by *cPP. The
1770    variable *cPP is advanced to the character following the special
1771    register name if one is found, and retains its original position
1772    otherwise.
1773
1774    cPP      Pointer to pointer to string starting with a special register
1775             name.
1776
1777    sregpp   Pointer to Pointer to struct spec_reg, where a pointer to the
1778             register description will be stored.
1779
1780    Return 1 iff a correct special register name is found.  */
1781
1782 static int
1783 get_spec_reg (cPP, sregpp)
1784      char **cPP;
1785      const struct cris_spec_reg **sregpp;
1786 {
1787   char *s1;
1788   const char *s2;
1789   char *name_begin = *cPP;
1790
1791   const struct cris_spec_reg *sregp;
1792
1793   /* Handle a sometimes-mandatory dollar sign as register prefix.  */
1794   if (*name_begin == REGISTER_PREFIX_CHAR)
1795     name_begin++;
1796   else if (demand_register_prefix)
1797     return 0;
1798
1799   /* Loop over all special registers.  */
1800   for (sregp = cris_spec_regs; sregp->name != NULL; sregp++)
1801     {
1802       /* Start over from beginning of the supposed name.  */
1803       s1 = name_begin;
1804       s2 = sregp->name;
1805
1806       while (*s2 != '\0' && TOLOWER (*s1) == *s2)
1807         {
1808           s1++;
1809           s2++;
1810         }
1811
1812       /* For a match, we must have consumed the name in the table, and we
1813          must be outside what could be part of a name.  Assume here that a
1814          test for alphanumerics is sufficient for a name test.  */
1815       if (*s2 == 0 && ! ISALNUM (*s1))
1816         {
1817           /* We have a match.  Update the pointer and be done.  */
1818           *cPP = s1;
1819           *sregpp = sregp;
1820           return 1;
1821         }
1822     }
1823
1824   /* If we got here, we did not find any name.  */
1825   return 0;
1826 }
1827
1828 /* Get an unprefixed or side-effect-prefix operand from the string pointed
1829    out by *cPP.  The pointer *cPP is advanced to the character following
1830    the indirect operand if we have success, else it contains an undefined
1831    value.
1832
1833    cPP           Pointer to pointer to string beginning with the first
1834                  character of the supposed operand.
1835
1836    prefixp       Pointer to structure containing an optional instruction
1837                  prefix.
1838
1839    is_autoincp   Pointer to int indicating the indirect or autoincrement
1840                  bits.
1841
1842    src_regnop    Pointer to int containing the source register number in
1843                  the instruction.
1844
1845    imm_foundp    Pointer to an int indicating if an immediate expression
1846                  is found.
1847
1848    imm_exprP     Pointer to a structure containing an immediate
1849                  expression, if success and if *imm_foundp is nonzero.
1850
1851    Return 1 iff a correct indirect operand is found.  */
1852
1853 static int
1854 get_autoinc_prefix_or_indir_op (cPP, prefixp, is_autoincp, src_regnop,
1855                                 imm_foundp, imm_exprP)
1856      char **cPP;
1857      struct cris_prefix *prefixp;
1858      int *is_autoincp;
1859      int *src_regnop;
1860      int *imm_foundp;
1861      expressionS *imm_exprP;
1862 {
1863   /* Assume there was no immediate mode expression.  */
1864   *imm_foundp = 0;
1865
1866   if (**cPP == '[')
1867     {
1868       /* So this operand is one of:
1869          Indirect: [rN]
1870          Autoincrement: [rN+]
1871          Indexed with assign: [rN=rM+rO.S]
1872          Offset with assign: [rN=rM+I], [rN=rM+[rO].s], [rN=rM+[rO+].s]
1873
1874          Either way, consume the '['.  */
1875       (*cPP)++;
1876
1877       /* Get the rN register.  */
1878       if (! get_gen_reg (cPP, src_regnop))
1879         /* If there was no register, then this cannot match.  */
1880         return 0;
1881       else
1882         {
1883           /* We got the register, now check the next character.  */
1884           switch (**cPP)
1885             {
1886             case ']':
1887               /* Indirect mode.  We're done here.  */
1888               prefixp->kind = PREFIX_NONE;
1889               *is_autoincp = 0;
1890               break;
1891
1892             case '+':
1893               /* This must be an auto-increment mode, if there's a
1894                  match.  */
1895               prefixp->kind = PREFIX_NONE;
1896               *is_autoincp = 1;
1897
1898               /* We consume this character and break out to check the
1899                  closing ']'.  */
1900               (*cPP)++;
1901               break;
1902
1903             case '=':
1904               /* This must be indexed with assign, or offset with assign
1905                  to match.  */
1906               (*cPP)++;
1907
1908               /* Either way, the next thing must be a register.  */
1909               if (! get_gen_reg (cPP, &prefixp->base_reg_number))
1910                 /* No register, no match.  */
1911                 return 0;
1912               else
1913                 {
1914                   /* We've consumed "[rN=rM", so we must be looking at
1915                      "+rO.s]" or "+I]", or "-I]", or "+[rO].s]" or
1916                      "+[rO+].s]".  */
1917                   if (**cPP == '+')
1918                     {
1919                       int index_reg_number;
1920                       (*cPP)++;
1921
1922                       if (**cPP == '[')
1923                         {
1924                           int size_bits;
1925                           /* This must be [rx=ry+[rz].s] or
1926                              [rx=ry+[rz+].s] or no match.  We must be
1927                              looking at rz after consuming the '['.  */
1928                           (*cPP)++;
1929
1930                           if (!get_gen_reg (cPP, &index_reg_number))
1931                             return 0;
1932
1933                           prefixp->kind = PREFIX_BDAP;
1934                           prefixp->opcode
1935                             = (BDAP_INDIR_OPCODE
1936                                + (prefixp->base_reg_number << 12)
1937                                + index_reg_number);
1938
1939                           if (**cPP == '+')
1940                             {
1941                               /* We've seen "[rx=ry+[rz+" here, so now we
1942                                  know that there must be "].s]" left to
1943                                  check.  */
1944                               (*cPP)++;
1945                               prefixp->opcode |= AUTOINCR_BIT << 8;
1946                             }
1947
1948                           /* If it wasn't autoincrement, we don't need to
1949                              add anything.  */
1950
1951                           /* Check the next-to-last ']'.  */
1952                           if (**cPP != ']')
1953                             return 0;
1954
1955                           (*cPP)++;
1956
1957                           /* Check the ".s" modifier.  */
1958                           if (! get_bwd_size_modifier (cPP, &size_bits))
1959                             return 0;
1960
1961                           prefixp->opcode |= size_bits << 4;
1962
1963                           /* Now we got [rx=ry+[rz+].s or [rx=ry+[rz].s.
1964                              We break out to check the final ']'.  */
1965                           break;
1966                         }
1967                       /* It wasn't an indirection.  Check if it's a
1968                          register.  */
1969                       else if (get_gen_reg (cPP, &index_reg_number))
1970                         {
1971                           int size_bits;
1972
1973                           /* Indexed with assign mode: "[rN+rM.S]".  */
1974                           prefixp->kind = PREFIX_BIAP;
1975                           prefixp->opcode
1976                             = (BIAP_OPCODE + (index_reg_number << 12)
1977                                + prefixp->base_reg_number /* << 0 */);
1978
1979                           if (! get_bwd_size_modifier (cPP, &size_bits))
1980                             /* Size missing, this isn't a match.  */
1981                             return 0;
1982                           else
1983                             {
1984                               /* Size found, break out to check the
1985                                  final ']'.  */
1986                               prefixp->opcode |= size_bits << 4;
1987                               break;
1988                             }
1989                         }
1990                       /* Not a register.  Then this must be "[rN+I]".  */
1991                       else if (cris_get_expression (cPP, &prefixp->expr))
1992                         {
1993                           /* We've got offset with assign mode.  Fill
1994                              in the blanks and break out to match the
1995                              final ']'.  */
1996                           prefixp->kind = PREFIX_BDAP_IMM;
1997
1998                           /* We tentatively put an opcode corresponding to
1999                              a 32-bit operand here, although it may be
2000                              relaxed when there's no PIC specifier for the
2001                              operand.  */
2002                           prefixp->opcode
2003                             = (BDAP_INDIR_OPCODE
2004                                | (prefixp->base_reg_number << 12)
2005                                | (AUTOINCR_BIT << 8)
2006                                | (2 << 4)
2007                                | REG_PC /* << 0 */);
2008
2009                           /* This can have a PIC suffix, specifying reloc
2010                              type to use.  */
2011                           if (pic && **cPP == PIC_SUFFIX_CHAR)
2012                             {
2013                               unsigned int relocsize;
2014
2015                               cris_get_pic_suffix (cPP, &prefixp->reloc,
2016                                                    &prefixp->expr);
2017
2018                               /* Tweak the size of the immediate operand
2019                                  in the prefix opcode if it isn't what we
2020                                  set.  */
2021                               relocsize
2022                                 = cris_get_pic_reloc_size (prefixp->reloc);
2023                               if (relocsize != 4)
2024                                 prefixp->opcode
2025                                   = ((prefixp->opcode & ~(3 << 4))
2026                                      | ((relocsize >> 1) << 4));
2027                             }
2028                           break;
2029                         }
2030                       else
2031                         /* Neither register nor expression found, so
2032                            this can't be a match.  */
2033                         return 0;
2034                     }
2035                   /* Not "[rN+" but perhaps "[rN-"?  */
2036                   else if (**cPP == '-')
2037                     {
2038                       /* We must have an offset with assign mode.  */
2039                       if (! cris_get_expression (cPP, &prefixp->expr))
2040                         /* No expression, no match.  */
2041                         return 0;
2042                       else
2043                         {
2044                           /* We've got offset with assign mode.  Fill
2045                              in the blanks and break out to match the
2046                              final ']'.
2047
2048                              Note that we don't allow a PIC suffix for an
2049                              operand with a minus sign.  */
2050                           prefixp->kind = PREFIX_BDAP_IMM;
2051                           break;
2052                         }
2053                     }
2054                   else
2055                     /* Neither '+' nor '-' after "[rN=rM".  Lose.  */
2056                     return 0;
2057                 }
2058             default:
2059               /* Neither ']' nor '+' nor '=' after "[rN".  Lose.  */
2060               return 0;
2061             }
2062         }
2063
2064       /* When we get here, we have a match and will just check the closing
2065          ']'.  We can still fail though.  */
2066       if (**cPP != ']')
2067         return 0;
2068       else
2069         {
2070           /* Don't forget to consume the final ']'.
2071              Then return in glory.  */
2072           (*cPP)++;
2073           return 1;
2074         }
2075     }
2076   /* No indirection.  Perhaps a constant?  */
2077   else if (cris_get_expression (cPP, imm_exprP))
2078     {
2079       /* Expression found, this is immediate mode.  */
2080       prefixp->kind = PREFIX_NONE;
2081       *is_autoincp = 1;
2082       *src_regnop = REG_PC;
2083       *imm_foundp = 1;
2084
2085       /* This can have a PIC suffix, specifying reloc type to use.  The
2086          caller must check that the reloc size matches the operand size.  */
2087       if (pic && **cPP == PIC_SUFFIX_CHAR)
2088         cris_get_pic_suffix (cPP, &prefixp->reloc, imm_exprP);
2089
2090       return 1;
2091     }
2092
2093   /* No luck today.  */
2094   return 0;
2095 }
2096
2097 /* This function gets an indirect operand in a three-address operand
2098    combination from the string pointed out by *cPP.  The pointer *cPP is
2099    advanced to the character following the indirect operand on success, or
2100    has an unspecified value on failure.
2101
2102    cPP       Pointer to pointer to string begining
2103              with the operand
2104
2105    prefixp   Pointer to structure containing an
2106              instruction prefix
2107
2108    Returns 1 iff a correct indirect operand is found.  */
2109
2110 static int
2111 get_3op_or_dip_prefix_op (cPP, prefixp)
2112      char **cPP;
2113      struct cris_prefix *prefixp;
2114 {
2115   int reg_number;
2116
2117   if (**cPP != '[')
2118     /* We must have a '[' or it's a clean failure.  */
2119     return 0;
2120
2121   /* Eat the first '['.  */
2122   (*cPP)++;
2123
2124   if (**cPP == '[')
2125     {
2126       /* A second '[', so this must be double-indirect mode.  */
2127       (*cPP)++;
2128       prefixp->kind = PREFIX_DIP;
2129       prefixp->opcode = DIP_OPCODE;
2130
2131       /* Get the register or fail entirely.  */
2132       if (! get_gen_reg (cPP, &reg_number))
2133         return 0;
2134       else
2135         {
2136           prefixp->opcode |= reg_number /* << 0 */ ;
2137           if (**cPP == '+')
2138             {
2139               /* Since we found a '+', this must be double-indirect
2140                  autoincrement mode.  */
2141               (*cPP)++;
2142               prefixp->opcode |= AUTOINCR_BIT << 8;
2143             }
2144
2145           /* There's nothing particular to do, if this was a
2146              double-indirect *without* autoincrement.  */
2147         }
2148
2149       /* Check the first ']'.  The second one is checked at the end.  */
2150       if (**cPP != ']')
2151         return 0;
2152
2153       /* Eat the first ']', so we'll be looking at a second ']'.  */
2154       (*cPP)++;
2155     }
2156   /* No second '['.  Then we should have a register here, making
2157      it "[rN".  */
2158   else if (get_gen_reg (cPP, &prefixp->base_reg_number))
2159     {
2160       /* This must be indexed or offset mode: "[rN+I]" or
2161          "[rN+rM.S]" or "[rN+[rM].S]" or "[rN+[rM+].S]".  */
2162       if (**cPP == '+')
2163         {
2164           int index_reg_number;
2165
2166           (*cPP)++;
2167
2168           if (**cPP == '[')
2169             {
2170               /* This is "[rx+["...  Expect a register next.  */
2171               int size_bits;
2172               (*cPP)++;
2173
2174               if (!get_gen_reg (cPP, &index_reg_number))
2175                 return 0;
2176
2177               prefixp->kind = PREFIX_BDAP;
2178               prefixp->opcode
2179                 = (BDAP_INDIR_OPCODE
2180                    + (prefixp->base_reg_number << 12)
2181                    + index_reg_number);
2182
2183               /* We've seen "[rx+[ry", so check if this is
2184                  autoincrement.  */
2185               if (**cPP == '+')
2186                 {
2187                   /* Yep, now at "[rx+[ry+".  */
2188                   (*cPP)++;
2189                   prefixp->opcode |= AUTOINCR_BIT << 8;
2190                 }
2191               /* If it wasn't autoincrement, we don't need to
2192                  add anything.  */
2193
2194               /* Check a first closing ']': "[rx+[ry]" or
2195                  "[rx+[ry+]".  */
2196               if (**cPP != ']')
2197                 return 0;
2198               (*cPP)++;
2199
2200               /* Now expect a size modifier ".S".  */
2201               if (! get_bwd_size_modifier (cPP, &size_bits))
2202                 return 0;
2203
2204               prefixp->opcode |= size_bits << 4;
2205
2206               /* Ok, all interesting stuff has been seen:
2207                  "[rx+[ry+].S" or "[rx+[ry].S".  We only need to
2208                  expect a final ']', which we'll do in a common
2209                  closing session.  */
2210             }
2211           /* Seen "[rN+", but not a '[', so check if we have a
2212              register.  */
2213           else if (get_gen_reg (cPP, &index_reg_number))
2214             {
2215               /* This is indexed mode: "[rN+rM.S]" or
2216                  "[rN+rM.S+]".  */
2217               int size_bits;
2218               prefixp->kind = PREFIX_BIAP;
2219               prefixp->opcode
2220                 = (BIAP_OPCODE
2221                    | prefixp->base_reg_number /* << 0 */
2222                    | (index_reg_number << 12));
2223
2224               /* Consume the ".S".  */
2225               if (! get_bwd_size_modifier (cPP, &size_bits))
2226                 /* Missing size, so fail.  */
2227                 return 0;
2228               else
2229                 /* Size found.  Add that piece and drop down to
2230                    the common checking of the closing ']'.  */
2231                 prefixp->opcode |= size_bits << 4;
2232             }
2233           /* Seen "[rN+", but not a '[' or a register, so then
2234              it must be a constant "I".  */
2235           else if (cris_get_expression (cPP, &prefixp->expr))
2236             {
2237               /* Expression found, so fill in the bits of offset
2238                  mode and drop down to check the closing ']'.  */
2239               prefixp->kind = PREFIX_BDAP_IMM;
2240
2241               /* We tentatively put an opcode corresponding to a 32-bit
2242                  operand here, although it may be relaxed when there's no
2243                  PIC specifier for the operand.  */
2244               prefixp->opcode
2245                 = (BDAP_INDIR_OPCODE
2246                    | (prefixp->base_reg_number << 12)
2247                    | (AUTOINCR_BIT << 8)
2248                    | (2 << 4)
2249                    | REG_PC /* << 0 */);
2250
2251               /* This can have a PIC suffix, specifying reloc type to use.  */
2252               if (pic && **cPP == PIC_SUFFIX_CHAR)
2253                 {
2254                   unsigned int relocsize;
2255
2256                   cris_get_pic_suffix (cPP, &prefixp->reloc, &prefixp->expr);
2257
2258                   /* Tweak the size of the immediate operand in the prefix
2259                      opcode if it isn't what we set.  */
2260                   relocsize = cris_get_pic_reloc_size (prefixp->reloc);
2261                   if (relocsize != 4)
2262                     prefixp->opcode
2263                       = ((prefixp->opcode & ~(3 << 4))
2264                          | ((relocsize >> 1) << 4));
2265                 }
2266             }
2267           else
2268             /* Nothing valid here: lose.  */
2269             return 0;
2270         }
2271       /* Seen "[rN" but no '+', so check if it's a '-'.  */
2272       else if (**cPP == '-')
2273         {
2274           /* Yep, we must have offset mode.  */
2275           if (! cris_get_expression (cPP, &prefixp->expr))
2276             /* No expression, so we lose.  */
2277             return 0;
2278           else
2279             {
2280               /* Expression found to make this offset mode, so
2281                  fill those bits and drop down to check the
2282                  closing ']'.
2283
2284                  Note that we don't allow a PIC suffix for
2285                  an operand with a minus sign like this.  */
2286               prefixp->kind = PREFIX_BDAP_IMM;
2287             }
2288         }
2289       else
2290         {
2291           /* We've seen "[rN", but not '+' or '-'; rather a ']'.
2292              Hmm.  Normally this is a simple indirect mode that we
2293              shouldn't match, but if we expect ']', then we have a
2294              zero offset, so it can be a three-address-operand,
2295              like "[rN],rO,rP", thus offset mode.
2296
2297              Don't eat the ']', that will be done in the closing
2298              ceremony.  */
2299           prefixp->expr.X_op = O_constant;
2300           prefixp->expr.X_add_number = 0;
2301           prefixp->expr.X_add_symbol = NULL;
2302           prefixp->expr.X_op_symbol = NULL;
2303           prefixp->kind = PREFIX_BDAP_IMM;
2304         }
2305     }
2306   /* A '[', but no second '[', and no register.  Check if we
2307      have an expression, making this "[I]" for a double-indirect
2308      prefix.  */
2309   else if (cris_get_expression (cPP, &prefixp->expr))
2310     {
2311       /* Expression found, the so called absolute mode for a
2312          double-indirect prefix on PC.  */
2313       prefixp->kind = PREFIX_DIP;
2314       prefixp->opcode = DIP_OPCODE | (AUTOINCR_BIT << 8) | REG_PC;
2315       prefixp->reloc = BFD_RELOC_32;
2316     }
2317   else
2318     /* Neither '[' nor register nor expression.  We lose.  */
2319     return 0;
2320
2321   /* We get here as a closing ceremony to a successful match.  We just
2322      need to check the closing ']'.  */
2323   if (**cPP != ']')
2324     /* Oops.  Close but no air-polluter.  */
2325     return 0;
2326
2327   /* Don't forget to consume that ']', before returning in glory.  */
2328   (*cPP)++;
2329   return 1;
2330 }
2331
2332 /* Get an expression from the string pointed out by *cPP.
2333    The pointer *cPP is advanced to the character following the expression
2334    on a success, or retains its original value otherwise.
2335
2336    cPP     Pointer to pointer to string beginning with the expression.
2337
2338    exprP   Pointer to structure containing the expression.
2339
2340    Return 1 iff a correct expression is found.  */
2341
2342 static int
2343 cris_get_expression (cPP, exprP)
2344      char **cPP;
2345      expressionS *exprP;
2346 {
2347   char *saved_input_line_pointer;
2348   segT exp;
2349
2350   /* The "expression" function expects to find an expression at the
2351      global variable input_line_pointer, so we have to save it to give
2352      the impression that we don't fiddle with global variables.  */
2353   saved_input_line_pointer = input_line_pointer;
2354   input_line_pointer = *cPP;
2355
2356   exp = expression (exprP);
2357   if (exprP->X_op == O_illegal || exprP->X_op == O_absent)
2358     {
2359       input_line_pointer = saved_input_line_pointer;
2360       return 0;
2361     }
2362
2363   /* Everything seems to be fine, just restore the global
2364      input_line_pointer and say we're successful.  */
2365   *cPP = input_line_pointer;
2366   input_line_pointer = saved_input_line_pointer;
2367   return 1;
2368 }
2369
2370 /* Get a sequence of flag characters from *spp.  The pointer *cPP is
2371    advanced to the character following the expression.  The flag
2372    characters are consecutive, no commas or spaces.
2373
2374    cPP       Pointer to pointer to string beginning with the expression.
2375
2376    flagp     Pointer to int to return the flags expression.
2377
2378    Return 1 iff a correct flags expression is found.  */
2379
2380 static int
2381 get_flags (cPP, flagsp)
2382      char **cPP;
2383      int *flagsp;
2384 {
2385   for (;;)
2386     {
2387       switch (**cPP)
2388         {
2389         case 'd':
2390         case 'D':
2391         case 'm':
2392         case 'M':
2393           *flagsp |= 0x80;
2394           break;
2395
2396         case 'e':
2397         case 'E':
2398         case 'b':
2399         case 'B':
2400           *flagsp |= 0x40;
2401           break;
2402
2403         case 'i':
2404         case 'I':
2405           *flagsp |= 0x20;
2406           break;
2407
2408         case 'x':
2409         case 'X':
2410           *flagsp |= 0x10;
2411           break;
2412
2413         case 'n':
2414         case 'N':
2415           *flagsp |= 0x8;
2416           break;
2417
2418         case 'z':
2419         case 'Z':
2420           *flagsp |= 0x4;
2421           break;
2422
2423         case 'v':
2424         case 'V':
2425           *flagsp |= 0x2;
2426           break;
2427
2428         case 'c':
2429         case 'C':
2430           *flagsp |= 1;
2431           break;
2432
2433         default:
2434           /* We consider this successful if we stop at a comma or
2435              whitespace.  Anything else, and we consider it a failure.  */
2436           if (**cPP != ','
2437               && **cPP != 0
2438               && ! ISSPACE (**cPP))
2439             return 0;
2440           else
2441             return 1;
2442         }
2443
2444       /* Don't forget to consume each flag character.  */
2445       (*cPP)++;
2446     }
2447 }
2448
2449 /* Generate code and fixes for a BDAP prefix.
2450
2451    base_regno   Int containing the base register number.
2452
2453    exprP        Pointer to structure containing the offset expression.  */
2454
2455 static void
2456 gen_bdap (base_regno, exprP)
2457      int base_regno;
2458      expressionS *exprP;
2459 {
2460   unsigned int opcode;
2461   char *opcodep;
2462
2463   /* Put out the prefix opcode; assume quick immediate mode at first.  */
2464   opcode = BDAP_QUICK_OPCODE | (base_regno << 12);
2465   opcodep = cris_insn_first_word_frag ();
2466   md_number_to_chars (opcodep, opcode, 2);
2467
2468   if (exprP->X_op == O_constant)
2469     {
2470       /* We have an absolute expression that we know the size of right
2471          now.  */
2472       long int value;
2473       int size;
2474
2475       value = exprP->X_add_number;
2476       if (value < -32768 || value > 32767)
2477         /* Outside range for a "word", make it a dword.  */
2478         size = 2;
2479       else
2480         /* Assume "word" size.  */
2481         size = 1;
2482
2483       /* If this is a signed-byte value, we can fit it into the prefix
2484          insn itself.  */
2485       if (value >= -128 && value <= 127)
2486         opcodep[0] = value;
2487       else
2488         {
2489           /* This is a word or dword displacement, which will be put in a
2490              word or dword after the prefix.  */
2491           char *p;
2492
2493           opcodep[0] = BDAP_PC_LOW + (size << 4);
2494           opcodep[1] &= 0xF0;
2495           opcodep[1] |= BDAP_INCR_HIGH;
2496           p = frag_more (1 << size);
2497           md_number_to_chars (p, value, 1 << size);
2498         }
2499     }
2500   else
2501     {
2502       /* Handle complex expressions.  */
2503       valueT addvalue
2504         = SIMPLE_EXPR (exprP) ? exprP->X_add_number : 0;
2505       symbolS *sym
2506         = (SIMPLE_EXPR (exprP)
2507            ? exprP->X_add_symbol : make_expr_symbol (exprP));
2508
2509       /* The expression is not defined yet but may become absolute.  We
2510          make it a relocation to be relaxed.  */
2511       frag_var (rs_machine_dependent, 4, 0,
2512                 ENCODE_RELAX (STATE_BASE_PLUS_DISP_PREFIX, STATE_UNDF),
2513                 sym, addvalue, opcodep);
2514     }
2515 }
2516
2517 /* Encode a branch displacement in the range -256..254 into the form used
2518    by CRIS conditional branch instructions.
2519
2520    offset  The displacement value in bytes.  */
2521
2522 static int
2523 branch_disp (offset)
2524      int offset;
2525 {
2526   int disp;
2527
2528   disp = offset & 0xFE;
2529
2530   if (offset < 0)
2531     disp |= 1;
2532
2533   return disp;
2534 }
2535
2536 /* Generate code and fixes for a 32-bit conditional branch instruction
2537    created by "extending" an existing 8-bit branch instruction.
2538
2539    opcodep    Pointer to the word containing the original 8-bit branch
2540               instruction.
2541
2542    writep     Pointer to "extension area" following the first instruction
2543               word.
2544
2545    fragP      Pointer to the frag containing the instruction.
2546
2547    add_symP,  Parts of the destination address expression.
2548    sub_symP,
2549    add_num.  */
2550
2551 static void
2552 gen_cond_branch_32 (opcodep, writep, fragP, add_symP, sub_symP, add_num)
2553      char *opcodep;
2554      char *writep;
2555      fragS *fragP;
2556      symbolS *add_symP;
2557      symbolS *sub_symP;
2558      long int add_num;
2559 {
2560   if (warn_for_branch_expansion)
2561     as_warn_where (fragP->fr_file, fragP->fr_line,
2562                    _("32-bit conditional branch generated"));
2563
2564   /* Here, writep points to what will be opcodep + 2.  First, we change
2565      the actual branch in opcodep[0] and opcodep[1], so that in the
2566      final insn, it will look like:
2567        opcodep+10: Bcc .-6
2568
2569      This means we don't have to worry about changing the opcode or
2570      messing with the delay-slot instruction.  So, we move it to last in
2571      the "extended" branch, and just change the displacement.  Admittedly,
2572      it's not the optimal extended construct, but we should get this
2573      rarely enough that it shouldn't matter.  */
2574
2575   writep[8] = branch_disp (-2 - 6);
2576   writep[9] = opcodep[1];
2577
2578   /* Then, we change the branch to an unconditional branch over the
2579      extended part, to the new location of the Bcc:
2580        opcodep:   BA .+10
2581        opcodep+2: NOP
2582
2583      Note that these two writes are to currently different locations,
2584      merged later.  */
2585
2586   md_number_to_chars (opcodep, BA_QUICK_OPCODE + 8, 2);
2587   md_number_to_chars (writep, NOP_OPCODE, 2);
2588
2589   /* Then the extended thing, the 32-bit jump insn.
2590        opcodep+4: JUMP [PC+]
2591      or, in the PIC case,
2592        opcodep+4: ADD [PC+],PC.  */
2593
2594   md_number_to_chars (writep + 2,
2595                       pic ? ADD_PC_INCR_OPCODE : JUMP_PC_INCR_OPCODE, 2);
2596
2597   /* We have to fill in the actual value too.
2598        opcodep+6: .DWORD
2599      This is most probably an expression, but we can cope with an absolute
2600      value too.  FIXME: Testcase needed with and without pic.  */
2601
2602   if (add_symP == NULL && sub_symP == NULL)
2603     {
2604       /* An absolute address.  */
2605       if (pic)
2606         fix_new (fragP, writep + 4 - fragP->fr_literal, 4,
2607                  section_symbol (absolute_section),
2608                  add_num, 1, BFD_RELOC_32_PCREL);
2609       else
2610         md_number_to_chars (writep + 4, add_num, 4);
2611     }
2612   else
2613     {
2614       if (sub_symP != NULL)
2615         as_bad_where (fragP->fr_file, fragP->fr_line,
2616                       _("Complex expression not supported"));
2617
2618       /* Not absolute, we have to make it a frag for later evaluation.  */
2619       fix_new (fragP, writep + 4 - fragP->fr_literal, 4, add_symP,
2620                add_num, pic ? 1 : 0, pic ? BFD_RELOC_32_PCREL : BFD_RELOC_32);
2621     }
2622 }
2623
2624 /* Get the size of an immediate-reloc in bytes.  Only valid for PIC
2625    relocs.  */
2626
2627 static unsigned int
2628 cris_get_pic_reloc_size (reloc)
2629      bfd_reloc_code_real_type reloc;
2630 {
2631   return reloc == BFD_RELOC_CRIS_16_GOTPLT || reloc == BFD_RELOC_CRIS_16_GOT
2632     ? 2 : 4;
2633 }
2634
2635 /* Store a reloc type at *RELOCP corresponding to the PIC suffix at *CPP.
2636    Adjust *EXPRP with any addend found after the PIC suffix.  */
2637
2638 static void
2639 cris_get_pic_suffix (cPP, relocp, exprP)
2640      char **cPP;
2641      bfd_reloc_code_real_type *relocp;
2642      expressionS *exprP;
2643 {
2644   char *s = *cPP;
2645   unsigned int i;
2646   expressionS const_expr;
2647
2648   const struct pic_suffixes_struct
2649   {
2650     const char *const suffix;
2651     unsigned int len;
2652     bfd_reloc_code_real_type reloc;
2653   } pic_suffixes[] =
2654     {
2655 #undef PICMAP
2656 #define PICMAP(s, r) {s, sizeof (s) - 1, r}
2657       /* Keep this in order with longest unambiguous prefix first.  */
2658       PICMAP ("GOTPLT16", BFD_RELOC_CRIS_16_GOTPLT),
2659       PICMAP ("GOTPLT", BFD_RELOC_CRIS_32_GOTPLT),
2660       PICMAP ("PLTG", BFD_RELOC_CRIS_32_PLT_GOTREL),
2661       PICMAP ("PLT", BFD_RELOC_CRIS_32_PLT_PCREL),
2662       PICMAP ("GOTOFF", BFD_RELOC_CRIS_32_GOTREL),
2663       PICMAP ("GOT16", BFD_RELOC_CRIS_16_GOT),
2664       PICMAP ("GOT", BFD_RELOC_CRIS_32_GOT)
2665     };
2666
2667   /* We've already seen the ':', so consume it.  */
2668   s++;
2669
2670   for (i = 0; i < sizeof (pic_suffixes)/sizeof (pic_suffixes[0]); i++)
2671     {
2672       if (strncmp (s, pic_suffixes[i].suffix, pic_suffixes[i].len) == 0
2673           && ! is_part_of_name (s[pic_suffixes[i].len]))
2674         {
2675           /* We have a match.  Consume the suffix and set the relocation
2676              type.   */
2677           s += pic_suffixes[i].len;
2678
2679           /* There can be a constant term appended.  If so, we will add it
2680              to *EXPRP.  */
2681           if (*s == '+' || *s == '-')
2682             {
2683               if (! cris_get_expression (&s, &const_expr))
2684                 /* There was some kind of syntax error.  Bail out.  */
2685                 break;
2686
2687               /* Allow complex expressions as the constant part.  It still
2688                  has to be an assembly-time constant or there will be an
2689                  error emitting the reloc.  This makes the PIC qualifiers
2690                  idempotent; foo:GOTOFF+32 == foo+32:GOTOFF.  The former we
2691                  recognize here; the latter is parsed in the incoming
2692                  expression.  */
2693               exprP->X_add_symbol = make_expr_symbol (exprP);
2694               exprP->X_op = O_add;
2695               exprP->X_add_number = 0;
2696               exprP->X_op_symbol = make_expr_symbol (&const_expr);
2697             }
2698
2699           *relocp = pic_suffixes[i].reloc;
2700           *cPP = s;
2701           return;
2702         }
2703     }
2704
2705   /* No match.  Don't consume anything; fall back and there will be a
2706      syntax error.  */
2707 }
2708
2709 /* This *could* be:
2710
2711    Turn a string in input_line_pointer into a floating point constant
2712    of type TYPE, and store the appropriate bytes in *LITP.  The number
2713    of LITTLENUMS emitted is stored in *SIZEP.
2714
2715    type   A character from FLTCHARS that describes what kind of
2716           floating-point number is wanted.
2717
2718    litp   A pointer to an array that the result should be stored in.
2719
2720    sizep  A pointer to an integer where the size of the result is stored.
2721
2722    But we don't support floating point constants in assembly code *at all*,
2723    since it's suboptimal and just opens up bug opportunities.  GCC emits
2724    the bit patterns as hex.  All we could do here is to emit what GCC
2725    would have done in the first place.  *Nobody* writes floating-point
2726    code as assembly code, but if they do, they should be able enough to
2727    find out the correct bit patterns and use them.  */
2728
2729 char *
2730 md_atof (type, litp, sizep)
2731      char type ATTRIBUTE_UNUSED;
2732      char *litp ATTRIBUTE_UNUSED;
2733      int *sizep ATTRIBUTE_UNUSED;
2734 {
2735   /* FIXME:  Is this function mentioned in the internals.texi manual?  If
2736      not, add it.  */
2737   return  _("Bad call to md_atof () - floating point formats are not supported");
2738 }
2739
2740 /* Turn a number as a fixS * into a series of bytes that represents the
2741    number on the target machine.  The purpose of this procedure is the
2742    same as that of md_number_to_chars but this procedure is supposed to
2743    handle general bit field fixes and machine-dependent fixups.
2744
2745    bufp        Pointer to an array where the result should be stored.
2746
2747    val        The value to store.
2748
2749    n          The number of bytes in "val" that should be stored.
2750
2751    fixP       The fix to be applied to the bit field starting at bufp.
2752
2753    seg        The segment containing this number.  */
2754
2755 static void
2756 cris_number_to_imm (bufp, val, n, fixP, seg)
2757      char *bufp;
2758      long val;
2759      int n;
2760      fixS *fixP;
2761      segT seg;
2762 {
2763   segT sym_seg;
2764
2765   know (n <= 4);
2766   know (fixP);
2767
2768   /* We put the relative "vma" for the other segment for inter-segment
2769      relocations in the object data to stay binary "compatible" (with an
2770      uninteresting old version) for the relocation.
2771      Maybe delete some day.  */
2772   if (fixP->fx_addsy
2773       && (sym_seg = S_GET_SEGMENT (fixP->fx_addsy)) != seg)
2774     val += sym_seg->vma;
2775
2776   if (fixP->fx_addsy != NULL || fixP->fx_pcrel)
2777     switch (fixP->fx_r_type)
2778       {
2779         /* These must be fully resolved when getting here.  */
2780       case BFD_RELOC_32_PCREL:
2781       case BFD_RELOC_16_PCREL:
2782       case BFD_RELOC_8_PCREL:
2783         as_bad_where (fixP->fx_frag->fr_file, fixP->fx_frag->fr_line,
2784                       _("PC-relative relocation must be trivially resolved"));
2785       default:
2786         ;
2787       }
2788
2789   switch (fixP->fx_r_type)
2790     {
2791       /* Ditto here, we put the addend into the object code as
2792          well as the reloc addend.  Keep it that way for now, to simplify
2793          regression tests on the object file contents.  FIXME:  Seems
2794          uninteresting now that we have a test suite.  */
2795
2796     case BFD_RELOC_CRIS_16_GOT:
2797     case BFD_RELOC_CRIS_32_GOT:
2798     case BFD_RELOC_CRIS_32_GOTREL:
2799     case BFD_RELOC_CRIS_16_GOTPLT:
2800     case BFD_RELOC_CRIS_32_GOTPLT:
2801     case BFD_RELOC_CRIS_32_PLT_GOTREL:
2802     case BFD_RELOC_CRIS_32_PLT_PCREL:
2803       /* We don't want to put in any kind of non-zero bits in the data
2804          being relocated for these.  */
2805       break;
2806
2807     case BFD_RELOC_32:
2808     case BFD_RELOC_32_PCREL:
2809       /* No use having warnings here, since most hosts have a 32-bit type
2810          for "long" (which will probably change soon, now that I wrote
2811          this).  */
2812       bufp[3] = (val >> 24) & 0xFF;
2813       bufp[2] = (val >> 16) & 0xFF;
2814       bufp[1] = (val >> 8) & 0xFF;
2815       bufp[0] = val & 0xFF;
2816       break;
2817
2818       /* FIXME: The 16 and 8-bit cases should have a way to check
2819          whether a signed or unsigned (or any signedness) number is
2820          accepted.
2821          FIXME: Does the as_bad calls find the line number by themselves,
2822          or should we change them into as_bad_where?  */
2823
2824     case BFD_RELOC_16:
2825     case BFD_RELOC_16_PCREL:
2826       if (val > 0xffff || val < -32768)
2827         as_bad (_("Value not in 16 bit range: %ld"), val);
2828       if (! fixP->fx_addsy)
2829         {
2830           bufp[1] = (val >> 8) & 0xFF;
2831           bufp[0] = val & 0xFF;
2832         }
2833       break;
2834
2835     case BFD_RELOC_8:
2836     case BFD_RELOC_8_PCREL:
2837       if (val > 255 || val < -128)
2838         as_bad (_("Value not in 8 bit range: %ld"), val);
2839       if (! fixP->fx_addsy)
2840         bufp[0] = val & 0xFF;
2841       break;
2842
2843     case BFD_RELOC_CRIS_UNSIGNED_4:
2844       if (val > 15 || val < 0)
2845         as_bad (_("Value not in 4 bit unsigned range: %ld"), val);
2846       if (! fixP->fx_addsy)
2847         bufp[0] |= val & 0x0F;
2848       break;
2849
2850     case BFD_RELOC_CRIS_UNSIGNED_5:
2851       if (val > 31 || val < 0)
2852         as_bad (_("Value not in 5 bit unsigned range: %ld"), val);
2853       if (! fixP->fx_addsy)
2854         bufp[0] |= val & 0x1F;
2855       break;
2856
2857     case BFD_RELOC_CRIS_SIGNED_6:
2858       if (val > 31 || val < -32)
2859         as_bad (_("Value not in 6 bit range: %ld"), val);
2860       if (! fixP->fx_addsy)
2861         bufp[0] |= val & 0x3F;
2862       break;
2863
2864     case BFD_RELOC_CRIS_UNSIGNED_6:
2865       if (val > 63 || val < 0)
2866         as_bad (_("Value not in 6 bit unsigned range: %ld"), val);
2867       if (! fixP->fx_addsy)
2868         bufp[0] |= val & 0x3F;
2869       break;
2870
2871     case BFD_RELOC_CRIS_BDISP8:
2872       if (! fixP->fx_addsy)
2873         bufp[0] = branch_disp (val);
2874       break;
2875
2876     case BFD_RELOC_NONE:
2877       /* May actually happen automatically.  For example at broken
2878          words, if the word turns out not to be broken.
2879          FIXME: When?  Which testcase?  */
2880       if (! fixP->fx_addsy)
2881         md_number_to_chars (bufp, val, n);
2882       break;
2883
2884     case BFD_RELOC_VTABLE_INHERIT:
2885       /* This borrowed from tc-ppc.c on a whim.  */
2886       if (fixP->fx_addsy
2887           && !S_IS_DEFINED (fixP->fx_addsy)
2888           && !S_IS_WEAK (fixP->fx_addsy))
2889         S_SET_WEAK (fixP->fx_addsy);
2890       /* Fall through.  */
2891
2892     case BFD_RELOC_VTABLE_ENTRY:
2893       fixP->fx_done = 0;
2894       break;
2895
2896     default:
2897       BAD_CASE (fixP->fx_r_type);
2898     }
2899 }
2900
2901 /* Processes machine-dependent command line options.  Called once for
2902    each option on the command line that the machine-independent part of
2903    GAS does not understand.  */
2904
2905 int
2906 md_parse_option (arg, argp)
2907      int arg;
2908      char *argp ATTRIBUTE_UNUSED;
2909 {
2910   switch (arg)
2911     {
2912     case 'H':
2913     case 'h':
2914       printf (_("Please use --help to see usage and options for this assembler.\n"));
2915       md_show_usage (stdout);
2916       exit (EXIT_SUCCESS);
2917
2918     case 'N':
2919       warn_for_branch_expansion = 1;
2920       return 1;
2921
2922     case OPTION_NO_US:
2923       demand_register_prefix = TRUE;
2924
2925       if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
2926         as_bad (_("--no-underscore is invalid with a.out format"));
2927       else
2928         symbols_have_leading_underscore = FALSE;
2929       return 1;
2930
2931     case OPTION_US:
2932       demand_register_prefix = FALSE;
2933       symbols_have_leading_underscore = TRUE;
2934       return 1;
2935
2936     case OPTION_PIC:
2937       pic = TRUE;
2938       return 1;
2939
2940     default:
2941       return 0;
2942     }
2943 }
2944
2945 /* Round up a section size to the appropriate boundary.  */
2946 valueT
2947 md_section_align (segment, size)
2948      segT segment;
2949      valueT size;
2950 {
2951   /* Round all sects to multiple of 4, except the bss section, which
2952      we'll round to word-size.
2953
2954      FIXME: Check if this really matters.  All sections should be
2955      rounded up, and all sections should (optionally) be assumed to be
2956      dword-aligned, it's just that there is actual usage of linking to a
2957      multiple of two.  */
2958   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
2959     {
2960       if (segment == bss_section)
2961         return (size + 1) & ~1;
2962       return (size + 3) & ~3;
2963     }
2964   else
2965     {
2966       /* FIXME: Is this wanted?  It matches the testsuite, but that's not
2967          really a valid reason.  */
2968       if (segment == text_section)
2969         return (size + 3) & ~3;
2970     }
2971
2972   return size;
2973 }
2974
2975 /* Generate a machine-dependent relocation.  */
2976 arelent *
2977 tc_gen_reloc (section, fixP)
2978      asection *section ATTRIBUTE_UNUSED;
2979      fixS *fixP;
2980 {
2981   arelent *relP;
2982   bfd_reloc_code_real_type code;
2983
2984   switch (fixP->fx_r_type)
2985     {
2986     case BFD_RELOC_CRIS_16_GOT:
2987     case BFD_RELOC_CRIS_32_GOT:
2988     case BFD_RELOC_CRIS_16_GOTPLT:
2989     case BFD_RELOC_CRIS_32_GOTPLT:
2990     case BFD_RELOC_CRIS_32_GOTREL:
2991     case BFD_RELOC_CRIS_32_PLT_GOTREL:
2992     case BFD_RELOC_CRIS_32_PLT_PCREL:
2993     case BFD_RELOC_32:
2994     case BFD_RELOC_16:
2995     case BFD_RELOC_8:
2996     case BFD_RELOC_VTABLE_INHERIT:
2997     case BFD_RELOC_VTABLE_ENTRY:
2998       code = fixP->fx_r_type;
2999       break;
3000     default:
3001       as_bad_where (fixP->fx_file, fixP->fx_line,
3002                     _("Semantics error.  This type of operand can not be relocated, it must be an assembly-time constant"));
3003       return 0;
3004     }
3005
3006   relP = (arelent *) xmalloc (sizeof (arelent));
3007   assert (relP != 0);
3008   relP->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3009   *relP->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
3010   relP->address = fixP->fx_frag->fr_address + fixP->fx_where;
3011
3012   if (fixP->fx_pcrel)
3013     relP->addend = 0;
3014   else
3015     relP->addend = fixP->fx_offset;
3016
3017   /* This is the standard place for KLUDGEs to work around bugs in
3018      bfd_install_relocation (first such note in the documentation
3019      appears with binutils-2.8).
3020
3021      That function bfd_install_relocation does the wrong thing with
3022      putting stuff into the addend of a reloc (it should stay out) for a
3023      weak symbol.  The really bad thing is that it adds the
3024      "segment-relative offset" of the symbol into the reloc.  In this
3025      case, the reloc should instead be relative to the symbol with no
3026      other offset than the assembly code shows; and since the symbol is
3027      weak, any local definition should be ignored until link time (or
3028      thereafter).
3029      To wit:  weaksym+42  should be weaksym+42 in the reloc,
3030      not weaksym+(offset_from_segment_of_local_weaksym_definition)
3031
3032      To "work around" this, we subtract the segment-relative offset of
3033      "known" weak symbols.  This evens out the extra offset.
3034
3035      That happens for a.out but not for ELF, since for ELF,
3036      bfd_install_relocation uses the "special function" field of the
3037      howto, and does not execute the code that needs to be undone.  */
3038
3039   if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3040       && fixP->fx_addsy && S_IS_WEAK (fixP->fx_addsy)
3041       && ! bfd_is_und_section (S_GET_SEGMENT (fixP->fx_addsy)))
3042     {
3043       relP->addend -= S_GET_VALUE (fixP->fx_addsy);
3044     }
3045
3046   relP->howto = bfd_reloc_type_lookup (stdoutput, code);
3047   if (! relP->howto)
3048     {
3049       const char *name;
3050
3051       name = S_GET_NAME (fixP->fx_addsy);
3052       if (name == NULL)
3053         name = _("<unknown>");
3054       as_fatal (_("Cannot generate relocation type for symbol %s, code %s"),
3055                 name, bfd_get_reloc_code_name (code));
3056     }
3057
3058   return relP;
3059 }
3060
3061 /* Machine-dependent usage-output.  */
3062
3063 void
3064 md_show_usage (stream)
3065      FILE *stream;
3066 {
3067   /* The messages are formatted to line up with the generic options.  */
3068   fprintf (stream, _("CRIS-specific options:\n"));
3069   fprintf (stream, "%s",
3070            _("  -h, -H                  Don't execute, print this help text.  Deprecated.\n"));
3071   fprintf (stream, "%s",
3072            _("  -N                      Warn when branches are expanded to jumps.\n"));
3073   fprintf (stream, "%s",
3074            _("  --underscore            User symbols are normally prepended with underscore.\n"));
3075   fprintf (stream, "%s",
3076            _("                          Registers will not need any prefix.\n"));
3077   fprintf (stream, "%s",
3078            _("  --no-underscore         User symbols do not have any prefix.\n"));
3079   fprintf (stream, "%s",
3080            _("                          Registers will require a `$'-prefix.\n"));
3081   fprintf (stream, "%s",
3082            _("  --pic                   Enable generation of position-independent code.\n"));
3083 }
3084
3085 /* Apply a fixS (fixup of an instruction or data that we didn't have
3086    enough info to complete immediately) to the data in a frag.  */
3087
3088 void
3089 md_apply_fix3 (fixP, valP, seg)
3090      fixS *fixP;
3091      valueT *valP;
3092      segT seg;
3093 {
3094   /* This assignment truncates upper bits if valueT is 64 bits (as with
3095      --enable-64-bit-bfd), which is fine here, though we cast to avoid
3096      any compiler warnings.  */
3097   long val = (long) *valP;
3098   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3099
3100   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
3101     fixP->fx_done = 1;
3102
3103   if (fixP->fx_bit_fixP || fixP->fx_im_disp != 0)
3104     {
3105       as_bad_where (fixP->fx_file, fixP->fx_line, _("Invalid relocation"));
3106       fixP->fx_done = 1;
3107     }
3108   else
3109     {
3110       /* We can't actually support subtracting a symbol.  */
3111       if (fixP->fx_subsy != (symbolS *) NULL)
3112         as_bad_where (fixP->fx_file, fixP->fx_line,
3113                       _("expression too complex"));
3114
3115       cris_number_to_imm (buf, val, fixP->fx_size, fixP, seg);
3116     }
3117 }
3118
3119 /* All relocations are relative to the location just after the fixup;
3120    the address of the fixup plus its size.  */
3121
3122 long
3123 md_pcrel_from (fixP)
3124      fixS *fixP;
3125 {
3126   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
3127
3128   /* FIXME:  We get here only at the end of assembly, when X in ".-X" is
3129      still unknown.  Since we don't have pc-relative relocations in a.out,
3130      this is invalid.  What to do if anything for a.out, is to add
3131      pc-relative relocations everywhere including the elinux program
3132      loader.  For ELF, allow straight-forward PC-relative relocations,
3133      which are always relative to the location after the relocation.  */
3134   if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3135       || (fixP->fx_r_type != BFD_RELOC_8_PCREL
3136           && fixP->fx_r_type != BFD_RELOC_16_PCREL
3137           && fixP->fx_r_type != BFD_RELOC_32_PCREL))
3138     as_bad_where (fixP->fx_file, fixP->fx_line,
3139                   _("Invalid pc-relative relocation"));
3140   return fixP->fx_size + addr;
3141 }
3142
3143 /* We have no need to give defaults for symbol-values.  */
3144 symbolS *
3145 md_undefined_symbol (name)
3146      char *name ATTRIBUTE_UNUSED;
3147 {
3148   return 0;
3149 }
3150
3151 /* If this function returns non-zero, it prevents the relocation
3152    against symbol(s) in the FIXP from being replaced with relocations
3153    against section symbols, and guarantees that a relocation will be
3154    emitted even when the value can be resolved locally.  */
3155 int
3156 md_cris_force_relocation (fixp)
3157      struct fix *fixp;
3158 {
3159   switch (fixp->fx_r_type)
3160     {
3161     case BFD_RELOC_VTABLE_INHERIT:
3162     case BFD_RELOC_VTABLE_ENTRY:
3163     case BFD_RELOC_CRIS_16_GOT:
3164     case BFD_RELOC_CRIS_32_GOT:
3165     case BFD_RELOC_CRIS_16_GOTPLT:
3166     case BFD_RELOC_CRIS_32_GOTPLT:
3167     case BFD_RELOC_CRIS_32_GOTREL:
3168     case BFD_RELOC_CRIS_32_PLT_GOTREL:
3169     case BFD_RELOC_CRIS_32_PLT_PCREL:
3170       return 1;
3171     default:
3172       ;
3173     }
3174
3175   return S_FORCE_RELOC (fixp->fx_addsy);
3176 }
3177
3178 /* Check and emit error if broken-word handling has failed to fix up a
3179    case-table.  This is called from write.c, after doing everything it
3180    knows about how to handle broken words.  */
3181
3182 void
3183 tc_cris_check_adjusted_broken_word (new_offset, brokwP)
3184      offsetT new_offset;
3185      struct broken_word *brokwP;
3186 {
3187   if (new_offset > 32767 || new_offset < -32768)
3188     /* We really want a genuine error, not a warning, so make it one.  */
3189     as_bad_where (brokwP->frag->fr_file, brokwP->frag->fr_line,
3190                   _("Adjusted signed .word (%ld) overflows: `switch'-statement too large."),
3191                   (long) new_offset);
3192 }
3193
3194 /* Make a leading REGISTER_PREFIX_CHAR mandatory for all registers.  */
3195
3196 static void cris_force_reg_prefix ()
3197 {
3198   demand_register_prefix = TRUE;
3199 }
3200
3201 /* Do not demand a leading REGISTER_PREFIX_CHAR for all registers.  */
3202
3203 static void cris_relax_reg_prefix ()
3204 {
3205   demand_register_prefix = FALSE;
3206 }
3207
3208 /* Adjust for having a leading '_' on all user symbols.  */
3209
3210 static void cris_sym_leading_underscore ()
3211 {
3212   /* We can't really do anything more than assert that what the program
3213      thinks symbol starts with agrees with the command-line options, since
3214      the bfd is already created.  */
3215
3216   if (!symbols_have_leading_underscore)
3217     as_bad (_(".syntax %s requires command-line option `--underscore'"),
3218             SYNTAX_USER_SYM_LEADING_UNDERSCORE);
3219 }
3220
3221 /* Adjust for not having any particular prefix on user symbols.  */
3222
3223 static void cris_sym_no_leading_underscore ()
3224 {
3225   if (symbols_have_leading_underscore)
3226     as_bad (_(".syntax %s requires command-line option `--no-underscore'"),
3227             SYNTAX_USER_SYM_NO_LEADING_UNDERSCORE);
3228 }
3229
3230 /* Handle the .syntax pseudo, which takes an argument that decides what
3231    syntax the assembly code has.  */
3232
3233 static void
3234 s_syntax (ignore)
3235      int ignore ATTRIBUTE_UNUSED;
3236 {
3237   static const struct syntaxes
3238   {
3239     const char *operand;
3240     void (*fn) PARAMS ((void));
3241   } syntax_table[] =
3242     {{SYNTAX_ENFORCE_REG_PREFIX, cris_force_reg_prefix},
3243      {SYNTAX_RELAX_REG_PREFIX, cris_relax_reg_prefix},
3244      {SYNTAX_USER_SYM_LEADING_UNDERSCORE, cris_sym_leading_underscore},
3245      {SYNTAX_USER_SYM_NO_LEADING_UNDERSCORE, cris_sym_no_leading_underscore}};
3246
3247   const struct syntaxes *sp;
3248
3249   for (sp = syntax_table;
3250        sp < syntax_table + sizeof (syntax_table) / sizeof (syntax_table[0]);
3251        sp++)
3252     {
3253       if (strncmp (input_line_pointer, sp->operand,
3254                    strlen (sp->operand)) == 0)
3255         {
3256           (sp->fn) ();
3257
3258           input_line_pointer += strlen (sp->operand);
3259           demand_empty_rest_of_line ();
3260           return;
3261         }
3262     }
3263
3264   as_bad (_("Unknown .syntax operand"));
3265 }
3266
3267 /* Wrapper for dwarf2_directive_file to emit error if this is seen when
3268    not emitting ELF.  */
3269
3270 static void
3271 s_cris_file (dummy)
3272      int dummy;
3273 {
3274   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3275     as_bad (_("Pseudodirective .file is only valid when generating ELF"));
3276   else
3277     dwarf2_directive_file (dummy);
3278 }
3279
3280 /* Wrapper for dwarf2_directive_loc to emit error if this is seen when not
3281    emitting ELF.  */
3282
3283 static void
3284 s_cris_loc (dummy)
3285      int dummy;
3286 {
3287   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3288     as_bad (_("Pseudodirective .loc is only valid when generating ELF"));
3289   else
3290     dwarf2_directive_loc (dummy);
3291 }
3292
3293 /*
3294  * Local variables:
3295  * eval: (c-set-style "gnu")
3296  * indent-tabs-mode: t
3297  * End:
3298  */