]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mn10300.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / gas / config / tc-mn10300.c
CommitLineData
ae1b99e4 1/* tc-mn10300.c -- Assembler code for the Matsushita 10300
0f91d763 2
e7ffb037 3 Copyright (C) 1996, 1997, 1998 Free Software Foundation.
0f91d763
JL
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include <stdio.h>
23#include <ctype.h>
24#include "as.h"
25#include "subsegs.h"
ae1b99e4 26#include "opcode/mn10300.h"
0f91d763
JL
27\f
28/* Structure to hold information about predefined registers. */
29struct reg_name
30{
31 const char *name;
32 int value;
33};
34
35/* Generic assembler global variables which must be defined by all targets. */
36
37/* Characters which always start a comment. */
38const char comment_chars[] = "#";
39
40/* Characters which start a comment at the beginning of a line. */
41const char line_comment_chars[] = ";#";
42
43/* Characters which may be used to separate multiple commands on a
44 single line. */
45const char line_separator_chars[] = ";";
46
47/* Characters which are used to indicate an exponent in a floating
48 point number. */
49const char EXP_CHARS[] = "eE";
50
51/* Characters which mean that a number is a floating point constant,
52 as in 0d1.0. */
53const char FLT_CHARS[] = "dD";
54\f
55
590c50d8
ILT
56const relax_typeS md_relax_table[] = {
57 /* bCC relaxing */
58 {0x7f, -0x80, 2, 1},
59 {0x7fff, -0x8000, 5, 2},
60 {0x7fffffff, -0x80000000, 7, 0},
61
62 /* bCC relaxing (uncommon cases) */
63 {0x7f, -0x80, 3, 4},
64 {0x7fff, -0x8000, 6, 5},
65 {0x7fffffff, -0x80000000, 8, 0},
66
67 /* call relaxing */
68 {0x7fff, -0x8000, 5, 7},
69 {0x7fffffff, -0x80000000, 7, 0},
70
71 /* calls relaxing */
72 {0x7fff, -0x8000, 4, 9},
73 {0x7fffffff, -0x80000000, 6, 0},
74
75 /* jmp relaxing */
76 {0x7f, -0x80, 2, 11},
77 {0x7fff, -0x8000, 3, 12},
78 {0x7fffffff, -0x80000000, 5, 0},
79
80};
81
0f91d763 82/* local functions */
43d695a1
JL
83static void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
84 const struct mn10300_operand *,
85 offsetT, char *, unsigned,
86 unsigned));
87static unsigned long check_operand PARAMS ((unsigned long,
88 const struct mn10300_operand *,
89 offsetT));
0f91d763 90static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
ca82e4eb
JL
91static boolean data_register_name PARAMS ((expressionS *expressionP));
92static boolean address_register_name PARAMS ((expressionS *expressionP));
93static boolean other_register_name PARAMS ((expressionS *expressionP));
3be26805 94static void set_arch_mach PARAMS ((int));
0f91d763 95
3be26805 96static int current_machine;
0f91d763
JL
97
98/* fixups */
99#define MAX_INSN_FIXUPS (5)
ae1b99e4 100struct mn10300_fixup
0f91d763
JL
101{
102 expressionS exp;
103 int opindex;
104 bfd_reloc_code_real_type reloc;
105};
ae1b99e4 106struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
0f91d763 107static int fc;
711eeac0
JL
108
109/* We must store the value of each register operand so that we can
110 verify that certain registers do not match. */
111int mn10300_reg_operands[MN10300_MAX_OPERANDS];
0f91d763
JL
112\f
113const char *md_shortopts = "";
114struct option md_longopts[] = {
115 {NULL, no_argument, NULL, 0}
116};
117size_t md_longopts_size = sizeof(md_longopts);
118
119/* The target specific pseudo-ops which we support. */
120const pseudo_typeS md_pseudo_table[] =
121{
3be26805 122 { "am30", set_arch_mach, 300 },
9e58f7c1 123 /* start-sanitize-cygnus */
3be26805 124 { "am33", set_arch_mach, 330 },
9e58f7c1 125 /* end-sanitize-cygnus */
3be26805
JL
126 { "mn10300", set_arch_mach, 300 },
127 {NULL, 0, 0}
0f91d763
JL
128};
129
130/* Opcode hash table. */
ae1b99e4 131static struct hash_control *mn10300_hash;
0f91d763
JL
132
133/* This table is sorted. Suitable for searching by a binary search. */
1217102f 134static const struct reg_name data_registers[] =
0f91d763 135{
ca82e4eb
JL
136 { "d0", 0 },
137 { "d1", 1 },
138 { "d2", 2 },
139 { "d3", 3 },
0f91d763 140};
1217102f 141#define DATA_REG_NAME_CNT (sizeof(data_registers) / sizeof(struct reg_name))
0f91d763 142
1217102f 143static const struct reg_name address_registers[] =
0f91d763 144{
ca82e4eb
JL
145 { "a0", 0 },
146 { "a1", 1 },
147 { "a2", 2 },
148 { "a3", 3 },
0f91d763 149};
1217102f 150#define ADDRESS_REG_NAME_CNT (sizeof(address_registers) / sizeof(struct reg_name))
0f91d763 151
9e58f7c1 152/* start-sanitize-cygnus */
e7ffb037
JL
153static const struct reg_name r_registers[] =
154{
5d626b5e
JL
155 { "a0", 8 },
156 { "a1", 9 },
157 { "a2", 10 },
158 { "a3", 11 },
41e9f56c 159 { "d0", 12 },
5d626b5e
JL
160 { "d1", 13 },
161 { "d2", 14 },
162 { "d3", 15 },
163 { "e0", 0 },
164 { "e1", 1 },
41e9f56c
JL
165 { "e10", 10 },
166 { "e11", 11 },
167 { "e12", 12 },
168 { "e13", 13 },
169 { "e14", 14 },
170 { "e15", 15 },
5d626b5e
JL
171 { "e2", 2 },
172 { "e3", 3 },
173 { "e4", 4 },
174 { "e5", 5 },
175 { "e6", 6 },
176 { "e7", 7 },
177 { "e8", 8 },
178 { "e9", 9 },
e7ffb037
JL
179 { "r0", 0 },
180 { "r1", 1 },
181 { "r10", 10 },
182 { "r11", 11 },
183 { "r12", 12 },
184 { "r13", 13 },
185 { "r14", 14 },
186 { "r15", 15 },
187 { "r2", 2 },
188 { "r3", 3 },
189 { "r4", 4 },
190 { "r5", 5 },
191 { "r6", 6 },
192 { "r7", 7 },
193 { "r8", 8 },
194 { "r9", 9 },
195};
196#define R_REG_NAME_CNT (sizeof(r_registers) / sizeof(struct reg_name))
197
198static const struct reg_name xr_registers[] =
199{
5d626b5e
JL
200 { "mcrh", 2 },
201 { "mcrl", 3 },
202 { "mcvf", 4 },
203 { "mdrq", 1 },
204 { "sp", 0 },
e7ffb037
JL
205 { "xr0", 0 },
206 { "xr1", 1 },
207 { "xr10", 10 },
208 { "xr11", 11 },
209 { "xr12", 12 },
210 { "xr13", 13 },
211 { "xr14", 14 },
212 { "xr15", 15 },
213 { "xr2", 2 },
214 { "xr3", 3 },
215 { "xr4", 4 },
216 { "xr5", 5 },
217 { "xr6", 6 },
218 { "xr7", 7 },
219 { "xr8", 8 },
220 { "xr9", 9 },
221};
222#define XR_REG_NAME_CNT (sizeof(xr_registers) / sizeof(struct reg_name))
223
9e58f7c1 224/* end-sanitize-cygnus */
e7ffb037 225
1217102f 226static const struct reg_name other_registers[] =
0f91d763 227{
ca82e4eb
JL
228 { "mdr", 0 },
229 { "psw", 0 },
230 { "sp", 0 },
0f91d763 231};
1217102f 232#define OTHER_REG_NAME_CNT (sizeof(other_registers) / sizeof(struct reg_name))
0f91d763
JL
233
234/* reg_name_search does a binary search of the given register table
235 to see if "name" is a valid regiter name. Returns the register
236 number from the array on success, or -1 on failure. */
237
238static int
239reg_name_search (regs, regcount, name)
240 const struct reg_name *regs;
241 int regcount;
242 const char *name;
243{
244 int middle, low, high;
245 int cmp;
246
247 low = 0;
248 high = regcount - 1;
249
250 do
251 {
252 middle = (low + high) / 2;
253 cmp = strcasecmp (name, regs[middle].name);
254 if (cmp < 0)
255 high = middle - 1;
256 else if (cmp > 0)
257 low = middle + 1;
258 else
259 return regs[middle].value;
260 }
261 while (low <= high);
262 return -1;
263}
264
265
9e58f7c1 266/* start-sanitize-cygnus */
e7ffb037
JL
267/* Summary of register_name().
268 *
269 * in: Input_line_pointer points to 1st char of operand.
270 *
271 * out: A expressionS.
272 * The operand may have been a register: in this case, X_op == O_register,
273 * X_add_number is set to the register number, and truth is returned.
274 * Input_line_pointer->(next non-blank) char after operand, or is in
275 * its original state.
276 */
277static boolean
278r_register_name (expressionP)
279 expressionS *expressionP;
280{
281 int reg_number;
282 char *name;
283 char *start;
284 char c;
285
286 /* Find the spelling of the operand */
287 start = name = input_line_pointer;
288
289 c = get_symbol_end ();
290 reg_number = reg_name_search (r_registers, R_REG_NAME_CNT, name);
291
292 /* look to see if it's in the register table */
293 if (reg_number >= 0)
294 {
295 expressionP->X_op = O_register;
296 expressionP->X_add_number = reg_number;
297
298 /* make the rest nice */
299 expressionP->X_add_symbol = NULL;
300 expressionP->X_op_symbol = NULL;
301 *input_line_pointer = c; /* put back the delimiting char */
302 return true;
303 }
304 else
305 {
306 /* reset the line as if we had not done anything */
307 *input_line_pointer = c; /* put back the delimiting char */
308 input_line_pointer = start; /* reset input_line pointer */
309 return false;
310 }
311}
312
313/* Summary of register_name().
314 *
315 * in: Input_line_pointer points to 1st char of operand.
316 *
317 * out: A expressionS.
318 * The operand may have been a register: in this case, X_op == O_register,
319 * X_add_number is set to the register number, and truth is returned.
320 * Input_line_pointer->(next non-blank) char after operand, or is in
321 * its original state.
322 */
323static boolean
324xr_register_name (expressionP)
325 expressionS *expressionP;
326{
327 int reg_number;
328 char *name;
329 char *start;
330 char c;
331
332 /* Find the spelling of the operand */
333 start = name = input_line_pointer;
334
335 c = get_symbol_end ();
336 reg_number = reg_name_search (xr_registers, XR_REG_NAME_CNT, name);
337
338 /* look to see if it's in the register table */
339 if (reg_number >= 0)
340 {
341 expressionP->X_op = O_register;
342 expressionP->X_add_number = reg_number;
343
344 /* make the rest nice */
345 expressionP->X_add_symbol = NULL;
346 expressionP->X_op_symbol = NULL;
347 *input_line_pointer = c; /* put back the delimiting char */
348 return true;
349 }
350 else
351 {
352 /* reset the line as if we had not done anything */
353 *input_line_pointer = c; /* put back the delimiting char */
354 input_line_pointer = start; /* reset input_line pointer */
355 return false;
356 }
357}
9e58f7c1 358/* end-sanitize-cygnus */
e7ffb037 359
0f91d763
JL
360/* Summary of register_name().
361 *
362 * in: Input_line_pointer points to 1st char of operand.
363 *
364 * out: A expressionS.
365 * The operand may have been a register: in this case, X_op == O_register,
366 * X_add_number is set to the register number, and truth is returned.
367 * Input_line_pointer->(next non-blank) char after operand, or is in
368 * its original state.
369 */
370static boolean
1217102f 371data_register_name (expressionP)
0f91d763
JL
372 expressionS *expressionP;
373{
374 int reg_number;
375 char *name;
376 char *start;
377 char c;
378
379 /* Find the spelling of the operand */
380 start = name = input_line_pointer;
381
382 c = get_symbol_end ();
1217102f 383 reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
0f91d763
JL
384
385 /* look to see if it's in the register table */
386 if (reg_number >= 0)
387 {
388 expressionP->X_op = O_register;
389 expressionP->X_add_number = reg_number;
390
391 /* make the rest nice */
392 expressionP->X_add_symbol = NULL;
393 expressionP->X_op_symbol = NULL;
394 *input_line_pointer = c; /* put back the delimiting char */
395 return true;
396 }
397 else
398 {
399 /* reset the line as if we had not done anything */
400 *input_line_pointer = c; /* put back the delimiting char */
401 input_line_pointer = start; /* reset input_line pointer */
402 return false;
403 }
404}
405
1217102f 406/* Summary of register_name().
0f91d763
JL
407 *
408 * in: Input_line_pointer points to 1st char of operand.
409 *
410 * out: A expressionS.
411 * The operand may have been a register: in this case, X_op == O_register,
412 * X_add_number is set to the register number, and truth is returned.
413 * Input_line_pointer->(next non-blank) char after operand, or is in
414 * its original state.
415 */
416static boolean
1217102f 417address_register_name (expressionP)
0f91d763
JL
418 expressionS *expressionP;
419{
420 int reg_number;
421 char *name;
422 char *start;
423 char c;
424
425 /* Find the spelling of the operand */
426 start = name = input_line_pointer;
427
428 c = get_symbol_end ();
1217102f 429 reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
0f91d763
JL
430
431 /* look to see if it's in the register table */
432 if (reg_number >= 0)
433 {
434 expressionP->X_op = O_register;
435 expressionP->X_add_number = reg_number;
436
437 /* make the rest nice */
438 expressionP->X_add_symbol = NULL;
439 expressionP->X_op_symbol = NULL;
440 *input_line_pointer = c; /* put back the delimiting char */
441 return true;
442 }
443 else
444 {
445 /* reset the line as if we had not done anything */
446 *input_line_pointer = c; /* put back the delimiting char */
447 input_line_pointer = start; /* reset input_line pointer */
448 return false;
449 }
450}
451
1217102f 452/* Summary of register_name().
0f91d763
JL
453 *
454 * in: Input_line_pointer points to 1st char of operand.
455 *
456 * out: A expressionS.
457 * The operand may have been a register: in this case, X_op == O_register,
458 * X_add_number is set to the register number, and truth is returned.
459 * Input_line_pointer->(next non-blank) char after operand, or is in
460 * its original state.
461 */
462static boolean
1217102f 463other_register_name (expressionP)
0f91d763
JL
464 expressionS *expressionP;
465{
466 int reg_number;
467 char *name;
468 char *start;
469 char c;
470
471 /* Find the spelling of the operand */
472 start = name = input_line_pointer;
473
474 c = get_symbol_end ();
1217102f 475 reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
0f91d763
JL
476
477 /* look to see if it's in the register table */
478 if (reg_number >= 0)
479 {
1217102f 480 expressionP->X_op = O_register;
0f91d763
JL
481 expressionP->X_add_number = reg_number;
482
483 /* make the rest nice */
484 expressionP->X_add_symbol = NULL;
485 expressionP->X_op_symbol = NULL;
486 *input_line_pointer = c; /* put back the delimiting char */
487 return true;
488 }
489 else
490 {
491 /* reset the line as if we had not done anything */
492 *input_line_pointer = c; /* put back the delimiting char */
493 input_line_pointer = start; /* reset input_line pointer */
494 return false;
495 }
496}
497
498void
499md_show_usage (stream)
500 FILE *stream;
501{
e7ffb037
JL
502 fprintf(stream, _("MN10300 options:\n\
503none yet\n"));
0f91d763
JL
504}
505
506int
507md_parse_option (c, arg)
508 int c;
509 char *arg;
510{
511 return 0;
512}
513
514symbolS *
515md_undefined_symbol (name)
516 char *name;
517{
518 return 0;
519}
520
521char *
522md_atof (type, litp, sizep)
523 int type;
524 char *litp;
525 int *sizep;
526{
527 int prec;
528 LITTLENUM_TYPE words[4];
529 char *t;
530 int i;
531
532 switch (type)
533 {
534 case 'f':
535 prec = 2;
536 break;
537
538 case 'd':
539 prec = 4;
540 break;
541
542 default:
543 *sizep = 0;
544 return "bad call to md_atof";
545 }
546
547 t = atof_ieee (input_line_pointer, type, words);
548 if (t)
549 input_line_pointer = t;
550
551 *sizep = prec * 2;
552
553 for (i = prec - 1; i >= 0; i--)
554 {
555 md_number_to_chars (litp, (valueT) words[i], 2);
556 litp += 2;
557 }
558
559 return NULL;
560}
561
562
563void
564md_convert_frag (abfd, sec, fragP)
565 bfd *abfd;
566 asection *sec;
567 fragS *fragP;
568{
590c50d8
ILT
569 static unsigned long label_count = 0;
570 char buf[40];
571
572 subseg_change (sec, 0);
573 if (fragP->fr_subtype == 0)
574 {
575 fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
576 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
577 fragP->fr_var = 0;
578 fragP->fr_fix += 2;
579 }
580 else if (fragP->fr_subtype == 1)
581 {
582 /* Reverse the condition of the first branch. */
583 int offset = fragP->fr_fix;
584 int opcode = fragP->fr_literal[offset] & 0xff;
585
586 switch (opcode)
587 {
588 case 0xc8:
589 opcode = 0xc9;
590 break;
591 case 0xc9:
592 opcode = 0xc8;
593 break;
594 case 0xc0:
595 opcode = 0xc2;
596 break;
597 case 0xc2:
598 opcode = 0xc0;
599 break;
600 case 0xc3:
601 opcode = 0xc1;
602 break;
603 case 0xc1:
604 opcode = 0xc3;
605 break;
606 case 0xc4:
607 opcode = 0xc6;
608 break;
609 case 0xc6:
610 opcode = 0xc4;
611 break;
612 case 0xc7:
613 opcode = 0xc5;
614 break;
615 case 0xc5:
616 opcode = 0xc7;
617 break;
618 default:
619 abort ();
620 }
621 fragP->fr_literal[offset] = opcode;
622
623 /* Create a fixup for the reversed conditional branch. */
e7ffb037 624 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
590c50d8
ILT
625 fix_new (fragP, fragP->fr_fix + 1, 1,
626 symbol_new (buf, sec, 0, fragP->fr_next),
627 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
628
629 /* Now create the unconditional branch + fixup to the
630 final target. */
631 fragP->fr_literal[offset + 2] = 0xcc;
632 fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
633 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
634 fragP->fr_var = 0;
635 fragP->fr_fix += 5;
636 }
637 else if (fragP->fr_subtype == 2)
638 {
639 /* Reverse the condition of the first branch. */
640 int offset = fragP->fr_fix;
641 int opcode = fragP->fr_literal[offset] & 0xff;
642
643 switch (opcode)
644 {
645 case 0xc8:
646 opcode = 0xc9;
647 break;
648 case 0xc9:
649 opcode = 0xc8;
650 break;
651 case 0xc0:
652 opcode = 0xc2;
653 break;
654 case 0xc2:
655 opcode = 0xc0;
656 break;
657 case 0xc3:
658 opcode = 0xc1;
659 break;
660 case 0xc1:
661 opcode = 0xc3;
662 break;
663 case 0xc4:
664 opcode = 0xc6;
665 break;
666 case 0xc6:
667 opcode = 0xc4;
668 break;
669 case 0xc7:
670 opcode = 0xc5;
671 break;
672 case 0xc5:
673 opcode = 0xc7;
674 break;
675 default:
676 abort ();
677 }
678 fragP->fr_literal[offset] = opcode;
679
680 /* Create a fixup for the reversed conditional branch. */
e7ffb037 681 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
590c50d8
ILT
682 fix_new (fragP, fragP->fr_fix + 1, 1,
683 symbol_new (buf, sec, 0, fragP->fr_next),
684 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
685
686 /* Now create the unconditional branch + fixup to the
687 final target. */
688 fragP->fr_literal[offset + 2] = 0xdc;
689 fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
690 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
691 fragP->fr_var = 0;
692 fragP->fr_fix += 7;
693 }
694 else if (fragP->fr_subtype == 3)
695 {
696 fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
697 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
698 fragP->fr_var = 0;
699 fragP->fr_fix += 3;
700 }
701 else if (fragP->fr_subtype == 4)
702 {
703 /* Reverse the condition of the first branch. */
704 int offset = fragP->fr_fix;
705 int opcode = fragP->fr_literal[offset + 1] & 0xff;
706
707 switch (opcode)
708 {
709 case 0xe8:
710 opcode = 0xe9;
711 break;
712 case 0xe9:
713 opcode = 0xe8;
714 break;
715 case 0xea:
716 opcode = 0xeb;
717 break;
718 case 0xeb:
719 opcode = 0xea;
720 break;
721 default:
722 abort ();
723 }
724 fragP->fr_literal[offset + 1] = opcode;
725
726 /* Create a fixup for the reversed conditional branch. */
e7ffb037 727 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
590c50d8
ILT
728 fix_new (fragP, fragP->fr_fix + 2, 1,
729 symbol_new (buf, sec, 0, fragP->fr_next),
730 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
731
732 /* Now create the unconditional branch + fixup to the
733 final target. */
734 fragP->fr_literal[offset + 3] = 0xcc;
735 fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
736 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
737 fragP->fr_var = 0;
738 fragP->fr_fix += 6;
739 }
740 else if (fragP->fr_subtype == 5)
741 {
742 /* Reverse the condition of the first branch. */
743 int offset = fragP->fr_fix;
744 int opcode = fragP->fr_literal[offset + 1] & 0xff;
745
746 switch (opcode)
747 {
748 case 0xe8:
749 opcode = 0xe9;
750 break;
751 case 0xea:
752 opcode = 0xeb;
753 break;
754 case 0xeb:
755 opcode = 0xea;
756 break;
757 default:
758 abort ();
759 }
760 fragP->fr_literal[offset + 1] = opcode;
761
762 /* Create a fixup for the reversed conditional branch. */
e7ffb037 763 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
590c50d8
ILT
764 fix_new (fragP, fragP->fr_fix + 2, 1,
765 symbol_new (buf, sec, 0, fragP->fr_next),
766 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
767
768 /* Now create the unconditional branch + fixup to the
769 final target. */
770 fragP->fr_literal[offset + 3] = 0xdc;
771 fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
772 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
773 fragP->fr_var = 0;
774 fragP->fr_fix += 8;
775 }
776 else if (fragP->fr_subtype == 6)
777 {
778 int offset = fragP->fr_fix;
779 fragP->fr_literal[offset] = 0xcd;
780 fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
781 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
782 fragP->fr_var = 0;
783 fragP->fr_fix += 5;
784 }
785 else if (fragP->fr_subtype == 7)
786 {
787 int offset = fragP->fr_fix;
788 fragP->fr_literal[offset] = 0xdd;
789 fragP->fr_literal[offset + 5] = fragP->fr_literal[offset + 3];
790 fragP->fr_literal[offset + 6] = fragP->fr_literal[offset + 4];
791
e7ffb037
JL
792 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
793 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
590c50d8
ILT
794 fragP->fr_var = 0;
795 fragP->fr_fix += 7;
796 }
797 else if (fragP->fr_subtype == 8)
798 {
799 int offset = fragP->fr_fix;
800 fragP->fr_literal[offset] = 0xfa;
801 fragP->fr_literal[offset + 1] = 0xff;
802 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
803 fragP->fr_offset + 2, 1, BFD_RELOC_16_PCREL);
804 fragP->fr_var = 0;
805 fragP->fr_fix += 4;
806 }
807 else if (fragP->fr_subtype == 9)
808 {
809 int offset = fragP->fr_fix;
810 fragP->fr_literal[offset] = 0xfc;
811 fragP->fr_literal[offset + 1] = 0xff;
812
813 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
814 fragP->fr_offset + 2, 1, BFD_RELOC_32_PCREL);
815 fragP->fr_var = 0;
816 fragP->fr_fix += 6;
817 }
818 else if (fragP->fr_subtype == 10)
819 {
820 fragP->fr_literal[fragP->fr_fix] = 0xca;
821 fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
822 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
823 fragP->fr_var = 0;
824 fragP->fr_fix += 2;
825 }
826 else if (fragP->fr_subtype == 11)
827 {
828 int offset = fragP->fr_fix;
829 fragP->fr_literal[offset] = 0xcc;
830
831 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
832 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
833 fragP->fr_var = 0;
834 fragP->fr_fix += 3;
835 }
836 else if (fragP->fr_subtype == 12)
837 {
838 int offset = fragP->fr_fix;
839 fragP->fr_literal[offset] = 0xdc;
840
841 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
842 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
843 fragP->fr_var = 0;
844 fragP->fr_fix += 5;
845 }
846 else
847 abort ();
0f91d763
JL
848}
849
850valueT
851md_section_align (seg, addr)
852 asection *seg;
853 valueT addr;
854{
855 int align = bfd_get_section_alignment (stdoutput, seg);
856 return ((addr + (1 << align) - 1) & (-1 << align));
857}
858
859void
860md_begin ()
861{
862 char *prev_name = "";
ae1b99e4 863 register const struct mn10300_opcode *op;
0f91d763 864
ae1b99e4 865 mn10300_hash = hash_new();
0f91d763 866
ae1b99e4 867 /* Insert unique names into hash table. The MN10300 instruction set
0f91d763
JL
868 has many identical opcode names that have different opcodes based
869 on the operands. This hash table then provides a quick index to
870 the first opcode with a particular name in the opcode table. */
871
d9a9c18f 872 op = mn10300_opcodes;
0f91d763
JL
873 while (op->name)
874 {
875 if (strcmp (prev_name, op->name))
876 {
877 prev_name = (char *) op->name;
ae1b99e4 878 hash_insert (mn10300_hash, op->name, (char *) op);
0f91d763
JL
879 }
880 op++;
881 }
d9a9c18f
JL
882
883 /* This is both a simplification (we don't have to write md_apply_fix)
884 and support for future optimizations (branch shortening and similar
711eeac0 885 stuff in the linker). */
d9a9c18f 886 linkrelax = 1;
3be26805
JL
887
888 /* Set the default machine type. */
889 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, 300))
890 as_warn (_("could not set architecture and machine"));
891
892 current_machine = 300;
0f91d763
JL
893}
894
0f91d763
JL
895void
896md_assemble (str)
897 char *str;
898{
899 char *s;
ae1b99e4
JL
900 struct mn10300_opcode *opcode;
901 struct mn10300_opcode *next_opcode;
0f91d763 902 const unsigned char *opindex_ptr;
590c50d8 903 int next_opindex, relaxable;
ca82e4eb 904 unsigned long insn, extension, size = 0;
0f91d763
JL
905 char *f;
906 int i;
907 int match;
0f91d763
JL
908
909 /* Get the opcode. */
910 for (s = str; *s != '\0' && ! isspace (*s); s++)
911 ;
912 if (*s != '\0')
913 *s++ = '\0';
914
915 /* find the first opcode with the proper name */
ae1b99e4 916 opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
0f91d763
JL
917 if (opcode == NULL)
918 {
e7ffb037 919 as_bad (_("Unrecognized opcode: `%s'"), str);
0f91d763
JL
920 return;
921 }
922
923 str = s;
924 while (isspace (*str))
925 ++str;
926
927 input_line_pointer = str;
928
929 for(;;)
930 {
711eeac0 931 const char *errmsg;
1217102f 932 int op_idx;
1217102f 933 char *hold;
68328dc6 934 int extra_shift = 0;
0f91d763 935
3be26805 936
711eeac0
JL
937 errmsg = _("Invalid opcode/operands");
938
939 /* Reset the array of register operands. */
940 memset (mn10300_reg_operands, -1, sizeof (mn10300_reg_operands));
941
590c50d8 942 relaxable = 0;
0f91d763
JL
943 fc = 0;
944 match = 0;
945 next_opindex = 0;
946 insn = opcode->opcode;
bfe5059c 947 extension = 0;
3be26805
JL
948
949 /* If the instruction is not available on the current machine
950 then it can not possibly match. */
951 if (opcode->machine
952 && (opcode->machine != current_machine))
953 goto error;
954
1217102f
JL
955 for (op_idx = 1, opindex_ptr = opcode->operands;
956 *opindex_ptr != 0;
957 opindex_ptr++, op_idx++)
0f91d763 958 {
ae1b99e4 959 const struct mn10300_operand *operand;
0f91d763
JL
960 expressionS ex;
961
962 if (next_opindex == 0)
963 {
ae1b99e4 964 operand = &mn10300_operands[*opindex_ptr];
0f91d763
JL
965 }
966 else
967 {
ae1b99e4 968 operand = &mn10300_operands[next_opindex];
0f91d763
JL
969 next_opindex = 0;
970 }
971
7f02192d 972 while (*str == ' ' || *str == ',')
0f91d763
JL
973 ++str;
974
590c50d8
ILT
975 if (operand->flags & MN10300_OPERAND_RELAX)
976 relaxable = 1;
977
0f91d763
JL
978 /* Gather the operand. */
979 hold = input_line_pointer;
980 input_line_pointer = str;
981
a6be605a
JL
982 if (operand->flags & MN10300_OPERAND_PAREN)
983 {
984 if (*input_line_pointer != ')' && *input_line_pointer != '(')
985 {
986 input_line_pointer = hold;
987 str = hold;
988 goto error;
989 }
1217102f 990 input_line_pointer++;
a6be605a 991 goto keep_going;
1217102f 992 }
1217102f 993 /* See if we can match the operands. */
a6be605a 994 else if (operand->flags & MN10300_OPERAND_DREG)
0f91d763 995 {
1217102f 996 if (!data_register_name (&ex))
0f91d763 997 {
1217102f
JL
998 input_line_pointer = hold;
999 str = hold;
0f91d763
JL
1000 goto error;
1001 }
1217102f
JL
1002 }
1003 else if (operand->flags & MN10300_OPERAND_AREG)
1004 {
1005 if (!address_register_name (&ex))
0f91d763 1006 {
1217102f
JL
1007 input_line_pointer = hold;
1008 str = hold;
1009 goto error;
0f91d763 1010 }
1217102f
JL
1011 }
1012 else if (operand->flags & MN10300_OPERAND_SP)
1013 {
1014 char *start = input_line_pointer;
1015 char c = get_symbol_end ();
1016
e7ffb037 1017 if (strcasecmp (start, "sp") != 0)
1217102f
JL
1018 {
1019 *input_line_pointer = c;
1020 input_line_pointer = hold;
1021 str = hold;
1022 goto error;
1023 }
1024 *input_line_pointer = c;
1025 goto keep_going;
1026 }
9e58f7c1 1027 /* start-sanitize-cygnus */
e7ffb037
JL
1028 else if (operand->flags & MN10300_OPERAND_RREG)
1029 {
1030 if (!r_register_name (&ex))
1031 {
1032 input_line_pointer = hold;
1033 str = hold;
1034 goto error;
1035 }
1036 }
1037 else if (operand->flags & MN10300_OPERAND_XRREG)
1038 {
1039 if (!xr_register_name (&ex))
1040 {
1041 input_line_pointer = hold;
1042 str = hold;
1043 goto error;
1044 }
1045 }
1046 else if (operand->flags & MN10300_OPERAND_USP)
1047 {
1048 char *start = input_line_pointer;
1049 char c = get_symbol_end ();
1050
1051 if (strcasecmp (start, "usp") != 0)
1052 {
1053 *input_line_pointer = c;
1054 input_line_pointer = hold;
1055 str = hold;
1056 goto error;
1057 }
1058 *input_line_pointer = c;
1059 goto keep_going;
1060 }
1061 else if (operand->flags & MN10300_OPERAND_SSP)
1062 {
1063 char *start = input_line_pointer;
1064 char c = get_symbol_end ();
1065
1066 if (strcasecmp (start, "ssp") != 0)
1067 {
1068 *input_line_pointer = c;
1069 input_line_pointer = hold;
1070 str = hold;
1071 goto error;
1072 }
1073 *input_line_pointer = c;
1074 goto keep_going;
1075 }
1076 else if (operand->flags & MN10300_OPERAND_MSP)
1077 {
1078 char *start = input_line_pointer;
1079 char c = get_symbol_end ();
1080
1081 if (strcasecmp (start, "msp") != 0)
1082 {
1083 *input_line_pointer = c;
1084 input_line_pointer = hold;
1085 str = hold;
1086 goto error;
1087 }
1088 *input_line_pointer = c;
1089 goto keep_going;
1090 }
1091 else if (operand->flags & MN10300_OPERAND_PC)
1092 {
1093 char *start = input_line_pointer;
1094 char c = get_symbol_end ();
1095
1096 if (strcasecmp (start, "pc") != 0)
1097 {
1098 *input_line_pointer = c;
1099 input_line_pointer = hold;
1100 str = hold;
1101 goto error;
1102 }
1103 *input_line_pointer = c;
1104 goto keep_going;
1105 }
1106 else if (operand->flags & MN10300_OPERAND_EPSW)
1107 {
1108 char *start = input_line_pointer;
1109 char c = get_symbol_end ();
1110
1111 if (strcasecmp (start, "epsw") != 0)
1112 {
1113 *input_line_pointer = c;
1114 input_line_pointer = hold;
1115 str = hold;
1116 goto error;
1117 }
1118 *input_line_pointer = c;
1119 goto keep_going;
1120 }
1121 else if (operand->flags & MN10300_OPERAND_PLUS)
1122 {
1123 if (*input_line_pointer != '+')
1124 {
1125 input_line_pointer = hold;
1126 str = hold;
1127 goto error;
1128 }
1129 input_line_pointer++;
1130 goto keep_going;
1131 }
9e58f7c1 1132 /* end-sanitize-cygnus */
1217102f
JL
1133 else if (operand->flags & MN10300_OPERAND_PSW)
1134 {
1135 char *start = input_line_pointer;
1136 char c = get_symbol_end ();
1137
e7ffb037 1138 if (strcasecmp (start, "psw") != 0)
0f91d763 1139 {
1217102f
JL
1140 *input_line_pointer = c;
1141 input_line_pointer = hold;
1142 str = hold;
1143 goto error;
1144 }
1145 *input_line_pointer = c;
1146 goto keep_going;
1147 }
1148 else if (operand->flags & MN10300_OPERAND_MDR)
1149 {
1150 char *start = input_line_pointer;
1151 char c = get_symbol_end ();
0f91d763 1152
e7ffb037 1153 if (strcasecmp (start, "mdr") != 0)
1217102f
JL
1154 {
1155 *input_line_pointer = c;
1156 input_line_pointer = hold;
1157 str = hold;
1158 goto error;
0f91d763 1159 }
1217102f
JL
1160 *input_line_pointer = c;
1161 goto keep_going;
1162 }
7f02192d
JL
1163 else if (operand->flags & MN10300_OPERAND_REG_LIST)
1164 {
1165 unsigned int value = 0;
1166 if (*input_line_pointer != '[')
1167 {
1168 input_line_pointer = hold;
1169 str = hold;
1170 goto error;
1171 }
1172
1173 /* Eat the '['. */
1174 input_line_pointer++;
1175
e7ffb037
JL
1176 /* We used to reject a null register list here; however,
1177 we accept it now so the compiler can emit "call" instructions
1178 for all calls to named functions.
7f02192d 1179
e7ffb037
JL
1180 The linker can then fill in the appropriate bits for the
1181 register list and stack size or change the instruction
1182 into a "calls" if using "call" is not profitable. */
7f02192d
JL
1183 while (*input_line_pointer != ']')
1184 {
1185 char *start;
1186 char c;
1187
1188 if (*input_line_pointer == ',')
1189 input_line_pointer++;
1190
1191 start = input_line_pointer;
1192 c = get_symbol_end ();
1193
e7ffb037 1194 if (strcasecmp (start, "d2") == 0)
7f02192d
JL
1195 {
1196 value |= 0x80;
1197 *input_line_pointer = c;
1198 }
e7ffb037 1199 else if (strcasecmp (start, "d3") == 0)
7f02192d
JL
1200 {
1201 value |= 0x40;
1202 *input_line_pointer = c;
1203 }
e7ffb037 1204 else if (strcasecmp (start, "a2") == 0)
7f02192d
JL
1205 {
1206 value |= 0x20;
1207 *input_line_pointer = c;
1208 }
e7ffb037 1209 else if (strcasecmp (start, "a3") == 0)
7f02192d
JL
1210 {
1211 value |= 0x10;
1212 *input_line_pointer = c;
1213 }
e7ffb037 1214 else if (strcasecmp (start, "other") == 0)
7f02192d
JL
1215 {
1216 value |= 0x08;
1217 *input_line_pointer = c;
1218 }
9e58f7c1 1219 /* start-sanitize-cygnus */
e7ffb037
JL
1220 else if (strcasecmp (start, "exreg0") == 0)
1221 {
1222 value |= 0x04;
1223 *input_line_pointer = c;
1224 }
1225 else if (strcasecmp (start, "exreg1") == 0)
1226 {
1227 value |= 0x02;
1228 *input_line_pointer = c;
1229 }
1230 else if (strcasecmp (start, "exother") == 0)
1231 {
1232 value |= 0x01;
1233 *input_line_pointer = c;
1234 }
1235 else if (strcasecmp (start, "all") == 0)
1236 {
1237 value |= 0xff;
1238 *input_line_pointer = c;
1239 }
9e58f7c1 1240 /* end-sanitize-cygnus */
7f02192d
JL
1241 else
1242 {
1243 input_line_pointer = hold;
1244 str = hold;
1245 goto error;
1246 }
1247 }
1248 input_line_pointer++;
1249 mn10300_insert_operand (&insn, &extension, operand,
1250 value, (char *) NULL, 0, 0);
1251 goto keep_going;
1252
1253 }
1217102f
JL
1254 else if (data_register_name (&ex))
1255 {
1256 input_line_pointer = hold;
1257 str = hold;
1258 goto error;
1259 }
1260 else if (address_register_name (&ex))
1261 {
1262 input_line_pointer = hold;
1263 str = hold;
1264 goto error;
1265 }
1266 else if (other_register_name (&ex))
1267 {
1268 input_line_pointer = hold;
1269 str = hold;
1270 goto error;
0f91d763 1271 }
9e58f7c1 1272 /* start-sanitize-cygnus */
41e9f56c
JL
1273 else if (r_register_name (&ex))
1274 {
1275 input_line_pointer = hold;
1276 str = hold;
1277 goto error;
1278 }
1279 else if (xr_register_name (&ex))
1280 {
1281 input_line_pointer = hold;
1282 str = hold;
1283 goto error;
1284 }
9e58f7c1 1285 /* end-sanitize-cygnus */
a6be605a
JL
1286 else if (*str == ')' || *str == '(')
1287 {
1288 input_line_pointer = hold;
1289 str = hold;
1290 goto error;
1291 }
0f91d763
JL
1292 else
1293 {
1217102f
JL
1294 expression (&ex);
1295 }
1296
0f91d763
JL
1297 switch (ex.X_op)
1298 {
1299 case O_illegal:
e7ffb037 1300 errmsg = _("illegal operand");
0f91d763
JL
1301 goto error;
1302 case O_absent:
e7ffb037 1303 errmsg = _("missing operand");
0f91d763
JL
1304 goto error;
1305 case O_register:
e7ffb037
JL
1306 {
1307 int mask;
1308
1309 mask = MN10300_OPERAND_DREG | MN10300_OPERAND_AREG;
9e58f7c1 1310 /* start-sanitize-cygnus */
e7ffb037 1311 mask |= MN10300_OPERAND_RREG | MN10300_OPERAND_XRREG;
9e58f7c1 1312 /* end-sanitize-cygnus */
e7ffb037
JL
1313 if ((operand->flags & mask) == 0)
1314 {
1315 input_line_pointer = hold;
1316 str = hold;
1317 goto error;
1318 }
0f91d763 1319
e7ffb037
JL
1320 if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
1321 extra_shift = 8;
1322 else if (opcode->format == FMT_D2
1323 || opcode->format == FMT_D4
1324 || opcode->format == FMT_S2
1325 || opcode->format == FMT_S4
1326 || opcode->format == FMT_S6
1327 || opcode->format == FMT_D5)
1328 extra_shift = 16;
9e58f7c1 1329 /* start-sanitize-cygnus */
e7ffb037
JL
1330 else if (opcode->format == FMT_D7)
1331 extra_shift = 8;
573916e3
JL
1332 else if (opcode->format == FMT_D8 || opcode->format == FMT_D9)
1333 extra_shift = 8;
9e58f7c1 1334 /* end-sanitize-cygnus */
e7ffb037
JL
1335 else
1336 extra_shift = 0;
68328dc6 1337
e7ffb037
JL
1338 mn10300_insert_operand (&insn, &extension, operand,
1339 ex.X_add_number, (char *) NULL,
1340 0, extra_shift);
68328dc6 1341
711eeac0
JL
1342
1343 /* And note the register number in the register array. */
1344 mn10300_reg_operands[op_idx - 1] = ex.X_add_number;
e7ffb037
JL
1345 break;
1346 }
0f91d763
JL
1347
1348 case O_constant:
1217102f
JL
1349 /* If this operand can be promoted, and it doesn't
1350 fit into the allocated bitfield for this insn,
1351 then promote it (ie this opcode does not match). */
590c50d8
ILT
1352 if (operand->flags
1353 & (MN10300_OPERAND_PROMOTE | MN10300_OPERAND_RELAX)
1217102f
JL
1354 && ! check_operand (insn, operand, ex.X_add_number))
1355 {
1356 input_line_pointer = hold;
1357 str = hold;
1358 goto error;
1359 }
1360
bfe5059c
JL
1361 mn10300_insert_operand (&insn, &extension, operand,
1362 ex.X_add_number, (char *) NULL,
1363 0, 0);
0f91d763
JL
1364 break;
1365
1366 default:
1217102f
JL
1367 /* If this operand can be promoted, then this opcode didn't
1368 match since we can't know if it needed promotion! */
1369 if (operand->flags & MN10300_OPERAND_PROMOTE)
1370 {
1371 input_line_pointer = hold;
1372 str = hold;
1373 goto error;
1374 }
1375
0f91d763
JL
1376 /* We need to generate a fixup for this expression. */
1377 if (fc >= MAX_INSN_FIXUPS)
e7ffb037 1378 as_fatal (_("too many fixups"));
0f91d763
JL
1379 fixups[fc].exp = ex;
1380 fixups[fc].opindex = *opindex_ptr;
1381 fixups[fc].reloc = BFD_RELOC_UNUSED;
1382 ++fc;
1383 break;
1384 }
1385
1217102f 1386keep_going:
0f91d763
JL
1387 str = input_line_pointer;
1388 input_line_pointer = hold;
1389
7f02192d 1390 while (*str == ' ' || *str == ',')
0f91d763 1391 ++str;
1217102f 1392
1217102f 1393 }
a6be605a
JL
1394
1395 /* Make sure we used all the operands! */
1396 if (*str != ',')
1217102f 1397 match = 1;
0f91d763 1398
711eeac0
JL
1399 /* If this instruction has registers that must not match, verify
1400 that they do indeed not match. */
1401 if (opcode->no_match_operands)
1402 {
1403 int i;
1404
1405 /* Look at each operand to see if it's marked. */
1406 for (i = 0; i < MN10300_MAX_OPERANDS; i++)
1407 {
1408 if ((1 << i) & opcode->no_match_operands)
1409 {
1410 int j;
1411
1412 /* operand I is marked. Check that it does not match any
1413 operands > I which are marked. */
1414 for (j = i + 1; j < MN10300_MAX_OPERANDS; j++)
1415 {
1416 if (((1 << j) & opcode->no_match_operands)
1417 && mn10300_reg_operands[i] == mn10300_reg_operands[j])
1418 {
1419 errmsg = _("Invalid register specification.");
1420 match = 0;
1421 goto error;
1422 }
1423 }
1424 }
1425 }
1426 }
1427
0f91d763
JL
1428 error:
1429 if (match == 0)
1430 {
1431 next_opcode = opcode + 1;
42aa2435 1432 if (!strcmp(next_opcode->name, opcode->name))
0f91d763
JL
1433 {
1434 opcode = next_opcode;
1435 continue;
1436 }
1437
1438 as_bad ("%s", errmsg);
1439 return;
1440 }
1441 break;
1442 }
1443
1444 while (isspace (*str))
1445 ++str;
1446
1447 if (*str != '\0')
e7ffb037 1448 as_bad (_("junk at end of line: `%s'"), str);
0f91d763
JL
1449
1450 input_line_pointer = str;
1451
778c521b
JL
1452 /* Determine the size of the instruction. */
1453 if (opcode->format == FMT_S0)
1217102f 1454 size = 1;
0f91d763 1455
778c521b 1456 if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
0f91d763 1457 size = 2;
0f91d763 1458
778c521b 1459 if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
1217102f 1460 size = 3;
0f91d763 1461
9e58f7c1 1462 /* start-sanitize-cygnus */
e7ffb037
JL
1463 if (opcode->format == FMT_D6)
1464 size = 3;
1465
573916e3 1466 if (opcode->format == FMT_D7 || opcode->format == FMT_D10)
e7ffb037 1467 size = 4;
573916e3
JL
1468
1469 if (opcode->format == FMT_D8)
1470 size = 6;
1471
1472 if (opcode->format == FMT_D9)
1473 size = 7;
9e58f7c1 1474 /* end-sanitize-cygnus */
e7ffb037 1475
778c521b 1476 if (opcode->format == FMT_S4)
1217102f 1477 size = 5;
0f91d763 1478
778c521b 1479 if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
1217102f 1480 size = 7;
0f91d763 1481
778c521b 1482 if (opcode->format == FMT_D2)
1217102f 1483 size = 4;
0f91d763 1484
778c521b 1485 if (opcode->format == FMT_D4)
1217102f
JL
1486 size = 6;
1487
590c50d8 1488 if (relaxable && fc > 0)
8ca71631 1489 {
590c50d8
ILT
1490 int type;
1491
1492 /* bCC */
1493 if (size == 2)
e7ffb037
JL
1494 {
1495 /* Handle bra specially. Basically treat it like jmp so
1496 that we automatically handle 8, 16 and 32 bit offsets
1497 correctly as well as jumps to an undefined address.
1498
1499 It is also important to not treat it like other bCC
1500 instructions since the long forms of bra is different
1501 from other bCC instructions. */
1502 if (opcode->opcode == 0xca00)
1503 type = 10;
1504 else
1505 type = 0;
1506 }
590c50d8
ILT
1507 /* call */
1508 else if (size == 5)
1509 type = 6;
1510 /* calls */
1511 else if (size == 4)
1512 type = 8;
1513 /* jmp */
1514 else if (size == 3 && opcode->opcode == 0xcc0000)
1515 type = 10;
1516 /* bCC (uncommon cases) */
1517 else
1518 type = 3;
d9a9c18f 1519
590c50d8
ILT
1520 f = frag_var (rs_machine_dependent, 8, 8 - size, type,
1521 fixups[0].exp.X_add_symbol,
1522 fixups[0].exp.X_add_number,
1523 (char *)fixups[0].opindex);
1524
1525 /* This is pretty hokey. We basically just care about the
1526 opcode, so we have to write out the first word big endian.
1527
1528 The exception is "call", which has two operands that we
1529 care about.
1530
1531 The first operand (the register list) happens to be in the
1532 first instruction word, and will be in the right place if
1533 we output the first word in big endian mode.
1534
1535 The second operand (stack size) is in the extension word,
1536 and we want it to appear as the first character in the extension
1537 word (as it appears in memory). Luckily, writing the extension
1538 word in big endian format will do what we want. */
1539 number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
1540 if (size > 8)
1541 {
1542 number_to_chars_bigendian (f + 4, extension, 4);
1543 number_to_chars_bigendian (f + 8, 0, size - 8);
1544 }
1545 else if (size > 4)
1546 number_to_chars_bigendian (f + 4, extension, size - 4);
1547 }
1548 else
d9a9c18f 1549 {
590c50d8
ILT
1550 /* Allocate space for the instruction. */
1551 f = frag_more (size);
1552
1553 /* Fill in bytes for the instruction. Note that opcode fields
1554 are written big-endian, 16 & 32bit immediates are written
1555 little endian. Egad. */
1556 if (opcode->format == FMT_S0
1557 || opcode->format == FMT_S1
1558 || opcode->format == FMT_D0
9e58f7c1 1559 /* start-sanitize-cygnus */
e7ffb037
JL
1560 || opcode->format == FMT_D6
1561 || opcode->format == FMT_D7
573916e3 1562 || opcode->format == FMT_D10
9e58f7c1 1563 /* end-sanitize-cygnus */
590c50d8
ILT
1564 || opcode->format == FMT_D1)
1565 {
1566 number_to_chars_bigendian (f, insn, size);
1567 }
1568 else if (opcode->format == FMT_S2
1569 && opcode->opcode != 0xdf0000
1570 && opcode->opcode != 0xde0000)
1571 {
1572 /* A format S2 instruction that is _not_ "ret" and "retf". */
1573 number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
1574 number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
1575 }
1576 else if (opcode->format == FMT_S2)
1577 {
1578 /* This must be a ret or retf, which is written entirely in
1579 big-endian format. */
1580 number_to_chars_bigendian (f, insn, 3);
1581 }
1582 else if (opcode->format == FMT_S4
1583 && opcode->opcode != 0xdc000000)
1584 {
1585 /* This must be a format S4 "call" instruction. What a pain. */
1586 unsigned long temp = (insn >> 8) & 0xffff;
1587 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1588 number_to_chars_littleendian (f + 1, temp, 2);
1589 number_to_chars_bigendian (f + 3, insn & 0xff, 1);
1590 number_to_chars_bigendian (f + 4, extension & 0xff, 1);
1591 }
1592 else if (opcode->format == FMT_S4)
1593 {
1594 /* This must be a format S4 "jmp" instruction. */
1595 unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
1596 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1597 number_to_chars_littleendian (f + 1, temp, 4);
1598 }
1599 else if (opcode->format == FMT_S6)
1600 {
1601 unsigned long temp = ((insn & 0xffffff) << 8)
1602 | ((extension >> 16) & 0xff);
1603 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1604 number_to_chars_littleendian (f + 1, temp, 4);
1605 number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
1606 number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1607 }
1608 else if (opcode->format == FMT_D2
1609 && opcode->opcode != 0xfaf80000
1610 && opcode->opcode != 0xfaf00000
1611 && opcode->opcode != 0xfaf40000)
1612 {
1613 /* A format D2 instruction where the 16bit immediate is
1614 really a single 16bit value, not two 8bit values. */
1615 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1616 number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
1617 }
1618 else if (opcode->format == FMT_D2)
1619 {
1620 /* A format D2 instruction where the 16bit immediate
1621 is really two 8bit immediates. */
1622 number_to_chars_bigendian (f, insn, 4);
1623 }
1624 else if (opcode->format == FMT_D4)
1625 {
1626 unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
1627 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1628 number_to_chars_littleendian (f + 2, temp, 4);
1629 }
1630 else if (opcode->format == FMT_D5)
1631 {
1632 unsigned long temp = ((insn & 0xffff) << 16)
1633 | ((extension >> 8) & 0xffff);
1634 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1635 number_to_chars_littleendian (f + 2, temp, 4);
1636 number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1637 }
9e58f7c1 1638 /* start-sanitize-cygnus */
573916e3
JL
1639 else if (opcode->format == FMT_D8)
1640 {
1641 unsigned long temp = ((insn & 0xff) << 16) | (extension & 0xffff);
1642 number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
1643 number_to_chars_bigendian (f + 3, (temp & 0xff), 1);
1644 number_to_chars_littleendian (f + 4, temp >> 8, 2);
1645 }
1646 else if (opcode->format == FMT_D9)
1647 {
1648 unsigned long temp = ((insn & 0xff) << 24) | (extension & 0xffffff);
1649 number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
1650 number_to_chars_littleendian (f + 3, temp, 4);
1651 }
9e58f7c1 1652 /* end-sanitize-cygnus */
d9a9c18f 1653
590c50d8
ILT
1654 /* Create any fixups. */
1655 for (i = 0; i < fc; i++)
d9a9c18f 1656 {
590c50d8
ILT
1657 const struct mn10300_operand *operand;
1658
1659 operand = &mn10300_operands[fixups[i].opindex];
1660 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1661 {
1662 reloc_howto_type *reloc_howto;
1663 int size;
1664 int offset;
1665 fixS *fixP;
d9a9c18f 1666
590c50d8 1667 reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
d9a9c18f 1668
590c50d8
ILT
1669 if (!reloc_howto)
1670 abort();
d9a9c18f 1671
590c50d8 1672 size = bfd_get_reloc_size (reloc_howto);
d9a9c18f 1673
590c50d8
ILT
1674 if (size < 1 || size > 4)
1675 abort();
d9a9c18f 1676
590c50d8
ILT
1677 offset = 4 - size;
1678 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1679 size, &fixups[i].exp,
1680 reloc_howto->pc_relative,
1681 fixups[i].reloc);
1682 }
d9a9c18f 1683 else
590c50d8
ILT
1684 {
1685 int reloc, pcrel, reloc_size, offset;
1686 fixS *fixP;
1687
1688 reloc = BFD_RELOC_NONE;
1689 /* How big is the reloc? Remember SPLIT relocs are
1690 implicitly 32bits. */
1691 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1692 reloc_size = 32;
9e58f7c1 1693 /* start-sanitize-cygnus */
5d626b5e 1694 else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
573916e3 1695 reloc_size = 24;
9e58f7c1 1696 /* end-sanitize-cygnus */
590c50d8
ILT
1697 else
1698 reloc_size = operand->bits;
1699
1700 /* Is the reloc pc-relative? */
1701 pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
1702
1703 /* Gross. This disgusting hack is to make sure we
1704 get the right offset for the 16/32 bit reloc in
1705 "call" instructions. Basically they're a pain
1706 because the reloc isn't at the end of the instruction. */
1707 if ((size == 5 || size == 7)
1708 && (((insn >> 24) & 0xff) == 0xcd
1709 || ((insn >> 24) & 0xff) == 0xdd))
1710 size -= 2;
1711
1712 /* Similarly for certain bit instructions which don't
1713 hav their 32bit reloc at the tail of the instruction. */
1714 if (size == 7
1715 && (((insn >> 16) & 0xffff) == 0xfe00
1716 || ((insn >> 16) & 0xffff) == 0xfe01
1717 || ((insn >> 16) & 0xffff) == 0xfe02))
1718 size -= 1;
d9a9c18f 1719
590c50d8 1720 offset = size - reloc_size / 8;
d9a9c18f 1721
590c50d8
ILT
1722 /* Choose a proper BFD relocation type. */
1723 if (pcrel)
1724 {
1725 if (reloc_size == 32)
1726 reloc = BFD_RELOC_32_PCREL;
1727 else if (reloc_size == 16)
1728 reloc = BFD_RELOC_16_PCREL;
1729 else if (reloc_size == 8)
1730 reloc = BFD_RELOC_8_PCREL;
1731 else
1732 abort ();
1733 }
d9a9c18f 1734 else
590c50d8
ILT
1735 {
1736 if (reloc_size == 32)
1737 reloc = BFD_RELOC_32;
1738 else if (reloc_size == 16)
1739 reloc = BFD_RELOC_16;
1740 else if (reloc_size == 8)
1741 reloc = BFD_RELOC_8;
1742 else
1743 abort ();
1744 }
1745
1746 /* Convert the size of the reloc into what fix_new_exp wants. */
1747 reloc_size = reloc_size / 8;
1748 if (reloc_size == 8)
1749 reloc_size = 0;
d9a9c18f 1750 else if (reloc_size == 16)
590c50d8
ILT
1751 reloc_size = 1;
1752 else if (reloc_size == 32)
1753 reloc_size = 2;
1754
1755 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1756 reloc_size, &fixups[i].exp, pcrel,
1757 ((bfd_reloc_code_real_type) reloc));
d9a9c18f 1758
590c50d8
ILT
1759 if (pcrel)
1760 fixP->fx_offset += offset;
1761 }
d9a9c18f
JL
1762 }
1763 }
0f91d763
JL
1764}
1765
1766
1767/* if while processing a fixup, a reloc really needs to be created */
1768/* then it is done here */
1769
1770arelent *
1771tc_gen_reloc (seg, fixp)
1772 asection *seg;
1773 fixS *fixp;
1774{
1775 arelent *reloc;
590c50d8 1776 reloc = (arelent *) xmalloc (sizeof (arelent));
a405bb3e 1777
0f91d763
JL
1778 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1779 if (reloc->howto == (reloc_howto_type *) NULL)
1780 {
1781 as_bad_where (fixp->fx_file, fixp->fx_line,
e7ffb037 1782 _("reloc %d not supported by object file format"),
a405bb3e 1783 (int)fixp->fx_r_type);
0f91d763
JL
1784 return NULL;
1785 }
a405bb3e
JL
1786 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1787
1788 if (fixp->fx_addsy && fixp->fx_subsy)
1789 {
e7ffb037
JL
1790
1791 if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
1792 || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
1793 {
1794 as_bad_where (fixp->fx_file, fixp->fx_line,
1795 "Difference of symbols in different sections is not supported");
1796 return NULL;
1797 }
1798
a405bb3e
JL
1799 reloc->sym_ptr_ptr = &bfd_abs_symbol;
1800 reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
1801 - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
1802 }
1803 else
1804 {
1805 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1806 reloc->addend = fixp->fx_offset;
1807 }
0f91d763
JL
1808 return reloc;
1809}
1810
1811int
1812md_estimate_size_before_relax (fragp, seg)
1813 fragS *fragp;
1814 asection *seg;
1815{
590c50d8
ILT
1816 if (fragp->fr_subtype == 0)
1817 return 2;
1818 if (fragp->fr_subtype == 3)
1819 return 3;
1820 if (fragp->fr_subtype == 6)
1821 {
1822 if (!S_IS_DEFINED (fragp->fr_symbol)
1823 || seg != S_GET_SEGMENT (fragp->fr_symbol))
1824 {
1825 fragp->fr_subtype = 7;
1826 return 7;
1827 }
1828 else
1829 return 5;
1830 }
1831 if (fragp->fr_subtype == 8)
1832 {
1833 if (!S_IS_DEFINED (fragp->fr_symbol)
1834 || seg != S_GET_SEGMENT (fragp->fr_symbol))
1835 {
1836 fragp->fr_subtype = 9;
1837 return 6;
1838 }
1839 else
1840 return 4;
1841 }
1842 if (fragp->fr_subtype == 10)
1843 {
1844 if (!S_IS_DEFINED (fragp->fr_symbol)
1845 || seg != S_GET_SEGMENT (fragp->fr_symbol))
1846 {
1847 fragp->fr_subtype = 12;
1848 return 5;
1849 }
1850 else
1851 return 2;
1852 }
0f91d763
JL
1853}
1854
1855long
1856md_pcrel_from (fixp)
1857 fixS *fixp;
1858{
0671e7f6
JL
1859 return fixp->fx_frag->fr_address;
1860#if 0
0f91d763
JL
1861 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1862 {
1863 /* The symbol is undefined. Let the linker figure it out. */
1864 return 0;
1865 }
1866 return fixp->fx_frag->fr_address + fixp->fx_where;
0671e7f6 1867#endif
0f91d763
JL
1868}
1869
1870int
1871md_apply_fix3 (fixp, valuep, seg)
1872 fixS *fixp;
1873 valueT *valuep;
1874 segT seg;
1875{
d9a9c18f
JL
1876 /* We shouldn't ever get here because linkrelax is nonzero. */
1877 abort ();
1217102f
JL
1878 fixp->fx_done = 1;
1879 return 0;
0f91d763
JL
1880}
1881
0f91d763
JL
1882/* Insert an operand value into an instruction. */
1883
bfe5059c
JL
1884static void
1885mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
1886 unsigned long *insnp;
1887 unsigned long *extensionp;
ae1b99e4 1888 const struct mn10300_operand *operand;
0f91d763
JL
1889 offsetT val;
1890 char *file;
1891 unsigned int line;
68328dc6 1892 unsigned int shift;
0f91d763 1893{
43d695a1
JL
1894 /* No need to check 32bit operands for a bit. Note that
1895 MN10300_OPERAND_SPLIT is an implicit 32bit operand. */
1896 if (operand->bits != 32
1897 && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
0f91d763
JL
1898 {
1899 long min, max;
1900 offsetT test;
573916e3
JL
1901 int bits;
1902
1903 bits = operand->bits;
9e58f7c1 1904 /* start-sanitize-cygnus */
573916e3
JL
1905 if (operand->flags & MN10300_OPERAND_24BIT)
1906 bits = 24;
9e58f7c1 1907 /* end-sanitize-cygnus */
0f91d763 1908
1217102f
JL
1909 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1910 {
573916e3
JL
1911 max = (1 << (bits - 1)) - 1;
1912 min = - (1 << (bits - 1));
1217102f
JL
1913 }
1914 else
0f91d763 1915 {
573916e3 1916 max = (1 << bits) - 1;
0f91d763
JL
1917 min = 0;
1918 }
1919
1920 test = val;
1921
1922
1923 if (test < (offsetT) min || test > (offsetT) max)
1924 {
1925 const char *err =
e7ffb037 1926 _("operand out of range (%s not between %ld and %ld)");
0f91d763
JL
1927 char buf[100];
1928
1929 sprint_value (buf, test);
1930 if (file == (char *) NULL)
1931 as_warn (err, buf, min, max);
1932 else
1933 as_warn_where (file, line, err, buf, min, max);
1934 }
1935 }
1936
cdde2f5c
JL
1937 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1938 {
ca82e4eb 1939 *insnp |= (val >> (32 - operand->bits)) & ((1 << operand->bits) - 1);
43d695a1
JL
1940 *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
1941 << operand->shift);
cdde2f5c 1942 }
9e58f7c1 1943 /* start-sanitize-cygnus */
573916e3
JL
1944 else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
1945 {
1946 *insnp |= (val >> (24 - operand->bits)) & ((1 << operand->bits) - 1);
1947 *extensionp |= ((val & ((1 << (24 - operand->bits)) - 1))
1948 << operand->shift);
1949 }
9e58f7c1 1950 /* end-sanitize-cygnus */
cdde2f5c 1951 else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
bfe5059c
JL
1952 {
1953 *insnp |= (((long) val & ((1 << operand->bits) - 1))
1954 << (operand->shift + shift));
1955
1956 if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1957 *insnp |= (((long) val & ((1 << operand->bits) - 1))
e7ffb037 1958 << (operand->shift + shift + operand->bits));
bfe5059c
JL
1959 }
1960 else
1961 {
1962 *extensionp |= (((long) val & ((1 << operand->bits) - 1))
1963 << (operand->shift + shift));
68328dc6 1964
bfe5059c
JL
1965 if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1966 *extensionp |= (((long) val & ((1 << operand->bits) - 1))
e7ffb037 1967 << (operand->shift + shift + operand->bits));
bfe5059c 1968 }
0f91d763 1969}
1217102f
JL
1970
1971static unsigned long
1972check_operand (insn, operand, val)
1973 unsigned long insn;
1974 const struct mn10300_operand *operand;
1975 offsetT val;
1976{
43d695a1
JL
1977 /* No need to check 32bit operands for a bit. Note that
1978 MN10300_OPERAND_SPLIT is an implicit 32bit operand. */
1979 if (operand->bits != 32
1980 && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1217102f
JL
1981 {
1982 long min, max;
1983 offsetT test;
573916e3
JL
1984 int bits;
1985
1986 bits = operand->bits;
9e58f7c1 1987 /* start-sanitize-cygnus */
573916e3
JL
1988 if (operand->flags & MN10300_OPERAND_24BIT)
1989 bits = 24;
9e58f7c1 1990 /* end-sanitize-cygnus */
1217102f
JL
1991
1992 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1993 {
573916e3
JL
1994 max = (1 << (bits - 1)) - 1;
1995 min = - (1 << (bits - 1));
1217102f
JL
1996 }
1997 else
1998 {
573916e3 1999 max = (1 << bits) - 1;
1217102f
JL
2000 min = 0;
2001 }
2002
2003 test = val;
2004
2005
2006 if (test < (offsetT) min || test > (offsetT) max)
2007 return 0;
2008 else
2009 return 1;
2010 }
2011 return 1;
2012}
3be26805
JL
2013
2014static void
2015set_arch_mach (mach)
2016 int mach;
2017{
2018 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, mach))
2019 as_warn (_("could not set architecture and machine"));
2020
2021 current_machine = mach;
2022}