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