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