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