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