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