2002-11-20 Klee Dienes <kdienes@apple.com>
[external/binutils.git] / gas / config / tc-mcore.c
1 /* tc-mcore.c -- Assemble code for M*Core
2    Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to the Free
18    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
21 #include <stdio.h>
22 #include "as.h"
23 #include "bfd.h"
24 #include "subsegs.h"
25 #define DEFINE_TABLE
26 #include "../opcodes/mcore-opc.h"
27 #include "safe-ctype.h"
28 #include <string.h>
29
30 #ifdef OBJ_ELF
31 #include "elf/mcore.h"
32 #endif
33
34 #ifndef streq
35 #define streq(a,b) (strcmp (a, b) == 0)
36 #endif
37
38 /* Forward declarations for dumb compilers.  */
39 static void   mcore_s_literals PARAMS ((int));
40 static void   mcore_cons PARAMS ((int));
41 static void   mcore_float_cons PARAMS ((int));
42 static void   mcore_stringer PARAMS ((int));
43 static void   mcore_fill   PARAMS ((int));
44 static int    log2 PARAMS ((unsigned int));
45 static char * parse_reg    PARAMS ((char *, unsigned *));
46 static char * parse_creg   PARAMS ((char *, unsigned *));
47 static char * parse_exp    PARAMS ((char *, expressionS *));
48 static char * parse_rt     PARAMS ((char *, char **, int, expressionS *));
49 static char * parse_imm    PARAMS ((char *, unsigned *, unsigned, unsigned));
50 static char * parse_mem    PARAMS ((char *, unsigned *, unsigned *, unsigned));
51 static char * parse_psrmod PARAMS ((char *, unsigned *));
52 static void   make_name PARAMS ((char *, char *, int));
53 static int    enter_literal PARAMS ((expressionS *, int));
54 static void   dump_literals PARAMS ((int));
55 static void   check_literals PARAMS ((int, int));
56 static void   mcore_s_text    PARAMS ((int));
57 static void   mcore_s_data    PARAMS ((int));
58 static void   mcore_s_section PARAMS ((int));
59 static void   mcore_s_bss     PARAMS ((int));
60 #ifdef OBJ_ELF
61 static void   mcore_s_comm    PARAMS ((int));
62 #endif
63
64 /* Several places in this file insert raw instructions into the
65    object. They should use MCORE_INST_XXX macros to get the opcodes
66    and then use these two macros to crack the MCORE_INST value into
67    the appropriate byte values.  */
68 #define INST_BYTE0(x)  (target_big_endian ? (((x) >> 8) & 0xFF) : ((x) & 0xFF))
69 #define INST_BYTE1(x)  (target_big_endian ? ((x) & 0xFF) : (((x) >> 8) & 0xFF))
70
71 const char comment_chars[] = "#/";
72 const char line_separator_chars[] = ";";
73 const char line_comment_chars[] = "#/";
74
75 const int md_reloc_size = 8;
76
77 static int do_jsri2bsr = 0;     /* Change here from 1 by Cruess 19 August 97.  */
78 static int sifilter_mode = 0;
79
80 const char EXP_CHARS[] = "eE";
81
82 /* Chars that mean this number is a floating point constant
83     As in 0f12.456
84     or    0d1.2345e12  */
85 const char FLT_CHARS[] = "rRsSfFdDxXpP";
86
87 #define C(what,length) (((what) << 2) + (length))
88 #define GET_WHAT(x)    ((x >> 2))
89
90 /* These are the two types of relaxable instruction */
91 #define COND_JUMP  1
92 #define UNCD_JUMP  2
93
94 #define UNDEF_DISP      0
95 #define DISP12          1
96 #define DISP32          2
97 #define UNDEF_WORD_DISP 3
98
99 #define C12_LEN         2
100 #define C32_LEN        10       /* allow for align */
101 #define U12_LEN         2
102 #define U32_LEN         8       /* allow for align */
103
104 typedef enum
105 {
106   M210,
107   M340
108 }
109 cpu_type;
110
111 cpu_type cpu = M340;
112
113 /* Initialize the relax table.  */
114 const relax_typeS md_relax_table[] = {
115   {    0,     0, 0,       0 },
116   {    0,     0, 0,       0 },
117   {    0,     0, 0,       0 },
118   {    0,     0, 0,       0 },
119
120   /* COND_JUMP */
121   {    0,     0, 0,       0 },                    /* UNDEF_DISP */
122   { 2048, -2046, C12_LEN, C(COND_JUMP, DISP32) }, /* DISP12 */
123   {    0,     0, C32_LEN, 0 },                    /* DISP32 */
124   {    0,     0, C32_LEN, 0 },                    /* UNDEF_WORD_DISP */
125
126   /* UNCD_JUMP */
127   {    0,     0, 0,       0 },                    /* UNDEF_DISP */
128   { 2048, -2046, U12_LEN, C(UNCD_JUMP, DISP32) }, /* DISP12 */
129   {    0,     0, U32_LEN, 0 },                    /* DISP32 */
130   {    0,     0, U32_LEN, 0 }                     /* UNDEF_WORD_DISP */
131
132 };
133
134 /* Literal pool data structures.  */
135 struct literal
136 {
137   unsigned short  refcnt;
138   unsigned char   ispcrel;
139   unsigned char   unused;
140   expressionS     e;
141 };
142
143 #define MAX_POOL_SIZE   (1024/4)
144 static struct literal litpool [MAX_POOL_SIZE];
145 static unsigned poolsize;
146 static unsigned poolnumber;
147 static unsigned long poolspan;
148
149 /* SPANPANIC: the point at which we get too scared and force a dump
150    of the literal pool, and perhaps put a branch in place.
151    Calculated as:
152                  1024   span of lrw/jmpi/jsri insn (actually span+1)
153                 -2      possible alignment at the insn.
154                 -2      possible alignment to get the table aligned.
155                 -2      an inserted branch around the table.
156              == 1018
157    at 1018, we might be in trouble.
158    -- so we have to be smaller than 1018 and since we deal with 2-byte
159    instructions, the next good choice is 1016.
160    -- Note we have a test case that fails when we've got 1018 here.  */
161 #define SPANPANIC       (1016)          /* 1024 - 1 entry - 2 byte rounding.  */
162 #define SPANCLOSE       (900)
163 #define SPANEXIT        (600)
164 static symbolS * poolsym;               /* label for current pool.  */
165 static char poolname[8];
166 static struct hash_control * opcode_hash_control;       /* Opcode mnemonics.  */
167
168 /* This table describes all the machine specific pseudo-ops the assembler
169    has to support.  The fields are:
170      Pseudo-op name without dot
171      Function to call to execute this pseudo-op
172      Integer arg to pass to the function.   */
173 const pseudo_typeS md_pseudo_table[] =
174 {
175   { "export",   s_globl,          0 },
176   { "import",   s_ignore,         0 },
177   { "literals", mcore_s_literals, 0 },
178   { "page",     listing_eject,    0 },
179
180   /* The following are to intercept the placement of data into the text
181      section (eg addresses for a switch table), so that the space they
182      occupy can be taken into account when deciding whether or not to
183      dump the current literal pool.
184      XXX - currently we do not cope with the .space and .dcb.d directives.  */
185   { "ascii",    mcore_stringer,       0 },
186   { "asciz",    mcore_stringer,       1 },
187   { "byte",     mcore_cons,           1 },
188   { "dc",       mcore_cons,           2 },
189   { "dc.b",     mcore_cons,           1 },
190   { "dc.d",     mcore_float_cons,    'd'},
191   { "dc.l",     mcore_cons,           4 },
192   { "dc.s",     mcore_float_cons,    'f'},
193   { "dc.w",     mcore_cons,           2 },
194   { "dc.x",     mcore_float_cons,    'x'},
195   { "double",   mcore_float_cons,    'd'},
196   { "float",    mcore_float_cons,    'f'},
197   { "hword",    mcore_cons,           2 },
198   { "int",      mcore_cons,           4 },
199   { "long",     mcore_cons,           4 },
200   { "octa",     mcore_cons,          16 },
201   { "quad",     mcore_cons,           8 },
202   { "short",    mcore_cons,           2 },
203   { "single",   mcore_float_cons,    'f'},
204   { "string",   mcore_stringer,       1 },
205   { "word",     mcore_cons,           2 },
206   { "fill",     mcore_fill,           0 },
207
208   /* Allow for the effect of section changes.  */
209   { "text",      mcore_s_text,    0 },
210   { "data",      mcore_s_data,    0 },
211   { "bss",       mcore_s_bss,     1 },
212 #ifdef OBJ_EF
213   { "comm",      mcore_s_comm,    0 },
214 #endif
215   { "section",   mcore_s_section, 0 },
216   { "section.s", mcore_s_section, 0 },
217   { "sect",      mcore_s_section, 0 },
218   { "sect.s",    mcore_s_section, 0 },
219
220   { 0,          0,                0 }
221 };
222
223 static void
224 mcore_s_literals (ignore)
225      int ignore;
226 {
227   dump_literals (0);
228   demand_empty_rest_of_line ();
229 }
230
231 static void
232 mcore_cons (nbytes)
233      int nbytes;
234 {
235   if (now_seg == text_section)
236     {
237       char * ptr = input_line_pointer;
238       int    commas = 1;
239
240       /* Count the number of commas on the line.  */
241       while (! is_end_of_line [(unsigned char) * ptr])
242         commas += * ptr ++ == ',';
243
244       poolspan += nbytes * commas;
245     }
246
247   cons (nbytes);
248
249   /* In theory we ought to call check_literals (2,0) here in case
250      we need to dump the literal table.  We cannot do this however,
251      as the directives that we are intercepting may be being used
252      to build a switch table, and we must not interfere with its
253      contents.  Instead we cross our fingers and pray...  */
254 }
255
256 static void
257 mcore_float_cons (float_type)
258      int float_type;
259 {
260   if (now_seg == text_section)
261     {
262       char * ptr = input_line_pointer;
263       int    commas = 1;
264
265 #ifdef REPEAT_CONS_EXPRESSIONS
266 #error REPEAT_CONS_EXPRESSIONS not handled
267 #endif
268
269       /* Count the number of commas on the line.  */
270       while (! is_end_of_line [(unsigned char) * ptr])
271         commas += * ptr ++ == ',';
272
273       /* We would like to compute "hex_float (float_type) * commas"
274          but hex_float is not exported from read.c  */
275       float_type == 'f' ? 4 : (float_type == 'd' ? 8 : 12);
276       poolspan += float_type * commas;
277     }
278
279   float_cons (float_type);
280
281   /* See the comment in mcore_cons () about calling check_literals.
282      It is unlikely that a switch table will be constructed using
283      floating point values, but it is still likely that an indexed
284      table of floating point constants is being created by these
285      directives, so again we must not interfere with their placement.  */
286 }
287
288 static void
289 mcore_stringer (append_zero)
290      int append_zero;
291 {
292   if (now_seg == text_section)
293     {
294       char * ptr = input_line_pointer;
295
296       /* In theory we should compute how many bytes are going to
297          be occupied by the string(s) and add this to the poolspan.
298          To keep things simple however, we just add the number of
299          bytes left on the current line.  This will be an over-
300          estimate, which is OK, and automatically allows for the
301          appending a zero byte, since the real string(s) is/are
302          required to be enclosed in double quotes.  */
303       while (! is_end_of_line [(unsigned char) * ptr])
304         ptr ++;
305
306       poolspan += ptr - input_line_pointer;
307     }
308
309   stringer (append_zero);
310
311   /* We call check_literals here in case a large number of strings are
312      being placed into the text section with a sequence of stringer
313      directives.  In theory we could be upsetting something if these
314      strings are actually in an indexed table instead of referenced by
315      individual labels.  Let us hope that that never happens.  */
316   check_literals (2, 0);
317 }
318
319 static void
320 mcore_fill (unused)
321      int unused;
322 {
323   if (now_seg == text_section)
324     {
325       char * str = input_line_pointer;
326       int    size = 1;
327       int    repeat;
328
329       repeat = atoi (str);
330
331       /* Look to see if a size has been specified.  */
332       while (*str != '\n' && *str != 0 && *str != ',')
333         ++ str;
334
335       if (* str == ',')
336         {
337           size = atoi (str + 1);
338
339           if (size > 8)
340             size = 8;
341           else if (size < 0)
342             size = 0;
343         }
344
345       poolspan += size * repeat;
346     }
347
348   s_fill (unused);
349
350   check_literals (2, 0);
351 }
352
353 /* Handle the section changing pseudo-ops.  These call through to the
354    normal implementations, but they dump the literal pool first.  */
355 static void
356 mcore_s_text (ignore)
357      int ignore;
358 {
359   dump_literals (0);
360
361 #ifdef OBJ_ELF
362   obj_elf_text (ignore);
363 #else
364   s_text (ignore);
365 #endif
366 }
367
368 static void
369 mcore_s_data (ignore)
370      int ignore;
371 {
372   dump_literals (0);
373
374 #ifdef OBJ_ELF
375   obj_elf_data (ignore);
376 #else
377   s_data (ignore);
378 #endif
379 }
380
381 static void
382 mcore_s_section (ignore)
383      int ignore;
384 {
385   /* Scan forwards to find the name of the section.  If the section
386      being switched to is ".line" then this is a DWARF1 debug section
387      which is arbitarily placed inside generated code.  In this case
388      do not dump the literal pool because it is a) inefficient and
389      b) would require the generation of extra code to jump around the
390      pool.  */
391   char * ilp = input_line_pointer;
392
393   while (*ilp != 0 && ISSPACE (*ilp))
394     ++ ilp;
395
396   if (strncmp (ilp, ".line", 5) == 0
397       && (ISSPACE (ilp[5]) || *ilp == '\n' || *ilp == '\r'))
398     ;
399   else
400     dump_literals (0);
401
402 #ifdef OBJ_ELF
403   obj_elf_section (ignore);
404 #endif
405 #ifdef OBJ_COFF
406   obj_coff_section (ignore);
407 #endif
408 }
409
410 static void
411 mcore_s_bss (needs_align)
412      int needs_align;
413 {
414   dump_literals (0);
415
416   s_lcomm_bytes (needs_align);
417 }
418
419 #ifdef OBJ_ELF
420 static void
421 mcore_s_comm (needs_align)
422      int needs_align;
423 {
424   dump_literals (0);
425
426   obj_elf_common (needs_align);
427 }
428 #endif
429
430 /* This function is called once, at assembler startup time.  This should
431    set up all the tables, etc that the MD part of the assembler needs.  */
432 void
433 md_begin ()
434 {
435   const mcore_opcode_info * opcode;
436   char * prev_name = "";
437
438   opcode_hash_control = hash_new ();
439
440   /* Insert unique names into hash table */
441   for (opcode = mcore_table; opcode->name; opcode ++)
442     {
443       if (! streq (prev_name, opcode->name))
444         {
445           prev_name = opcode->name;
446           hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
447         }
448     }
449 }
450
451 static int reg_m;
452 static int reg_n;
453 static expressionS immediate;   /* absolute expression */
454
455 /* Get a log2(val).  */
456 static int
457 log2 (val)
458     unsigned int val;
459 {
460     int log = -1;
461     while (val != 0)
462       {
463         log ++;
464         val >>= 1;
465       }
466
467     return log;
468 }
469
470 /* Try to parse a reg name.  */
471 static char *
472 parse_reg (s, reg)
473      char * s;
474      unsigned * reg;
475 {
476   /* Strip leading whitespace.  */
477   while (ISSPACE (* s))
478     ++ s;
479
480   if (TOLOWER (s[0]) == 'r')
481     {
482       if (s[1] == '1' && s[2] >= '0' && s[2] <= '5')
483         {
484           *reg = 10 + s[2] - '0';
485           return s + 3;
486         }
487
488       if (s[1] >= '0' && s[1] <= '9')
489         {
490           *reg = s[1] - '0';
491           return s + 2;
492         }
493     }
494   else if (   TOLOWER (s[0]) == 's'
495            && TOLOWER (s[1]) == 'p'
496            && ! ISALNUM (s[2]))
497     {
498       * reg = 0;
499       return s + 2;
500     }
501
502   as_bad (_("register expected, but saw '%.6s'"), s);
503   return s;
504 }
505
506 static struct Cregs
507 {
508   char * name;
509   unsigned int crnum;
510 }
511 cregs[] =
512 {
513   { "psr",       0},
514   { "vbr",       1},
515   { "epsr",      2},
516   { "fpsr",      3},
517   { "epc",       4},
518   { "fpc",       5},
519   { "ss0",       6},
520   { "ss1",       7},
521   { "ss2",       8},
522   { "ss3",       9},
523   { "ss4",      10},
524   { "gcr",      11},
525   { "gsr",      12},
526   { "",          0}
527 };
528
529 static char *
530 parse_creg (s, reg)
531      char * s;
532      unsigned * reg;
533 {
534   int i;
535
536   /* Strip leading whitespace.  */
537   while (ISSPACE (* s))
538     ++s;
539
540   if ((TOLOWER (s[0]) == 'c' && TOLOWER (s[1]) == 'r'))
541     {
542       if (s[2] == '3' && s[3] >= '0' && s[3] <= '1')
543         {
544           *reg = 30 + s[3] - '0';
545           return s + 4;
546         }
547
548       if (s[2] == '2' && s[3] >= '0' && s[3] <= '9')
549         {
550           *reg = 20 + s[3] - '0';
551           return s + 4;
552         }
553
554       if (s[2] == '1' && s[3] >= '0' && s[3] <= '9')
555         {
556           *reg = 10 + s[3] - '0';
557           return s + 4;
558         }
559
560       if (s[2] >= '0' && s[2] <= '9')
561         {
562           *reg = s[2] - '0';
563           return s + 3;
564         }
565     }
566
567   /* Look at alternate creg names before giving error.  */
568   for (i = 0; cregs[i].name[0] != '\0'; i++)
569     {
570       char buf [10];
571       int  length;
572       int  j;
573
574       length = strlen (cregs[i].name);
575
576       for (j = 0; j < length; j++)
577         buf[j] = TOLOWER (s[j]);
578
579       if (strncmp (cregs[i].name, buf, length) == 0)
580         {
581           *reg = cregs[i].crnum;
582           return s + length;
583         }
584     }
585
586   as_bad (_("control register expected, but saw '%.6s'"), s);
587
588   return s;
589 }
590
591 static char *
592 parse_psrmod (s, reg)
593   char *     s;
594   unsigned * reg;
595 {
596   int  i;
597   char buf[10];
598   static struct psrmods
599   {
600     char *       name;
601     unsigned int value;
602   }
603   psrmods[] =
604   {
605     { "ie", 1 },
606     { "fe", 2 },
607     { "ee", 4 },
608     { "af", 8 } /* Really 0 and non-combinable.  */
609   };
610
611   for (i = 0; i < 2; i++)
612     buf[i] = TOLOWER (s[i]);
613
614   for (i = sizeof (psrmods) / sizeof (psrmods[0]); i--;)
615     {
616       if (! strncmp (psrmods[i].name, buf, 2))
617         {
618           * reg = psrmods[i].value;
619
620           return s + 2;
621         }
622     }
623
624   as_bad (_("bad/missing psr specifier"));
625
626   * reg = 0;
627
628   return s;
629 }
630
631 static char *
632 parse_exp (s, e)
633      char * s;
634      expressionS * e;
635 {
636   char * save;
637   char * new;
638
639   /* Skip whitespace.  */
640   while (ISSPACE (* s))
641     ++ s;
642
643   save = input_line_pointer;
644   input_line_pointer = s;
645
646   expression (e);
647
648   if (e->X_op == O_absent)
649     as_bad (_("missing operand"));
650
651   new = input_line_pointer;
652   input_line_pointer = save;
653
654   return new;
655 }
656
657 static void
658 make_name (s, p, n)
659      char * s;
660      char * p;
661      int n;
662 {
663   static const char hex[] = "0123456789ABCDEF";
664
665   s[0] = p[0];
666   s[1] = p[1];
667   s[2] = p[2];
668   s[3] = hex[(n >> 12) & 0xF];
669   s[4] = hex[(n >>  8) & 0xF];
670   s[5] = hex[(n >>  4) & 0xF];
671   s[6] = hex[(n)       & 0xF];
672   s[7] = 0;
673 }
674
675 #define POOL_END_LABEL   ".LE"
676 #define POOL_START_LABEL ".LS"
677
678 static void
679 dump_literals (isforce)
680      int isforce;
681 {
682   int i;
683   struct literal * p;
684   symbolS * brarsym;
685
686   if (poolsize == 0)
687     return;
688
689   /* Must we branch around the literal table? */
690   if (isforce)
691     {
692       char * output;
693       char brarname[8];
694
695       make_name (brarname, POOL_END_LABEL, poolnumber);
696
697       brarsym = symbol_make (brarname);
698
699       symbol_table_insert (brarsym);
700
701       output = frag_var (rs_machine_dependent,
702                          md_relax_table[C (UNCD_JUMP, DISP32)].rlx_length,
703                          md_relax_table[C (UNCD_JUMP, DISP12)].rlx_length,
704                          C (UNCD_JUMP, 0), brarsym, 0, 0);
705       output[0] = INST_BYTE0 (MCORE_INST_BR);   /* br .+xxx */
706       output[1] = INST_BYTE1 (MCORE_INST_BR);
707     }
708
709   /* Make sure that the section is sufficiently aligned and that
710      the literal table is aligned within it.  */
711   record_alignment (now_seg, 2);
712   frag_align (2, 0, 0);
713
714   colon (S_GET_NAME (poolsym));
715
716   for (i = 0, p = litpool; i < poolsize; i++, p++)
717     emit_expr (& p->e, 4);
718
719   if (isforce)
720     colon (S_GET_NAME (brarsym));
721
722    poolsize = 0;
723 }
724
725 static void
726 check_literals (kind, offset)
727      int kind;
728      int offset;
729 {
730   poolspan += offset;
731
732   /* SPANCLOSE and SPANEXIT are smaller numbers than SPANPANIC.
733      SPANPANIC means that we must dump now.
734      kind == 0 is any old instruction.
735      kind  > 0 means we just had a control transfer instruction.
736      kind == 1 means within a function
737      kind == 2 means we just left a function
738
739      The dump_literals (1) call inserts a branch around the table, so
740      we first look to see if its a situation where we won't have to
741      insert a branch (e.g., the previous instruction was an unconditional
742      branch).
743
744      SPANPANIC is the point where we must dump a single-entry pool.
745      it accounts for alignments and an inserted branch.
746      the 'poolsize*2' accounts for the scenario where we do:
747        lrw r1,lit1; lrw r2,lit2; lrw r3,lit3
748      Note that the 'lit2' reference is 2 bytes further along
749      but the literal it references will be 4 bytes further along,
750      so we must consider the poolsize into this equation.
751      This is slightly over-cautious, but guarantees that we won't
752      panic because a relocation is too distant.  */
753
754   if (poolspan > SPANCLOSE && kind > 0)
755     dump_literals (0);
756   else if (poolspan > SPANEXIT && kind > 1)
757     dump_literals (0);
758   else if (poolspan >= (SPANPANIC - poolsize * 2))
759     dump_literals (1);
760 }
761
762 static int
763 enter_literal (e, ispcrel)
764      expressionS * e;
765      int ispcrel;
766 {
767   int i;
768   struct literal * p;
769
770   if (poolsize >= MAX_POOL_SIZE - 2)
771     {
772       /* The literal pool is as full as we can handle. We have
773          to be 2 entries shy of the 1024/4=256 entries because we
774          have to allow for the branch (2 bytes) and the alignment
775          (2 bytes before the first insn referencing the pool and
776          2 bytes before the pool itself) == 6 bytes, rounds up
777          to 2 entries.  */
778       dump_literals (1);
779     }
780
781   if (poolsize == 0)
782     {
783       /* Create new literal pool.  */
784       if (++ poolnumber > 0xFFFF)
785         as_fatal (_("more than 65K literal pools"));
786
787       make_name (poolname, POOL_START_LABEL, poolnumber);
788       poolsym = symbol_make (poolname);
789       symbol_table_insert (poolsym);
790       poolspan = 0;
791     }
792
793   /* Search pool for value so we don't have duplicates.  */
794   for (p = litpool, i = 0; i < poolsize; i++, p++)
795     {
796       if (e->X_op == p->e.X_op
797           && e->X_add_symbol == p->e.X_add_symbol
798           && e->X_add_number == p->e.X_add_number
799           && ispcrel == p->ispcrel)
800         {
801           p->refcnt ++;
802           return i;
803         }
804     }
805
806   p->refcnt  = 1;
807   p->ispcrel = ispcrel;
808   p->e       = * e;
809
810   poolsize ++;
811
812   return i;
813 }
814
815 /* Parse a literal specification. -- either new or old syntax.
816    old syntax: the user supplies the label and places the literal.
817    new syntax: we put it into the literal pool.  */
818 static char *
819 parse_rt (s, outputp, ispcrel, ep)
820      char * s;
821      char ** outputp;
822      int ispcrel;
823      expressionS * ep;
824 {
825   expressionS e;
826   int n;
827
828   if (ep)
829     /* Indicate nothing there.  */
830     ep->X_op = O_absent;
831
832   if (*s == '[')
833     {
834       s = parse_exp (s + 1, & e);
835
836       if (*s == ']')
837         s++;
838       else
839         as_bad (_("missing ']'"));
840     }
841   else
842     {
843       s = parse_exp (s, & e);
844
845       n = enter_literal (& e, ispcrel);
846
847       if (ep)
848         *ep = e;
849
850       /* Create a reference to pool entry.  */
851       e.X_op         = O_symbol;
852       e.X_add_symbol = poolsym;
853       e.X_add_number = n << 2;
854     }
855
856   * outputp = frag_more (2);
857
858   fix_new_exp (frag_now, (*outputp) - frag_now->fr_literal, 2, & e, 1,
859                BFD_RELOC_MCORE_PCREL_IMM8BY4);
860
861   return s;
862 }
863
864 static char *
865 parse_imm (s, val, min, max)
866      char * s;
867      unsigned * val;
868      unsigned min;
869      unsigned max;
870 {
871   char * new;
872   expressionS e;
873
874   new = parse_exp (s, & e);
875
876   if (e.X_op == O_absent)
877     ; /* An error message has already been emitted.  */
878   else if (e.X_op != O_constant)
879     as_bad (_("operand must be a constant"));
880   else if (e.X_add_number < min || e.X_add_number > max)
881     as_bad (_("operand must be absolute in range %d..%d, not %d"),
882             min, max, e.X_add_number);
883
884   * val = e.X_add_number;
885
886   return new;
887 }
888
889 static char *
890 parse_mem (s, reg, off, siz)
891      char * s;
892      unsigned * reg;
893      unsigned * off;
894      unsigned siz;
895 {
896   char * new;
897
898   * off = 0;
899
900   while (ISSPACE (* s))
901     ++ s;
902
903   if (* s == '(')
904     {
905       s = parse_reg (s + 1, reg);
906
907       while (ISSPACE (* s))
908         ++ s;
909
910       if (* s == ',')
911         {
912           s = parse_imm (s + 1, off, 0, 63);
913
914           if (siz > 1)
915             {
916               if (siz > 2)
917                 {
918                   if (* off & 0x3)
919                     as_bad (_("operand must be a multiple of 4"));
920
921                   * off >>= 2;
922                 }
923               else
924                 {
925                   if (* off & 0x1)
926                     as_bad (_("operand must be a multiple of 2"));
927
928                   * off >>= 1;
929                 }
930             }
931         }
932
933       while (ISSPACE (* s))
934         ++ s;
935
936       if (* s == ')')
937         s ++;
938     }
939   else
940     as_bad (_("base register expected"));
941
942   return s;
943 }
944
945 /* This is the guts of the machine-dependent assembler.  STR points to a
946    machine dependent instruction.  This function is supposed to emit
947    the frags/bytes it assembles to.  */
948
949 void
950 md_assemble (str)
951      char * str;
952 {
953   char * op_start;
954   char * op_end;
955   mcore_opcode_info * opcode;
956   char * output;
957   int nlen = 0;
958   unsigned short inst;
959   unsigned reg;
960   unsigned off;
961   unsigned isize;
962   expressionS e;
963   char name[20];
964
965   /* Drop leading whitespace.  */
966   while (ISSPACE (* str))
967     str ++;
968
969   /* Find the op code end.  */
970   for (op_start = op_end = str;
971        nlen < 20 && !is_end_of_line [(unsigned char) *op_end] && *op_end != ' ';
972        op_end++)
973     {
974       name[nlen] = op_start[nlen];
975       nlen++;
976     }
977
978   name [nlen] = 0;
979
980   if (nlen == 0)
981     {
982       as_bad (_("can't find opcode "));
983       return;
984     }
985
986   opcode = (mcore_opcode_info *) hash_find (opcode_hash_control, name);
987   if (opcode == NULL)
988     {
989       as_bad (_("unknown opcode \"%s\""), name);
990       return;
991     }
992
993   inst = opcode->inst;
994   isize = 2;
995
996   switch (opcode->opclass)
997     {
998     case O0:
999       output = frag_more (2);
1000       break;
1001
1002     case OT:
1003       op_end = parse_imm (op_end + 1, & reg, 0, 3);
1004       inst |= reg;
1005       output = frag_more (2);
1006       break;
1007
1008     case O1:
1009       op_end = parse_reg (op_end + 1, & reg);
1010       inst |= reg;
1011       output = frag_more (2);
1012       break;
1013
1014     case JMP:
1015       op_end = parse_reg (op_end + 1, & reg);
1016       inst |= reg;
1017       output = frag_more (2);
1018       /* In a sifilter mode, we emit this insn 2 times,
1019          fixes problem of an interrupt during a jmp..  */
1020       if (sifilter_mode)
1021         {
1022           output[0] = INST_BYTE0 (inst);
1023           output[1] = INST_BYTE1 (inst);
1024           output = frag_more (2);
1025         }
1026       break;
1027
1028     case JSR:
1029       op_end = parse_reg (op_end + 1, & reg);
1030
1031       if (reg == 15)
1032         as_bad (_("invalid register: r15 illegal"));
1033
1034       inst |= reg;
1035       output = frag_more (2);
1036
1037       if (sifilter_mode)
1038         {
1039           /* Replace with:  bsr .+2 ; addi r15,6; jmp rx ; jmp rx */
1040           inst = MCORE_INST_BSR;        /* with 0 displacement */
1041           output[0] = INST_BYTE0 (inst);
1042           output[1] = INST_BYTE1 (inst);
1043
1044           output = frag_more (2);
1045           inst = MCORE_INST_ADDI;
1046           inst |= 15;                   /* addi r15,6 */
1047           inst |= (6 - 1) << 4;         /* over the jmp's */
1048           output[0] = INST_BYTE0 (inst);
1049           output[1] = INST_BYTE1 (inst);
1050
1051           output = frag_more (2);
1052           inst = MCORE_INST_JMP | reg;
1053           output[0] = INST_BYTE0 (inst);
1054           output[1] = INST_BYTE1 (inst);
1055
1056           output = frag_more (2);               /* 2nd emitted in fallthru */
1057         }
1058       break;
1059
1060     case OC:
1061       op_end = parse_reg (op_end + 1, & reg);
1062       inst |= reg;
1063
1064       /* Skip whitespace.  */
1065       while (ISSPACE (* op_end))
1066         ++ op_end;
1067
1068       if (*op_end == ',')
1069         {
1070           op_end = parse_creg (op_end + 1, & reg);
1071           inst |= reg << 4;
1072         }
1073
1074       output = frag_more (2);
1075       break;
1076
1077     case MULSH:
1078       if (cpu == M210)
1079         {
1080           as_bad (_("M340 specific opcode used when assembling for M210"));
1081           break;
1082         }
1083       /* drop through...  */
1084     case O2:
1085       op_end = parse_reg (op_end + 1, & reg);
1086       inst |= reg;
1087
1088       /* Skip whitespace.  */
1089       while (ISSPACE (* op_end))
1090         ++ op_end;
1091
1092       if (* op_end == ',')
1093         {
1094           op_end = parse_reg (op_end + 1, & reg);
1095           inst |= reg << 4;
1096         }
1097       else
1098         as_bad (_("second operand missing"));
1099
1100       output = frag_more (2);
1101       break;
1102
1103     case X1:    /* Handle both syntax-> xtrb- r1,rx OR xtrb- rx */
1104       op_end = parse_reg (op_end + 1, & reg);
1105
1106       /* Skip whitespace.  */
1107       while (ISSPACE (* op_end))
1108         ++ op_end;
1109
1110       if (* op_end == ',')      /* xtrb- r1,rx */
1111         {
1112           if (reg != 1)
1113             as_bad (_("destination register must be r1"));
1114
1115           op_end = parse_reg (op_end + 1, & reg);
1116         }
1117
1118       inst |= reg;
1119       output = frag_more (2);
1120       break;
1121
1122     case O1R1:  /* div- rx,r1 */
1123       op_end = parse_reg (op_end + 1, & reg);
1124       inst |= reg;
1125
1126       /* Skip whitespace.  */
1127       while (ISSPACE (* op_end))
1128         ++ op_end;
1129
1130       if (* op_end == ',')
1131         {
1132           op_end = parse_reg (op_end + 1, & reg);
1133           if (reg != 1)
1134             as_bad (_("source register must be r1"));
1135         }
1136       else
1137         as_bad (_("second operand missing"));
1138
1139       output = frag_more (2);
1140       break;
1141
1142     case OI:
1143       op_end = parse_reg (op_end + 1, & reg);
1144       inst |= reg;
1145
1146       /* Skip whitespace.  */
1147       while (ISSPACE (* op_end))
1148         ++ op_end;
1149
1150       if (* op_end == ',')
1151         {
1152           op_end = parse_imm (op_end + 1, & reg, 1, 32);
1153           inst |= (reg - 1) << 4;
1154         }
1155       else
1156         as_bad (_("second operand missing"));
1157
1158       output = frag_more (2);
1159       break;
1160
1161     case OB:
1162       op_end = parse_reg (op_end + 1, & reg);
1163       inst |= reg;
1164
1165       /* Skip whitespace.  */
1166       while (ISSPACE (* op_end))
1167         ++ op_end;
1168
1169       if (* op_end == ',')
1170         {
1171           op_end = parse_imm (op_end + 1, & reg, 0, 31);
1172           inst |= reg << 4;
1173         }
1174       else
1175         as_bad (_("second operand missing"));
1176
1177       output = frag_more (2);
1178       break;
1179
1180     case OB2:           /* like OB, but arg is 2^n instead of n */
1181       op_end = parse_reg (op_end + 1, & reg);
1182       inst |= reg;
1183
1184       /* Skip whitespace.  */
1185       while (ISSPACE (* op_end))
1186         ++ op_end;
1187
1188       if (* op_end == ',')
1189         {
1190           op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1191           /* Further restrict the immediate to a power of two.  */
1192           if ((reg & (reg - 1)) == 0)
1193             reg = log2 (reg);
1194           else
1195             {
1196               reg = 0;
1197               as_bad (_("immediate is not a power of two"));
1198             }
1199           inst |= (reg) << 4;
1200         }
1201       else
1202         as_bad (_("second operand missing"));
1203
1204       output = frag_more (2);
1205       break;
1206
1207     case OBRa:  /* Specific for bgeni: imm of 0->6 translate to movi.  */
1208     case OBRb:
1209     case OBRc:
1210       op_end = parse_reg (op_end + 1, & reg);
1211       inst |= reg;
1212
1213       /* Skip whitespace.  */
1214       while (ISSPACE (* op_end))
1215         ++ op_end;
1216
1217       if (* op_end == ',')
1218         {
1219           op_end = parse_imm (op_end + 1, & reg, 0, 31);
1220           /* immediate values of 0 -> 6 translate to movi */
1221           if (reg <= 6)
1222             {
1223               inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1224               reg = 0x1 << reg;
1225               as_warn (_("translating bgeni to movi"));
1226             }
1227           inst &= ~ 0x01f0;
1228           inst |= reg << 4;
1229         }
1230       else
1231         as_bad (_("second operand missing"));
1232
1233       output = frag_more (2);
1234       break;
1235
1236     case OBR2:  /* like OBR, but arg is 2^n instead of n */
1237       op_end = parse_reg (op_end + 1, & reg);
1238       inst |= reg;
1239
1240       /* Skip whitespace.  */
1241       while (ISSPACE (* op_end))
1242         ++ op_end;
1243
1244       if (* op_end == ',')
1245         {
1246           op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1247
1248           /* Further restrict the immediate to a power of two.  */
1249           if ((reg & (reg - 1)) == 0)
1250             reg = log2 (reg);
1251           else
1252             {
1253               reg = 0;
1254               as_bad (_("immediate is not a power of two"));
1255             }
1256
1257           /* Immediate values of 0 -> 6 translate to movi.  */
1258           if (reg <= 6)
1259             {
1260               inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1261               reg = 0x1 << reg;
1262               as_warn (_("translating mgeni to movi"));
1263             }
1264
1265           inst |= reg << 4;
1266         }
1267       else
1268         as_bad (_("second operand missing"));
1269
1270       output = frag_more (2);
1271       break;
1272
1273     case OMa:   /* Specific for bmaski: imm 1->7 translate to movi.  */
1274     case OMb:
1275     case OMc:
1276       op_end = parse_reg (op_end + 1, & reg);
1277       inst |= reg;
1278
1279       /* Skip whitespace.  */
1280       while (ISSPACE (* op_end))
1281         ++ op_end;
1282
1283       if (* op_end == ',')
1284         {
1285           op_end = parse_imm (op_end + 1, & reg, 1, 32);
1286
1287           /* Immediate values of 1 -> 7 translate to movi.  */
1288           if (reg <= 7)
1289             {
1290               inst = (inst & 0xF) | MCORE_INST_BMASKI_ALT;
1291               reg = (0x1 << reg) - 1;
1292               inst |= reg << 4;
1293
1294               as_warn (_("translating bmaski to movi"));
1295             }
1296           else
1297             {
1298               inst &= ~ 0x01F0;
1299               inst |= (reg & 0x1F) << 4;
1300             }
1301         }
1302       else
1303         as_bad (_("second operand missing"));
1304
1305       output = frag_more (2);
1306       break;
1307
1308     case SI:
1309       op_end = parse_reg (op_end + 1, & reg);
1310       inst |= reg;
1311
1312       /* Skip whitespace.  */
1313       while (ISSPACE (* op_end))
1314         ++ op_end;
1315
1316       if (* op_end == ',')
1317         {
1318           op_end = parse_imm (op_end + 1, & reg, 1, 31);
1319           inst |= reg << 4;
1320         }
1321       else
1322         as_bad (_("second operand missing"));
1323
1324       output = frag_more (2);
1325       break;
1326
1327     case I7:
1328       op_end = parse_reg (op_end + 1, & reg);
1329       inst |= reg;
1330
1331       /* Skip whitespace.  */
1332       while (ISSPACE (* op_end))
1333         ++ op_end;
1334
1335       if (* op_end == ',')
1336         {
1337           op_end = parse_imm (op_end + 1, & reg, 0, 0x7F);
1338           inst |= reg << 4;
1339         }
1340       else
1341         as_bad (_("second operand missing"));
1342
1343       output = frag_more (2);
1344       break;
1345
1346     case LS:
1347       op_end = parse_reg (op_end + 1, & reg);
1348       inst |= reg << 8;
1349
1350       /* Skip whitespace.  */
1351       while (ISSPACE (* op_end))
1352         ++ op_end;
1353
1354       if (* op_end == ',')
1355         {
1356           int size;
1357
1358           if ((inst & 0x6000) == 0)
1359             size = 4;
1360           else if ((inst & 0x6000) == 0x4000)
1361             size = 2;
1362           else if ((inst & 0x6000) == 0x2000)
1363             size = 1;
1364
1365           op_end = parse_mem (op_end + 1, & reg, & off, size);
1366
1367           if (off > 16)
1368             as_bad (_("displacement too large (%d)"), off);
1369           else
1370             inst |= (reg) | (off << 4);
1371         }
1372       else
1373         as_bad (_("second operand missing"));
1374
1375       output = frag_more (2);
1376       break;
1377
1378     case LR:
1379       op_end = parse_reg (op_end + 1, & reg);
1380
1381       if (reg == 0 || reg == 15)
1382         as_bad (_("Invalid register: r0 and r15 illegal"));
1383
1384       inst |= (reg << 8);
1385
1386       /* Skip whitespace.  */
1387       while (ISSPACE (* op_end))
1388         ++ op_end;
1389
1390       if (* op_end == ',')
1391         {
1392           /* parse_rt calls frag_more() for us.  */
1393           input_line_pointer = parse_rt (op_end + 1, & output, 0, 0);
1394           op_end = input_line_pointer;
1395         }
1396       else
1397         {
1398           as_bad (_("second operand missing"));
1399           output = frag_more (2);               /* save its space */
1400         }
1401       break;
1402
1403     case LJ:
1404       input_line_pointer = parse_rt (op_end + 1, & output, 1, 0);
1405       /* parse_rt() calls frag_more() for us.  */
1406       op_end = input_line_pointer;
1407       break;
1408
1409     case RM:
1410       op_end = parse_reg (op_end + 1, & reg);
1411
1412       if (reg == 0 || reg == 15)
1413         as_bad (_("bad starting register: r0 and r15 invalid"));
1414
1415       inst |= reg;
1416
1417       /* Skip whitespace.  */
1418       while (ISSPACE (* op_end))
1419         ++ op_end;
1420
1421       if (* op_end == '-')
1422         {
1423           op_end = parse_reg (op_end + 1, & reg);
1424
1425           if (reg != 15)
1426             as_bad (_("ending register must be r15"));
1427
1428           /* Skip whitespace.  */
1429           while (ISSPACE (* op_end))
1430             ++ op_end;
1431         }
1432
1433       if (* op_end == ',')
1434         {
1435           op_end ++;
1436
1437           /* Skip whitespace.  */
1438           while (ISSPACE (* op_end))
1439             ++ op_end;
1440
1441           if (* op_end == '(')
1442             {
1443               op_end = parse_reg (op_end + 1, & reg);
1444
1445               if (reg != 0)
1446                 as_bad (_("bad base register: must be r0"));
1447
1448               if (* op_end == ')')
1449                 op_end ++;
1450             }
1451           else
1452             as_bad (_("base register expected"));
1453         }
1454       else
1455         as_bad (_("second operand missing"));
1456
1457       output = frag_more (2);
1458       break;
1459
1460     case RQ:
1461       op_end = parse_reg (op_end + 1, & reg);
1462
1463       if (reg != 4)
1464         as_fatal (_("first register must be r4"));
1465
1466       /* Skip whitespace.  */
1467       while (ISSPACE (* op_end))
1468         ++ op_end;
1469
1470       if (* op_end == '-')
1471         {
1472           op_end = parse_reg (op_end + 1, & reg);
1473
1474           if (reg != 7)
1475             as_fatal (_("last register must be r7"));
1476
1477           /* Skip whitespace.  */
1478           while (ISSPACE (* op_end))
1479             ++ op_end;
1480
1481           if (* op_end == ',')
1482             {
1483               op_end ++;
1484
1485               /* Skip whitespace.  */
1486               while (ISSPACE (* op_end))
1487                 ++ op_end;
1488
1489               if (* op_end == '(')
1490                 {
1491                   op_end = parse_reg (op_end + 1, & reg);
1492
1493                   if (reg >= 4 && reg <= 7)
1494                     as_fatal ("base register cannot be r4, r5, r6, or r7");
1495
1496                   inst |= reg;
1497
1498                   /* Skip whitespace.  */
1499                   while (ISSPACE (* op_end))
1500                     ++ op_end;
1501
1502                   if (* op_end == ')')
1503                     op_end ++;
1504                 }
1505               else
1506                 as_bad (_("base register expected"));
1507             }
1508           else
1509             as_bad (_("second operand missing"));
1510         }
1511       else
1512         as_bad (_("reg-reg expected"));
1513
1514       output = frag_more (2);
1515       break;
1516
1517     case BR:
1518       input_line_pointer = parse_exp (op_end + 1, & e);
1519       op_end = input_line_pointer;
1520
1521       output = frag_more (2);
1522
1523       fix_new_exp (frag_now, output-frag_now->fr_literal,
1524                    2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM11BY2);
1525       break;
1526
1527     case BL:
1528       op_end = parse_reg (op_end + 1, & reg);
1529       inst |= reg << 4;
1530
1531       /* Skip whitespace.  */
1532       while (ISSPACE (* op_end))
1533         ++ op_end;
1534
1535       if (* op_end == ',')
1536         {
1537           op_end = parse_exp (op_end + 1, & e);
1538           output = frag_more (2);
1539
1540           fix_new_exp (frag_now, output-frag_now->fr_literal,
1541                        2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM4BY2);
1542         }
1543       else
1544         {
1545           as_bad (_("second operand missing"));
1546           output = frag_more (2);
1547         }
1548       break;
1549
1550     case JC:
1551       input_line_pointer = parse_exp (op_end + 1, & e);
1552       op_end = input_line_pointer;
1553
1554       output = frag_var (rs_machine_dependent,
1555                          md_relax_table[C (COND_JUMP, DISP32)].rlx_length,
1556                          md_relax_table[C (COND_JUMP, DISP12)].rlx_length,
1557                          C (COND_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1558       isize = C32_LEN;
1559       break;
1560
1561     case JU:
1562       input_line_pointer = parse_exp (op_end + 1, & e);
1563       op_end = input_line_pointer;
1564
1565       output = frag_var (rs_machine_dependent,
1566                          md_relax_table[C (UNCD_JUMP, DISP32)].rlx_length,
1567                          md_relax_table[C (UNCD_JUMP, DISP12)].rlx_length,
1568                          C (UNCD_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1569       isize = U32_LEN;
1570       break;
1571
1572     case JL:
1573       inst = MCORE_INST_JSRI;           /* jsri */
1574       input_line_pointer = parse_rt (op_end + 1, & output, 1, & e);
1575       /* parse_rt() calls frag_more for us.  */
1576       op_end = input_line_pointer;
1577
1578       /* Only do this if we know how to do it ...  */
1579       if (e.X_op != O_absent && do_jsri2bsr)
1580         {
1581           /* Look at adding the R_PCREL_JSRIMM11BY2.  */
1582           fix_new_exp (frag_now, output-frag_now->fr_literal,
1583                        2, & e, 1, BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2);
1584         }
1585       break;
1586
1587     case RSI:                           /* SI, but imm becomes 32-imm */
1588       op_end = parse_reg (op_end + 1, & reg);
1589       inst |= reg;
1590
1591       /* Skip whitespace.  */
1592       while (ISSPACE (* op_end))
1593         ++ op_end;
1594
1595       if (* op_end == ',')
1596         {
1597           op_end = parse_imm (op_end + 1, & reg, 1, 31);
1598
1599           reg = 32 - reg;
1600           inst |= reg << 4;
1601         }
1602       else
1603         as_bad (_("second operand missing"));
1604
1605       output = frag_more (2);
1606       break;
1607
1608     case DO21:                  /* O2, dup rd, lit must be 1 */
1609       op_end = parse_reg (op_end + 1, & reg);
1610       inst |= reg;
1611       inst |= reg << 4;
1612
1613       /* Skip whitespace.  */
1614       while (ISSPACE (* op_end))
1615         ++ op_end;
1616
1617       if (* op_end == ',')
1618         {
1619           op_end = parse_imm (op_end + 1, & reg, 1, 31);
1620
1621           if (reg != 1)
1622             as_bad (_("second operand must be 1"));
1623         }
1624       else
1625         as_bad (_("second operand missing"));
1626
1627       output = frag_more (2);
1628       break;
1629
1630     case SIa:
1631       op_end = parse_reg (op_end + 1, & reg);
1632       inst |= reg;
1633
1634       /* Skip whitespace.  */
1635       while (ISSPACE (* op_end))
1636         ++ op_end;
1637
1638       if (* op_end == ',')
1639         {
1640           op_end = parse_imm (op_end + 1, & reg, 1, 31);
1641
1642           if (reg == 0)
1643             as_bad (_("zero used as immediate value"));
1644
1645           inst |= reg << 4;
1646         }
1647       else
1648         as_bad (_("second operand missing"));
1649
1650       output = frag_more (2);
1651       break;
1652
1653     case OPSR:
1654       if (cpu == M210)
1655         {
1656           as_bad (_("M340 specific opcode used when assembling for M210"));
1657           break;
1658         }
1659
1660       op_end = parse_psrmod (op_end + 1, & reg);
1661
1662       /* Look for further selectors.  */
1663       while (* op_end == ',')
1664         {
1665           unsigned value;
1666
1667           op_end = parse_psrmod (op_end + 1, & value);
1668
1669           if (value & reg)
1670             as_bad (_("duplicated psr bit specifier"));
1671
1672           reg |= value;
1673         }
1674
1675       if (reg > 8)
1676         as_bad (_("`af' must appear alone"));
1677
1678       inst |= (reg & 0x7);
1679       output = frag_more (2);
1680       break;
1681
1682     default:
1683       as_bad (_("unimplemented opcode \"%s\""), name);
1684     }
1685
1686   /* Drop whitespace after all the operands have been parsed.  */
1687   while (ISSPACE (* op_end))
1688     op_end ++;
1689
1690   /* Give warning message if the insn has more operands than required.  */
1691   if (strcmp (op_end, opcode->name) && strcmp (op_end, ""))
1692     as_warn (_("ignoring operands: %s "), op_end);
1693
1694   output[0] = INST_BYTE0 (inst);
1695   output[1] = INST_BYTE1 (inst);
1696
1697   check_literals (opcode->transfer, isize);
1698 }
1699
1700 symbolS *
1701 md_undefined_symbol (name)
1702        char * name;
1703 {
1704   return 0;
1705 }
1706
1707 void
1708 md_mcore_end ()
1709 {
1710   dump_literals (0);
1711   subseg_set (text_section, 0);
1712 }
1713
1714 /* Various routines to kill one day.  */
1715 /* Equal to MAX_PRECISION in atof-ieee.c */
1716 #define MAX_LITTLENUMS 6
1717
1718 /* Turn a string in input_line_pointer into a floating point constant of type
1719    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
1720    emitted is stored in *sizeP.  An error message is returned, or NULL on OK.*/
1721 char *
1722 md_atof (type, litP, sizeP)
1723      int type;
1724      char * litP;
1725      int * sizeP;
1726 {
1727   int prec;
1728   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1729   int    i;
1730   char * t;
1731   char * atof_ieee ();
1732
1733   switch (type)
1734     {
1735     case 'f':
1736     case 'F':
1737     case 's':
1738     case 'S':
1739       prec = 2;
1740       break;
1741
1742     case 'd':
1743     case 'D':
1744     case 'r':
1745     case 'R':
1746       prec = 4;
1747       break;
1748
1749     case 'x':
1750     case 'X':
1751       prec = 6;
1752       break;
1753
1754     case 'p':
1755     case 'P':
1756       prec = 6;
1757       break;
1758
1759     default:
1760       *sizeP = 0;
1761       return _("Bad call to MD_NTOF()");
1762     }
1763
1764   t = atof_ieee (input_line_pointer, type, words);
1765
1766   if (t)
1767     input_line_pointer = t;
1768
1769   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1770
1771   if (! target_big_endian)
1772     {
1773       for (i = prec - 1; i >= 0; i--)
1774         {
1775           md_number_to_chars (litP, (valueT) words[i],
1776                               sizeof (LITTLENUM_TYPE));
1777           litP += sizeof (LITTLENUM_TYPE);
1778         }
1779     }
1780   else
1781     for (i = 0; i < prec; i++)
1782       {
1783         md_number_to_chars (litP, (valueT) words[i],
1784                             sizeof (LITTLENUM_TYPE));
1785         litP += sizeof (LITTLENUM_TYPE);
1786       }
1787
1788   return 0;
1789 }
1790 \f
1791 const char * md_shortopts = "";
1792
1793 #define OPTION_JSRI2BSR_ON      (OPTION_MD_BASE + 0)
1794 #define OPTION_JSRI2BSR_OFF     (OPTION_MD_BASE + 1)
1795 #define OPTION_SIFILTER_ON      (OPTION_MD_BASE + 2)
1796 #define OPTION_SIFILTER_OFF     (OPTION_MD_BASE + 3)
1797 #define OPTION_CPU              (OPTION_MD_BASE + 4)
1798 #define OPTION_EB               (OPTION_MD_BASE + 5)
1799 #define OPTION_EL               (OPTION_MD_BASE + 6)
1800
1801 struct option md_longopts[] =
1802 {
1803   { "no-jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_OFF},
1804   { "jsri2bsr",    no_argument, NULL, OPTION_JSRI2BSR_ON},
1805   { "sifilter",    no_argument, NULL, OPTION_SIFILTER_ON},
1806   { "no-sifilter", no_argument, NULL, OPTION_SIFILTER_OFF},
1807   { "cpu",         required_argument, NULL, OPTION_CPU},
1808   { "EB",          no_argument, NULL, OPTION_EB},
1809   { "EL",          no_argument, NULL, OPTION_EL},
1810   { NULL,          no_argument, NULL, 0}
1811 };
1812
1813 size_t md_longopts_size = sizeof (md_longopts);
1814
1815 int
1816 md_parse_option (c, arg)
1817      int c;
1818      char * arg;
1819 {
1820   int i;
1821   char * p;
1822
1823   switch (c)
1824     {
1825     case OPTION_CPU:
1826       if (streq (arg, "210"))
1827         {
1828           cpu = M210;
1829           target_big_endian = 1;
1830         }
1831       else if (streq (arg, "340"))
1832         cpu = M340;
1833       else
1834         as_warn (_("unrecognised cpu type '%s'"), arg);
1835       break;
1836
1837     case OPTION_EB: target_big_endian = 1; break;
1838     case OPTION_EL: target_big_endian = 0; cpu = M340; break;
1839     case OPTION_JSRI2BSR_ON:  do_jsri2bsr = 1;   break;
1840     case OPTION_JSRI2BSR_OFF: do_jsri2bsr = 0;   break;
1841     case OPTION_SIFILTER_ON:  sifilter_mode = 1; break;
1842     case OPTION_SIFILTER_OFF: sifilter_mode = 0; break;
1843     default:                  return 0;
1844     }
1845
1846   return 1;
1847 }
1848
1849 void
1850 md_show_usage (stream)
1851      FILE * stream;
1852 {
1853   fprintf (stream, _("\
1854 MCORE specific options:\n\
1855   -{no-}jsri2bsr          {dis}able jsri to bsr transformation (def: dis)\n\
1856   -{no-}sifilter          {dis}able silicon filter behavior (def: dis)\n\
1857   -cpu=[210|340]          select CPU type\n\
1858   -EB                     assemble for a big endian system (default)\n\
1859   -EL                     assemble for a little endian system\n"));
1860 }
1861 \f
1862 int md_short_jump_size;
1863
1864 void
1865 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1866      char * ptr;
1867      addressT from_Nddr;
1868      addressT to_Nddr;
1869      fragS * frag;
1870      symbolS * to_symbol;
1871 {
1872   as_fatal (_("failed sanity check: short_jump"));
1873 }
1874
1875 void
1876 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1877      char * ptr;
1878      addressT from_Nddr;
1879      addressT to_Nddr;
1880      fragS * frag;
1881      symbolS * to_symbol;
1882 {
1883   as_fatal (_("failed sanity check: long_jump"));
1884 }
1885
1886 /* Called after relaxing, change the frags so they know how big they are.  */
1887 void
1888 md_convert_frag (abfd, sec, fragP)
1889      bfd * abfd;
1890      segT sec;
1891      register fragS * fragP;
1892 {
1893   unsigned char * buffer;
1894   int targ_addr = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
1895
1896   buffer = (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1897
1898   switch (fragP->fr_subtype)
1899     {
1900     case C (COND_JUMP, DISP12):
1901     case C (UNCD_JUMP, DISP12):
1902       {
1903         /* Get the address of the end of the instruction.  */
1904         int next_inst = fragP->fr_fix + fragP->fr_address + 2;
1905         unsigned char t0;
1906         int disp = targ_addr - next_inst;
1907
1908         if (disp & 1)
1909           as_bad (_("odd displacement at %x"), next_inst - 2);
1910
1911         disp >>= 1;
1912
1913         if (! target_big_endian)
1914           {
1915             t0 = buffer[1] & 0xF8;
1916
1917             md_number_to_chars (buffer, disp, 2);
1918
1919             buffer[1] = (buffer[1] & 0x07) | t0;
1920           }
1921         else
1922           {
1923             t0 = buffer[0] & 0xF8;
1924
1925             md_number_to_chars (buffer, disp, 2);
1926
1927             buffer[0] = (buffer[0] & 0x07) | t0;
1928           }
1929
1930         fragP->fr_fix += 2;
1931       }
1932       break;
1933
1934     case C (COND_JUMP, DISP32):
1935     case C (COND_JUMP, UNDEF_WORD_DISP):
1936       {
1937         /* A conditional branch wont fit into 12 bits so:
1938          *      b!cond  1f
1939          *      jmpi    0f
1940          *      .align 2
1941          * 0:   .long disp
1942          * 1:
1943          *
1944          * if the b!cond is 4 byte aligned, the literal which would
1945          * go at x+4 will also be aligned.
1946          */
1947         int first_inst = fragP->fr_fix + fragP->fr_address;
1948         int needpad = (first_inst & 3);
1949
1950         if (! target_big_endian)
1951           buffer[1] ^= 0x08;
1952         else
1953           buffer[0] ^= 0x08;    /* Toggle T/F bit */
1954
1955         buffer[2] = INST_BYTE0 (MCORE_INST_JMPI);       /* Build jmpi */
1956         buffer[3] = INST_BYTE1 (MCORE_INST_JMPI);
1957
1958         if (needpad)
1959           {
1960             if (! target_big_endian)
1961               {
1962                 buffer[0] = 4;  /* branch over jmpi, pad, and ptr */
1963                 buffer[2] = 1;  /* jmpi offset of 1 gets the pointer */
1964               }
1965             else
1966               {
1967                 buffer[1] = 4;  /* branch over jmpi, pad, and ptr */
1968                 buffer[3] = 1;  /* jmpi offset of 1 gets the pointer */
1969               }
1970
1971             buffer[4] = 0;      /* alignment/pad */
1972             buffer[5] = 0;
1973             buffer[6] = 0;      /* space for 32 bit address */
1974             buffer[7] = 0;
1975             buffer[8] = 0;
1976             buffer[9] = 0;
1977
1978             /* Make reloc for the long disp */
1979             fix_new (fragP, fragP->fr_fix + 6, 4,
1980                      fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1981
1982             fragP->fr_fix += C32_LEN;
1983           }
1984         else
1985           {
1986             /* See comment below about this given gas' limitations for
1987                shrinking the fragment. '3' is the amount of code that
1988                we inserted here, but '4' is right for the space we reserved
1989                for this fragment.  */
1990             if (! target_big_endian)
1991               {
1992                 buffer[0] = 3;  /* branch over jmpi, and ptr */
1993                 buffer[2] = 0;  /* jmpi offset of 0 gets the pointer */
1994               }
1995             else
1996               {
1997                 buffer[1] = 3;  /* branch over jmpi, and ptr */
1998                 buffer[3] = 0;  /* jmpi offset of 0 gets the pointer */
1999               }
2000
2001             buffer[4] = 0;      /* space for 32 bit address */
2002             buffer[5] = 0;
2003             buffer[6] = 0;
2004             buffer[7] = 0;
2005
2006             /* Make reloc for the long disp.  */
2007             fix_new (fragP, fragP->fr_fix + 4, 4,
2008                      fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
2009             fragP->fr_fix += C32_LEN;
2010
2011             /* Frag is actually shorter (see the other side of this ifdef)
2012                but gas isn't prepared for that.  We have to re-adjust
2013                the branch displacement so that it goes beyond the
2014                full length of the fragment, not just what we actually
2015                filled in.  */
2016             if (! target_big_endian)
2017               buffer[0] = 4;    /* jmpi, ptr, and the 'tail pad' */
2018             else
2019               buffer[1] = 4;    /* jmpi, ptr, and the 'tail pad' */
2020           }
2021       }
2022       break;
2023
2024     case C (UNCD_JUMP, DISP32):
2025     case C (UNCD_JUMP, UNDEF_WORD_DISP):
2026       {
2027         /* An unconditional branch will not fit in 12 bits, make code which
2028            looks like:
2029                 jmpi    0f
2030                 .align 2
2031              0: .long disp
2032            we need a pad if "first_inst" is 4 byte aligned.
2033            [because the natural literal place is x + 2]  */
2034         int first_inst = fragP->fr_fix + fragP->fr_address;
2035         int needpad = !(first_inst & 3);
2036
2037         buffer[0] = INST_BYTE0 (MCORE_INST_JMPI);       /* Build jmpi */
2038         buffer[1] = INST_BYTE1 (MCORE_INST_JMPI);
2039
2040         if (needpad)
2041           {
2042             if (! target_big_endian)
2043               buffer[0] = 1;    /* jmpi offset of 1 since padded */
2044             else
2045               buffer[1] = 1;    /* jmpi offset of 1 since padded */
2046             buffer[2] = 0;      /* alignment */
2047             buffer[3] = 0;
2048             buffer[4] = 0;      /* space for 32 bit address */
2049             buffer[5] = 0;
2050             buffer[6] = 0;
2051             buffer[7] = 0;
2052
2053             /* Make reloc for the long disp.  */
2054             fix_new (fragP, fragP->fr_fix + 4, 4,
2055                      fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
2056
2057             fragP->fr_fix += U32_LEN;
2058           }
2059         else
2060           {
2061             if (! target_big_endian)
2062               buffer[0] = 0;    /* jmpi offset of 0 if no pad */
2063             else
2064               buffer[1] = 0;    /* jmpi offset of 0 if no pad */
2065             buffer[2] = 0;      /* space for 32 bit address */
2066             buffer[3] = 0;
2067             buffer[4] = 0;
2068             buffer[5] = 0;
2069
2070             /* Make reloc for the long disp.  */
2071             fix_new (fragP, fragP->fr_fix + 2, 4,
2072                      fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
2073             fragP->fr_fix += U32_LEN;
2074           }
2075       }
2076       break;
2077
2078     default:
2079       abort ();
2080     }
2081 }
2082
2083 /* Applies the desired value to the specified location.
2084    Also sets up addends for 'rela' type relocations.  */
2085
2086 void
2087 md_apply_fix3 (fixP, valP, segment)
2088      fixS *   fixP;
2089      valueT * valP;
2090      segT     segment;
2091 {
2092   char *       buf  = fixP->fx_where + fixP->fx_frag->fr_literal;
2093   char *       file = fixP->fx_file ? fixP->fx_file : _("unknown");
2094   const char * symname;
2095   /* Note: use offsetT because it is signed, valueT is unsigned.  */
2096   offsetT      val  = *valP;
2097
2098   symname = fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : _("<unknown>");
2099   /* Save this for the addend in the relocation record.  */
2100   fixP->fx_addnumber = val;
2101
2102   if (fixP->fx_addsy != NULL)
2103     {
2104 #ifdef OBJ_ELF
2105       /* For ELF we can just return and let the reloc that will be generated
2106          take care of everything.  For COFF we still have to insert 'val'
2107          into the insn since the addend field will be ignored.  */
2108       return;
2109 #endif
2110     }
2111   else
2112     fixP->fx_done = 1;
2113
2114   switch (fixP->fx_r_type)
2115     {
2116     case BFD_RELOC_MCORE_PCREL_IMM11BY2:     /* second byte of 2 byte opcode */
2117       if ((val & 1) != 0)
2118         as_bad_where (file, fixP->fx_line,
2119                       _("odd distance branch (0x%x bytes)"), val);
2120       val /= 2;
2121       if (((val & ~0x3ff) != 0) && ((val | 0x3ff) != -1))
2122         as_bad_where (file, fixP->fx_line,
2123                       _("pcrel for branch to %s too far (0x%x)"),
2124                       symname, val);
2125       if (target_big_endian)
2126         {
2127           buf[0] |= ((val >> 8) & 0x7);
2128           buf[1] |= (val & 0xff);
2129         }
2130       else
2131         {
2132           buf[1] |= ((val >> 8) & 0x7);
2133           buf[0] |= (val & 0xff);
2134         }
2135       break;
2136
2137     case BFD_RELOC_MCORE_PCREL_IMM8BY4: /* lower 8 bits of 2 byte opcode */
2138       val += 3;
2139       val /= 4;
2140       if (val & ~0xff)
2141         as_bad_where (file, fixP->fx_line,
2142                       _("pcrel for lrw/jmpi/jsri to %s too far (0x%x)"),
2143                       symname, val);
2144       else if (! target_big_endian)
2145         buf[0] |= (val & 0xff);
2146       else
2147         buf[1] |= (val & 0xff);
2148       break;
2149
2150     case BFD_RELOC_MCORE_PCREL_IMM4BY2: /* loopt instruction */
2151       if ((val < -32) || (val > -2))
2152         as_bad_where (file, fixP->fx_line,
2153                       _("pcrel for loopt too far (0x%x)"), val);
2154       val /= 2;
2155       if (! target_big_endian)
2156         buf[0] |= (val & 0xf);
2157       else
2158         buf[1] |= (val & 0xf);
2159       break;
2160
2161     case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2162       /* Conditional linker map jsri to bsr.  */
2163       /* If its a local target and close enough, fix it.
2164          NB: >= -2k for backwards bsr; < 2k for forwards...  */
2165       if (fixP->fx_addsy == 0 && val >= -2048  && val < 2048)
2166         {
2167           long nval = (val / 2) & 0x7ff;
2168           nval |= MCORE_INST_BSR;
2169
2170           /* REPLACE the instruction, don't just modify it.  */
2171           buf[0] = INST_BYTE0 (nval);
2172           buf[1] = INST_BYTE1 (nval);
2173         }
2174       else
2175         fixP->fx_done = 0;
2176       break;
2177
2178     case BFD_RELOC_MCORE_PCREL_32:
2179     case BFD_RELOC_VTABLE_INHERIT:
2180     case BFD_RELOC_VTABLE_ENTRY:
2181       fixP->fx_done = 0;
2182       break;
2183
2184     default:
2185       if (fixP->fx_addsy != NULL)
2186         {
2187           /* If the fix is an absolute reloc based on a symbol's
2188              address, then it cannot be resolved until the final link.  */
2189           fixP->fx_done = 0;
2190         }
2191 #ifdef OBJ_ELF
2192       else
2193 #endif
2194         {
2195           if (fixP->fx_size == 4)
2196             ;
2197           else if (fixP->fx_size == 2 && val >= -32768 && val <= 32767)
2198             ;
2199           else if (fixP->fx_size == 1 && val >= -256 && val <= 255)
2200             ;
2201           else
2202             abort ();
2203           md_number_to_chars (buf, val, fixP->fx_size);
2204         }
2205       break;
2206     }
2207 }
2208
2209 void
2210 md_operand (expressionP)
2211      expressionS * expressionP;
2212 {
2213   /* Ignore leading hash symbol, if poresent.  */
2214   if (* input_line_pointer == '#')
2215     {
2216       input_line_pointer ++;
2217       expression (expressionP);
2218     }
2219 }
2220
2221 int md_long_jump_size;
2222
2223 /* Called just before address relaxation, return the length
2224    by which a fragment must grow to reach it's destination.  */
2225 int
2226 md_estimate_size_before_relax (fragP, segment_type)
2227      register fragS * fragP;
2228      register segT segment_type;
2229 {
2230   switch (fragP->fr_subtype)
2231     {
2232     default:
2233       abort ();
2234
2235     case C (UNCD_JUMP, UNDEF_DISP):
2236       /* Used to be a branch to somewhere which was unknown.  */
2237       if (!fragP->fr_symbol)
2238         {
2239           fragP->fr_subtype = C (UNCD_JUMP, DISP12);
2240         }
2241       else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2242         {
2243           fragP->fr_subtype = C (UNCD_JUMP, DISP12);
2244         }
2245       else
2246         {
2247           fragP->fr_subtype = C (UNCD_JUMP, UNDEF_WORD_DISP);
2248         }
2249       break;
2250
2251     case C (COND_JUMP, UNDEF_DISP):
2252       /* Used to be a branch to somewhere which was unknown.  */
2253       if (fragP->fr_symbol
2254           && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2255         {
2256           /* Got a symbol and it's defined in this segment, become byte
2257              sized - maybe it will fix up */
2258           fragP->fr_subtype = C (COND_JUMP, DISP12);
2259         }
2260       else if (fragP->fr_symbol)
2261         {
2262           /* Its got a segment, but its not ours, so it will always be long.  */
2263           fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
2264         }
2265       else
2266         {
2267           /* We know the abs value.  */
2268           fragP->fr_subtype = C (COND_JUMP, DISP12);
2269         }
2270       break;
2271
2272     case C (UNCD_JUMP, DISP12):
2273     case C (UNCD_JUMP, DISP32):
2274     case C (UNCD_JUMP, UNDEF_WORD_DISP):
2275     case C (COND_JUMP, DISP12):
2276     case C (COND_JUMP, DISP32):
2277     case C (COND_JUMP, UNDEF_WORD_DISP):
2278       /* When relaxing a section for the second time, we don't need to
2279          do anything besides return the current size.  */
2280       break;
2281     }
2282
2283   return md_relax_table[fragP->fr_subtype].rlx_length;
2284 }
2285
2286 /* Put number into target byte order.  */
2287 void
2288 md_number_to_chars (ptr, use, nbytes)
2289      char * ptr;
2290      valueT use;
2291      int nbytes;
2292 {
2293   if (! target_big_endian)
2294     switch (nbytes)
2295       {
2296       case 4: ptr[3] = (use >> 24) & 0xff; /* fall through */
2297       case 3: ptr[2] = (use >> 16) & 0xff; /* fall through */
2298       case 2: ptr[1] = (use >>  8) & 0xff; /* fall through */
2299       case 1: ptr[0] = (use >>  0) & 0xff;    break;
2300       default: abort ();
2301       }
2302   else
2303     switch (nbytes)
2304       {
2305       case 4: *ptr++ = (use >> 24) & 0xff; /* fall through */
2306       case 3: *ptr++ = (use >> 16) & 0xff; /* fall through */
2307       case 2: *ptr++ = (use >>  8) & 0xff; /* fall through */
2308       case 1: *ptr++ = (use >>  0) & 0xff;    break;
2309       default: abort ();
2310       }
2311 }
2312
2313 /* Round up a section size to the appropriate boundary.  */
2314 valueT
2315 md_section_align (segment, size)
2316      segT segment;
2317      valueT size;
2318 {
2319   return size;                  /* Byte alignment is fine */
2320 }
2321
2322 /* The location from which a PC relative jump should be calculated,
2323    given a PC relative reloc.  */
2324 long
2325 md_pcrel_from_section (fixp, sec)
2326      fixS * fixp;
2327      segT sec;
2328 {
2329 #ifdef OBJ_ELF
2330   /* If the symbol is undefined or defined in another section
2331      we leave the add number alone for the linker to fix it later.
2332      Only account for the PC pre-bump (which is 2 bytes on the MCore).  */
2333   if (fixp->fx_addsy != (symbolS *) NULL
2334       && (! S_IS_DEFINED (fixp->fx_addsy)
2335           || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
2336
2337   {
2338     assert (fixp->fx_size == 2);        /* must be an insn */
2339     return fixp->fx_size;
2340   }
2341 #endif
2342
2343   /* The case where we are going to resolve things...  */
2344   return  fixp->fx_size + fixp->fx_where + fixp->fx_frag->fr_address;
2345 }
2346
2347 #define F(SZ,PCREL)             (((SZ) << 1) + (PCREL))
2348 #define MAP(SZ,PCREL,TYPE)      case F (SZ, PCREL): code = (TYPE); break
2349
2350 arelent *
2351 tc_gen_reloc (section, fixp)
2352      asection * section;
2353      fixS * fixp;
2354 {
2355   arelent * rel;
2356   bfd_reloc_code_real_type code;
2357   int handled = 0;
2358
2359   switch (fixp->fx_r_type)
2360     {
2361       /* These confuse the size/pcrel macro approach.  */
2362     case BFD_RELOC_VTABLE_INHERIT:
2363     case BFD_RELOC_VTABLE_ENTRY:
2364     case BFD_RELOC_MCORE_PCREL_IMM4BY2:
2365     case BFD_RELOC_MCORE_PCREL_IMM8BY4:
2366     case BFD_RELOC_MCORE_PCREL_IMM11BY2:
2367     case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2368     case BFD_RELOC_RVA:
2369       code = fixp->fx_r_type;
2370       break;
2371
2372     default:
2373       switch (F (fixp->fx_size, fixp->fx_pcrel))
2374         {
2375           MAP (1, 0, BFD_RELOC_8);
2376           MAP (2, 0, BFD_RELOC_16);
2377           MAP (4, 0, BFD_RELOC_32);
2378           MAP (1, 1, BFD_RELOC_8_PCREL);
2379           MAP (2, 1, BFD_RELOC_16_PCREL);
2380           MAP (4, 1, BFD_RELOC_32_PCREL);
2381         default:
2382           code = fixp->fx_r_type;
2383           as_bad (_("Can not do %d byte %srelocation"),
2384                   fixp->fx_size,
2385                   fixp->fx_pcrel ? _("pc-relative") : "");
2386         }
2387       break;
2388   }
2389
2390   rel = (arelent *) xmalloc (sizeof (arelent));
2391   rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2392   *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2393   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2394   /* Always pass the addend along!  */
2395   rel->addend = fixp->fx_addnumber;
2396
2397   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
2398
2399   if (rel->howto == NULL)
2400     {
2401       as_bad_where (fixp->fx_file, fixp->fx_line,
2402                     _("Cannot represent relocation type %s"),
2403                     bfd_get_reloc_code_name (code));
2404
2405       /* Set howto to a garbage value so that we can keep going.  */
2406       rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2407       assert (rel->howto != NULL);
2408     }
2409
2410   return rel;
2411 }
2412
2413 #ifdef OBJ_ELF
2414 /* See whether we need to force a relocation into the output file.
2415    This is used to force out switch and PC relative relocations when
2416    relaxing.  */
2417 int
2418 mcore_force_relocation (fix)
2419      fixS * fix;
2420 {
2421   if (   fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2422       || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2423       || fix->fx_r_type == BFD_RELOC_RVA)
2424     return 1;
2425
2426   return S_FORCE_RELOC (fix->fx_addsy);
2427 }
2428
2429 /* Return true if the fix can be handled by GAS, false if it must
2430    be passed through to the linker.  */
2431 boolean
2432 mcore_fix_adjustable (fixP)
2433    fixS * fixP;
2434 {
2435   /* We need the symbol name for the VTABLE entries.  */
2436   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2437       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2438     return 0;
2439
2440   return 1;
2441 }
2442 #endif /* OBJ_ELF */