* elflink.h (elf_merge_symbol): When overriding a weak symbol with
[binutils.git] / gas / config / tc-mcore.c
blob4f4e8e95670ff9a7ba9053d68cbd83a95a95da90
1 /* tc-mcore.c -- Assemble code for M*Core
2 Copyright (C) 1999 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
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 <ctype.h>
28 #include <string.h>
30 #ifdef OBJ_ELF
31 #include "elf/mcore.h"
32 #endif
34 #ifndef streq
35 #define streq(a,b) (strcmp (a, b) == 0)
36 #endif
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 void make_name PARAMS ((char *, char *, int));
49 static int enter_literal PARAMS ((expressionS *, int));
50 static char * parse_rt PARAMS ((char *, char **, int, expressionS *));
51 static char * parse_imm PARAMS ((char *, unsigned *, unsigned, unsigned));
52 static char * parse_mem PARAMS ((char *, unsigned *, unsigned *, unsigned));
53 static void dump_literals PARAMS ((int));
54 static void check_literals PARAMS ((int, int));
55 static void mcore_s_text PARAMS ((int));
56 static void mcore_s_data PARAMS ((int));
57 static void mcore_s_section PARAMS ((int));
58 static void mcore_s_bss PARAMS ((int));
59 #ifdef OBJ_ELF
60 static void mcore_s_comm PARAMS ((int));
61 #endif
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) (((x) >> 8) & 0xFF)
69 #define INST_BYTE1(x) ((x) & 0xFF)
71 const char comment_chars[] = "#/";
72 const char line_separator_chars[] = ";";
73 const char line_comment_chars[] = "#/";
75 const int md_reloc_size = 8;
77 static int do_jsri2bsr = 0; /* change here from 1 by Cruess 19 August 97 */
78 static int sifilter_mode = 0;
80 const char EXP_CHARS[] = "eE";
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";
87 #define C(what,length) (((what) << 2) + (length))
88 #define GET_WHAT(x) ((x >> 2))
90 /* These are the two types of relaxable instruction */
91 #define COND_JUMP 1
92 #define UNCD_JUMP 2
94 #define UNDEF_DISP 0
95 #define COND12 1
96 #define COND32 2
97 #define UNCD12 1
98 #define UNCD32 2
99 #define UNDEF_WORD_DISP 4
100 #define END 5
102 #define C12_LEN 2
103 #define C32_LEN 10 /* allow for align */
104 #define U12_LEN 2
105 #define U32_LEN 8 /* allow for align */
108 /* Initialize the relax table */
109 const relax_typeS md_relax_table[] =
111 { 1, 1, 0, 0 }, /* 0: unused */
112 { 1, 1, 0, 0 }, /* 1: unused */
113 { 1, 1, 0, 0 }, /* 2: unused */
114 { 1, 1, 0, 0 }, /* 3: unused */
115 { 1, 1, 0, 0 }, /* 4: unused */
116 { 2048, -2046, C12_LEN, C(COND_JUMP, COND32) }, /* 5: C(COND_JUMP, COND12) */
117 { 0, 0, C32_LEN, 0 }, /* 6: C(COND_JUMP, COND32) */
118 { 1, 1, 0, 0 }, /* 7: unused */
119 { 1, 1, 0, 0 }, /* 8: unused */
120 { 2048, -2046, U12_LEN, C(UNCD_JUMP, UNCD32) }, /* 9: C(UNCD_JUMP, UNCD12) */
121 { 0, 0, U32_LEN, 0 }, /*10: C(UNCD_JUMP, UNCD32) */
122 { 1, 1, 0, 0 }, /*11: unused */
123 { 0, 0, 0, 0 } /*12: unused */
126 /* LITERAL POOL DATA STRUCTURES */
127 struct literal
129 unsigned short refcnt;
130 unsigned char ispcrel;
131 unsigned char unused;
132 expressionS e;
135 #define MAX_POOL_SIZE (1024/4)
136 static struct literal litpool [MAX_POOL_SIZE];
137 static unsigned poolsize;
138 static unsigned poolnumber;
139 static unsigned long poolspan;
141 /* SPANPANIC: the point at which we get too scared and force a dump
142 of the literal pool, and perhaps put a branch in place.
143 Calculated as:
144 1024 span of lrw/jmpi/jsri insn (actually span+1)
145 -2 possible alignment at the insn.
146 -2 possible alignment to get the table aligned.
147 -2 an inserted branch around the table.
148 == 1018
149 at 1018, we might be in trouble.
150 -- so we have to be smaller than 1018 and since we deal with 2-byte
151 instructions, the next good choice is 1016.
152 -- Note we have a test case that fails when we've got 1018 here. */
153 #define SPANPANIC (1016) /* 1024 - 1 entry - 2 byte rounding */
154 #define SPANCLOSE (900)
155 #define SPANEXIT (600)
156 static symbolS * poolsym; /* label for current pool */
157 static char poolname[8];
158 static struct hash_control * opcode_hash_control; /* Opcode mnemonics */
160 /* This table describes all the machine specific pseudo-ops the assembler
161 has to support. The fields are:
162 Pseudo-op name without dot
163 Function to call to execute this pseudo-op
164 Integer arg to pass to the function */
165 const pseudo_typeS md_pseudo_table[] =
167 { "export", s_globl, 0 },
168 { "import", s_ignore, 0 },
169 { "literals", mcore_s_literals, 0 },
170 { "page", listing_eject, 0 },
172 /* The following are to intercept the placement of data into the text
173 section (eg addresses for a switch table), so that the space they
174 occupy can be taken into account when deciding whether or not to
175 dump the current literal pool.
176 XXX - currently we do not cope with the .space and .dcb.d directives. */
177 { "ascii", mcore_stringer, 0 },
178 { "asciz", mcore_stringer, 1 },
179 { "byte", mcore_cons, 1 },
180 { "dc", mcore_cons, 2 },
181 { "dc.b", mcore_cons, 1 },
182 { "dc.d", mcore_float_cons, 'd'},
183 { "dc.l", mcore_cons, 4 },
184 { "dc.s", mcore_float_cons, 'f'},
185 { "dc.w", mcore_cons, 2 },
186 { "dc.x", mcore_float_cons, 'x'},
187 { "double", mcore_float_cons, 'd'},
188 { "float", mcore_float_cons, 'f'},
189 { "hword", mcore_cons, 2 },
190 { "int", mcore_cons, 4 },
191 { "long", mcore_cons, 4 },
192 { "octa", mcore_cons, 16 },
193 { "quad", mcore_cons, 8 },
194 { "short", mcore_cons, 2 },
195 { "single", mcore_float_cons, 'f'},
196 { "string", mcore_stringer, 1 },
197 { "word", mcore_cons, 2 },
198 { "fill", mcore_fill, 0 },
200 /* Allow for the effect of section changes. */
201 { "text", mcore_s_text, 0 },
202 { "data", mcore_s_data, 0 },
203 { "bss", mcore_s_bss, 1 },
204 #ifdef OBJ_EF
205 { "comm", mcore_s_comm, 0 },
206 #endif
207 { "section", mcore_s_section, 0 },
208 { "section.s", mcore_s_section, 0 },
209 { "sect", mcore_s_section, 0 },
210 { "sect.s", mcore_s_section, 0 },
212 { 0, 0, 0 }
215 static void
216 mcore_s_literals (ignore)
217 int ignore;
219 dump_literals (0);
220 demand_empty_rest_of_line ();
224 static void
225 mcore_cons (nbytes)
226 int nbytes;
228 if (now_seg == text_section)
230 char * ptr = input_line_pointer;
231 int commas = 1;
233 /* Count the number of commas on the line. */
234 while (! is_end_of_line [* ptr])
235 commas += * ptr ++ == ',';
237 poolspan += nbytes * commas;
240 cons (nbytes);
242 /* In theory we ought to call check_literals (2,0) here in case
243 we need to dump the literal table. We cannot do this however,
244 as the directives that we are intercepting may be being used
245 to build a switch table, and we must not interfere with its
246 contents. Instead we cross our fingers and pray... */
249 static void
250 mcore_float_cons (float_type)
251 int float_type;
253 if (now_seg == text_section)
255 char * ptr = input_line_pointer;
256 int commas = 1;
258 #ifdef REPEAT_CONS_EXPRESSIONS
259 #error REPEAT_CONS_EXPRESSIONS not handled
260 #endif
262 /* Count the number of commas on the line. */
263 while (! is_end_of_line [* ptr])
264 commas += * ptr ++ == ',';
266 /* We would like to compute "hex_float (float_type) * commas"
267 but hex_float is not exported from read.c */
268 float_type == 'f' ? 4 : (float_type == 'd' ? 8 : 12);
269 poolspan += float_type * commas;
272 float_cons (float_type);
274 /* See the comment in mcore_cons () about calling check_literals.
275 It is unlikely that a switch table will be constructed using
276 floating point values, but it is still likely that an indexed
277 table of floating point constants is being created by these
278 directives, so again we must not interfere with their placement. */
281 static void
282 mcore_stringer (append_zero)
283 int append_zero;
285 if (now_seg == text_section)
287 char * ptr = input_line_pointer;
289 /* In theory we should compute how many bytes are going to
290 be occupied by the string(s) and add this to the poolspan.
291 To keep things simple however, we just add the number of
292 bytes left on the current line. This will be an over-
293 estimate, which is OK, and automatically allows for the
294 appending a zero byte, since the real string(s) is/are
295 required to be enclosed in double quotes. */
296 while (! is_end_of_line [* ptr])
297 ptr ++;
299 poolspan += ptr - input_line_pointer;
302 stringer (append_zero);
304 /* We call check_literals here in case a large number of strings are
305 being placed into the text section with a sequence of stringer
306 directives. In theory we could be upsetting something if these
307 strings are actually in an indexed table instead of referenced by
308 individual labels. Let us hope that that never happens. */
309 check_literals (2, 0);
312 static void
313 mcore_fill (unused)
314 int unused;
316 if (now_seg == text_section)
318 char * str = input_line_pointer;
319 int size = 1;
320 int repeat;
322 repeat = atoi (str);
324 /* Look to see if a size has been specified. */
325 while (*str != '\n' && *str != 0 && *str != ',')
326 ++ str;
328 if (* str == ',')
330 size = atoi (str + 1);
332 if (size > 8)
333 size = 8;
334 else if (size < 0)
335 size = 0;
338 poolspan += size * repeat;
341 s_fill (unused);
343 check_literals (2, 0);
346 /* Handle the section changing pseudo-ops. These call through to the
347 normal implementations, but they dump the literal pool first. */
349 static void
350 mcore_s_text (ignore)
351 int ignore;
353 dump_literals (0);
355 #ifdef OBJ_ELF
356 obj_elf_text (ignore);
357 #else
358 s_text (ignore);
359 #endif
362 static void
363 mcore_s_data (ignore)
364 int ignore;
366 dump_literals (0);
368 #ifdef OBJ_ELF
369 obj_elf_data (ignore);
370 #else
371 s_data (ignore);
372 #endif
375 static void
376 mcore_s_section (ignore)
377 int ignore;
379 /* Scan forwards to find the name of the section. If the section
380 being switched to is ".line" then this is a DWARF1 debug section
381 which is arbitarily placed inside generated code. In this case
382 do not dump the literal pool because it is a) inefficient and
383 b) would require the generation of extra code to jump around the
384 pool. */
385 char * ilp = input_line_pointer;
387 while (*ilp != 0 && isspace(*ilp))
388 ++ ilp;
390 if (strncmp (ilp, ".line", 5) == 0
391 && (isspace (ilp[5]) || *ilp == '\n' || *ilp == '\r'))
393 else
394 dump_literals (0);
396 #ifdef OBJ_ELF
397 obj_elf_section (ignore);
398 #endif
399 #ifdef OBJ_COFF
400 obj_coff_section (ignore);
401 #endif
404 static void
405 mcore_s_bss (needs_align)
406 int needs_align;
408 dump_literals (0);
410 s_lcomm_bytes (needs_align);
413 #ifdef OBJ_ELF
414 static void
415 mcore_s_comm (needs_align)
416 int needs_align;
418 dump_literals (0);
420 obj_elf_common (needs_align);
422 #endif
424 /* This function is called once, at assembler startup time. This should
425 set up all the tables, etc that the MD part of the assembler needs. */
426 void
427 md_begin ()
429 mcore_opcode_info * opcode;
430 char * prev_name = "";
432 opcode_hash_control = hash_new ();
434 /* Insert unique names into hash table */
435 for (opcode = mcore_table; opcode->name; opcode ++)
437 if (streq (prev_name, opcode->name))
439 /* Make all the opcodes with the same name point to the same
440 string. */
441 opcode->name = prev_name;
443 else
445 prev_name = opcode->name;
446 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
451 static int reg_m;
452 static int reg_n;
453 static expressionS immediate; /* absolute expression */
455 /* Get a log2(val). */
456 static int
457 log2 (val)
458 unsigned int val;
460 int log = -1;
461 while (val != 0)
463 log ++;
464 val >>= 1;
467 return log;
470 /* Try to parse a reg name. */
471 static char *
472 parse_reg (s, reg)
473 char * s;
474 unsigned * reg;
476 /* Strip leading whitespace. */
477 while (isspace (* s))
478 ++ s;
480 if (tolower (s[0]) == 'r')
482 if (s[1] == '1' && s[2] >= '0' && s[2] <= '5')
484 *reg = 10 + s[2] - '0';
485 return s + 3;
488 if (s[1] >= '0' && s[1] <= '9')
490 *reg = s[1] - '0';
491 return s + 2;
494 else if ( tolower (s[0]) == 's'
495 && tolower (s[1]) == 'p'
496 && ! isalnum (s[2]))
498 * reg = 0;
499 return s + 2;
502 as_bad (_("register expected, but saw '%.6s'"), s);
503 return s;
506 static struct Cregs
508 char * name;
509 unsigned int crnum;
511 cregs[] =
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}
529 static char *
530 parse_creg (s, reg)
531 char * s;
532 unsigned * reg;
534 int i;
536 /* Strip leading whitespace. */
537 while (isspace (* s))
538 ++s;
540 if ((tolower (s[0]) == 'c' && tolower (s[1]) == 'r'))
542 if (s[2] == '3' && s[3] >= '0' && s[3] <= '1')
544 *reg = 30 + s[3] - '0';
545 return s + 4;
548 if (s[2] == '2' && s[3] >= '0' && s[3] <= '9')
550 *reg = 20 + s[3] - '0';
551 return s + 4;
554 if (s[2] == '1' && s[3] >= '0' && s[3] <= '9')
556 *reg = 10 + s[3] - '0';
557 return s + 4;
560 if (s[2] >= '0' && s[2] <= '9')
562 *reg = s[2] - '0';
563 return s + 3;
567 /* Look at alternate creg names before giving error. */
568 for (i = 0; cregs[i].name[0] != '\0'; i++)
570 char buf [10];
571 int length;
572 int j;
574 length = strlen (cregs[i].name);
576 for (j = 0; j < length; j++)
577 buf[j] = tolower (s[j]);
579 if (strncmp (cregs[i].name, buf, length) == 0)
581 *reg = cregs[i].crnum;
582 return s + length;
586 as_bad (_("control register expected, but saw '%.6s'"), s);
588 return s;
591 static char *
592 parse_exp (s, e)
593 char * s;
594 expressionS * e;
596 char * save;
597 char * new;
599 /* Skip whitespace. */
600 while (isspace (* s))
601 ++ s;
603 save = input_line_pointer;
604 input_line_pointer = s;
606 expression (e);
608 if (e->X_op == O_absent)
609 as_bad (_("missing operand"));
611 new = input_line_pointer;
612 input_line_pointer = save;
614 return new;
617 static void
618 make_name (s, p, n)
619 char * s;
620 char * p;
621 int n;
623 static const char hex[] = "0123456789ABCDEF";
625 s[0] = p[0];
626 s[1] = p[1];
627 s[2] = p[2];
628 s[3] = hex[(n >> 12) & 0xF];
629 s[4] = hex[(n >> 8) & 0xF];
630 s[5] = hex[(n >> 4) & 0xF];
631 s[6] = hex[(n) & 0xF];
632 s[7] = 0;
635 #define POOL_END_LABEL ".LE"
636 #define POOL_START_LABEL ".LS"
638 static void
639 dump_literals (isforce)
640 int isforce;
642 int i;
643 struct literal * p;
644 symbolS * brarsym;
646 if (poolsize == 0)
647 return;
649 /* Must we branch around the literal table? */
650 if (isforce)
652 char * output;
653 char brarname[8];
655 make_name (brarname, POOL_END_LABEL, poolnumber);
657 brarsym = symbol_make (brarname);
659 symbol_table_insert (brarsym);
661 output = frag_var (rs_machine_dependent,
662 md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length,
663 md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length,
664 C (UNCD_JUMP, 0), brarsym, 0, 0);
665 output[0] = INST_BYTE0 (MCORE_INST_BR); /* br .+xxx */
666 output[1] = INST_BYTE1 (MCORE_INST_BR);
669 /* Make sure that the section is sufficiently aligned and that
670 the literal table is aligned within it. */
671 record_alignment (now_seg, 2);
672 frag_align (2, 0, 0);
674 colon (S_GET_NAME (poolsym));
676 for (i = 0, p = litpool; i < poolsize; i++, p++)
677 emit_expr (& p->e, 4);
679 if (isforce)
680 colon (S_GET_NAME (brarsym));
682 poolsize = 0;
685 static void
686 check_literals (kind, offset)
687 int kind;
688 int offset;
690 poolspan += offset;
692 /* SPANCLOSE and SPANEXIT are smaller numbers than SPANPANIC.
693 SPANPANIC means that we must dump now.
694 kind == 0 is any old instruction.
695 kind > 0 means we just had a control transfer instruction.
696 kind == 1 means within a function
697 kind == 2 means we just left a function
699 The dump_literals (1) call inserts a branch around the table, so
700 we first look to see if its a situation where we won't have to
701 insert a branch (e.g., the previous instruction was an unconditional
702 branch).
704 SPANPANIC is the point where we must dump a single-entry pool.
705 it accounts for alignments and an inserted branch.
706 the 'poolsize*2' accounts for the scenario where we do:
707 lrw r1,lit1; lrw r2,lit2; lrw r3,lit3
708 Note that the 'lit2' reference is 2 bytes further along
709 but the literal it references will be 4 bytes further along,
710 so we must consider the poolsize into this equation.
711 This is slightly over-cautious, but guarantees that we won't
712 panic because a relocation is too distant. */
714 if (poolspan > SPANCLOSE && kind > 0)
715 dump_literals (0);
716 else if (poolspan > SPANEXIT && kind > 1)
717 dump_literals (0);
718 else if (poolspan >= (SPANPANIC - poolsize * 2))
719 dump_literals (1);
722 static int
723 enter_literal (e, ispcrel)
724 expressionS * e;
725 int ispcrel;
727 int i;
728 struct literal * p;
730 if (poolsize >= MAX_POOL_SIZE - 2)
732 /* The literal pool is as full as we can handle. We have
733 to be 2 entries shy of the 1024/4=256 entries because we
734 have to allow for the branch (2 bytes) and the alignment
735 (2 bytes before the first insn referencing the pool and
736 2 bytes before the pool itself) == 6 bytes, rounds up
737 to 2 entries. */
738 dump_literals (1);
741 if (poolsize == 0)
743 /* Create new literal pool. */
744 if (++ poolnumber > 0xFFFF)
745 as_fatal (_("more than 65K literal pools"));
747 make_name (poolname, POOL_START_LABEL, poolnumber);
748 poolsym = symbol_make (poolname);
749 symbol_table_insert (poolsym);
750 poolspan = 0;
753 /* Search pool for value so we don't have duplicates. */
754 for (p = litpool, i = 0; i < poolsize; i++, p++)
756 if (e->X_op == p->e.X_op
757 && e->X_add_symbol == p->e.X_add_symbol
758 && e->X_add_number == p->e.X_add_number
759 && ispcrel == p->ispcrel)
761 p->refcnt ++;
762 return i;
766 p->refcnt = 1;
767 p->ispcrel = ispcrel;
768 p->e = * e;
770 poolsize ++;
772 return i;
775 /* Parse a literal specification. -- either new or old syntax.
776 old syntax: the user supplies the label and places the literal.
777 new syntax: we put it into the literal pool. */
778 static char *
779 parse_rt (s, outputp, ispcrel, ep)
780 char * s;
781 char ** outputp;
782 int ispcrel;
783 expressionS * ep;
785 expressionS e;
786 int n;
788 if (ep)
789 /* Indicate nothing there. */
790 ep->X_op = O_absent;
792 if (*s == '[')
794 s = parse_exp (s + 1, & e);
796 if (*s == ']')
797 s++;
798 else
799 as_bad (_("missing ']'"));
801 else
803 s = parse_exp (s, & e);
805 n = enter_literal (& e, ispcrel);
807 if (ep)
808 *ep = e;
810 /* Create a reference to pool entry. */
811 e.X_op = O_symbol;
812 e.X_add_symbol = poolsym;
813 e.X_add_number = n << 2;
816 * outputp = frag_more (2);
818 fix_new_exp (frag_now, (*outputp) - frag_now->fr_literal, 2, & e, 1,
819 BFD_RELOC_MCORE_PCREL_IMM8BY4);
821 return s;
824 static char *
825 parse_imm (s, val, min, max)
826 char * s;
827 unsigned * val;
828 unsigned min;
829 unsigned max;
831 char * new;
832 expressionS e;
834 new = parse_exp (s, & e);
836 if (e.X_op == O_absent)
837 ; /* An error message has already been emitted. */
838 else if (e.X_op != O_constant)
839 as_bad (_("operand must be a constant"));
840 else if (e.X_add_number < min || e.X_add_number > max)
841 as_bad (_("operand must be absolute in range %d..%d, not %d"),
842 min, max, e.X_add_number);
844 * val = e.X_add_number;
846 return new;
849 static char *
850 parse_mem (s, reg, off, siz)
851 char * s;
852 unsigned * reg;
853 unsigned * off;
854 unsigned siz;
856 char * new;
858 * off = 0;
860 while (isspace (* s))
861 ++ s;
863 if (* s == '(')
865 s = parse_reg (s + 1, reg);
867 while (isspace (* s))
868 ++ s;
870 if (* s == ',')
872 s = parse_imm (s + 1, off, 0, 63);
874 if (siz > 1)
876 if (siz > 2)
878 if (* off & 0x3)
879 as_bad (_("operand must be a multiple of 4"));
881 * off >>= 2;
883 else
885 if (* off & 0x1)
886 as_bad (_("operand must be a multiple of 2"));
888 * off >>= 1;
893 while (isspace (* s))
894 ++ s;
896 if (* s == ')')
897 s ++;
899 else
900 as_bad (_("base register expected"));
902 return s;
905 /* This is the guts of the machine-dependent assembler. STR points to a
906 machine dependent instruction. This function is supposed to emit
907 the frags/bytes it assembles to. */
909 void
910 md_assemble (str)
911 char * str;
913 char * op_start;
914 char * op_end;
915 mcore_opcode_info * opcode;
916 char * output;
917 int nlen = 0;
918 unsigned short inst;
919 unsigned reg;
920 unsigned off;
921 unsigned isize;
922 expressionS e;
923 char name[20];
925 /* Drop leading whitespace. */
926 while (isspace (* str))
927 str ++;
929 /* Find the op code end. */
930 for (op_start = op_end = str;
931 * op_end && nlen < 20 && !is_end_of_line [*op_end] && *op_end != ' ';
932 op_end++)
934 name[nlen] = op_start[nlen];
935 nlen++;
938 name [nlen] = 0;
940 if (nlen == 0)
942 as_bad (_("can't find opcode "));
943 return;
946 opcode = (mcore_opcode_info *) hash_find (opcode_hash_control, name);
947 if (opcode == NULL)
949 as_bad (_("unknown opcode \"%s\""), name);
950 return;
953 inst = opcode->inst;
954 isize = 2;
956 switch (opcode->opclass)
958 case O0:
959 output = frag_more (2);
960 break;
962 case OT:
963 op_end = parse_imm (op_end + 1, & reg, 0, 3);
964 inst |= reg;
965 output = frag_more (2);
966 break;
968 case O1:
969 op_end = parse_reg (op_end + 1, & reg);
970 inst |= reg;
971 output = frag_more (2);
972 break;
974 case JMP:
975 op_end = parse_reg (op_end + 1, & reg);
976 inst |= reg;
977 output = frag_more (2);
978 /* In a sifilter mode, we emit this insn 2 times,
979 fixes problem of an interrupt during a jmp.. */
980 if (sifilter_mode)
982 output[0] = INST_BYTE0 (inst);
983 output[1] = INST_BYTE1 (inst);
984 output = frag_more (2);
986 break;
988 case JSR:
989 op_end = parse_reg (op_end + 1, & reg);
991 if (reg == 15)
992 as_bad (_("invalid register: r15 illegal"));
994 inst |= reg;
995 output = frag_more (2);
997 if (sifilter_mode)
999 /* Replace with: bsr .+2 ; addi r15,6; jmp rx ; jmp rx */
1000 inst = MCORE_INST_BSR; /* with 0 displacement */
1001 output[0] = INST_BYTE0 (inst);
1002 output[1] = INST_BYTE1 (inst);
1004 output = frag_more (2);
1005 inst = MCORE_INST_ADDI;
1006 inst |= 15; /* addi r15,6 */
1007 inst |= (6 - 1) << 4; /* over the jmp's */
1008 output[0] = INST_BYTE0 (inst);
1009 output[1] = INST_BYTE1 (inst);
1011 output = frag_more (2);
1012 inst = MCORE_INST_JMP | reg;
1013 output[0] = INST_BYTE0 (inst);
1014 output[1] = INST_BYTE1 (inst);
1016 output = frag_more (2); /* 2nd emitted in fallthru */
1018 break;
1020 case OC:
1021 op_end = parse_reg (op_end + 1, & reg);
1022 inst |= reg;
1024 /* Skip whitespace. */
1025 while (isspace (* op_end))
1026 ++ op_end;
1028 if (*op_end == ',')
1030 op_end = parse_creg (op_end + 1, & reg);
1031 inst |= reg << 4;
1034 output = frag_more (2);
1035 break;
1037 case O2:
1038 op_end = parse_reg (op_end + 1, & reg);
1039 inst |= reg;
1041 /* Skip whitespace. */
1042 while (isspace (* op_end))
1043 ++ op_end;
1045 if (* op_end == ',')
1047 op_end = parse_reg (op_end + 1, & reg);
1048 inst |= reg << 4;
1050 else
1051 as_bad (_("second operand missing"));
1053 output = frag_more (2);
1054 break;
1056 case X1: /* Handle both syntax-> xtrb- r1,rx OR xtrb- rx */
1057 op_end = parse_reg (op_end + 1, & reg);
1059 /* Skip whitespace. */
1060 while (isspace (* op_end))
1061 ++ op_end;
1063 if (* op_end == ',') /* xtrb- r1,rx */
1065 if (reg != 1)
1066 as_bad (_("destination register must be r1"));
1068 op_end = parse_reg (op_end + 1, & reg);
1071 inst |= reg;
1072 output = frag_more (2);
1073 break;
1075 case O1R1: /* div- rx,r1 */
1076 op_end = parse_reg (op_end + 1, & reg);
1077 inst |= reg;
1079 /* Skip whitespace. */
1080 while (isspace (* op_end))
1081 ++ op_end;
1083 if (* op_end == ',')
1085 op_end = parse_reg (op_end + 1, & reg);
1086 if (reg != 1)
1087 as_bad (_("source register must be r1"));
1089 else
1090 as_bad (_("second operand missing"));
1092 output = frag_more (2);
1093 break;
1095 case OI:
1096 op_end = parse_reg (op_end + 1, & reg);
1097 inst |= reg;
1099 /* Skip whitespace. */
1100 while (isspace (* op_end))
1101 ++ op_end;
1103 if (* op_end == ',')
1105 op_end = parse_imm (op_end + 1, & reg, 1, 32);
1106 inst |= (reg - 1) << 4;
1108 else
1109 as_bad (_("second operand missing"));
1111 output = frag_more (2);
1112 break;
1114 case OB:
1115 op_end = parse_reg (op_end + 1, & reg);
1116 inst |= reg;
1118 /* Skip whitespace. */
1119 while (isspace (* op_end))
1120 ++ op_end;
1122 if (* op_end == ',')
1124 op_end = parse_imm (op_end + 1, & reg, 0, 31);
1125 inst |= reg << 4;
1127 else
1128 as_bad (_("second operand missing"));
1130 output = frag_more (2);
1131 break;
1133 case OB2: /* like OB, but arg is 2^n instead of n */
1134 op_end = parse_reg (op_end + 1, & reg);
1135 inst |= reg;
1137 /* Skip whitespace. */
1138 while (isspace (* op_end))
1139 ++ op_end;
1141 if (* op_end == ',')
1143 op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1144 /* Further restrict the immediate to a power of two. */
1145 if ((reg & (reg - 1)) == 0)
1146 reg = log2 (reg);
1147 else
1149 reg = 0;
1150 as_bad (_("immediate is not a power of two"));
1152 inst |= (reg) << 4;
1154 else
1155 as_bad (_("second operand missing"));
1157 output = frag_more (2);
1158 break;
1160 case OBRa: /* Specific for bgeni: imm of 0->6 translate to movi. */
1161 case OBRb:
1162 case OBRc:
1163 op_end = parse_reg (op_end + 1, & reg);
1164 inst |= reg;
1166 /* Skip whitespace. */
1167 while (isspace (* op_end))
1168 ++ op_end;
1170 if (* op_end == ',')
1172 op_end = parse_imm (op_end + 1, & reg, 0, 31);
1173 /* immediate values of 0 -> 6 translate to movi */
1174 if (reg <= 6)
1176 inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1177 reg = 0x1 << reg;
1178 as_warn (_("translating bgeni to movi"));
1180 inst &= ~ 0x01f0;
1181 inst |= reg << 4;
1183 else
1184 as_bad (_("second operand missing"));
1186 output = frag_more (2);
1187 break;
1189 case OBR2: /* like OBR, but arg is 2^n instead of n */
1190 op_end = parse_reg (op_end + 1, & reg);
1191 inst |= reg;
1193 /* Skip whitespace. */
1194 while (isspace (* op_end))
1195 ++ op_end;
1197 if (* op_end == ',')
1199 op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1201 /* Further restrict the immediate to a power of two. */
1202 if ((reg & (reg - 1)) == 0)
1203 reg = log2 (reg);
1204 else
1206 reg = 0;
1207 as_bad (_("immediate is not a power of two"));
1210 /* Immediate values of 0 -> 6 translate to movi. */
1211 if (reg <= 6)
1213 inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1214 reg = 0x1 << reg;
1215 as_warn (_("translating mgeni to movi"));
1218 inst |= reg << 4;
1220 else
1221 as_bad (_("second operand missing"));
1223 output = frag_more (2);
1224 break;
1226 case OMa: /* Specific for bmaski: imm 1->7 translate to movi. */
1227 case OMb:
1228 case OMc:
1229 op_end = parse_reg (op_end + 1, & reg);
1230 inst |= reg;
1232 /* Skip whitespace. */
1233 while (isspace (* op_end))
1234 ++ op_end;
1236 if (* op_end == ',')
1238 op_end = parse_imm (op_end + 1, & reg, 1, 32);
1240 /* Immediate values of 1 -> 7 translate to movi. */
1241 if (reg <= 7)
1243 inst = (inst & 0xF) | MCORE_INST_BMASKI_ALT;
1244 reg = (0x1 << reg) - 1;
1245 inst |= reg << 4;
1247 as_warn (_("translating bmaski to movi"));
1249 else
1251 inst &= ~ 0x01F0;
1252 inst |= (reg & 0x1F) << 4;
1255 else
1256 as_bad (_("second operand missing"));
1258 output = frag_more (2);
1259 break;
1261 case SI:
1262 op_end = parse_reg (op_end + 1, & reg);
1263 inst |= reg;
1265 /* Skip whitespace. */
1266 while (isspace (* op_end))
1267 ++ op_end;
1269 if (* op_end == ',')
1271 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1272 inst |= reg << 4;
1274 else
1275 as_bad (_("second operand missing"));
1277 output = frag_more (2);
1278 break;
1280 case I7:
1281 op_end = parse_reg (op_end + 1, & reg);
1282 inst |= reg;
1284 /* Skip whitespace. */
1285 while (isspace (* op_end))
1286 ++ op_end;
1288 if (* op_end == ',')
1290 op_end = parse_imm (op_end + 1, & reg, 0, 0x7F);
1291 inst |= reg << 4;
1293 else
1294 as_bad (_("second operand missing"));
1296 output = frag_more (2);
1297 break;
1299 case LS:
1300 op_end = parse_reg (op_end + 1, & reg);
1301 inst |= reg << 8;
1303 /* Skip whitespace. */
1304 while (isspace (* op_end))
1305 ++ op_end;
1307 if (* op_end == ',')
1309 int size;
1311 if ((inst & 0x6000) == 0)
1312 size = 4;
1313 else if ((inst & 0x6000) == 0x4000)
1314 size = 2;
1315 else if ((inst & 0x6000) == 0x2000)
1316 size = 1;
1318 op_end = parse_mem (op_end + 1, & reg, & off, size);
1320 if (off > 16)
1321 as_bad (_("displacement too large (%d)"), off);
1322 else
1323 inst |= (reg) | (off << 4);
1325 else
1326 as_bad (_("second operand missing"));
1328 output = frag_more (2);
1329 break;
1331 case LR:
1332 op_end = parse_reg (op_end + 1, & reg);
1334 if (reg == 0 || reg == 15)
1335 as_bad (_("Invalid register: r0 and r15 illegal"));
1337 inst |= (reg << 8);
1339 /* Skip whitespace. */
1340 while (isspace (* op_end))
1341 ++ op_end;
1343 if (* op_end == ',')
1344 /* parse_rt calls frag_more() for us. */
1345 input_line_pointer = parse_rt (op_end + 1, & output, 0, 0);
1346 else
1348 as_bad (_("second operand missing"));
1349 output = frag_more (2); /* save its space */
1351 break;
1353 case LJ:
1354 input_line_pointer = parse_rt (op_end + 1, & output, 1, 0);
1355 /* parse_rt() calls frag_more() for us. */
1356 break;
1358 case RM:
1359 op_end = parse_reg (op_end + 1, & reg);
1361 if (reg == 0 || reg == 15)
1362 as_bad (_("bad starting register: r0 and r15 invalid"));
1364 inst |= reg;
1366 /* Skip whitespace. */
1367 while (isspace (* op_end))
1368 ++ op_end;
1370 if (* op_end == '-')
1372 op_end = parse_reg (op_end + 1, & reg);
1374 if (reg != 15)
1375 as_bad (_("ending register must be r15"));
1377 /* Skip whitespace. */
1378 while (isspace (* op_end))
1379 ++ op_end;
1382 if (* op_end == ',')
1384 op_end ++;
1386 /* Skip whitespace. */
1387 while (isspace (* op_end))
1388 ++ op_end;
1390 if (* op_end == '(')
1392 op_end = parse_reg (op_end + 1, & reg);
1394 if (reg != 0)
1395 as_bad (_("bad base register: must be r0"));
1397 if (* op_end == ')')
1398 op_end ++;
1400 else
1401 as_bad (_("base register expected"));
1403 else
1404 as_bad (_("second operand missing"));
1406 output = frag_more (2);
1407 break;
1409 case RQ:
1410 op_end = parse_reg (op_end + 1, & reg);
1412 if (reg != 4)
1413 as_fatal (_("first register must be r4"));
1415 /* Skip whitespace. */
1416 while (isspace (* op_end))
1417 ++ op_end;
1419 if (* op_end == '-')
1421 op_end = parse_reg (op_end + 1, & reg);
1423 if (reg != 7)
1424 as_fatal (_("last register must be r7"));
1426 /* Skip whitespace. */
1427 while (isspace (* op_end))
1428 ++ op_end;
1430 if (* op_end == ',')
1432 op_end ++;
1434 /* Skip whitespace. */
1435 while (isspace (* op_end))
1436 ++ op_end;
1438 if (* op_end == '(')
1440 op_end = parse_reg (op_end + 1, & reg);
1442 if (reg >= 4 && reg <= 7)
1443 as_fatal ("base register cannot be r4, r5, r6, or r7");
1445 inst |= reg;
1447 /* Skip whitespace. */
1448 while (isspace (* op_end))
1449 ++ op_end;
1451 if (* op_end == ')')
1452 op_end ++;
1454 else
1455 as_bad (_("base register expected"));
1457 else
1458 as_bad (_("second operand missing"));
1460 else
1461 as_bad (_("reg-reg expected"));
1463 output = frag_more (2);
1464 break;
1466 case BR:
1467 input_line_pointer = parse_exp (op_end + 1, & e);
1469 output = frag_more (2);
1471 fix_new_exp (frag_now, output-frag_now->fr_literal,
1472 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM11BY2);
1473 break;
1475 case BL:
1476 op_end = parse_reg (op_end + 1, & reg);
1477 inst |= reg << 4;
1479 /* Skip whitespace. */
1480 while (isspace (* op_end))
1481 ++ op_end;
1483 if (* op_end == ',')
1485 op_end = parse_exp (op_end + 1, & e);
1486 output = frag_more (2);
1488 fix_new_exp (frag_now, output-frag_now->fr_literal,
1489 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM4BY2);
1491 else
1493 as_bad (_("second operand missing"));
1494 output = frag_more (2);
1496 break;
1498 case JC:
1499 input_line_pointer = parse_exp (op_end + 1, & e);
1501 output = frag_var (rs_machine_dependent,
1502 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
1503 md_relax_table[C (COND_JUMP, COND12)].rlx_length,
1504 C (COND_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1505 isize = C32_LEN;
1506 break;
1508 case JU:
1509 input_line_pointer = parse_exp (op_end + 1, & e);
1510 output = frag_var (rs_machine_dependent,
1511 md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length,
1512 md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length,
1513 C (UNCD_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1514 isize = U32_LEN;
1515 break;
1517 case JL:
1518 inst = MCORE_INST_JSRI; /* jsri */
1519 input_line_pointer = parse_rt (op_end + 1, & output, 1, & e);
1520 /* parse_rt() calls frag_more for us. */
1522 /* Only do this if we know how to do it ... */
1523 if (e.X_op != O_absent && do_jsri2bsr)
1525 /* Look at adding the R_PCREL_JSRIMM11BY2. */
1526 fix_new_exp (frag_now, output-frag_now->fr_literal,
1527 2, & e, 1, BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2);
1529 break;
1531 case RSI: /* SI, but imm becomes 32-imm */
1532 op_end = parse_reg (op_end + 1, & reg);
1533 inst |= reg;
1535 /* Skip whitespace. */
1536 while (isspace (* op_end))
1537 ++ op_end;
1539 if (* op_end == ',')
1541 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1543 reg = 32 - reg;
1544 inst |= reg << 4;
1546 else
1547 as_bad (_("second operand missing"));
1549 output = frag_more (2);
1550 break;
1552 case DO21: /* O2, dup rd, lit must be 1 */
1553 op_end = parse_reg (op_end + 1, & reg);
1554 inst |= reg;
1555 inst |= reg << 4;
1557 /* Skip whitespace. */
1558 while (isspace (* op_end))
1559 ++ op_end;
1561 if (* op_end == ',')
1563 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1565 if (reg != 1)
1566 as_bad (_("second operand must be 1"));
1568 else
1569 as_bad (_("second operand missing"));
1571 output = frag_more (2);
1572 break;
1574 case SIa:
1575 op_end = parse_reg (op_end + 1, & reg);
1576 inst |= reg;
1578 /* Skip whitespace. */
1579 while (isspace (* op_end))
1580 ++ op_end;
1582 if (* op_end == ',')
1584 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1586 if (reg == 0)
1587 as_bad (_("zero used as immediate value"));
1589 inst |= reg << 4;
1591 else
1592 as_bad (_("second operand missing"));
1594 output = frag_more (2);
1595 break;
1597 default:
1598 as_bad (_("unimplemented opcode \"%s\""), name);
1601 output[0] = INST_BYTE0 (inst);
1602 output[1] = INST_BYTE1 (inst);
1604 check_literals (opcode->transfer, isize);
1607 symbolS *
1608 md_undefined_symbol (name)
1609 char * name;
1611 return 0;
1614 void
1615 md_mcore_end ()
1617 dump_literals (0);
1618 subseg_set (text_section, 0);
1621 /* Various routines to kill one day. */
1622 /* Equal to MAX_PRECISION in atof-ieee.c */
1623 #define MAX_LITTLENUMS 6
1625 /* Turn a string in input_line_pointer into a floating point constant of type
1626 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1627 emitted is stored in *sizeP. An error message is returned, or NULL on OK.*/
1628 char *
1629 md_atof (type, litP, sizeP)
1630 int type;
1631 char * litP;
1632 int * sizeP;
1634 int prec;
1635 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1636 int i;
1637 char * t;
1638 char * atof_ieee ();
1640 switch (type)
1642 case 'f':
1643 case 'F':
1644 case 's':
1645 case 'S':
1646 prec = 2;
1647 break;
1649 case 'd':
1650 case 'D':
1651 case 'r':
1652 case 'R':
1653 prec = 4;
1654 break;
1656 case 'x':
1657 case 'X':
1658 prec = 6;
1659 break;
1661 case 'p':
1662 case 'P':
1663 prec = 6;
1664 break;
1666 default:
1667 *sizeP = 0;
1668 return _("Bad call to MD_NTOF()");
1671 t = atof_ieee (input_line_pointer, type, words);
1673 if (t)
1674 input_line_pointer = t;
1676 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1678 for (i = 0; i < prec; i++)
1680 md_number_to_chars (litP, (valueT) words[i],
1681 sizeof (LITTLENUM_TYPE));
1682 litP += sizeof (LITTLENUM_TYPE);
1685 return 0;
1688 CONST char * md_shortopts = "";
1690 #define OPTION_JSRI2BSR_ON (OPTION_MD_BASE + 0)
1691 #define OPTION_JSRI2BSR_OFF (OPTION_MD_BASE + 1)
1692 #define OPTION_SIFILTER_ON (OPTION_MD_BASE + 2)
1693 #define OPTION_SIFILTER_OFF (OPTION_MD_BASE + 3)
1695 struct option md_longopts[] =
1697 { "no-jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_OFF},
1698 { "jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_ON},
1699 { "sifilter", no_argument, NULL, OPTION_SIFILTER_ON},
1700 { "no-sifilter", no_argument, NULL, OPTION_SIFILTER_OFF},
1701 { NULL, no_argument, NULL, 0}
1704 size_t md_longopts_size = sizeof (md_longopts);
1707 md_parse_option (c, arg)
1708 int c;
1709 char * arg;
1711 int i;
1712 char * p;
1714 switch (c)
1717 case OPTION_JSRI2BSR_ON: do_jsri2bsr = 1; break;
1718 case OPTION_JSRI2BSR_OFF: do_jsri2bsr = 0; break;
1719 case OPTION_SIFILTER_ON: sifilter_mode = 1; break;
1720 case OPTION_SIFILTER_OFF: sifilter_mode = 0; break;
1721 default: return 0;
1724 return 1;
1727 void
1728 md_show_usage (stream)
1729 FILE * stream;
1731 fprintf (stream, _("\
1732 MCORE specific options:\n\
1733 -{no-}jsri2bsr {dis}able jsri to bsr transformation (def: dis)\n\
1734 -{no-}sifilter {dis}able silicon filter behavior (def: dis)"));
1737 int md_short_jump_size;
1739 void
1740 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1741 char * ptr;
1742 addressT from_Nddr;
1743 addressT to_Nddr;
1744 fragS * frag;
1745 symbolS * to_symbol;
1747 as_fatal (_("failed sanity check: short_jump"));
1750 void
1751 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1752 char * ptr;
1753 addressT from_Nddr;
1754 addressT to_Nddr;
1755 fragS * frag;
1756 symbolS * to_symbol;
1758 as_fatal (_("failed sanity check: long_jump"));
1761 /* Called after relaxing, change the frags so they know how big they are. */
1762 void
1763 md_convert_frag (abfd, sec, fragP)
1764 bfd * abfd;
1765 segT sec;
1766 register fragS * fragP;
1768 unsigned char * buffer;
1769 int targ_addr = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
1771 buffer = (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1772 targ_addr += symbol_get_frag (fragP->fr_symbol)->fr_address;
1774 switch (fragP->fr_subtype)
1776 case C (COND_JUMP, COND12):
1777 case C (UNCD_JUMP, UNCD12):
1779 /* Get the address of the end of the instruction. */
1780 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
1781 unsigned char t0;
1782 int disp = targ_addr - next_inst;
1784 if (disp & 1)
1785 as_bad (_("odd displacement at %x"), next_inst - 2);
1787 disp >>= 1;
1789 t0 = buffer[0] & 0xF8;
1791 md_number_to_chars (buffer, disp, 2);
1793 buffer[0] = (buffer[0] & 0x07) | t0;
1796 fragP->fr_fix += 2;
1797 fragP->fr_var = 0;
1799 break;
1801 case C (COND_JUMP, COND32):
1802 case C (COND_JUMP, UNDEF_WORD_DISP):
1804 /* A conditional branch wont fit into 12 bits so:
1805 * b!cond 1f
1806 * jmpi 0f
1807 * .align 2
1808 * 0: .long disp
1809 * 1:
1811 * if the b!cond is 4 byte aligned, the literal which would
1812 * go at x+4 will also be aligned.
1814 int first_inst = fragP->fr_fix + fragP->fr_address;
1815 int needpad = (first_inst & 3);
1817 buffer[0] ^= 0x08; /* Toggle T/F bit */
1819 buffer[2] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi */
1820 buffer[3] = INST_BYTE1 (MCORE_INST_JMPI);
1822 if (needpad)
1825 buffer[1] = 4; /* branch over jmpi, pad, and ptr */
1826 buffer[3] = 1; /* jmpi offset of 1 gets the pointer */
1829 buffer[4] = 0; /* alignment/pad */
1830 buffer[5] = 0;
1831 buffer[6] = 0; /* space for 32 bit address */
1832 buffer[7] = 0;
1833 buffer[8] = 0;
1834 buffer[9] = 0;
1836 /* Make reloc for the long disp */
1837 fix_new (fragP, fragP->fr_fix + 6, 4,
1838 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1840 fragP->fr_fix += C32_LEN;
1842 else
1844 /* See comment below about this given gas' limitations for
1845 shrinking the fragment. '3' is the amount of code that
1846 we inserted here, but '4' is right for the space we reserved
1847 for this fragment. */
1849 buffer[1] = 3; /* branch over jmpi, and ptr */
1850 buffer[3] = 0; /* jmpi offset of 0 gets the pointer */
1853 buffer[4] = 0; /* space for 32 bit address */
1854 buffer[5] = 0;
1855 buffer[6] = 0;
1856 buffer[7] = 0;
1858 /* Make reloc for the long disp. */
1859 fix_new (fragP, fragP->fr_fix + 4, 4,
1860 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1861 fragP->fr_fix += C32_LEN;
1863 /* Frag is actually shorter (see the other side of this ifdef)
1864 but gas isn't prepared for that. We have to re-adjust
1865 the branch displacement so that it goes beyond the
1866 full length of the fragment, not just what we actually
1867 filled in. */
1868 buffer[1] = 4; /* jmpi, ptr, and the 'tail pad' */
1871 fragP->fr_var = 0;
1873 break;
1875 case C (UNCD_JUMP, UNCD32):
1876 case C (UNCD_JUMP, UNDEF_WORD_DISP):
1878 /* An unconditional branch will not fit in 12 bits, make code which
1879 looks like:
1880 jmpi 0f
1881 .align 2
1882 0: .long disp
1883 we need a pad if "first_inst" is 4 byte aligned.
1884 [because the natural literal place is x + 2] */
1885 int first_inst = fragP->fr_fix + fragP->fr_address;
1886 int needpad = !(first_inst & 3);
1888 buffer[0] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi */
1889 buffer[1] = INST_BYTE1 (MCORE_INST_JMPI);
1891 if (needpad)
1893 buffer[1] = 1; /* jmpi offset of 1 since padded */
1894 buffer[2] = 0; /* alignment */
1895 buffer[3] = 0;
1896 buffer[4] = 0; /* space for 32 bit address */
1897 buffer[5] = 0;
1898 buffer[6] = 0;
1899 buffer[7] = 0;
1901 /* Make reloc for the long disp. */
1902 fix_new (fragP, fragP->fr_fix + 4, 4,
1903 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1905 fragP->fr_fix += U32_LEN;
1907 else
1909 buffer[1] = 0; /* jmpi offset of 0 if no pad */
1910 buffer[2] = 0; /* space for 32 bit address */
1911 buffer[3] = 0;
1912 buffer[4] = 0;
1913 buffer[5] = 0;
1915 /* Make reloc for the long disp. */
1916 fix_new (fragP, fragP->fr_fix + 2, 4,
1917 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1918 fragP->fr_fix += U32_LEN;
1921 fragP->fr_var = 0;
1923 break;
1925 default:
1926 abort ();
1930 /* Applies the desired value to the specified location.
1931 Also sets up addends for 'rela' type relocations. */
1933 md_apply_fix3 (fixP, valp, segment)
1934 fixS * fixP;
1935 valueT * valp;
1936 segT segment;
1938 char * buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1939 char * file = fixP->fx_file ? fixP->fx_file : _("unknown");
1940 const char * symname;
1941 /* Note: use offsetT because it is signed, valueT is unsigned. */
1942 offsetT val = (offsetT) * valp;
1944 symname = fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : _("<unknown>");
1945 /* Save this for the addend in the relocation record. */
1946 fixP->fx_addnumber = val;
1948 /* If the fix is relative to a symbol which is not defined, or not
1949 in the same segment as the fix, we cannot resolve it here. */
1950 if (fixP->fx_addsy != NULL
1951 && ( ! S_IS_DEFINED (fixP->fx_addsy)
1952 || (S_GET_SEGMENT (fixP->fx_addsy) != segment)))
1954 fixP->fx_done = 0;
1955 #ifdef OBJ_ELF
1956 /* For ELF we can just return and let the reloc that will be generated
1957 take care of everything. For COFF we still have to insert 'val'
1958 into the insn since the addend field will be ignored. */
1959 return 0;
1960 #endif
1962 else
1963 fixP->fx_done = 1;
1965 switch (fixP->fx_r_type)
1967 case BFD_RELOC_MCORE_PCREL_IMM11BY2: /* second byte of 2 byte opcode */
1968 if ((val & 1) != 0)
1969 as_bad_where (file, fixP->fx_line,
1970 _("odd distance branch (0x%x bytes)"), val);
1971 val /= 2;
1972 if (((val & ~0x3ff) != 0) && ((val | 0x3ff) != -1))
1973 as_bad_where (file, fixP->fx_line,
1974 _("pcrel for branch to %s too far (0x%x)"),
1975 symname, val);
1976 buf[0] |= ((val >> 8) & 0x7);
1977 buf[1] |= (val & 0xff);
1978 break;
1980 case BFD_RELOC_MCORE_PCREL_IMM8BY4: /* lower 8 bits of 2 byte opcode */
1981 val += 3;
1982 val /= 4;
1983 if (val & ~0xff)
1984 as_bad_where (file, fixP->fx_line,
1985 _("pcrel for lrw/jmpi/jsri to %s too far (0x%x)"),
1986 symname, val);
1987 else
1988 buf[1] |= (val & 0xff);
1989 break;
1991 case BFD_RELOC_MCORE_PCREL_IMM4BY2: /* loopt instruction */
1992 if ((val < -32) || (val > -2))
1993 as_bad_where (file, fixP->fx_line,
1994 _("pcrel for loopt too far (0x%x)"), val);
1995 val /= 2;
1996 buf[1] |= (val & 0xf);
1997 break;
1999 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2000 /* Conditional linker map jsri to bsr. */
2001 /* If its a local target and close enough, fix it.
2002 NB: >= -2k for backwards bsr; < 2k for forwards... */
2003 if (fixP->fx_addsy == 0 && val >= -2048 && val < 2048)
2005 long nval = (val / 2) & 0x7ff;
2006 nval |= MCORE_INST_BSR;
2008 /* REPLACE the instruction, don't just modify it. */
2009 buf[0] = INST_BYTE0 (nval);
2010 buf[1] = INST_BYTE1 (nval);
2012 else
2013 fixP->fx_done = 0;
2014 break;
2016 case BFD_RELOC_MCORE_PCREL_32:
2017 case BFD_RELOC_VTABLE_INHERIT:
2018 case BFD_RELOC_VTABLE_ENTRY:
2019 fixP->fx_done = 0;
2020 break;
2022 default:
2023 if (fixP->fx_addsy != NULL)
2025 /* If the fix is an absolute reloc based on a symbol's
2026 address, then it cannot be resolved until the final link. */
2027 fixP->fx_done = 0;
2029 #ifdef OBJ_ELF
2030 else
2031 #endif
2033 if (fixP->fx_size == 4)
2035 else if (fixP->fx_size == 2 && val >= -32768 && val <= 32767)
2037 else if (fixP->fx_size == 1 && val >= -256 && val <= 255)
2039 else
2040 abort ();
2041 md_number_to_chars (buf, val, fixP->fx_size);
2043 break;
2046 return 0; /* Return value is ignored. */
2049 void
2050 md_operand (expressionP)
2051 expressionS * expressionP;
2053 /* Ignore leading hash symbol, if poresent. */
2054 if (* input_line_pointer == '#')
2056 input_line_pointer ++;
2057 expression (expressionP);
2061 int md_long_jump_size;
2063 /* Called just before address relaxation, return the length
2064 by which a fragment must grow to reach it's destination. */
2066 md_estimate_size_before_relax (fragP, segment_type)
2067 register fragS * fragP;
2068 register segT segment_type;
2070 switch (fragP->fr_subtype)
2072 case C (UNCD_JUMP, UNDEF_DISP):
2073 /* Used to be a branch to somewhere which was unknown. */
2074 if (!fragP->fr_symbol)
2076 fragP->fr_subtype = C (UNCD_JUMP, UNCD12);
2077 fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length;
2079 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2081 fragP->fr_subtype = C (UNCD_JUMP, UNCD12);
2082 fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length;
2084 else
2086 fragP->fr_subtype = C (UNCD_JUMP, UNDEF_WORD_DISP);
2087 fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length;
2088 return md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length;
2090 break;
2092 default:
2093 abort ();
2095 case C (COND_JUMP, UNDEF_DISP):
2096 /* Used to be a branch to somewhere which was unknown. */
2097 if (fragP->fr_symbol
2098 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2100 /* Got a symbol and it's defined in this segment, become byte
2101 sized - maybe it will fix up */
2102 fragP->fr_subtype = C (COND_JUMP, COND12);
2103 fragP->fr_var = md_relax_table[C (COND_JUMP, COND12)].rlx_length;
2105 else if (fragP->fr_symbol)
2107 /* Its got a segment, but its not ours, so it will always be long. */
2108 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
2109 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
2110 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
2112 else
2114 /* We know the abs value. */
2115 fragP->fr_subtype = C (COND_JUMP, COND12);
2116 fragP->fr_var = md_relax_table[C (COND_JUMP, COND12)].rlx_length;
2119 break;
2122 return fragP->fr_var;
2125 /* Put number into target byte order. */
2126 void
2127 md_number_to_chars (ptr, use, nbytes)
2128 char * ptr;
2129 valueT use;
2130 int nbytes;
2132 switch (nbytes)
2134 case 4: *ptr++ = (use >> 24) & 0xff; /* fall through */
2135 case 3: *ptr++ = (use >> 16) & 0xff; /* fall through */
2136 case 2: *ptr++ = (use >> 8) & 0xff; /* fall through */
2137 case 1: *ptr++ = (use >> 0) & 0xff; break;
2138 default: abort ();
2142 /* Round up a section size to the appropriate boundary. */
2143 valueT
2144 md_section_align (segment, size)
2145 segT segment;
2146 valueT size;
2148 return size; /* Byte alignment is fine */
2152 /* The location from which a PC relative jump should be calculated,
2153 given a PC relative reloc. */
2154 long
2155 md_pcrel_from_section (fixp, sec)
2156 fixS * fixp;
2157 segT sec;
2159 #ifdef OBJ_ELF
2160 /* If the symbol is undefined or defined in another section
2161 we leave the add number alone for the linker to fix it later.
2162 Only account for the PC pre-bump (which is 2 bytes on the MCore). */
2163 if (fixp->fx_addsy != (symbolS *) NULL
2164 && (! S_IS_DEFINED (fixp->fx_addsy)
2165 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
2168 assert (fixp->fx_size == 2); /* must be an insn */
2169 return fixp->fx_size;
2171 #endif
2173 /* The case where we are going to resolve things... */
2174 return fixp->fx_size + fixp->fx_where + fixp->fx_frag->fr_address;
2177 #define F(SZ,PCREL) (((SZ) << 1) + (PCREL))
2178 #define MAP(SZ,PCREL,TYPE) case F (SZ, PCREL): code = (TYPE); break
2180 arelent *
2181 tc_gen_reloc (section, fixp)
2182 asection * section;
2183 fixS * fixp;
2185 arelent * rel;
2186 bfd_reloc_code_real_type code;
2187 int handled = 0;
2189 switch (fixp->fx_r_type)
2191 /* These confuse the size/pcrel macro approach. */
2192 case BFD_RELOC_VTABLE_INHERIT:
2193 case BFD_RELOC_VTABLE_ENTRY:
2194 case BFD_RELOC_MCORE_PCREL_IMM4BY2:
2195 case BFD_RELOC_MCORE_PCREL_IMM8BY4:
2196 case BFD_RELOC_MCORE_PCREL_IMM11BY2:
2197 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2198 code = fixp->fx_r_type;
2199 break;
2201 default:
2202 switch (F (fixp->fx_size, fixp->fx_pcrel))
2204 MAP (1, 0, BFD_RELOC_8);
2205 MAP (2, 0, BFD_RELOC_16);
2206 MAP (4, 0, BFD_RELOC_32);
2207 MAP (1, 1, BFD_RELOC_8_PCREL);
2208 MAP (2, 1, BFD_RELOC_16_PCREL);
2209 MAP (4, 1, BFD_RELOC_32_PCREL);
2210 default:
2211 code = fixp->fx_r_type;
2212 as_bad (_("Can not do %d byte %srelocation"),
2213 fixp->fx_size,
2214 fixp->fx_pcrel ? _("pc-relative") : "");
2216 break;
2219 rel = (arelent *) xmalloc (sizeof (arelent));
2220 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2221 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2222 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2223 /* Always pass the addend along! */
2224 rel->addend = fixp->fx_addnumber;
2226 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
2228 if (rel->howto == NULL)
2230 as_bad_where (fixp->fx_file, fixp->fx_line,
2231 _("Cannot represent relocation type %s"),
2232 bfd_get_reloc_code_name (code));
2234 /* Set howto to a garbage value so that we can keep going. */
2235 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2236 assert (rel->howto != NULL);
2239 return rel;
2242 #ifdef OBJ_ELF
2243 /* See whether we need to force a relocation into the output file.
2244 This is used to force out switch and PC relative relocations when
2245 relaxing. */
2247 mcore_force_relocation (fix)
2248 fixS * fix;
2250 if ( fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2251 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2252 return 1;
2254 return 0;
2257 /* Return true if the fix can be handled by GAS, false if it must
2258 be passed through to the linker. */
2259 boolean
2260 mcore_fix_adjustable (fixP)
2261 fixS * fixP;
2263 if (fixP->fx_addsy == NULL)
2264 return 1;
2266 /* We need the symbol name for the VTABLE entries. */
2267 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2268 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2269 return 0;
2271 return 1;
2273 #endif /* OBJ_ELF */