file gasp.info was initially added on branch binutils-2_10-branch.
[binutils.git] / gas / config / tc-mcore.c
blob3b232148cf7cfd4902f8dbf2877d31f185740c9c
1 /* tc-mcore.c -- Assemble code for M*Core
2 Copyright (C) 1999, 2000 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 char * parse_rt PARAMS ((char *, char **, int, expressionS *));
49 static char * parse_imm PARAMS ((char *, unsigned *, unsigned, unsigned));
50 static char * parse_mem PARAMS ((char *, unsigned *, unsigned *, unsigned));
51 static char * parse_psrmod PARAMS ((char *, unsigned *));
52 static void make_name PARAMS ((char *, char *, int));
53 static int enter_literal PARAMS ((expressionS *, int));
54 static void dump_literals PARAMS ((int));
55 static void check_literals PARAMS ((int, int));
56 static void mcore_s_text PARAMS ((int));
57 static void mcore_s_data PARAMS ((int));
58 static void mcore_s_section PARAMS ((int));
59 static void mcore_s_bss PARAMS ((int));
60 #ifdef OBJ_ELF
61 static void mcore_s_comm PARAMS ((int));
62 #endif
64 /* Several places in this file insert raw instructions into the
65 object. They should use MCORE_INST_XXX macros to get the opcodes
66 and then use these two macros to crack the MCORE_INST value into
67 the appropriate byte values. */
68 #define INST_BYTE0(x) (target_big_endian ? (((x) >> 8) & 0xFF) : ((x) & 0xFF))
69 #define INST_BYTE1(x) (target_big_endian ? ((x) & 0xFF) : (((x) >> 8) & 0xFF))
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 */
107 typedef enum
109 M210,
110 M340
112 cpu_type;
114 cpu_type cpu = M340;
116 /* Initialize the relax table. */
117 const relax_typeS md_relax_table[] =
119 { 1, 1, 0, 0 }, /* 0: unused */
120 { 1, 1, 0, 0 }, /* 1: unused */
121 { 1, 1, 0, 0 }, /* 2: unused */
122 { 1, 1, 0, 0 }, /* 3: unused */
123 { 1, 1, 0, 0 }, /* 4: unused */
124 { 2048, -2046, C12_LEN, C(COND_JUMP, COND32) }, /* 5: C(COND_JUMP, COND12) */
125 { 0, 0, C32_LEN, 0 }, /* 6: C(COND_JUMP, COND32) */
126 { 1, 1, 0, 0 }, /* 7: unused */
127 { 1, 1, 0, 0 }, /* 8: unused */
128 { 2048, -2046, U12_LEN, C(UNCD_JUMP, UNCD32) }, /* 9: C(UNCD_JUMP, UNCD12) */
129 { 0, 0, U32_LEN, 0 }, /*10: C(UNCD_JUMP, UNCD32) */
130 { 1, 1, 0, 0 }, /*11: unused */
131 { 0, 0, 0, 0 } /*12: unused */
134 /* Literal pool data structures. */
135 struct literal
137 unsigned short refcnt;
138 unsigned char ispcrel;
139 unsigned char unused;
140 expressionS e;
143 #define MAX_POOL_SIZE (1024/4)
144 static struct literal litpool [MAX_POOL_SIZE];
145 static unsigned poolsize;
146 static unsigned poolnumber;
147 static unsigned long poolspan;
149 /* SPANPANIC: the point at which we get too scared and force a dump
150 of the literal pool, and perhaps put a branch in place.
151 Calculated as:
152 1024 span of lrw/jmpi/jsri insn (actually span+1)
153 -2 possible alignment at the insn.
154 -2 possible alignment to get the table aligned.
155 -2 an inserted branch around the table.
156 == 1018
157 at 1018, we might be in trouble.
158 -- so we have to be smaller than 1018 and since we deal with 2-byte
159 instructions, the next good choice is 1016.
160 -- Note we have a test case that fails when we've got 1018 here. */
161 #define SPANPANIC (1016) /* 1024 - 1 entry - 2 byte rounding. */
162 #define SPANCLOSE (900)
163 #define SPANEXIT (600)
164 static symbolS * poolsym; /* label for current pool. */
165 static char poolname[8];
166 static struct hash_control * opcode_hash_control; /* Opcode mnemonics. */
168 /* This table describes all the machine specific pseudo-ops the assembler
169 has to support. The fields are:
170 Pseudo-op name without dot
171 Function to call to execute this pseudo-op
172 Integer arg to pass to the function. */
173 const pseudo_typeS md_pseudo_table[] =
175 { "export", s_globl, 0 },
176 { "import", s_ignore, 0 },
177 { "literals", mcore_s_literals, 0 },
178 { "page", listing_eject, 0 },
180 /* The following are to intercept the placement of data into the text
181 section (eg addresses for a switch table), so that the space they
182 occupy can be taken into account when deciding whether or not to
183 dump the current literal pool.
184 XXX - currently we do not cope with the .space and .dcb.d directives. */
185 { "ascii", mcore_stringer, 0 },
186 { "asciz", mcore_stringer, 1 },
187 { "byte", mcore_cons, 1 },
188 { "dc", mcore_cons, 2 },
189 { "dc.b", mcore_cons, 1 },
190 { "dc.d", mcore_float_cons, 'd'},
191 { "dc.l", mcore_cons, 4 },
192 { "dc.s", mcore_float_cons, 'f'},
193 { "dc.w", mcore_cons, 2 },
194 { "dc.x", mcore_float_cons, 'x'},
195 { "double", mcore_float_cons, 'd'},
196 { "float", mcore_float_cons, 'f'},
197 { "hword", mcore_cons, 2 },
198 { "int", mcore_cons, 4 },
199 { "long", mcore_cons, 4 },
200 { "octa", mcore_cons, 16 },
201 { "quad", mcore_cons, 8 },
202 { "short", mcore_cons, 2 },
203 { "single", mcore_float_cons, 'f'},
204 { "string", mcore_stringer, 1 },
205 { "word", mcore_cons, 2 },
206 { "fill", mcore_fill, 0 },
208 /* Allow for the effect of section changes. */
209 { "text", mcore_s_text, 0 },
210 { "data", mcore_s_data, 0 },
211 { "bss", mcore_s_bss, 1 },
212 #ifdef OBJ_EF
213 { "comm", mcore_s_comm, 0 },
214 #endif
215 { "section", mcore_s_section, 0 },
216 { "section.s", mcore_s_section, 0 },
217 { "sect", mcore_s_section, 0 },
218 { "sect.s", mcore_s_section, 0 },
220 { 0, 0, 0 }
223 static void
224 mcore_s_literals (ignore)
225 int ignore;
227 dump_literals (0);
228 demand_empty_rest_of_line ();
232 static void
233 mcore_cons (nbytes)
234 int nbytes;
236 if (now_seg == text_section)
238 char * ptr = input_line_pointer;
239 int commas = 1;
241 /* Count the number of commas on the line. */
242 while (! is_end_of_line [* ptr])
243 commas += * ptr ++ == ',';
245 poolspan += nbytes * commas;
248 cons (nbytes);
250 /* In theory we ought to call check_literals (2,0) here in case
251 we need to dump the literal table. We cannot do this however,
252 as the directives that we are intercepting may be being used
253 to build a switch table, and we must not interfere with its
254 contents. Instead we cross our fingers and pray... */
257 static void
258 mcore_float_cons (float_type)
259 int float_type;
261 if (now_seg == text_section)
263 char * ptr = input_line_pointer;
264 int commas = 1;
266 #ifdef REPEAT_CONS_EXPRESSIONS
267 #error REPEAT_CONS_EXPRESSIONS not handled
268 #endif
270 /* Count the number of commas on the line. */
271 while (! is_end_of_line [* ptr])
272 commas += * ptr ++ == ',';
274 /* We would like to compute "hex_float (float_type) * commas"
275 but hex_float is not exported from read.c */
276 float_type == 'f' ? 4 : (float_type == 'd' ? 8 : 12);
277 poolspan += float_type * commas;
280 float_cons (float_type);
282 /* See the comment in mcore_cons () about calling check_literals.
283 It is unlikely that a switch table will be constructed using
284 floating point values, but it is still likely that an indexed
285 table of floating point constants is being created by these
286 directives, so again we must not interfere with their placement. */
289 static void
290 mcore_stringer (append_zero)
291 int append_zero;
293 if (now_seg == text_section)
295 char * ptr = input_line_pointer;
297 /* In theory we should compute how many bytes are going to
298 be occupied by the string(s) and add this to the poolspan.
299 To keep things simple however, we just add the number of
300 bytes left on the current line. This will be an over-
301 estimate, which is OK, and automatically allows for the
302 appending a zero byte, since the real string(s) is/are
303 required to be enclosed in double quotes. */
304 while (! is_end_of_line [* ptr])
305 ptr ++;
307 poolspan += ptr - input_line_pointer;
310 stringer (append_zero);
312 /* We call check_literals here in case a large number of strings are
313 being placed into the text section with a sequence of stringer
314 directives. In theory we could be upsetting something if these
315 strings are actually in an indexed table instead of referenced by
316 individual labels. Let us hope that that never happens. */
317 check_literals (2, 0);
320 static void
321 mcore_fill (unused)
322 int unused;
324 if (now_seg == text_section)
326 char * str = input_line_pointer;
327 int size = 1;
328 int repeat;
330 repeat = atoi (str);
332 /* Look to see if a size has been specified. */
333 while (*str != '\n' && *str != 0 && *str != ',')
334 ++ str;
336 if (* str == ',')
338 size = atoi (str + 1);
340 if (size > 8)
341 size = 8;
342 else if (size < 0)
343 size = 0;
346 poolspan += size * repeat;
349 s_fill (unused);
351 check_literals (2, 0);
354 /* Handle the section changing pseudo-ops. These call through to the
355 normal implementations, but they dump the literal pool first. */
356 static void
357 mcore_s_text (ignore)
358 int ignore;
360 dump_literals (0);
362 #ifdef OBJ_ELF
363 obj_elf_text (ignore);
364 #else
365 s_text (ignore);
366 #endif
369 static void
370 mcore_s_data (ignore)
371 int ignore;
373 dump_literals (0);
375 #ifdef OBJ_ELF
376 obj_elf_data (ignore);
377 #else
378 s_data (ignore);
379 #endif
382 static void
383 mcore_s_section (ignore)
384 int ignore;
386 /* Scan forwards to find the name of the section. If the section
387 being switched to is ".line" then this is a DWARF1 debug section
388 which is arbitarily placed inside generated code. In this case
389 do not dump the literal pool because it is a) inefficient and
390 b) would require the generation of extra code to jump around the
391 pool. */
392 char * ilp = input_line_pointer;
394 while (*ilp != 0 && isspace(*ilp))
395 ++ ilp;
397 if (strncmp (ilp, ".line", 5) == 0
398 && (isspace (ilp[5]) || *ilp == '\n' || *ilp == '\r'))
400 else
401 dump_literals (0);
403 #ifdef OBJ_ELF
404 obj_elf_section (ignore);
405 #endif
406 #ifdef OBJ_COFF
407 obj_coff_section (ignore);
408 #endif
411 static void
412 mcore_s_bss (needs_align)
413 int needs_align;
415 dump_literals (0);
417 s_lcomm_bytes (needs_align);
420 #ifdef OBJ_ELF
421 static void
422 mcore_s_comm (needs_align)
423 int needs_align;
425 dump_literals (0);
427 obj_elf_common (needs_align);
429 #endif
431 /* This function is called once, at assembler startup time. This should
432 set up all the tables, etc that the MD part of the assembler needs. */
433 void
434 md_begin ()
436 mcore_opcode_info * opcode;
437 char * prev_name = "";
439 opcode_hash_control = hash_new ();
441 /* Insert unique names into hash table */
442 for (opcode = mcore_table; opcode->name; opcode ++)
444 if (streq (prev_name, opcode->name))
446 /* Make all the opcodes with the same name point to the same
447 string. */
448 opcode->name = prev_name;
450 else
452 prev_name = opcode->name;
453 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
458 static int reg_m;
459 static int reg_n;
460 static expressionS immediate; /* absolute expression */
462 /* Get a log2(val). */
463 static int
464 log2 (val)
465 unsigned int val;
467 int log = -1;
468 while (val != 0)
470 log ++;
471 val >>= 1;
474 return log;
477 /* Try to parse a reg name. */
478 static char *
479 parse_reg (s, reg)
480 char * s;
481 unsigned * reg;
483 /* Strip leading whitespace. */
484 while (isspace (* s))
485 ++ s;
487 if (tolower (s[0]) == 'r')
489 if (s[1] == '1' && s[2] >= '0' && s[2] <= '5')
491 *reg = 10 + s[2] - '0';
492 return s + 3;
495 if (s[1] >= '0' && s[1] <= '9')
497 *reg = s[1] - '0';
498 return s + 2;
501 else if ( tolower (s[0]) == 's'
502 && tolower (s[1]) == 'p'
503 && ! isalnum (s[2]))
505 * reg = 0;
506 return s + 2;
509 as_bad (_("register expected, but saw '%.6s'"), s);
510 return s;
513 static struct Cregs
515 char * name;
516 unsigned int crnum;
518 cregs[] =
520 { "psr", 0},
521 { "vbr", 1},
522 { "epsr", 2},
523 { "fpsr", 3},
524 { "epc", 4},
525 { "fpc", 5},
526 { "ss0", 6},
527 { "ss1", 7},
528 { "ss2", 8},
529 { "ss3", 9},
530 { "ss4", 10},
531 { "gcr", 11},
532 { "gsr", 12},
533 { "", 0}
536 static char *
537 parse_creg (s, reg)
538 char * s;
539 unsigned * reg;
541 int i;
543 /* Strip leading whitespace. */
544 while (isspace (* s))
545 ++s;
547 if ((tolower (s[0]) == 'c' && tolower (s[1]) == 'r'))
549 if (s[2] == '3' && s[3] >= '0' && s[3] <= '1')
551 *reg = 30 + s[3] - '0';
552 return s + 4;
555 if (s[2] == '2' && s[3] >= '0' && s[3] <= '9')
557 *reg = 20 + s[3] - '0';
558 return s + 4;
561 if (s[2] == '1' && s[3] >= '0' && s[3] <= '9')
563 *reg = 10 + s[3] - '0';
564 return s + 4;
567 if (s[2] >= '0' && s[2] <= '9')
569 *reg = s[2] - '0';
570 return s + 3;
574 /* Look at alternate creg names before giving error. */
575 for (i = 0; cregs[i].name[0] != '\0'; i++)
577 char buf [10];
578 int length;
579 int j;
581 length = strlen (cregs[i].name);
583 for (j = 0; j < length; j++)
584 buf[j] = tolower (s[j]);
586 if (strncmp (cregs[i].name, buf, length) == 0)
588 *reg = cregs[i].crnum;
589 return s + length;
593 as_bad (_("control register expected, but saw '%.6s'"), s);
595 return s;
598 static char *
599 parse_psrmod (s, reg)
600 char * s;
601 unsigned * reg;
603 int i;
604 char buf[10];
605 static struct psrmods
607 char * name;
608 unsigned int value;
610 psrmods[] =
612 { "ie", 1 },
613 { "fe", 2 },
614 { "ee", 4 },
615 { "af", 8 } /* Really 0 and non-combinable. */
618 for (i = 0; i < 2; i++)
619 buf[i] = isascii (s[i]) ? tolower (s[i]) : 0;
621 for (i = sizeof (psrmods) / sizeof (psrmods[0]); i--;)
623 if (! strncmp (psrmods[i].name, buf, 2))
625 * reg = psrmods[i].value;
627 return s + 2;
631 as_bad (_("bad/missing psr specifier"));
633 * reg = 0;
635 return s;
638 static char *
639 parse_exp (s, e)
640 char * s;
641 expressionS * e;
643 char * save;
644 char * new;
646 /* Skip whitespace. */
647 while (isspace (* s))
648 ++ s;
650 save = input_line_pointer;
651 input_line_pointer = s;
653 expression (e);
655 if (e->X_op == O_absent)
656 as_bad (_("missing operand"));
658 new = input_line_pointer;
659 input_line_pointer = save;
661 return new;
664 static void
665 make_name (s, p, n)
666 char * s;
667 char * p;
668 int n;
670 static const char hex[] = "0123456789ABCDEF";
672 s[0] = p[0];
673 s[1] = p[1];
674 s[2] = p[2];
675 s[3] = hex[(n >> 12) & 0xF];
676 s[4] = hex[(n >> 8) & 0xF];
677 s[5] = hex[(n >> 4) & 0xF];
678 s[6] = hex[(n) & 0xF];
679 s[7] = 0;
682 #define POOL_END_LABEL ".LE"
683 #define POOL_START_LABEL ".LS"
685 static void
686 dump_literals (isforce)
687 int isforce;
689 int i;
690 struct literal * p;
691 symbolS * brarsym;
693 if (poolsize == 0)
694 return;
696 /* Must we branch around the literal table? */
697 if (isforce)
699 char * output;
700 char brarname[8];
702 make_name (brarname, POOL_END_LABEL, poolnumber);
704 brarsym = symbol_make (brarname);
706 symbol_table_insert (brarsym);
708 output = frag_var (rs_machine_dependent,
709 md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length,
710 md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length,
711 C (UNCD_JUMP, 0), brarsym, 0, 0);
712 output[0] = INST_BYTE0 (MCORE_INST_BR); /* br .+xxx */
713 output[1] = INST_BYTE1 (MCORE_INST_BR);
716 /* Make sure that the section is sufficiently aligned and that
717 the literal table is aligned within it. */
718 record_alignment (now_seg, 2);
719 frag_align (2, 0, 0);
721 colon (S_GET_NAME (poolsym));
723 for (i = 0, p = litpool; i < poolsize; i++, p++)
724 emit_expr (& p->e, 4);
726 if (isforce)
727 colon (S_GET_NAME (brarsym));
729 poolsize = 0;
732 static void
733 check_literals (kind, offset)
734 int kind;
735 int offset;
737 poolspan += offset;
739 /* SPANCLOSE and SPANEXIT are smaller numbers than SPANPANIC.
740 SPANPANIC means that we must dump now.
741 kind == 0 is any old instruction.
742 kind > 0 means we just had a control transfer instruction.
743 kind == 1 means within a function
744 kind == 2 means we just left a function
746 The dump_literals (1) call inserts a branch around the table, so
747 we first look to see if its a situation where we won't have to
748 insert a branch (e.g., the previous instruction was an unconditional
749 branch).
751 SPANPANIC is the point where we must dump a single-entry pool.
752 it accounts for alignments and an inserted branch.
753 the 'poolsize*2' accounts for the scenario where we do:
754 lrw r1,lit1; lrw r2,lit2; lrw r3,lit3
755 Note that the 'lit2' reference is 2 bytes further along
756 but the literal it references will be 4 bytes further along,
757 so we must consider the poolsize into this equation.
758 This is slightly over-cautious, but guarantees that we won't
759 panic because a relocation is too distant. */
761 if (poolspan > SPANCLOSE && kind > 0)
762 dump_literals (0);
763 else if (poolspan > SPANEXIT && kind > 1)
764 dump_literals (0);
765 else if (poolspan >= (SPANPANIC - poolsize * 2))
766 dump_literals (1);
769 static int
770 enter_literal (e, ispcrel)
771 expressionS * e;
772 int ispcrel;
774 int i;
775 struct literal * p;
777 if (poolsize >= MAX_POOL_SIZE - 2)
779 /* The literal pool is as full as we can handle. We have
780 to be 2 entries shy of the 1024/4=256 entries because we
781 have to allow for the branch (2 bytes) and the alignment
782 (2 bytes before the first insn referencing the pool and
783 2 bytes before the pool itself) == 6 bytes, rounds up
784 to 2 entries. */
785 dump_literals (1);
788 if (poolsize == 0)
790 /* Create new literal pool. */
791 if (++ poolnumber > 0xFFFF)
792 as_fatal (_("more than 65K literal pools"));
794 make_name (poolname, POOL_START_LABEL, poolnumber);
795 poolsym = symbol_make (poolname);
796 symbol_table_insert (poolsym);
797 poolspan = 0;
800 /* Search pool for value so we don't have duplicates. */
801 for (p = litpool, i = 0; i < poolsize; i++, p++)
803 if (e->X_op == p->e.X_op
804 && e->X_add_symbol == p->e.X_add_symbol
805 && e->X_add_number == p->e.X_add_number
806 && ispcrel == p->ispcrel)
808 p->refcnt ++;
809 return i;
813 p->refcnt = 1;
814 p->ispcrel = ispcrel;
815 p->e = * e;
817 poolsize ++;
819 return i;
822 /* Parse a literal specification. -- either new or old syntax.
823 old syntax: the user supplies the label and places the literal.
824 new syntax: we put it into the literal pool. */
825 static char *
826 parse_rt (s, outputp, ispcrel, ep)
827 char * s;
828 char ** outputp;
829 int ispcrel;
830 expressionS * ep;
832 expressionS e;
833 int n;
835 if (ep)
836 /* Indicate nothing there. */
837 ep->X_op = O_absent;
839 if (*s == '[')
841 s = parse_exp (s + 1, & e);
843 if (*s == ']')
844 s++;
845 else
846 as_bad (_("missing ']'"));
848 else
850 s = parse_exp (s, & e);
852 n = enter_literal (& e, ispcrel);
854 if (ep)
855 *ep = e;
857 /* Create a reference to pool entry. */
858 e.X_op = O_symbol;
859 e.X_add_symbol = poolsym;
860 e.X_add_number = n << 2;
863 * outputp = frag_more (2);
865 fix_new_exp (frag_now, (*outputp) - frag_now->fr_literal, 2, & e, 1,
866 BFD_RELOC_MCORE_PCREL_IMM8BY4);
868 return s;
871 static char *
872 parse_imm (s, val, min, max)
873 char * s;
874 unsigned * val;
875 unsigned min;
876 unsigned max;
878 char * new;
879 expressionS e;
881 new = parse_exp (s, & e);
883 if (e.X_op == O_absent)
884 ; /* An error message has already been emitted. */
885 else if (e.X_op != O_constant)
886 as_bad (_("operand must be a constant"));
887 else if (e.X_add_number < min || e.X_add_number > max)
888 as_bad (_("operand must be absolute in range %d..%d, not %d"),
889 min, max, e.X_add_number);
891 * val = e.X_add_number;
893 return new;
896 static char *
897 parse_mem (s, reg, off, siz)
898 char * s;
899 unsigned * reg;
900 unsigned * off;
901 unsigned siz;
903 char * new;
905 * off = 0;
907 while (isspace (* s))
908 ++ s;
910 if (* s == '(')
912 s = parse_reg (s + 1, reg);
914 while (isspace (* s))
915 ++ s;
917 if (* s == ',')
919 s = parse_imm (s + 1, off, 0, 63);
921 if (siz > 1)
923 if (siz > 2)
925 if (* off & 0x3)
926 as_bad (_("operand must be a multiple of 4"));
928 * off >>= 2;
930 else
932 if (* off & 0x1)
933 as_bad (_("operand must be a multiple of 2"));
935 * off >>= 1;
940 while (isspace (* s))
941 ++ s;
943 if (* s == ')')
944 s ++;
946 else
947 as_bad (_("base register expected"));
949 return s;
952 /* This is the guts of the machine-dependent assembler. STR points to a
953 machine dependent instruction. This function is supposed to emit
954 the frags/bytes it assembles to. */
956 void
957 md_assemble (str)
958 char * str;
960 char * op_start;
961 char * op_end;
962 mcore_opcode_info * opcode;
963 char * output;
964 int nlen = 0;
965 unsigned short inst;
966 unsigned reg;
967 unsigned off;
968 unsigned isize;
969 expressionS e;
970 char name[20];
972 /* Drop leading whitespace. */
973 while (isspace (* str))
974 str ++;
976 /* Find the op code end. */
977 for (op_start = op_end = str;
978 * op_end && nlen < 20 && !is_end_of_line [*op_end] && *op_end != ' ';
979 op_end++)
981 name[nlen] = op_start[nlen];
982 nlen++;
985 name [nlen] = 0;
987 if (nlen == 0)
989 as_bad (_("can't find opcode "));
990 return;
993 opcode = (mcore_opcode_info *) hash_find (opcode_hash_control, name);
994 if (opcode == NULL)
996 as_bad (_("unknown opcode \"%s\""), name);
997 return;
1000 inst = opcode->inst;
1001 isize = 2;
1003 switch (opcode->opclass)
1005 case O0:
1006 output = frag_more (2);
1007 break;
1009 case OT:
1010 op_end = parse_imm (op_end + 1, & reg, 0, 3);
1011 inst |= reg;
1012 output = frag_more (2);
1013 break;
1015 case O1:
1016 op_end = parse_reg (op_end + 1, & reg);
1017 inst |= reg;
1018 output = frag_more (2);
1019 break;
1021 case JMP:
1022 op_end = parse_reg (op_end + 1, & reg);
1023 inst |= reg;
1024 output = frag_more (2);
1025 /* In a sifilter mode, we emit this insn 2 times,
1026 fixes problem of an interrupt during a jmp.. */
1027 if (sifilter_mode)
1029 output[0] = INST_BYTE0 (inst);
1030 output[1] = INST_BYTE1 (inst);
1031 output = frag_more (2);
1033 break;
1035 case JSR:
1036 op_end = parse_reg (op_end + 1, & reg);
1038 if (reg == 15)
1039 as_bad (_("invalid register: r15 illegal"));
1041 inst |= reg;
1042 output = frag_more (2);
1044 if (sifilter_mode)
1046 /* Replace with: bsr .+2 ; addi r15,6; jmp rx ; jmp rx */
1047 inst = MCORE_INST_BSR; /* with 0 displacement */
1048 output[0] = INST_BYTE0 (inst);
1049 output[1] = INST_BYTE1 (inst);
1051 output = frag_more (2);
1052 inst = MCORE_INST_ADDI;
1053 inst |= 15; /* addi r15,6 */
1054 inst |= (6 - 1) << 4; /* over the jmp's */
1055 output[0] = INST_BYTE0 (inst);
1056 output[1] = INST_BYTE1 (inst);
1058 output = frag_more (2);
1059 inst = MCORE_INST_JMP | reg;
1060 output[0] = INST_BYTE0 (inst);
1061 output[1] = INST_BYTE1 (inst);
1063 output = frag_more (2); /* 2nd emitted in fallthru */
1065 break;
1067 case OC:
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_creg (op_end + 1, & reg);
1078 inst |= reg << 4;
1081 output = frag_more (2);
1082 break;
1084 case MULSH:
1085 if (cpu == M210)
1087 as_bad (_("M340 specific opcode used when assembling for M210"));
1088 break;
1090 /* drop through... */
1091 case O2:
1092 op_end = parse_reg (op_end + 1, & reg);
1093 inst |= reg;
1095 /* Skip whitespace. */
1096 while (isspace (* op_end))
1097 ++ op_end;
1099 if (* op_end == ',')
1101 op_end = parse_reg (op_end + 1, & reg);
1102 inst |= reg << 4;
1104 else
1105 as_bad (_("second operand missing"));
1107 output = frag_more (2);
1108 break;
1110 case X1: /* Handle both syntax-> xtrb- r1,rx OR xtrb- rx */
1111 op_end = parse_reg (op_end + 1, & reg);
1113 /* Skip whitespace. */
1114 while (isspace (* op_end))
1115 ++ op_end;
1117 if (* op_end == ',') /* xtrb- r1,rx */
1119 if (reg != 1)
1120 as_bad (_("destination register must be r1"));
1122 op_end = parse_reg (op_end + 1, & reg);
1125 inst |= reg;
1126 output = frag_more (2);
1127 break;
1129 case O1R1: /* div- rx,r1 */
1130 op_end = parse_reg (op_end + 1, & reg);
1131 inst |= reg;
1133 /* Skip whitespace. */
1134 while (isspace (* op_end))
1135 ++ op_end;
1137 if (* op_end == ',')
1139 op_end = parse_reg (op_end + 1, & reg);
1140 if (reg != 1)
1141 as_bad (_("source register must be r1"));
1143 else
1144 as_bad (_("second operand missing"));
1146 output = frag_more (2);
1147 break;
1149 case OI:
1150 op_end = parse_reg (op_end + 1, & reg);
1151 inst |= reg;
1153 /* Skip whitespace. */
1154 while (isspace (* op_end))
1155 ++ op_end;
1157 if (* op_end == ',')
1159 op_end = parse_imm (op_end + 1, & reg, 1, 32);
1160 inst |= (reg - 1) << 4;
1162 else
1163 as_bad (_("second operand missing"));
1165 output = frag_more (2);
1166 break;
1168 case OB:
1169 op_end = parse_reg (op_end + 1, & reg);
1170 inst |= reg;
1172 /* Skip whitespace. */
1173 while (isspace (* op_end))
1174 ++ op_end;
1176 if (* op_end == ',')
1178 op_end = parse_imm (op_end + 1, & reg, 0, 31);
1179 inst |= reg << 4;
1181 else
1182 as_bad (_("second operand missing"));
1184 output = frag_more (2);
1185 break;
1187 case OB2: /* like OB, but arg is 2^n instead of n */
1188 op_end = parse_reg (op_end + 1, & reg);
1189 inst |= reg;
1191 /* Skip whitespace. */
1192 while (isspace (* op_end))
1193 ++ op_end;
1195 if (* op_end == ',')
1197 op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1198 /* Further restrict the immediate to a power of two. */
1199 if ((reg & (reg - 1)) == 0)
1200 reg = log2 (reg);
1201 else
1203 reg = 0;
1204 as_bad (_("immediate is not a power of two"));
1206 inst |= (reg) << 4;
1208 else
1209 as_bad (_("second operand missing"));
1211 output = frag_more (2);
1212 break;
1214 case OBRa: /* Specific for bgeni: imm of 0->6 translate to movi. */
1215 case OBRb:
1216 case OBRc:
1217 op_end = parse_reg (op_end + 1, & reg);
1218 inst |= reg;
1220 /* Skip whitespace. */
1221 while (isspace (* op_end))
1222 ++ op_end;
1224 if (* op_end == ',')
1226 op_end = parse_imm (op_end + 1, & reg, 0, 31);
1227 /* immediate values of 0 -> 6 translate to movi */
1228 if (reg <= 6)
1230 inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1231 reg = 0x1 << reg;
1232 as_warn (_("translating bgeni to movi"));
1234 inst &= ~ 0x01f0;
1235 inst |= reg << 4;
1237 else
1238 as_bad (_("second operand missing"));
1240 output = frag_more (2);
1241 break;
1243 case OBR2: /* like OBR, but arg is 2^n instead of n */
1244 op_end = parse_reg (op_end + 1, & reg);
1245 inst |= reg;
1247 /* Skip whitespace. */
1248 while (isspace (* op_end))
1249 ++ op_end;
1251 if (* op_end == ',')
1253 op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31);
1255 /* Further restrict the immediate to a power of two. */
1256 if ((reg & (reg - 1)) == 0)
1257 reg = log2 (reg);
1258 else
1260 reg = 0;
1261 as_bad (_("immediate is not a power of two"));
1264 /* Immediate values of 0 -> 6 translate to movi. */
1265 if (reg <= 6)
1267 inst = (inst & 0xF) | MCORE_INST_BGENI_ALT;
1268 reg = 0x1 << reg;
1269 as_warn (_("translating mgeni to movi"));
1272 inst |= reg << 4;
1274 else
1275 as_bad (_("second operand missing"));
1277 output = frag_more (2);
1278 break;
1280 case OMa: /* Specific for bmaski: imm 1->7 translate to movi. */
1281 case OMb:
1282 case OMc:
1283 op_end = parse_reg (op_end + 1, & reg);
1284 inst |= reg;
1286 /* Skip whitespace. */
1287 while (isspace (* op_end))
1288 ++ op_end;
1290 if (* op_end == ',')
1292 op_end = parse_imm (op_end + 1, & reg, 1, 32);
1294 /* Immediate values of 1 -> 7 translate to movi. */
1295 if (reg <= 7)
1297 inst = (inst & 0xF) | MCORE_INST_BMASKI_ALT;
1298 reg = (0x1 << reg) - 1;
1299 inst |= reg << 4;
1301 as_warn (_("translating bmaski to movi"));
1303 else
1305 inst &= ~ 0x01F0;
1306 inst |= (reg & 0x1F) << 4;
1309 else
1310 as_bad (_("second operand missing"));
1312 output = frag_more (2);
1313 break;
1315 case SI:
1316 op_end = parse_reg (op_end + 1, & reg);
1317 inst |= reg;
1319 /* Skip whitespace. */
1320 while (isspace (* op_end))
1321 ++ op_end;
1323 if (* op_end == ',')
1325 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1326 inst |= reg << 4;
1328 else
1329 as_bad (_("second operand missing"));
1331 output = frag_more (2);
1332 break;
1334 case I7:
1335 op_end = parse_reg (op_end + 1, & reg);
1336 inst |= reg;
1338 /* Skip whitespace. */
1339 while (isspace (* op_end))
1340 ++ op_end;
1342 if (* op_end == ',')
1344 op_end = parse_imm (op_end + 1, & reg, 0, 0x7F);
1345 inst |= reg << 4;
1347 else
1348 as_bad (_("second operand missing"));
1350 output = frag_more (2);
1351 break;
1353 case LS:
1354 op_end = parse_reg (op_end + 1, & reg);
1355 inst |= reg << 8;
1357 /* Skip whitespace. */
1358 while (isspace (* op_end))
1359 ++ op_end;
1361 if (* op_end == ',')
1363 int size;
1365 if ((inst & 0x6000) == 0)
1366 size = 4;
1367 else if ((inst & 0x6000) == 0x4000)
1368 size = 2;
1369 else if ((inst & 0x6000) == 0x2000)
1370 size = 1;
1372 op_end = parse_mem (op_end + 1, & reg, & off, size);
1374 if (off > 16)
1375 as_bad (_("displacement too large (%d)"), off);
1376 else
1377 inst |= (reg) | (off << 4);
1379 else
1380 as_bad (_("second operand missing"));
1382 output = frag_more (2);
1383 break;
1385 case LR:
1386 op_end = parse_reg (op_end + 1, & reg);
1388 if (reg == 0 || reg == 15)
1389 as_bad (_("Invalid register: r0 and r15 illegal"));
1391 inst |= (reg << 8);
1393 /* Skip whitespace. */
1394 while (isspace (* op_end))
1395 ++ op_end;
1397 if (* op_end == ',')
1399 /* parse_rt calls frag_more() for us. */
1400 input_line_pointer = parse_rt (op_end + 1, & output, 0, 0);
1401 op_end = input_line_pointer;
1403 else
1405 as_bad (_("second operand missing"));
1406 output = frag_more (2); /* save its space */
1408 break;
1410 case LJ:
1411 input_line_pointer = parse_rt (op_end + 1, & output, 1, 0);
1412 /* parse_rt() calls frag_more() for us. */
1413 op_end = input_line_pointer;
1414 break;
1416 case RM:
1417 op_end = parse_reg (op_end + 1, & reg);
1419 if (reg == 0 || reg == 15)
1420 as_bad (_("bad starting register: r0 and r15 invalid"));
1422 inst |= reg;
1424 /* Skip whitespace. */
1425 while (isspace (* op_end))
1426 ++ op_end;
1428 if (* op_end == '-')
1430 op_end = parse_reg (op_end + 1, & reg);
1432 if (reg != 15)
1433 as_bad (_("ending register must be r15"));
1435 /* Skip whitespace. */
1436 while (isspace (* op_end))
1437 ++ op_end;
1440 if (* op_end == ',')
1442 op_end ++;
1444 /* Skip whitespace. */
1445 while (isspace (* op_end))
1446 ++ op_end;
1448 if (* op_end == '(')
1450 op_end = parse_reg (op_end + 1, & reg);
1452 if (reg != 0)
1453 as_bad (_("bad base register: must be r0"));
1455 if (* op_end == ')')
1456 op_end ++;
1458 else
1459 as_bad (_("base register expected"));
1461 else
1462 as_bad (_("second operand missing"));
1464 output = frag_more (2);
1465 break;
1467 case RQ:
1468 op_end = parse_reg (op_end + 1, & reg);
1470 if (reg != 4)
1471 as_fatal (_("first register must be r4"));
1473 /* Skip whitespace. */
1474 while (isspace (* op_end))
1475 ++ op_end;
1477 if (* op_end == '-')
1479 op_end = parse_reg (op_end + 1, & reg);
1481 if (reg != 7)
1482 as_fatal (_("last register must be r7"));
1484 /* Skip whitespace. */
1485 while (isspace (* op_end))
1486 ++ op_end;
1488 if (* op_end == ',')
1490 op_end ++;
1492 /* Skip whitespace. */
1493 while (isspace (* op_end))
1494 ++ op_end;
1496 if (* op_end == '(')
1498 op_end = parse_reg (op_end + 1, & reg);
1500 if (reg >= 4 && reg <= 7)
1501 as_fatal ("base register cannot be r4, r5, r6, or r7");
1503 inst |= reg;
1505 /* Skip whitespace. */
1506 while (isspace (* op_end))
1507 ++ op_end;
1509 if (* op_end == ')')
1510 op_end ++;
1512 else
1513 as_bad (_("base register expected"));
1515 else
1516 as_bad (_("second operand missing"));
1518 else
1519 as_bad (_("reg-reg expected"));
1521 output = frag_more (2);
1522 break;
1524 case BR:
1525 input_line_pointer = parse_exp (op_end + 1, & e);
1526 op_end = input_line_pointer;
1528 output = frag_more (2);
1530 fix_new_exp (frag_now, output-frag_now->fr_literal,
1531 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM11BY2);
1532 break;
1534 case BL:
1535 op_end = parse_reg (op_end + 1, & reg);
1536 inst |= reg << 4;
1538 /* Skip whitespace. */
1539 while (isspace (* op_end))
1540 ++ op_end;
1542 if (* op_end == ',')
1544 op_end = parse_exp (op_end + 1, & e);
1545 output = frag_more (2);
1547 fix_new_exp (frag_now, output-frag_now->fr_literal,
1548 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM4BY2);
1550 else
1552 as_bad (_("second operand missing"));
1553 output = frag_more (2);
1555 break;
1557 case JC:
1558 input_line_pointer = parse_exp (op_end + 1, & e);
1559 op_end = input_line_pointer;
1561 output = frag_var (rs_machine_dependent,
1562 md_relax_table[C (COND_JUMP, COND32)].rlx_length,
1563 md_relax_table[C (COND_JUMP, COND12)].rlx_length,
1564 C (COND_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1565 isize = C32_LEN;
1566 break;
1568 case JU:
1569 input_line_pointer = parse_exp (op_end + 1, & e);
1570 op_end = input_line_pointer;
1572 output = frag_var (rs_machine_dependent,
1573 md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length,
1574 md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length,
1575 C (UNCD_JUMP, 0), e.X_add_symbol, e.X_add_number, 0);
1576 isize = U32_LEN;
1577 break;
1579 case JL:
1580 inst = MCORE_INST_JSRI; /* jsri */
1581 input_line_pointer = parse_rt (op_end + 1, & output, 1, & e);
1582 /* parse_rt() calls frag_more for us. */
1583 op_end = input_line_pointer;
1585 /* Only do this if we know how to do it ... */
1586 if (e.X_op != O_absent && do_jsri2bsr)
1588 /* Look at adding the R_PCREL_JSRIMM11BY2. */
1589 fix_new_exp (frag_now, output-frag_now->fr_literal,
1590 2, & e, 1, BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2);
1592 break;
1594 case RSI: /* SI, but imm becomes 32-imm */
1595 op_end = parse_reg (op_end + 1, & reg);
1596 inst |= reg;
1598 /* Skip whitespace. */
1599 while (isspace (* op_end))
1600 ++ op_end;
1602 if (* op_end == ',')
1604 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1606 reg = 32 - reg;
1607 inst |= reg << 4;
1609 else
1610 as_bad (_("second operand missing"));
1612 output = frag_more (2);
1613 break;
1615 case DO21: /* O2, dup rd, lit must be 1 */
1616 op_end = parse_reg (op_end + 1, & reg);
1617 inst |= reg;
1618 inst |= reg << 4;
1620 /* Skip whitespace. */
1621 while (isspace (* op_end))
1622 ++ op_end;
1624 if (* op_end == ',')
1626 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1628 if (reg != 1)
1629 as_bad (_("second operand must be 1"));
1631 else
1632 as_bad (_("second operand missing"));
1634 output = frag_more (2);
1635 break;
1637 case SIa:
1638 op_end = parse_reg (op_end + 1, & reg);
1639 inst |= reg;
1641 /* Skip whitespace. */
1642 while (isspace (* op_end))
1643 ++ op_end;
1645 if (* op_end == ',')
1647 op_end = parse_imm (op_end + 1, & reg, 1, 31);
1649 if (reg == 0)
1650 as_bad (_("zero used as immediate value"));
1652 inst |= reg << 4;
1654 else
1655 as_bad (_("second operand missing"));
1657 output = frag_more (2);
1658 break;
1660 case OPSR:
1661 if (cpu == M210)
1663 as_bad (_("M340 specific opcode used when assembling for M210"));
1664 break;
1667 op_end = parse_psrmod (op_end + 1, & reg);
1669 /* Look for further selectors. */
1670 while (* op_end == ',')
1672 unsigned value;
1674 op_end = parse_psrmod (op_end + 1, & value);
1676 if (value & reg)
1677 as_bad (_("duplicated psr bit specifier"));
1679 reg |= value;
1682 if (reg > 8)
1683 as_bad (_("`af' must appear alone"));
1685 inst |= (reg & 0x7);
1686 output = frag_more (2);
1687 break;
1689 default:
1690 as_bad (_("unimplemented opcode \"%s\""), name);
1693 /* Drop whitespace after all the operands have been parsed. */
1694 while (isspace (* op_end))
1695 op_end ++;
1697 /* Give warning message if the insn has more operands than required. */
1698 if (strcmp (op_end, opcode->name) && strcmp (op_end, ""))
1699 as_warn (_("ignoring operands: %s "), op_end);
1701 output[0] = INST_BYTE0 (inst);
1702 output[1] = INST_BYTE1 (inst);
1704 check_literals (opcode->transfer, isize);
1707 symbolS *
1708 md_undefined_symbol (name)
1709 char * name;
1711 return 0;
1714 void
1715 md_mcore_end ()
1717 dump_literals (0);
1718 subseg_set (text_section, 0);
1721 /* Various routines to kill one day. */
1722 /* Equal to MAX_PRECISION in atof-ieee.c */
1723 #define MAX_LITTLENUMS 6
1725 /* Turn a string in input_line_pointer into a floating point constant of type
1726 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1727 emitted is stored in *sizeP. An error message is returned, or NULL on OK.*/
1728 char *
1729 md_atof (type, litP, sizeP)
1730 int type;
1731 char * litP;
1732 int * sizeP;
1734 int prec;
1735 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1736 int i;
1737 char * t;
1738 char * atof_ieee ();
1740 switch (type)
1742 case 'f':
1743 case 'F':
1744 case 's':
1745 case 'S':
1746 prec = 2;
1747 break;
1749 case 'd':
1750 case 'D':
1751 case 'r':
1752 case 'R':
1753 prec = 4;
1754 break;
1756 case 'x':
1757 case 'X':
1758 prec = 6;
1759 break;
1761 case 'p':
1762 case 'P':
1763 prec = 6;
1764 break;
1766 default:
1767 *sizeP = 0;
1768 return _("Bad call to MD_NTOF()");
1771 t = atof_ieee (input_line_pointer, type, words);
1773 if (t)
1774 input_line_pointer = t;
1776 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1778 if (! target_big_endian)
1780 for (i = prec - 1; i >= 0; i--)
1782 md_number_to_chars (litP, (valueT) words[i],
1783 sizeof (LITTLENUM_TYPE));
1784 litP += sizeof (LITTLENUM_TYPE);
1787 else
1788 for (i = 0; i < prec; i++)
1790 md_number_to_chars (litP, (valueT) words[i],
1791 sizeof (LITTLENUM_TYPE));
1792 litP += sizeof (LITTLENUM_TYPE);
1795 return 0;
1798 CONST char * md_shortopts = "";
1800 #define OPTION_JSRI2BSR_ON (OPTION_MD_BASE + 0)
1801 #define OPTION_JSRI2BSR_OFF (OPTION_MD_BASE + 1)
1802 #define OPTION_SIFILTER_ON (OPTION_MD_BASE + 2)
1803 #define OPTION_SIFILTER_OFF (OPTION_MD_BASE + 3)
1804 #define OPTION_CPU (OPTION_MD_BASE + 4)
1805 #define OPTION_EB (OPTION_MD_BASE + 5)
1806 #define OPTION_EL (OPTION_MD_BASE + 6)
1808 struct option md_longopts[] =
1810 { "no-jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_OFF},
1811 { "jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_ON},
1812 { "sifilter", no_argument, NULL, OPTION_SIFILTER_ON},
1813 { "no-sifilter", no_argument, NULL, OPTION_SIFILTER_OFF},
1814 { "cpu", required_argument, NULL, OPTION_CPU},
1815 { "EB", no_argument, NULL, OPTION_EB},
1816 { "EL", no_argument, NULL, OPTION_EL},
1817 { NULL, no_argument, NULL, 0}
1820 size_t md_longopts_size = sizeof (md_longopts);
1823 md_parse_option (c, arg)
1824 int c;
1825 char * arg;
1827 int i;
1828 char * p;
1830 switch (c)
1832 case OPTION_CPU:
1833 if (streq (arg, "210"))
1835 cpu = M210;
1836 target_big_endian = 1;
1838 else if (streq (arg, "340"))
1839 cpu = M340;
1840 else
1841 as_warn (_("unrecognised cpu type '%s'"), arg);
1842 break;
1844 case OPTION_EB: target_big_endian = 1; break;
1845 case OPTION_EL: target_big_endian = 0; cpu = M340; break;
1846 case OPTION_JSRI2BSR_ON: do_jsri2bsr = 1; break;
1847 case OPTION_JSRI2BSR_OFF: do_jsri2bsr = 0; break;
1848 case OPTION_SIFILTER_ON: sifilter_mode = 1; break;
1849 case OPTION_SIFILTER_OFF: sifilter_mode = 0; break;
1850 default: return 0;
1853 return 1;
1856 void
1857 md_show_usage (stream)
1858 FILE * stream;
1860 fprintf (stream, _("\
1861 MCORE specific options:\n\
1862 -{no-}jsri2bsr {dis}able jsri to bsr transformation (def: dis)\n\
1863 -{no-}sifilter {dis}able silicon filter behavior (def: dis)\n\
1864 -cpu=[210|340] select CPU type\n\
1865 -EB assemble for a big endian system (default)\n\
1866 -EL assemble for a little endian system\n"));
1869 int md_short_jump_size;
1871 void
1872 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1873 char * ptr;
1874 addressT from_Nddr;
1875 addressT to_Nddr;
1876 fragS * frag;
1877 symbolS * to_symbol;
1879 as_fatal (_("failed sanity check: short_jump"));
1882 void
1883 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1884 char * ptr;
1885 addressT from_Nddr;
1886 addressT to_Nddr;
1887 fragS * frag;
1888 symbolS * to_symbol;
1890 as_fatal (_("failed sanity check: long_jump"));
1893 /* Called after relaxing, change the frags so they know how big they are. */
1894 void
1895 md_convert_frag (abfd, sec, fragP)
1896 bfd * abfd;
1897 segT sec;
1898 register fragS * fragP;
1900 unsigned char * buffer;
1901 int targ_addr = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
1903 buffer = (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1904 targ_addr += symbol_get_frag (fragP->fr_symbol)->fr_address;
1906 switch (fragP->fr_subtype)
1908 case C (COND_JUMP, COND12):
1909 case C (UNCD_JUMP, UNCD12):
1911 /* Get the address of the end of the instruction. */
1912 int next_inst = fragP->fr_fix + fragP->fr_address + 2;
1913 unsigned char t0;
1914 int disp = targ_addr - next_inst;
1916 if (disp & 1)
1917 as_bad (_("odd displacement at %x"), next_inst - 2);
1919 disp >>= 1;
1921 if (! target_big_endian)
1923 t0 = buffer[1] & 0xF8;
1925 md_number_to_chars (buffer, disp, 2);
1927 buffer[1] = (buffer[1] & 0x07) | t0;
1929 else
1931 t0 = buffer[0] & 0xF8;
1933 md_number_to_chars (buffer, disp, 2);
1935 buffer[0] = (buffer[0] & 0x07) | t0;
1938 fragP->fr_fix += 2;
1939 fragP->fr_var = 0;
1941 break;
1943 case C (COND_JUMP, COND32):
1944 case C (COND_JUMP, UNDEF_WORD_DISP):
1946 /* A conditional branch wont fit into 12 bits so:
1947 * b!cond 1f
1948 * jmpi 0f
1949 * .align 2
1950 * 0: .long disp
1951 * 1:
1953 * if the b!cond is 4 byte aligned, the literal which would
1954 * go at x+4 will also be aligned.
1956 int first_inst = fragP->fr_fix + fragP->fr_address;
1957 int needpad = (first_inst & 3);
1959 if (! target_big_endian)
1960 buffer[1] ^= 0x08;
1961 else
1962 buffer[0] ^= 0x08; /* Toggle T/F bit */
1964 buffer[2] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi */
1965 buffer[3] = INST_BYTE1 (MCORE_INST_JMPI);
1967 if (needpad)
1969 if (! target_big_endian)
1971 buffer[0] = 4; /* branch over jmpi, pad, and ptr */
1972 buffer[2] = 1; /* jmpi offset of 1 gets the pointer */
1974 else
1976 buffer[1] = 4; /* branch over jmpi, pad, and ptr */
1977 buffer[3] = 1; /* jmpi offset of 1 gets the pointer */
1980 buffer[4] = 0; /* alignment/pad */
1981 buffer[5] = 0;
1982 buffer[6] = 0; /* space for 32 bit address */
1983 buffer[7] = 0;
1984 buffer[8] = 0;
1985 buffer[9] = 0;
1987 /* Make reloc for the long disp */
1988 fix_new (fragP, fragP->fr_fix + 6, 4,
1989 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
1991 fragP->fr_fix += C32_LEN;
1993 else
1995 /* See comment below about this given gas' limitations for
1996 shrinking the fragment. '3' is the amount of code that
1997 we inserted here, but '4' is right for the space we reserved
1998 for this fragment. */
1999 if (! target_big_endian)
2001 buffer[0] = 3; /* branch over jmpi, and ptr */
2002 buffer[2] = 0; /* jmpi offset of 0 gets the pointer */
2004 else
2006 buffer[1] = 3; /* branch over jmpi, and ptr */
2007 buffer[3] = 0; /* jmpi offset of 0 gets the pointer */
2010 buffer[4] = 0; /* space for 32 bit address */
2011 buffer[5] = 0;
2012 buffer[6] = 0;
2013 buffer[7] = 0;
2015 /* Make reloc for the long disp. */
2016 fix_new (fragP, fragP->fr_fix + 4, 4,
2017 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
2018 fragP->fr_fix += C32_LEN;
2020 /* Frag is actually shorter (see the other side of this ifdef)
2021 but gas isn't prepared for that. We have to re-adjust
2022 the branch displacement so that it goes beyond the
2023 full length of the fragment, not just what we actually
2024 filled in. */
2025 if (! target_big_endian)
2026 buffer[0] = 4; /* jmpi, ptr, and the 'tail pad' */
2027 else
2028 buffer[1] = 4; /* jmpi, ptr, and the 'tail pad' */
2031 fragP->fr_var = 0;
2033 break;
2035 case C (UNCD_JUMP, UNCD32):
2036 case C (UNCD_JUMP, UNDEF_WORD_DISP):
2038 /* An unconditional branch will not fit in 12 bits, make code which
2039 looks like:
2040 jmpi 0f
2041 .align 2
2042 0: .long disp
2043 we need a pad if "first_inst" is 4 byte aligned.
2044 [because the natural literal place is x + 2] */
2045 int first_inst = fragP->fr_fix + fragP->fr_address;
2046 int needpad = !(first_inst & 3);
2048 buffer[0] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi */
2049 buffer[1] = INST_BYTE1 (MCORE_INST_JMPI);
2051 if (needpad)
2053 if (! target_big_endian)
2054 buffer[0] = 1; /* jmpi offset of 1 since padded */
2055 else
2056 buffer[1] = 1; /* jmpi offset of 1 since padded */
2057 buffer[2] = 0; /* alignment */
2058 buffer[3] = 0;
2059 buffer[4] = 0; /* space for 32 bit address */
2060 buffer[5] = 0;
2061 buffer[6] = 0;
2062 buffer[7] = 0;
2064 /* Make reloc for the long disp. */
2065 fix_new (fragP, fragP->fr_fix + 4, 4,
2066 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
2068 fragP->fr_fix += U32_LEN;
2070 else
2072 if (! target_big_endian)
2073 buffer[0] = 0; /* jmpi offset of 0 if no pad */
2074 else
2075 buffer[1] = 0; /* jmpi offset of 0 if no pad */
2076 buffer[2] = 0; /* space for 32 bit address */
2077 buffer[3] = 0;
2078 buffer[4] = 0;
2079 buffer[5] = 0;
2081 /* Make reloc for the long disp. */
2082 fix_new (fragP, fragP->fr_fix + 2, 4,
2083 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32);
2084 fragP->fr_fix += U32_LEN;
2087 fragP->fr_var = 0;
2089 break;
2091 default:
2092 abort ();
2096 /* Applies the desired value to the specified location.
2097 Also sets up addends for 'rela' type relocations. */
2099 md_apply_fix3 (fixP, valp, segment)
2100 fixS * fixP;
2101 valueT * valp;
2102 segT segment;
2104 char * buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2105 char * file = fixP->fx_file ? fixP->fx_file : _("unknown");
2106 const char * symname;
2107 /* Note: use offsetT because it is signed, valueT is unsigned. */
2108 offsetT val = (offsetT) * valp;
2110 symname = fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : _("<unknown>");
2111 /* Save this for the addend in the relocation record. */
2112 fixP->fx_addnumber = val;
2114 /* If the fix is relative to a symbol which is not defined, or not
2115 in the same segment as the fix, we cannot resolve it here. */
2116 if (fixP->fx_addsy != NULL
2117 && ( ! S_IS_DEFINED (fixP->fx_addsy)
2118 || (S_GET_SEGMENT (fixP->fx_addsy) != segment)))
2120 fixP->fx_done = 0;
2121 #ifdef OBJ_ELF
2122 /* For ELF we can just return and let the reloc that will be generated
2123 take care of everything. For COFF we still have to insert 'val'
2124 into the insn since the addend field will be ignored. */
2125 return 0;
2126 #endif
2128 else
2129 fixP->fx_done = 1;
2131 switch (fixP->fx_r_type)
2133 case BFD_RELOC_MCORE_PCREL_IMM11BY2: /* second byte of 2 byte opcode */
2134 if ((val & 1) != 0)
2135 as_bad_where (file, fixP->fx_line,
2136 _("odd distance branch (0x%x bytes)"), val);
2137 val /= 2;
2138 if (((val & ~0x3ff) != 0) && ((val | 0x3ff) != -1))
2139 as_bad_where (file, fixP->fx_line,
2140 _("pcrel for branch to %s too far (0x%x)"),
2141 symname, val);
2142 if (target_big_endian)
2144 buf[0] |= ((val >> 8) & 0x7);
2145 buf[1] |= (val & 0xff);
2147 else
2149 buf[1] |= ((val >> 8) & 0x7);
2150 buf[0] |= (val & 0xff);
2152 break;
2154 case BFD_RELOC_MCORE_PCREL_IMM8BY4: /* lower 8 bits of 2 byte opcode */
2155 val += 3;
2156 val /= 4;
2157 if (val & ~0xff)
2158 as_bad_where (file, fixP->fx_line,
2159 _("pcrel for lrw/jmpi/jsri to %s too far (0x%x)"),
2160 symname, val);
2161 else if (! target_big_endian)
2162 buf[0] |= (val & 0xff);
2163 else
2164 buf[1] |= (val & 0xff);
2165 break;
2167 case BFD_RELOC_MCORE_PCREL_IMM4BY2: /* loopt instruction */
2168 if ((val < -32) || (val > -2))
2169 as_bad_where (file, fixP->fx_line,
2170 _("pcrel for loopt too far (0x%x)"), val);
2171 val /= 2;
2172 if (! target_big_endian)
2173 buf[0] |= (val & 0xf);
2174 else
2175 buf[1] |= (val & 0xf);
2176 break;
2178 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2179 /* Conditional linker map jsri to bsr. */
2180 /* If its a local target and close enough, fix it.
2181 NB: >= -2k for backwards bsr; < 2k for forwards... */
2182 if (fixP->fx_addsy == 0 && val >= -2048 && val < 2048)
2184 long nval = (val / 2) & 0x7ff;
2185 nval |= MCORE_INST_BSR;
2187 /* REPLACE the instruction, don't just modify it. */
2188 buf[0] = INST_BYTE0 (nval);
2189 buf[1] = INST_BYTE1 (nval);
2191 else
2192 fixP->fx_done = 0;
2193 break;
2195 case BFD_RELOC_MCORE_PCREL_32:
2196 case BFD_RELOC_VTABLE_INHERIT:
2197 case BFD_RELOC_VTABLE_ENTRY:
2198 fixP->fx_done = 0;
2199 break;
2201 default:
2202 if (fixP->fx_addsy != NULL)
2204 /* If the fix is an absolute reloc based on a symbol's
2205 address, then it cannot be resolved until the final link. */
2206 fixP->fx_done = 0;
2208 #ifdef OBJ_ELF
2209 else
2210 #endif
2212 if (fixP->fx_size == 4)
2214 else if (fixP->fx_size == 2 && val >= -32768 && val <= 32767)
2216 else if (fixP->fx_size == 1 && val >= -256 && val <= 255)
2218 else
2219 abort ();
2220 md_number_to_chars (buf, val, fixP->fx_size);
2222 break;
2225 return 0; /* Return value is ignored. */
2228 void
2229 md_operand (expressionP)
2230 expressionS * expressionP;
2232 /* Ignore leading hash symbol, if poresent. */
2233 if (* input_line_pointer == '#')
2235 input_line_pointer ++;
2236 expression (expressionP);
2240 int md_long_jump_size;
2242 /* Called just before address relaxation, return the length
2243 by which a fragment must grow to reach it's destination. */
2245 md_estimate_size_before_relax (fragP, segment_type)
2246 register fragS * fragP;
2247 register segT segment_type;
2249 switch (fragP->fr_subtype)
2251 case C (UNCD_JUMP, UNDEF_DISP):
2252 /* Used to be a branch to somewhere which was unknown. */
2253 if (!fragP->fr_symbol)
2255 fragP->fr_subtype = C (UNCD_JUMP, UNCD12);
2256 fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length;
2258 else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2260 fragP->fr_subtype = C (UNCD_JUMP, UNCD12);
2261 fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length;
2263 else
2265 fragP->fr_subtype = C (UNCD_JUMP, UNDEF_WORD_DISP);
2266 fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length;
2267 return md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length;
2269 break;
2271 default:
2272 abort ();
2274 case C (COND_JUMP, UNDEF_DISP):
2275 /* Used to be a branch to somewhere which was unknown. */
2276 if (fragP->fr_symbol
2277 && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
2279 /* Got a symbol and it's defined in this segment, become byte
2280 sized - maybe it will fix up */
2281 fragP->fr_subtype = C (COND_JUMP, COND12);
2282 fragP->fr_var = md_relax_table[C (COND_JUMP, COND12)].rlx_length;
2284 else if (fragP->fr_symbol)
2286 /* Its got a segment, but its not ours, so it will always be long. */
2287 fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
2288 fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
2289 return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
2291 else
2293 /* We know the abs value. */
2294 fragP->fr_subtype = C (COND_JUMP, COND12);
2295 fragP->fr_var = md_relax_table[C (COND_JUMP, COND12)].rlx_length;
2298 break;
2301 return fragP->fr_var;
2304 /* Put number into target byte order. */
2305 void
2306 md_number_to_chars (ptr, use, nbytes)
2307 char * ptr;
2308 valueT use;
2309 int nbytes;
2311 if (! target_big_endian)
2312 switch (nbytes)
2314 case 4: ptr[3] = (use >> 24) & 0xff; /* fall through */
2315 case 3: ptr[2] = (use >> 16) & 0xff; /* fall through */
2316 case 2: ptr[1] = (use >> 8) & 0xff; /* fall through */
2317 case 1: ptr[0] = (use >> 0) & 0xff; break;
2318 default: abort ();
2320 else
2321 switch (nbytes)
2323 case 4: *ptr++ = (use >> 24) & 0xff; /* fall through */
2324 case 3: *ptr++ = (use >> 16) & 0xff; /* fall through */
2325 case 2: *ptr++ = (use >> 8) & 0xff; /* fall through */
2326 case 1: *ptr++ = (use >> 0) & 0xff; break;
2327 default: abort ();
2331 /* Round up a section size to the appropriate boundary. */
2332 valueT
2333 md_section_align (segment, size)
2334 segT segment;
2335 valueT size;
2337 return size; /* Byte alignment is fine */
2341 /* The location from which a PC relative jump should be calculated,
2342 given a PC relative reloc. */
2343 long
2344 md_pcrel_from_section (fixp, sec)
2345 fixS * fixp;
2346 segT sec;
2348 #ifdef OBJ_ELF
2349 /* If the symbol is undefined or defined in another section
2350 we leave the add number alone for the linker to fix it later.
2351 Only account for the PC pre-bump (which is 2 bytes on the MCore). */
2352 if (fixp->fx_addsy != (symbolS *) NULL
2353 && (! S_IS_DEFINED (fixp->fx_addsy)
2354 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
2357 assert (fixp->fx_size == 2); /* must be an insn */
2358 return fixp->fx_size;
2360 #endif
2362 /* The case where we are going to resolve things... */
2363 return fixp->fx_size + fixp->fx_where + fixp->fx_frag->fr_address;
2366 #define F(SZ,PCREL) (((SZ) << 1) + (PCREL))
2367 #define MAP(SZ,PCREL,TYPE) case F (SZ, PCREL): code = (TYPE); break
2369 arelent *
2370 tc_gen_reloc (section, fixp)
2371 asection * section;
2372 fixS * fixp;
2374 arelent * rel;
2375 bfd_reloc_code_real_type code;
2376 int handled = 0;
2378 switch (fixp->fx_r_type)
2380 /* These confuse the size/pcrel macro approach. */
2381 case BFD_RELOC_VTABLE_INHERIT:
2382 case BFD_RELOC_VTABLE_ENTRY:
2383 case BFD_RELOC_MCORE_PCREL_IMM4BY2:
2384 case BFD_RELOC_MCORE_PCREL_IMM8BY4:
2385 case BFD_RELOC_MCORE_PCREL_IMM11BY2:
2386 case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2:
2387 case BFD_RELOC_RVA:
2388 code = fixp->fx_r_type;
2389 break;
2391 default:
2392 switch (F (fixp->fx_size, fixp->fx_pcrel))
2394 MAP (1, 0, BFD_RELOC_8);
2395 MAP (2, 0, BFD_RELOC_16);
2396 MAP (4, 0, BFD_RELOC_32);
2397 MAP (1, 1, BFD_RELOC_8_PCREL);
2398 MAP (2, 1, BFD_RELOC_16_PCREL);
2399 MAP (4, 1, BFD_RELOC_32_PCREL);
2400 default:
2401 code = fixp->fx_r_type;
2402 as_bad (_("Can not do %d byte %srelocation"),
2403 fixp->fx_size,
2404 fixp->fx_pcrel ? _("pc-relative") : "");
2406 break;
2409 rel = (arelent *) xmalloc (sizeof (arelent));
2410 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2411 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2412 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2413 /* Always pass the addend along! */
2414 rel->addend = fixp->fx_addnumber;
2416 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
2418 if (rel->howto == NULL)
2420 as_bad_where (fixp->fx_file, fixp->fx_line,
2421 _("Cannot represent relocation type %s"),
2422 bfd_get_reloc_code_name (code));
2424 /* Set howto to a garbage value so that we can keep going. */
2425 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2426 assert (rel->howto != NULL);
2429 return rel;
2432 #ifdef OBJ_ELF
2433 /* See whether we need to force a relocation into the output file.
2434 This is used to force out switch and PC relative relocations when
2435 relaxing. */
2437 mcore_force_relocation (fix)
2438 fixS * fix;
2440 if ( fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2441 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2442 || fix->fx_r_type == BFD_RELOC_RVA)
2443 return 1;
2445 return 0;
2448 /* Return true if the fix can be handled by GAS, false if it must
2449 be passed through to the linker. */
2450 boolean
2451 mcore_fix_adjustable (fixP)
2452 fixS * fixP;
2454 if (fixP->fx_addsy == NULL)
2455 return 1;
2457 /* We need the symbol name for the VTABLE entries. */
2458 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2459 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2460 return 0;
2462 return 1;
2464 #endif /* OBJ_ELF */