OSDN Git Service

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