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