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