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