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