* elf32-ppc.c (LWZU_0_X_11): Delete.
[binutils.git] / gas / config / tc-mcore.c
blobdfa7943fd38a153cc038a6ddbc827f2b2fbd92ea
1 /* tc-mcore.c -- Assemble code for M*Core
2 Copyright 1999, 2000, 2001, 2002, 2003, 2005
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include <stdio.h>
23 #include "as.h"
24 #include "bfd.h"
25 #include "subsegs.h"
26 #define DEFINE_TABLE
27 #include "../opcodes/mcore-opc.h"
28 #include "safe-ctype.h"
29 #include <string.h>
31 #ifdef OBJ_ELF
32 #include "elf/mcore.h"
33 #endif
35 #ifndef streq
36 #define streq(a,b) (strcmp (a, b) == 0)
37 #endif
39 /* Forward declarations for dumb compilers. */
41 /* Several places in this file insert raw instructions into the
42 object. They should use MCORE_INST_XXX macros to get the opcodes
43 and then use these two macros to crack the MCORE_INST value into
44 the appropriate byte values. */
45 #define INST_BYTE0(x) (target_big_endian ? (((x) >> 8) & 0xFF) : ((x) & 0xFF))
46 #define INST_BYTE1(x) (target_big_endian ? ((x) & 0xFF) : (((x) >> 8) & 0xFF))
48 const char comment_chars[] = "#/";
49 const char line_separator_chars[] = ";";
50 const char line_comment_chars[] = "#/";
52 const int md_reloc_size = 8;
54 static int do_jsri2bsr = 0; /* Change here from 1 by Cruess 19 August 97. */
55 static int sifilter_mode = 0;
57 const char EXP_CHARS[] = "eE";
59 /* Chars that mean this number is a floating point constant
60 As in 0f12.456
61 or 0d1.2345e12 */
62 const char FLT_CHARS[] = "rRsSfFdDxXpP";
64 #define C(what,length) (((what) << 2) + (length))
65 #define GET_WHAT(x) ((x >> 2))
67 /* These are the two types of relaxable instruction. */
68 #define COND_JUMP 1
69 #define UNCD_JUMP 2
71 #define UNDEF_DISP 0
72 #define DISP12 1
73 #define DISP32 2
74 #define UNDEF_WORD_DISP 3
76 #define C12_LEN 2
77 #define C32_LEN 10 /* Allow for align. */
78 #define U12_LEN 2
79 #define U32_LEN 8 /* Allow for align. */
81 typedef enum
83 M210,
84 M340
86 cpu_type;
88 cpu_type cpu = M340;
90 /* Initialize the relax table. */
91 const relax_typeS md_relax_table[] =
93 { 0, 0, 0, 0 },
94 { 0, 0, 0, 0 },
95 { 0, 0, 0, 0 },
96 { 0, 0, 0, 0 },
98 /* COND_JUMP */
99 { 0, 0, 0, 0 }, /* UNDEF_DISP */
100 { 2048, -2046, C12_LEN, C(COND_JUMP, DISP32) }, /* DISP12 */
101 { 0, 0, C32_LEN, 0 }, /* DISP32 */
102 { 0, 0, C32_LEN, 0 }, /* UNDEF_WORD_DISP */
104 /* UNCD_JUMP */
105 { 0, 0, 0, 0 }, /* UNDEF_DISP */
106 { 2048, -2046, U12_LEN, C(UNCD_JUMP, DISP32) }, /* DISP12 */
107 { 0, 0, U32_LEN, 0 }, /* DISP32 */
108 { 0, 0, U32_LEN, 0 } /* UNDEF_WORD_DISP */
112 /* Literal pool data structures. */
113 struct literal
115 unsigned short refcnt;
116 unsigned char ispcrel;
117 unsigned char unused;
118 expressionS e;
121 #define MAX_POOL_SIZE (1024/4)
122 static struct literal litpool [MAX_POOL_SIZE];
123 static unsigned poolsize;
124 static unsigned poolnumber;
125 static unsigned long poolspan;
127 /* SPANPANIC: the point at which we get too scared and force a dump
128 of the literal pool, and perhaps put a branch in place.
129 Calculated as:
130 1024 span of lrw/jmpi/jsri insn (actually span+1)
131 -2 possible alignment at the insn.
132 -2 possible alignment to get the table aligned.
133 -2 an inserted branch around the table.
134 == 1018
135 at 1018, we might be in trouble.
136 -- so we have to be smaller than 1018 and since we deal with 2-byte
137 instructions, the next good choice is 1016.
138 -- Note we have a test case that fails when we've got 1018 here. */
139 #define SPANPANIC (1016) /* 1024 - 1 entry - 2 byte rounding. */
140 #define SPANCLOSE (900)
141 #define SPANEXIT (600)
142 static symbolS * poolsym; /* Label for current pool. */
143 static char poolname[8];
144 static struct hash_control * opcode_hash_control; /* Opcode mnemonics. */
146 #define POOL_END_LABEL ".LE"
147 #define POOL_START_LABEL ".LS"
149 static void
150 make_name (char * s, char * p, int n)
152 static const char hex[] = "0123456789ABCDEF";
154 s[0] = p[0];
155 s[1] = p[1];
156 s[2] = p[2];
157 s[3] = hex[(n >> 12) & 0xF];
158 s[4] = hex[(n >> 8) & 0xF];
159 s[5] = hex[(n >> 4) & 0xF];
160 s[6] = hex[(n) & 0xF];
161 s[7] = 0;
164 static void
165 dump_literals (int isforce)
167 unsigned int i;
168 struct literal * p;
169 symbolS * brarsym = NULL;
171 if (poolsize == 0)
172 return;
174 /* Must we branch around the literal table? */
175 if (isforce)
177 char * output;
178 char brarname[8];
180 make_name (brarname, POOL_END_LABEL, poolnumber);
182 brarsym = symbol_make (brarname);
184 symbol_table_insert (brarsym);
186 output = frag_var (rs_machine_dependent,
187 md_relax_table[C (UNCD_JUMP, DISP32)].rlx_length,
188 md_relax_table[C (UNCD_JUMP, DISP12)].rlx_length,
189 C (UNCD_JUMP, 0), brarsym, 0, 0);
190 output[0] = INST_BYTE0 (MCORE_INST_BR); /* br .+xxx */
191 output[1] = INST_BYTE1 (MCORE_INST_BR);
194 /* Make sure that the section is sufficiently aligned and that
195 the literal table is aligned within it. */
196 record_alignment (now_seg, 2);
197 frag_align (2, 0, 0);
199 colon (S_GET_NAME (poolsym));
201 for (i = 0, p = litpool; i < poolsize; i++, p++)
202 emit_expr (& p->e, 4);
204 if (brarsym != NULL)
205 colon (S_GET_NAME (brarsym));
207 poolsize = 0;
210 static void
211 mcore_s_literals (int ignore ATTRIBUTE_UNUSED)
213 dump_literals (0);
214 demand_empty_rest_of_line ();
217 /* Perform FUNC (ARG), and track number of bytes added to frag. */
219 static void
220 mcore_pool_count (void (*func) (int), int arg)
222 const fragS *curr_frag = frag_now;
223 offsetT added = -frag_now_fix_octets ();
225 (*func) (arg);
227 while (curr_frag != frag_now)
229 added += curr_frag->fr_fix;
230 curr_frag = curr_frag->fr_next;
233 added += frag_now_fix_octets ();
234 poolspan += added;
237 static void
238 check_literals (int kind, int offset)
240 poolspan += offset;
242 /* SPANCLOSE and SPANEXIT are smaller numbers than SPANPANIC.
243 SPANPANIC means that we must dump now.
244 kind == 0 is any old instruction.
245 kind > 0 means we just had a control transfer instruction.
246 kind == 1 means within a function
247 kind == 2 means we just left a function
249 The dump_literals (1) call inserts a branch around the table, so
250 we first look to see if its a situation where we won't have to
251 insert a branch (e.g., the previous instruction was an unconditional
252 branch).
254 SPANPANIC is the point where we must dump a single-entry pool.
255 it accounts for alignments and an inserted branch.
256 the 'poolsize*2' accounts for the scenario where we do:
257 lrw r1,lit1; lrw r2,lit2; lrw r3,lit3
258 Note that the 'lit2' reference is 2 bytes further along
259 but the literal it references will be 4 bytes further along,
260 so we must consider the poolsize into this equation.
261 This is slightly over-cautious, but guarantees that we won't
262 panic because a relocation is too distant. */
264 if (poolspan > SPANCLOSE && kind > 0)
265 dump_literals (0);
266 else if (poolspan > SPANEXIT && kind > 1)
267 dump_literals (0);
268 else if (poolspan >= (SPANPANIC - poolsize * 2))
269 dump_literals (1);
272 static void
273 mcore_cons (int nbytes)
275 if (now_seg == text_section)
276 mcore_pool_count (cons, nbytes);
277 else
278 cons (nbytes);
280 /* In theory we ought to call check_literals (2,0) here in case
281 we need to dump the literal table. We cannot do this however,
282 as the directives that we are intercepting may be being used
283 to build a switch table, and we must not interfere with its
284 contents. Instead we cross our fingers and pray... */
287 static void
288 mcore_float_cons (int float_type)
290 if (now_seg == text_section)
291 mcore_pool_count (float_cons, float_type);
292 else
293 float_cons (float_type);
295 /* See the comment in mcore_cons () about calling check_literals.
296 It is unlikely that a switch table will be constructed using
297 floating point values, but it is still likely that an indexed
298 table of floating point constants is being created by these
299 directives, so again we must not interfere with their placement. */
302 static void
303 mcore_stringer (int append_zero)
305 if (now_seg == text_section)
306 mcore_pool_count (stringer, append_zero);
307 else
308 stringer (append_zero);
310 /* We call check_literals here in case a large number of strings are
311 being placed into the text section with a sequence of stringer
312 directives. In theory we could be upsetting something if these
313 strings are actually in an indexed table instead of referenced by
314 individual labels. Let us hope that that never happens. */
315 check_literals (2, 0);
318 static void
319 mcore_fill (int unused)
321 if (now_seg == text_section)
322 mcore_pool_count (s_fill, unused);
323 else
324 s_fill (unused);
326 check_literals (2, 0);
329 /* Handle the section changing pseudo-ops. These call through to the
330 normal implementations, but they dump the literal pool first. */
332 static void
333 mcore_s_text (int ignore)
335 dump_literals (0);
337 #ifdef OBJ_ELF
338 obj_elf_text (ignore);
339 #else
340 s_text (ignore);
341 #endif
344 static void
345 mcore_s_data (int ignore)
347 dump_literals (0);
349 #ifdef OBJ_ELF
350 obj_elf_data (ignore);
351 #else
352 s_data (ignore);
353 #endif
356 static void
357 mcore_s_section (int ignore)
359 /* Scan forwards to find the name of the section. If the section
360 being switched to is ".line" then this is a DWARF1 debug section
361 which is arbitrarily placed inside generated code. In this case
362 do not dump the literal pool because it is a) inefficient and
363 b) would require the generation of extra code to jump around the
364 pool. */
365 char * ilp = input_line_pointer;
367 while (*ilp != 0 && ISSPACE (*ilp))
368 ++ ilp;
370 if (strncmp (ilp, ".line", 5) == 0
371 && (ISSPACE (ilp[5]) || *ilp == '\n' || *ilp == '\r'))
373 else
374 dump_literals (0);
376 #ifdef OBJ_ELF
377 obj_elf_section (ignore);
378 #endif
379 #ifdef OBJ_COFF
380 obj_coff_section (ignore);
381 #endif
384 static void
385 mcore_s_bss (int needs_align)
387 dump_literals (0);
389 s_lcomm_bytes (needs_align);
392 #ifdef OBJ_ELF
393 static void
394 mcore_s_comm (int needs_align)
396 dump_literals (0);
398 obj_elf_common (needs_align);
400 #endif
402 /* This table describes all the machine specific pseudo-ops the assembler
403 has to support. The fields are:
404 Pseudo-op name without dot
405 Function to call to execute this pseudo-op
406 Integer arg to pass to the function. */
407 const pseudo_typeS md_pseudo_table[] =
409 { "export", s_globl, 0 },
410 { "import", s_ignore, 0 },
411 { "literals", mcore_s_literals, 0 },
412 { "page", listing_eject, 0 },
414 /* The following are to intercept the placement of data into the text
415 section (eg addresses for a switch table), so that the space they
416 occupy can be taken into account when deciding whether or not to
417 dump the current literal pool.
418 XXX - currently we do not cope with the .space and .dcb.d directives. */
419 { "ascii", mcore_stringer, 0 },
420 { "asciz", mcore_stringer, 1 },
421 { "byte", mcore_cons, 1 },
422 { "dc", mcore_cons, 2 },
423 { "dc.b", mcore_cons, 1 },
424 { "dc.d", mcore_float_cons, 'd'},
425 { "dc.l", mcore_cons, 4 },
426 { "dc.s", mcore_float_cons, 'f'},
427 { "dc.w", mcore_cons, 2 },
428 { "dc.x", mcore_float_cons, 'x'},
429 { "double", mcore_float_cons, 'd'},
430 { "float", mcore_float_cons, 'f'},
431 { "hword", mcore_cons, 2 },
432 { "int", mcore_cons, 4 },
433 { "long", mcore_cons, 4 },
434 { "octa", mcore_cons, 16 },
435 { "quad", mcore_cons, 8 },
436 { "short", mcore_cons, 2 },
437 { "single", mcore_float_cons, 'f'},
438 { "string", mcore_stringer, 1 },
439 { "word", mcore_cons, 2 },
440 { "fill", mcore_fill, 0 },
442 /* Allow for the effect of section changes. */
443 { "text", mcore_s_text, 0 },
444 { "data", mcore_s_data, 0 },
445 { "bss", mcore_s_bss, 1 },
446 #ifdef OBJ_ELF
447 { "comm", mcore_s_comm, 0 },
448 #endif
449 { "section", mcore_s_section, 0 },
450 { "section.s", mcore_s_section, 0 },
451 { "sect", mcore_s_section, 0 },
452 { "sect.s", mcore_s_section, 0 },
454 { 0, 0, 0 }
457 /* This function is called once, at assembler startup time. This should
458 set up all the tables, etc that the MD part of the assembler needs. */
460 void
461 md_begin (void)
463 const mcore_opcode_info * opcode;
464 char * prev_name = "";
466 opcode_hash_control = hash_new ();
468 /* Insert unique names into hash table. */
469 for (opcode = mcore_table; opcode->name; opcode ++)
471 if (! streq (prev_name, opcode->name))
473 prev_name = opcode->name;
474 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
479 /* Get a log2(val). */
481 static int
482 mylog2 (unsigned int val)
484 int log = -1;
486 while (val != 0)
488 log ++;
489 val >>= 1;
492 return log;
495 /* Try to parse a reg name. */
497 static char *
498 parse_reg (char * s, unsigned * reg)
500 /* Strip leading whitespace. */
501 while (ISSPACE (* s))
502 ++ s;
504 if (TOLOWER (s[0]) == 'r')
506 if (s[1] == '1' && s[2] >= '0' && s[2] <= '5')
508 *reg = 10 + s[2] - '0';
509 return s + 3;
512 if (s[1] >= '0' && s[1] <= '9')
514 *reg = s[1] - '0';
515 return s + 2;
518 else if ( TOLOWER (s[0]) == 's'
519 && TOLOWER (s[1]) == 'p'
520 && ! ISALNUM (s[2]))
522 * reg = 0;
523 return s + 2;
526 as_bad (_("register expected, but saw '%.6s'"), s);
527 return s;
530 static struct Cregs
532 char * name;
533 unsigned int crnum;
535 cregs[] =
537 { "psr", 0},
538 { "vbr", 1},
539 { "epsr", 2},
540 { "fpsr", 3},
541 { "epc", 4},
542 { "fpc", 5},
543 { "ss0", 6},
544 { "ss1", 7},
545 { "ss2", 8},
546 { "ss3", 9},
547 { "ss4", 10},
548 { "gcr", 11},
549 { "gsr", 12},
550 { "", 0}
553 static char *
554 parse_creg (char * s, unsigned * reg)
556 int i;
558 /* Strip leading whitespace. */
559 while (ISSPACE (* s))
560 ++s;
562 if ((TOLOWER (s[0]) == 'c' && TOLOWER (s[1]) == 'r'))
564 if (s[2] == '3' && s[3] >= '0' && s[3] <= '1')
566 *reg = 30 + s[3] - '0';
567 return s + 4;
570 if (s[2] == '2' && s[3] >= '0' && s[3] <= '9')
572 *reg = 20 + s[3] - '0';
573 return s + 4;
576 if (s[2] == '1' && s[3] >= '0' && s[3] <= '9')
578 *reg = 10 + s[3] - '0';
579 return s + 4;
582 if (s[2] >= '0' && s[2] <= '9')
584 *reg = s[2] - '0';
585 return s + 3;
589 /* Look at alternate creg names before giving error. */
590 for (i = 0; cregs[i].name[0] != '\0'; i++)
592 char buf [10];
593 int length;
594 int j;
596 length = strlen (cregs[i].name);
598 for (j = 0; j < length; j++)
599 buf[j] = TOLOWER (s[j]);
601 if (strncmp (cregs[i].name, buf, length) == 0)
603 *reg = cregs[i].crnum;
604 return s + length;
608 as_bad (_("control register expected, but saw '%.6s'"), s);
610 return s;
613 static char *
614 parse_psrmod (char * s, unsigned * reg)
616 int i;
617 char buf[10];
618 static struct psrmods
620 char * name;
621 unsigned int value;
623 psrmods[] =
625 { "ie", 1 },
626 { "fe", 2 },
627 { "ee", 4 },
628 { "af", 8 } /* Really 0 and non-combinable. */
631 for (i = 0; i < 2; i++)
632 buf[i] = TOLOWER (s[i]);
634 for (i = sizeof (psrmods) / sizeof (psrmods[0]); i--;)
636 if (! strncmp (psrmods[i].name, buf, 2))
638 * reg = psrmods[i].value;
640 return s + 2;
644 as_bad (_("bad/missing psr specifier"));
646 * reg = 0;
648 return s;
651 static char *
652 parse_exp (char * s, expressionS * e)
654 char * save;
655 char * new;
657 /* Skip whitespace. */
658 while (ISSPACE (* s))
659 ++ s;
661 save = input_line_pointer;
662 input_line_pointer = s;
664 expression (e);
666 if (e->X_op == O_absent)
667 as_bad (_("missing operand"));
669 new = input_line_pointer;
670 input_line_pointer = save;
672 return new;
675 static int
676 enter_literal (expressionS * e, int ispcrel)
678 unsigned int i;
679 struct literal * p;
681 if (poolsize >= MAX_POOL_SIZE - 2)
682 /* The literal pool is as full as we can handle. We have
683 to be 2 entries shy of the 1024/4=256 entries because we
684 have to allow for the branch (2 bytes) and the alignment
685 (2 bytes before the first insn referencing the pool and
686 2 bytes before the pool itself) == 6 bytes, rounds up
687 to 2 entries. */
688 dump_literals (1);
690 if (poolsize == 0)
692 /* Create new literal pool. */
693 if (++ poolnumber > 0xFFFF)
694 as_fatal (_("more than 65K literal pools"));
696 make_name (poolname, POOL_START_LABEL, poolnumber);
697 poolsym = symbol_make (poolname);
698 symbol_table_insert (poolsym);
699 poolspan = 0;
702 /* Search pool for value so we don't have duplicates. */
703 for (p = litpool, i = 0; i < poolsize; i++, p++)
705 if (e->X_op == p->e.X_op
706 && e->X_add_symbol == p->e.X_add_symbol
707 && e->X_add_number == p->e.X_add_number
708 && ispcrel == p->ispcrel)
710 p->refcnt ++;
711 return i;
715 p->refcnt = 1;
716 p->ispcrel = ispcrel;
717 p->e = * e;
719 poolsize ++;
721 return i;
724 /* Parse a literal specification. -- either new or old syntax.
725 old syntax: the user supplies the label and places the literal.
726 new syntax: we put it into the literal pool. */
728 static char *
729 parse_rt (char * s,
730 char ** outputp,
731 int ispcrel,
732 expressionS * ep)
734 expressionS e;
735 int n;
737 if (ep)
738 /* Indicate nothing there. */
739 ep->X_op = O_absent;
741 if (*s == '[')
743 s = parse_exp (s + 1, & e);
745 if (*s == ']')
746 s++;
747 else
748 as_bad (_("missing ']'"));
750 else
752 s = parse_exp (s, & e);
754 n = enter_literal (& e, ispcrel);
756 if (ep)
757 *ep = e;
759 /* Create a reference to pool entry. */
760 e.X_op = O_symbol;
761 e.X_add_symbol = poolsym;
762 e.X_add_number = n << 2;
765 * outputp = frag_more (2);
767 fix_new_exp (frag_now, (*outputp) - frag_now->fr_literal, 2, & e, 1,
768 BFD_RELOC_MCORE_PCREL_IMM8BY4);
770 return s;
773 static char *
774 parse_imm (char * s,
775 unsigned * val,
776 unsigned min,
777 unsigned max)
779 char * new;
780 expressionS e;
782 new = parse_exp (s, & e);
784 if (e.X_op == O_absent)
785 ; /* An error message has already been emitted. */
786 else if (e.X_op != O_constant)
787 as_bad (_("operand must be a constant"));
788 else if ((addressT) e.X_add_number < min || (addressT) e.X_add_number > max)
789 as_bad (_("operand must be absolute in range %u..%u, not %ld"),
790 min, max, (long) e.X_add_number);
792 * val = e.X_add_number;
794 return new;
797 static char *
798 parse_mem (char * s,
799 unsigned * reg,
800 unsigned * off,
801 unsigned siz)
803 * off = 0;
805 while (ISSPACE (* s))
806 ++ s;
808 if (* s == '(')
810 s = parse_reg (s + 1, reg);
812 while (ISSPACE (* s))
813 ++ s;
815 if (* s == ',')
817 s = parse_imm (s + 1, off, 0, 63);
819 if (siz > 1)
821 if (siz > 2)
823 if (* off & 0x3)
824 as_bad (_("operand must be a multiple of 4"));
826 * off >>= 2;
828 else
830 if (* off & 0x1)
831 as_bad (_("operand must be a multiple of 2"));
833 * off >>= 1;
838 while (ISSPACE (* s))
839 ++ s;
841 if (* s == ')')
842 s ++;
844 else
845 as_bad (_("base register expected"));
847 return s;
850 /* This is the guts of the machine-dependent assembler. STR points to a
851 machine dependent instruction. This function is supposed to emit
852 the frags/bytes it assembles to. */
854 void
855 md_assemble (char * str)
857 char * op_start;
858 char * op_end;
859 mcore_opcode_info * opcode;
860 char * output;
861 int nlen = 0;
862 unsigned short inst;
863 unsigned reg;
864 unsigned off;
865 unsigned isize;
866 expressionS e;
867 char name[20];
869 /* Drop leading whitespace. */
870 while (ISSPACE (* str))
871 str ++;
873 /* Find the op code end. */
874 for (op_start = op_end = str;
875 nlen < 20 && !is_end_of_line [(unsigned char) *op_end] && *op_end != ' ';
876 op_end++)
878 name[nlen] = op_start[nlen];
879 nlen++;
882 name [nlen] = 0;
884 if (nlen == 0)
886 as_bad (_("can't find opcode "));
887 return;
890 opcode = (mcore_opcode_info *) hash_find (opcode_hash_control, name);
891 if (opcode == NULL)
893 as_bad (_("unknown opcode \"%s\""), name);
894 return;
897 inst = opcode->inst;
898 isize = 2;
900 switch (opcode->opclass)
902 case O0:
903 output = frag_more (2);
904 break;
906 case OT:
907 op_end = parse_imm (op_end + 1, & reg, 0, 3);
908 inst |= reg;
909 output = frag_more (2);
910 break;
912 case O1:
913 op_end = parse_reg (op_end + 1, & reg);
914 inst |= reg;
915 output = frag_more (2);
916 break;
918 case JMP:
919 op_end = parse_reg (op_end + 1, & reg);
920 inst |= reg;
921 output = frag_more (2);
922 /* In a sifilter mode, we emit this insn 2 times,
923 fixes problem of an interrupt during a jmp.. */
924 if (sifilter_mode)
926 output[0] = INST_BYTE0 (inst);
927 output[1] = INST_BYTE1 (inst);
928 output = frag_more (2);
930 break;
932 case JSR:
933 op_end = parse_reg (op_end + 1, & reg);
935 if (reg == 15)
936 as_bad (_("invalid register: r15 illegal"));
938 inst |= reg;
939 output = frag_more (2);
941 if (sifilter_mode)
943 /* Replace with: bsr .+2 ; addi r15,6; jmp rx ; jmp rx. */
944 inst = MCORE_INST_BSR; /* With 0 displacement. */
945 output[0] = INST_BYTE0 (inst);
946 output[1] = INST_BYTE1 (inst);
948 output = frag_more (2);
949 inst = MCORE_INST_ADDI;
950 inst |= 15; /* addi r15,6 */
951 inst |= (6 - 1) << 4; /* Over the jmp's. */
952 output[0] = INST_BYTE0 (inst);
953 output[1] = INST_BYTE1 (inst);
955 output = frag_more (2);
956 inst = MCORE_INST_JMP | reg;
957 output[0] = INST_BYTE0 (inst);
958 output[1] = INST_BYTE1 (inst);
960 /* 2nd emitted in fallthrough. */
961 output = frag_more (2);
963 break;
965 case OC:
966 op_end = parse_reg (op_end + 1, & reg);
967 inst |= reg;
969 /* Skip whitespace. */
970 while (ISSPACE (* op_end))
971 ++ op_end;
973 if (*op_end == ',')
975 op_end = parse_creg (op_end + 1, & reg);
976 inst |= reg << 4;
979 output = frag_more (2);
980 break;
982 case MULSH:
983 if (cpu == M210)
985 as_bad (_("M340 specific opcode used when assembling for M210"));
986 break;
988 /* drop through... */
989 case O2:
990 op_end = parse_reg (op_end + 1, & reg);
991 inst |= reg;
993 /* Skip whitespace. */
994 while (ISSPACE (* op_end))
995 ++ op_end;
997 if (* op_end == ',')
999 op_end = parse_reg (op_end + 1, & reg);
1000 inst |= reg << 4;
1002 else
1003 as_bad (_("second operand missing"));
1005 output = frag_more (2);
1006 break;
1008 case X1:
1009 /* Handle both syntax-> xtrb- r1,rx OR xtrb- rx. */
1010 op_end = parse_reg (op_end + 1, & reg);
1012 /* Skip whitespace. */
1013 while (ISSPACE (* op_end))
1014 ++ op_end;
1016 if (* op_end == ',') /* xtrb- r1,rx. */
1018 if (reg != 1)
1019 as_bad (_("destination register must be r1"));
1021 op_end = parse_reg (op_end + 1, & reg);
1024 inst |= reg;
1025 output = frag_more (2);
1026 break;
1028 case O1R1: /* div- rx,r1. */
1029 op_end = parse_reg (op_end + 1, & reg);
1030 inst |= reg;
1032 /* Skip whitespace. */
1033 while (ISSPACE (* op_end))
1034 ++ op_end;
1036 if (* op_end == ',')
1038 op_end = parse_reg (op_end + 1, & reg);
1039 if (reg != 1)
1040 as_bad (_("source register must be r1"));
1042 else
1043 as_bad (_("second operand missing"));
1045 output = frag_more (2);
1046 break;
1048 case OI:
1049 op_end = parse_reg (op_end + 1, & reg);
1050 inst |= reg;
1052 /* Skip whitespace. */
1053 while (ISSPACE (* op_end))
1054 ++ op_end;
1056 if (* op_end == ',')
1058 op_end = parse_imm (op_end + 1, & reg, 1, 32);
1059 inst |= (reg - 1) << 4;
1061 else
1062 as_bad (_("second operand missing"));
1064 output = frag_more (2);
1065 break;
1067 case OB:
1068 op_end = parse_reg (op_end + 1, & reg);
1069 inst |= reg;
1071 /* Skip whitespace. */
1072 while (ISSPACE (* op_end))
1073 ++ op_end;
1075 if (* op_end == ',')
1077 op_end = parse_imm (op_end + 1, & reg, 0, 31);
1078 inst |= reg << 4;
1080 else
1081 as_bad (_("second operand missing"));
1083 output = frag_more (2);
1084 break;
1086 case OB2:
1087 /* Like OB, but arg is 2^n instead of n. */
1088 op_end = parse_reg (op_end + 1, & reg);
1089 inst |= reg;
1091 /* Skip whitespace. */
1092 while (ISSPACE (* op_end))
1093 ++ op_end;
1095 if (* op_end == ',')
1097 op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1098 /* Further restrict the immediate to a power of two. */
1099 if ((reg & (reg - 1)) == 0)
1100 reg = mylog2 (reg);
1101 else
1103 reg = 0;
1104 as_bad (_("immediate is not a power of two"));
1106 inst |= (reg) << 4;
1108 else
1109 as_bad (_("second operand missing"));
1111 output = frag_more (2);
1112 break;
1114 case OBRa: /* Specific for bgeni: imm of 0->6 translate to movi. */
1115 case OBRb:
1116 case OBRc:
1117 op_end = parse_reg (op_end + 1, & reg);
1118 inst |= reg;
1120 /* Skip whitespace. */
1121 while (ISSPACE (* op_end))
1122 ++ op_end;
1124 if (* op_end == ',')
1126 op_end = parse_imm (op_end + 1, & reg, 0, 31);
1127 /* Immediate values of 0 -> 6 translate to movi. */
1128 if (reg <= 6)
1130 inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1131 reg = 0x1 << reg;
1132 as_warn (_("translating bgeni to movi"));
1134 inst &= ~ 0x01f0;
1135 inst |= reg << 4;
1137 else
1138 as_bad (_("second operand missing"));
1140 output = frag_more (2);
1141 break;
1143 case OBR2: /* Like OBR, but arg is 2^n instead of n. */
1144 op_end = parse_reg (op_end + 1, & reg);
1145 inst |= reg;
1147 /* Skip whitespace. */
1148 while (ISSPACE (* op_end))
1149 ++ op_end;
1151 if (* op_end == ',')
1153 op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1155 /* Further restrict the immediate to a power of two. */
1156 if ((reg & (reg - 1)) == 0)
1157 reg = mylog2 (reg);
1158 else
1160 reg = 0;
1161 as_bad (_("immediate is not a power of two"));
1164 /* Immediate values of 0 -> 6 translate to movi. */
1165 if (reg <= 6)
1167 inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1168 reg = 0x1 << reg;
1169 as_warn (_("translating mgeni to movi"));
1172 inst |= reg << 4;
1174 else
1175 as_bad (_("second operand missing"));
1177 output = frag_more (2);
1178 break;
1180 case OMa: /* Specific for bmaski: imm 1->7 translate to movi. */
1181 case OMb:
1182 case OMc:
1183 op_end = parse_reg (op_end + 1, & reg);
1184 inst |= reg;
1186 /* Skip whitespace. */
1187 while (ISSPACE (* op_end))
1188 ++ op_end;
1190 if (* op_end == ',')
1192 op_end = parse_imm (op_end + 1, & reg, 1, 32);
1194 /* Immediate values of 1 -> 7 translate to movi. */
1195 if (reg <= 7)
1197 inst = (inst & 0xF) | MCORE_INST_BMASKI_ALT;
1198 reg = (0x1 << reg) - 1;
1199 inst |= reg << 4;
1201 as_warn (_("translating bmaski to movi"));
1203 else
1205 inst &= ~ 0x01F0;
1206 inst |= (reg & 0x1F) << 4;
1209 else
1210 as_bad (_("second operand missing"));
1212 output = frag_more (2);
1213 break;
1215 case SI:
1216 op_end = parse_reg (op_end + 1, & reg);
1217 inst |= reg;
1219 /* Skip whitespace. */
1220 while (ISSPACE (* op_end))
1221 ++ op_end;
1223 if (* op_end == ',')
1225 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1226 inst |= reg << 4;
1228 else
1229 as_bad (_("second operand missing"));
1231 output = frag_more (2);
1232 break;
1234 case I7:
1235 op_end = parse_reg (op_end + 1, & reg);
1236 inst |= reg;
1238 /* Skip whitespace. */
1239 while (ISSPACE (* op_end))
1240 ++ op_end;
1242 if (* op_end == ',')
1244 op_end = parse_imm (op_end + 1, & reg, 0, 0x7F);
1245 inst |= reg << 4;
1247 else
1248 as_bad (_("second operand missing"));
1250 output = frag_more (2);
1251 break;
1253 case LS:
1254 op_end = parse_reg (op_end + 1, & reg);
1255 inst |= reg << 8;
1257 /* Skip whitespace. */
1258 while (ISSPACE (* op_end))
1259 ++ op_end;
1261 if (* op_end == ',')
1263 int size;
1265 if ((inst & 0x6000) == 0)
1266 size = 4;
1267 else if ((inst & 0x6000) == 0x4000)
1268 size = 2;
1269 else if ((inst & 0x6000) == 0x2000)
1270 size = 1;
1271 else
1272 abort ();
1274 op_end = parse_mem (op_end + 1, & reg, & off, size);
1276 if (off > 16)
1277 as_bad (_("displacement too large (%d)"), off);
1278 else
1279 inst |= (reg) | (off << 4);
1281 else
1282 as_bad (_("second operand missing"));
1284 output = frag_more (2);
1285 break;
1287 case LR:
1288 op_end = parse_reg (op_end + 1, & reg);
1290 if (reg == 0 || reg == 15)
1291 as_bad (_("Invalid register: r0 and r15 illegal"));
1293 inst |= (reg << 8);
1295 /* Skip whitespace. */
1296 while (ISSPACE (* op_end))
1297 ++ op_end;
1299 if (* op_end == ',')
1301 /* parse_rt calls frag_more() for us. */
1302 input_line_pointer = parse_rt (op_end + 1, & output, 0, 0);
1303 op_end = input_line_pointer;
1305 else
1307 as_bad (_("second operand missing"));
1308 output = frag_more (2); /* save its space */
1310 break;
1312 case LJ:
1313 input_line_pointer = parse_rt (op_end + 1, & output, 1, 0);
1314 /* parse_rt() calls frag_more() for us. */
1315 op_end = input_line_pointer;
1316 break;
1318 case RM:
1319 op_end = parse_reg (op_end + 1, & reg);
1321 if (reg == 0 || reg == 15)
1322 as_bad (_("bad starting register: r0 and r15 invalid"));
1324 inst |= reg;
1326 /* Skip whitespace. */
1327 while (ISSPACE (* op_end))
1328 ++ op_end;
1330 if (* op_end == '-')
1332 op_end = parse_reg (op_end + 1, & reg);
1334 if (reg != 15)
1335 as_bad (_("ending register must be r15"));
1337 /* Skip whitespace. */
1338 while (ISSPACE (* op_end))
1339 ++ op_end;
1342 if (* op_end == ',')
1344 op_end ++;
1346 /* Skip whitespace. */
1347 while (ISSPACE (* op_end))
1348 ++ op_end;
1350 if (* op_end == '(')
1352 op_end = parse_reg (op_end + 1, & reg);
1354 if (reg != 0)
1355 as_bad (_("bad base register: must be r0"));
1357 if (* op_end == ')')
1358 op_end ++;
1360 else
1361 as_bad (_("base register expected"));
1363 else
1364 as_bad (_("second operand missing"));
1366 output = frag_more (2);
1367 break;
1369 case RQ:
1370 op_end = parse_reg (op_end + 1, & reg);
1372 if (reg != 4)
1373 as_fatal (_("first register must be r4"));
1375 /* Skip whitespace. */
1376 while (ISSPACE (* op_end))
1377 ++ op_end;
1379 if (* op_end == '-')
1381 op_end = parse_reg (op_end + 1, & reg);
1383 if (reg != 7)
1384 as_fatal (_("last register must be r7"));
1386 /* Skip whitespace. */
1387 while (ISSPACE (* op_end))
1388 ++ op_end;
1390 if (* op_end == ',')
1392 op_end ++;
1394 /* Skip whitespace. */
1395 while (ISSPACE (* op_end))
1396 ++ op_end;
1398 if (* op_end == '(')
1400 op_end = parse_reg (op_end + 1, & reg);
1402 if (reg >= 4 && reg <= 7)
1403 as_fatal ("base register cannot be r4, r5, r6, or r7");
1405 inst |= reg;
1407 /* Skip whitespace. */
1408 while (ISSPACE (* op_end))
1409 ++ op_end;
1411 if (* op_end == ')')
1412 op_end ++;
1414 else
1415 as_bad (_("base register expected"));
1417 else
1418 as_bad (_("second operand missing"));
1420 else
1421 as_bad (_("reg-reg expected"));
1423 output = frag_more (2);
1424 break;
1426 case BR:
1427 input_line_pointer = parse_exp (op_end + 1, & e);
1428 op_end = input_line_pointer;
1430 output = frag_more (2);
1432 fix_new_exp (frag_now, output-frag_now->fr_literal,
1433 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM11BY2);
1434 break;
1436 case BL:
1437 op_end = parse_reg (op_end + 1, & reg);
1438 inst |= reg << 4;
1440 /* Skip whitespace. */
1441 while (ISSPACE (* op_end))
1442 ++ op_end;
1444 if (* op_end == ',')
1446 op_end = parse_exp (op_end + 1, & e);
1447 output = frag_more (2);
1449 fix_new_exp (frag_now, output-frag_now->fr_literal,
1450 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM4BY2);
1452 else
1454 as_bad (_("second operand missing"));
1455 output = frag_more (2);
1457 break;
1459 case JC:
1460 input_line_pointer = parse_exp (op_end + 1, & e);
1461 op_end = input_line_pointer;
1463 output = frag_var (rs_machine_dependent,
1464 md_relax_table[C (COND_JUMP, DISP32)].rlx_length,
1465 md_relax_table[C (COND_JUMP, DISP12)].rlx_length,
1466 C (COND_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1467 isize = C32_LEN;
1468 break;
1470 case JU:
1471 input_line_pointer = parse_exp (op_end + 1, & e);
1472 op_end = input_line_pointer;
1474 output = frag_var (rs_machine_dependent,
1475 md_relax_table[C (UNCD_JUMP, DISP32)].rlx_length,
1476 md_relax_table[C (UNCD_JUMP, DISP12)].rlx_length,
1477 C (UNCD_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1478 isize = U32_LEN;
1479 break;
1481 case JL:
1482 inst = MCORE_INST_JSRI; /* jsri */
1483 input_line_pointer = parse_rt (op_end + 1, & output, 1, & e);
1484 /* parse_rt() calls frag_more for us. */
1485 op_end = input_line_pointer;
1487 /* Only do this if we know how to do it ... */
1488 if (e.X_op != O_absent && do_jsri2bsr)
1490 /* Look at adding the R_PCREL_JSRIMM11BY2. */
1491 fix_new_exp (frag_now, output-frag_now->fr_literal,
1492 2, & e, 1, BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2);
1494 break;
1496 case RSI:
1497 /* SI, but imm becomes 32-imm. */
1498 op_end = parse_reg (op_end + 1, & reg);
1499 inst |= reg;
1501 /* Skip whitespace. */
1502 while (ISSPACE (* op_end))
1503 ++ op_end;
1505 if (* op_end == ',')
1507 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1509 reg = 32 - reg;
1510 inst |= reg << 4;
1512 else
1513 as_bad (_("second operand missing"));
1515 output = frag_more (2);
1516 break;
1518 case DO21: /* O2, dup rd, lit must be 1 */
1519 op_end = parse_reg (op_end + 1, & reg);
1520 inst |= reg;
1521 inst |= reg << 4;
1523 /* Skip whitespace. */
1524 while (ISSPACE (* op_end))
1525 ++ op_end;
1527 if (* op_end == ',')
1529 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1531 if (reg != 1)
1532 as_bad (_("second operand must be 1"));
1534 else
1535 as_bad (_("second operand missing"));
1537 output = frag_more (2);
1538 break;
1540 case SIa:
1541 op_end = parse_reg (op_end + 1, & reg);
1542 inst |= reg;
1544 /* Skip whitespace. */
1545 while (ISSPACE (* op_end))
1546 ++ op_end;
1548 if (* op_end == ',')
1550 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1552 if (reg == 0)
1553 as_bad (_("zero used as immediate value"));
1555 inst |= reg << 4;
1557 else
1558 as_bad (_("second operand missing"));
1560 output = frag_more (2);
1561 break;
1563 case OPSR:
1564 if (cpu == M210)
1566 as_bad (_("M340 specific opcode used when assembling for M210"));
1567 break;
1570 op_end = parse_psrmod (op_end + 1, & reg);
1572 /* Look for further selectors. */
1573 while (* op_end == ',')
1575 unsigned value;
1577 op_end = parse_psrmod (op_end + 1, & value);
1579 if (value & reg)
1580 as_bad (_("duplicated psr bit specifier"));
1582 reg |= value;
1585 if (reg > 8)
1586 as_bad (_("`af' must appear alone"));
1588 inst |= (reg & 0x7);
1589 output = frag_more (2);
1590 break;
1592 default:
1593 as_bad (_("unimplemented opcode \"%s\""), name);
1596 /* Drop whitespace after all the operands have been parsed. */
1597 while (ISSPACE (* op_end))
1598 op_end ++;
1600 /* Give warning message if the insn has more operands than required. */
1601 if (strcmp (op_end, opcode->name) && strcmp (op_end, ""))
1602 as_warn (_("ignoring operands: %s "), op_end);
1604 output[0] = INST_BYTE0 (inst);
1605 output[1] = INST_BYTE1 (inst);
1607 check_literals (opcode->transfer, isize);
1610 symbolS *
1611 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1613 return 0;
1616 void
1617 md_mcore_end (void)
1619 dump_literals (0);
1620 subseg_set (text_section, 0);
1623 /* Various routines to kill one day. */
1624 /* Equal to MAX_PRECISION in atof-ieee.c. */
1625 #define MAX_LITTLENUMS 6
1627 /* Turn a string in input_line_pointer into a floating point constant of type
1628 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1629 emitted is stored in *sizeP. An error message is returned, or NULL on OK. */
1631 char *
1632 md_atof (int type, char * litP, int * sizeP)
1634 int prec;
1635 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1636 int i;
1637 char * t;
1639 switch (type)
1641 case 'f':
1642 case 'F':
1643 case 's':
1644 case 'S':
1645 prec = 2;
1646 break;
1648 case 'd':
1649 case 'D':
1650 case 'r':
1651 case 'R':
1652 prec = 4;
1653 break;
1655 case 'x':
1656 case 'X':
1657 prec = 6;
1658 break;
1660 case 'p':
1661 case 'P':
1662 prec = 6;
1663 break;
1665 default:
1666 *sizeP = 0;
1667 return _("Bad call to MD_NTOF()");
1670 t = atof_ieee (input_line_pointer, type, words);
1672 if (t)
1673 input_line_pointer = t;
1675 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1677 if (! target_big_endian)
1679 for (i = prec - 1; i >= 0; i--)
1681 md_number_to_chars (litP, (valueT) words[i],
1682 sizeof (LITTLENUM_TYPE));
1683 litP += sizeof (LITTLENUM_TYPE);
1686 else
1687 for (i = 0; i < prec; i++)
1689 md_number_to_chars (litP, (valueT) words[i],
1690 sizeof (LITTLENUM_TYPE));
1691 litP += sizeof (LITTLENUM_TYPE);
1694 return 0;
1697 const char * md_shortopts = "";
1699 enum options
1701 OPTION_JSRI2BSR_ON = OPTION_MD_BASE,
1702 OPTION_JSRI2BSR_OFF,
1703 OPTION_SIFILTER_ON,
1704 OPTION_SIFILTER_OFF,
1705 OPTION_CPU,
1706 OPTION_EB,
1707 OPTION_EL,
1710 struct option md_longopts[] =
1712 { "no-jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_OFF},
1713 { "jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_ON},
1714 { "sifilter", no_argument, NULL, OPTION_SIFILTER_ON},
1715 { "no-sifilter", no_argument, NULL, OPTION_SIFILTER_OFF},
1716 { "cpu", required_argument, NULL, OPTION_CPU},
1717 { "EB", no_argument, NULL, OPTION_EB},
1718 { "EL", no_argument, NULL, OPTION_EL},
1719 { NULL, no_argument, NULL, 0}
1722 size_t md_longopts_size = sizeof (md_longopts);
1725 md_parse_option (int c, char * arg)
1727 switch (c)
1729 case OPTION_CPU:
1730 if (streq (arg, "210"))
1732 cpu = M210;
1733 target_big_endian = 1;
1735 else if (streq (arg, "340"))
1736 cpu = M340;
1737 else
1738 as_warn (_("unrecognised cpu type '%s'"), arg);
1739 break;
1741 case OPTION_EB: target_big_endian = 1; break;
1742 case OPTION_EL: target_big_endian = 0; cpu = M340; break;
1743 case OPTION_JSRI2BSR_ON: do_jsri2bsr = 1; break;
1744 case OPTION_JSRI2BSR_OFF: do_jsri2bsr = 0; break;
1745 case OPTION_SIFILTER_ON: sifilter_mode = 1; break;
1746 case OPTION_SIFILTER_OFF: sifilter_mode = 0; break;
1747 default: return 0;
1750 return 1;
1753 void
1754 md_show_usage (FILE * stream)
1756 fprintf (stream, _("\
1757 MCORE specific options:\n\
1758 -{no-}jsri2bsr {dis}able jsri to bsr transformation (def: dis)\n\
1759 -{no-}sifilter {dis}able silicon filter behavior (def: dis)\n\
1760 -cpu=[210|340] select CPU type\n\
1761 -EB assemble for a big endian system (default)\n\
1762 -EL assemble for a little endian system\n"));
1765 int md_short_jump_size;
1767 void
1768 md_create_short_jump (char * ptr ATTRIBUTE_UNUSED,
1769 addressT from_Nddr ATTRIBUTE_UNUSED,
1770 addressT to_Nddr ATTRIBUTE_UNUSED,
1771 fragS * frag ATTRIBUTE_UNUSED,
1772 symbolS * to_symbol ATTRIBUTE_UNUSED)
1774 as_fatal (_("failed sanity check: short_jump"));
1777 void
1778 md_create_long_jump (char * ptr ATTRIBUTE_UNUSED,
1779 addressT from_Nddr ATTRIBUTE_UNUSED,
1780 addressT to_Nddr ATTRIBUTE_UNUSED,
1781 fragS * frag ATTRIBUTE_UNUSED,
1782 symbolS * to_symbol ATTRIBUTE_UNUSED)
1784 as_fatal (_("failed sanity check: long_jump"));
1787 /* Called after relaxing, change the frags so they know how big they are. */
1789 void
1790 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
1791 segT sec ATTRIBUTE_UNUSED,
1792 fragS * fragP)
1794 char *buffer;
1795 int targ_addr = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
1797 buffer = fragP->fr_fix + fragP->fr_literal;
1799 switch (fragP->fr_subtype)
1801 case C (COND_JUMP, DISP12):
1802 case C (UNCD_JUMP, DISP12):
1804 /* Get the address of the end of the instruction. */
1805 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
1806 unsigned char t0;
1807 int disp = targ_addr - next_inst;
1809 if (disp & 1)
1810 as_bad (_("odd displacement at %x"), next_inst - 2);
1812 disp >>= 1;
1814 if (! target_big_endian)
1816 t0 = buffer[1] & 0xF8;
1818 md_number_to_chars (buffer, disp, 2);
1820 buffer[1] = (buffer[1] & 0x07) | t0;
1822 else
1824 t0 = buffer[0] & 0xF8;
1826 md_number_to_chars (buffer, disp, 2);
1828 buffer[0] = (buffer[0] & 0x07) | t0;
1831 fragP->fr_fix += 2;
1833 break;
1835 case C (COND_JUMP, DISP32):
1836 case C (COND_JUMP, UNDEF_WORD_DISP):
1838 /* A conditional branch wont fit into 12 bits so:
1839 b!cond 1f
1840 jmpi 0f
1841 .align 2
1842 0: .long disp
1845 If the b!cond is 4 byte aligned, the literal which would
1846 go at x+4 will also be aligned. */
1847 int first_inst = fragP->fr_fix + fragP->fr_address;
1848 int needpad = (first_inst & 3);
1850 if (! target_big_endian)
1851 buffer[1] ^= 0x08;
1852 else
1853 buffer[0] ^= 0x08; /* Toggle T/F bit. */
1855 buffer[2] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi. */
1856 buffer[3] = INST_BYTE1 (MCORE_INST_JMPI);
1858 if (needpad)
1860 if (! target_big_endian)
1862 buffer[0] = 4; /* Branch over jmpi, pad, and ptr. */
1863 buffer[2] = 1; /* Jmpi offset of 1 gets the pointer. */
1865 else
1867 buffer[1] = 4; /* Branch over jmpi, pad, and ptr. */
1868 buffer[3] = 1; /* Jmpi offset of 1 gets the pointer. */
1871 buffer[4] = 0; /* Alignment/pad. */
1872 buffer[5] = 0;
1873 buffer[6] = 0; /* Space for 32 bit address. */
1874 buffer[7] = 0;
1875 buffer[8] = 0;
1876 buffer[9] = 0;
1878 /* Make reloc for the long disp. */
1879 fix_new (fragP, fragP->fr_fix + 6, 4,
1880 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1882 fragP->fr_fix += C32_LEN;
1884 else
1886 /* See comment below about this given gas' limitations for
1887 shrinking the fragment. '3' is the amount of code that
1888 we inserted here, but '4' is right for the space we reserved
1889 for this fragment. */
1890 if (! target_big_endian)
1892 buffer[0] = 3; /* Branch over jmpi, and ptr. */
1893 buffer[2] = 0; /* Jmpi offset of 0 gets the pointer. */
1895 else
1897 buffer[1] = 3; /* Branch over jmpi, and ptr. */
1898 buffer[3] = 0; /* Jmpi offset of 0 gets the pointer. */
1901 buffer[4] = 0; /* Space for 32 bit address. */
1902 buffer[5] = 0;
1903 buffer[6] = 0;
1904 buffer[7] = 0;
1906 /* Make reloc for the long disp. */
1907 fix_new (fragP, fragP->fr_fix + 4, 4,
1908 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1909 fragP->fr_fix += C32_LEN;
1911 /* Frag is actually shorter (see the other side of this ifdef)
1912 but gas isn't prepared for that. We have to re-adjust
1913 the branch displacement so that it goes beyond the
1914 full length of the fragment, not just what we actually
1915 filled in. */
1916 if (! target_big_endian)
1917 buffer[0] = 4; /* Jmpi, ptr, and the 'tail pad'. */
1918 else
1919 buffer[1] = 4; /* Jmpi, ptr, and the 'tail pad'. */
1922 break;
1924 case C (UNCD_JUMP, DISP32):
1925 case C (UNCD_JUMP, UNDEF_WORD_DISP):
1927 /* An unconditional branch will not fit in 12 bits, make code which
1928 looks like:
1929 jmpi 0f
1930 .align 2
1931 0: .long disp
1932 we need a pad if "first_inst" is 4 byte aligned.
1933 [because the natural literal place is x + 2]. */
1934 int first_inst = fragP->fr_fix + fragP->fr_address;
1935 int needpad = !(first_inst & 3);
1937 buffer[0] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi. */
1938 buffer[1] = INST_BYTE1 (MCORE_INST_JMPI);
1940 if (needpad)
1942 if (! target_big_endian)
1943 buffer[0] = 1; /* Jmpi offset of 1 since padded. */
1944 else
1945 buffer[1] = 1; /* Jmpi offset of 1 since padded. */
1946 buffer[2] = 0; /* Alignment. */
1947 buffer[3] = 0;
1948 buffer[4] = 0; /* Space for 32 bit address. */
1949 buffer[5] = 0;
1950 buffer[6] = 0;
1951 buffer[7] = 0;
1953 /* Make reloc for the long disp. */
1954 fix_new (fragP, fragP->fr_fix + 4, 4,
1955 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1957 fragP->fr_fix += U32_LEN;
1959 else
1961 if (! target_big_endian)
1962 buffer[0] = 0; /* Jmpi offset of 0 if no pad. */
1963 else
1964 buffer[1] = 0; /* Jmpi offset of 0 if no pad. */
1965 buffer[2] = 0; /* Space for 32 bit address. */
1966 buffer[3] = 0;
1967 buffer[4] = 0;
1968 buffer[5] = 0;
1970 /* Make reloc for the long disp. */
1971 fix_new (fragP, fragP->fr_fix + 2, 4,
1972 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1973 fragP->fr_fix += U32_LEN;
1976 break;
1978 default:
1979 abort ();
1983 /* Applies the desired value to the specified location.
1984 Also sets up addends for 'rela' type relocations. */
1986 void
1987 md_apply_fix3 (fixS * fixP,
1988 valueT * valP,
1989 segT segment ATTRIBUTE_UNUSED)
1991 char * buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1992 char * file = fixP->fx_file ? fixP->fx_file : _("unknown");
1993 const char * symname;
1994 /* Note: use offsetT because it is signed, valueT is unsigned. */
1995 offsetT val = *valP;
1997 symname = fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : _("<unknown>");
1998 /* Save this for the addend in the relocation record. */
1999 fixP->fx_addnumber = val;
2001 if (fixP->fx_addsy != NULL)
2003 #ifdef OBJ_ELF
2004 /* For ELF we can just return and let the reloc that will be generated
2005 take care of everything. For COFF we still have to insert 'val'
2006 into the insn since the addend field will be ignored. */
2007 return;
2008 #endif
2010 else
2011 fixP->fx_done = 1;
2013 switch (fixP->fx_r_type)
2015 /* Second byte of 2 byte opcode. */
2016 case BFD_RELOC_MCORE_PCREL_IMM11BY2:
2017 if ((val & 1) != 0)
2018 as_bad_where (file, fixP->fx_line,
2019 _("odd distance branch (0x%lx bytes)"), (long) val);
2020 val /= 2;
2021 if (((val & ~0x3ff) != 0) && ((val | 0x3ff) != -1))
2022 as_bad_where (file, fixP->fx_line,
2023 _("pcrel for branch to %s too far (0x%lx)"),
2024 symname, (long) val);
2025 if (target_big_endian)
2027 buf[0] |= ((val >> 8) & 0x7);
2028 buf[1] |= (val & 0xff);
2030 else
2032 buf[1] |= ((val >> 8) & 0x7);
2033 buf[0] |= (val & 0xff);
2035 break;
2037 /* Lower 8 bits of 2 byte opcode. */
2038 case BFD_RELOC_MCORE_PCREL_IMM8BY4:
2039 val += 3;
2040 val /= 4;
2041 if (val & ~0xff)
2042 as_bad_where (file, fixP->fx_line,
2043 _("pcrel for lrw/jmpi/jsri to %s too far (0x%lx)"),
2044 symname, (long) val);
2045 else if (! target_big_endian)
2046 buf[0] |= (val & 0xff);
2047 else
2048 buf[1] |= (val & 0xff);
2049 break;
2051 /* Loopt instruction. */
2052 case BFD_RELOC_MCORE_PCREL_IMM4BY2:
2053 if ((val < -32) || (val > -2))
2054 as_bad_where (file, fixP->fx_line,
2055 _("pcrel for loopt too far (0x%lx)"), (long) val);
2056 val /= 2;
2057 if (! target_big_endian)
2058 buf[0] |= (val & 0xf);
2059 else
2060 buf[1] |= (val & 0xf);
2061 break;
2063 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2064 /* Conditional linker map jsri to bsr. */
2065 /* If its a local target and close enough, fix it.
2066 NB: >= -2k for backwards bsr; < 2k for forwards... */
2067 if (fixP->fx_addsy == 0 && val >= -2048 && val < 2048)
2069 long nval = (val / 2) & 0x7ff;
2070 nval |= MCORE_INST_BSR;
2072 /* REPLACE the instruction, don't just modify it. */
2073 buf[0] = INST_BYTE0 (nval);
2074 buf[1] = INST_BYTE1 (nval);
2076 else
2077 fixP->fx_done = 0;
2078 break;
2080 case BFD_RELOC_MCORE_PCREL_32:
2081 case BFD_RELOC_VTABLE_INHERIT:
2082 case BFD_RELOC_VTABLE_ENTRY:
2083 fixP->fx_done = 0;
2084 break;
2086 default:
2087 if (fixP->fx_addsy != NULL)
2089 /* If the fix is an absolute reloc based on a symbol's
2090 address, then it cannot be resolved until the final link. */
2091 fixP->fx_done = 0;
2093 #ifdef OBJ_ELF
2094 else
2095 #endif
2097 if (fixP->fx_size == 4)
2099 else if (fixP->fx_size == 2 && val >= -32768 && val <= 32767)
2101 else if (fixP->fx_size == 1 && val >= -256 && val <= 255)
2103 else
2104 abort ();
2105 md_number_to_chars (buf, val, fixP->fx_size);
2107 break;
2111 void
2112 md_operand (expressionS * expressionP)
2114 /* Ignore leading hash symbol, if poresent. */
2115 if (* input_line_pointer == '#')
2117 input_line_pointer ++;
2118 expression (expressionP);
2122 int md_long_jump_size;
2124 /* Called just before address relaxation, return the length
2125 by which a fragment must grow to reach it's destination. */
2127 md_estimate_size_before_relax (fragS * fragP, segT segment_type)
2129 switch (fragP->fr_subtype)
2131 default:
2132 abort ();
2134 case C (UNCD_JUMP, UNDEF_DISP):
2135 /* Used to be a branch to somewhere which was unknown. */
2136 if (!fragP->fr_symbol)
2137 fragP->fr_subtype = C (UNCD_JUMP, DISP12);
2138 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2139 fragP->fr_subtype = C (UNCD_JUMP, DISP12);
2140 else
2141 fragP->fr_subtype = C (UNCD_JUMP, UNDEF_WORD_DISP);
2142 break;
2144 case C (COND_JUMP, UNDEF_DISP):
2145 /* Used to be a branch to somewhere which was unknown. */
2146 if (fragP->fr_symbol
2147 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2148 /* Got a symbol and it's defined in this segment, become byte
2149 sized - maybe it will fix up */
2150 fragP->fr_subtype = C (COND_JUMP, DISP12);
2151 else if (fragP->fr_symbol)
2152 /* Its got a segment, but its not ours, so it will always be long. */
2153 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
2154 else
2155 /* We know the abs value. */
2156 fragP->fr_subtype = C (COND_JUMP, DISP12);
2157 break;
2159 case C (UNCD_JUMP, DISP12):
2160 case C (UNCD_JUMP, DISP32):
2161 case C (UNCD_JUMP, UNDEF_WORD_DISP):
2162 case C (COND_JUMP, DISP12):
2163 case C (COND_JUMP, DISP32):
2164 case C (COND_JUMP, UNDEF_WORD_DISP):
2165 /* When relaxing a section for the second time, we don't need to
2166 do anything besides return the current size. */
2167 break;
2170 return md_relax_table[fragP->fr_subtype].rlx_length;
2173 /* Put number into target byte order. */
2175 void
2176 md_number_to_chars (char * ptr, valueT use, int nbytes)
2178 if (! target_big_endian)
2179 switch (nbytes)
2181 case 4: ptr[3] = (use >> 24) & 0xff; /* Fall through. */
2182 case 3: ptr[2] = (use >> 16) & 0xff; /* Fall through. */
2183 case 2: ptr[1] = (use >> 8) & 0xff; /* Fall through. */
2184 case 1: ptr[0] = (use >> 0) & 0xff; break;
2185 default: abort ();
2187 else
2188 switch (nbytes)
2190 case 4: *ptr++ = (use >> 24) & 0xff; /* Fall through. */
2191 case 3: *ptr++ = (use >> 16) & 0xff; /* Fall through. */
2192 case 2: *ptr++ = (use >> 8) & 0xff; /* Fall through. */
2193 case 1: *ptr++ = (use >> 0) & 0xff; break;
2194 default: abort ();
2198 /* Round up a section size to the appropriate boundary. */
2200 valueT
2201 md_section_align (segT segment ATTRIBUTE_UNUSED,
2202 valueT size)
2204 /* Byte alignment is fine. */
2205 return size;
2208 /* The location from which a PC relative jump should be calculated,
2209 given a PC relative reloc. */
2211 long
2212 md_pcrel_from_section (fixS * fixp, segT sec ATTRIBUTE_UNUSED)
2214 #ifdef OBJ_ELF
2215 /* If the symbol is undefined or defined in another section
2216 we leave the add number alone for the linker to fix it later.
2217 Only account for the PC pre-bump (which is 2 bytes on the MCore). */
2218 if (fixp->fx_addsy != (symbolS *) NULL
2219 && (! S_IS_DEFINED (fixp->fx_addsy)
2220 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
2223 assert (fixp->fx_size == 2); /* must be an insn */
2224 return fixp->fx_size;
2226 #endif
2228 /* The case where we are going to resolve things... */
2229 return fixp->fx_size + fixp->fx_where + fixp->fx_frag->fr_address;
2232 #define F(SZ,PCREL) (((SZ) << 1) + (PCREL))
2233 #define MAP(SZ,PCREL,TYPE) case F (SZ, PCREL): code = (TYPE); break
2235 arelent *
2236 tc_gen_reloc (asection * section ATTRIBUTE_UNUSED, fixS * fixp)
2238 arelent * rel;
2239 bfd_reloc_code_real_type code;
2241 switch (fixp->fx_r_type)
2243 /* These confuse the size/pcrel macro approach. */
2244 case BFD_RELOC_VTABLE_INHERIT:
2245 case BFD_RELOC_VTABLE_ENTRY:
2246 case BFD_RELOC_MCORE_PCREL_IMM4BY2:
2247 case BFD_RELOC_MCORE_PCREL_IMM8BY4:
2248 case BFD_RELOC_MCORE_PCREL_IMM11BY2:
2249 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2250 case BFD_RELOC_RVA:
2251 code = fixp->fx_r_type;
2252 break;
2254 default:
2255 switch (F (fixp->fx_size, fixp->fx_pcrel))
2257 MAP (1, 0, BFD_RELOC_8);
2258 MAP (2, 0, BFD_RELOC_16);
2259 MAP (4, 0, BFD_RELOC_32);
2260 MAP (1, 1, BFD_RELOC_8_PCREL);
2261 MAP (2, 1, BFD_RELOC_16_PCREL);
2262 MAP (4, 1, BFD_RELOC_32_PCREL);
2263 default:
2264 code = fixp->fx_r_type;
2265 as_bad (_("Can not do %d byte %srelocation"),
2266 fixp->fx_size,
2267 fixp->fx_pcrel ? _("pc-relative") : "");
2269 break;
2272 rel = xmalloc (sizeof (arelent));
2273 rel->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
2274 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2275 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2276 /* Always pass the addend along! */
2277 rel->addend = fixp->fx_addnumber;
2279 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
2281 if (rel->howto == NULL)
2283 as_bad_where (fixp->fx_file, fixp->fx_line,
2284 _("Cannot represent relocation type %s"),
2285 bfd_get_reloc_code_name (code));
2287 /* Set howto to a garbage value so that we can keep going. */
2288 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2289 assert (rel->howto != NULL);
2292 return rel;
2295 #ifdef OBJ_ELF
2296 /* See whether we need to force a relocation into the output file.
2297 This is used to force out switch and PC relative relocations when
2298 relaxing. */
2300 mcore_force_relocation (fixS * fix)
2302 if (fix->fx_r_type == BFD_RELOC_RVA)
2303 return 1;
2305 return generic_force_reloc (fix);
2308 /* Return true if the fix can be handled by GAS, false if it must
2309 be passed through to the linker. */
2311 bfd_boolean
2312 mcore_fix_adjustable (fixS * fixP)
2314 /* We need the symbol name for the VTABLE entries. */
2315 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2316 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2317 return 0;
2319 return 1;
2321 #endif /* OBJ_ELF */