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