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