Based on patch from Ronald F. Guilmette <rfg@monkeys.com>:
[external/binutils.git] / gas / config / tc-m68k.c
1 /* tc-m68k.c -- Assemble for the m68k family
2    Copyright (C) 1987, 91, 92, 93, 94, 95, 96, 1997
3    Free Software Foundation, Inc.
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 the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include <ctype.h>
23 #include "as.h"
24 #include "obstack.h"
25 #include "subsegs.h"
26
27 #include "opcode/m68k.h"
28 #include "m68k-parse.h"
29
30 /* This string holds the chars that always start a comment.  If the
31    pre-processor is disabled, these aren't very useful.  The macro
32    tc_comment_chars points to this.  We use this, rather than the
33    usual comment_chars, so that the --bitwise-or option will work.  */
34 #if (defined (OBJ_ELF) && ! defined (TE_PSOS) && ! defined (TE_LINUX)) || defined (TE_DELTA)
35 const char *m68k_comment_chars = "|#";
36 #else
37 const char *m68k_comment_chars = "|";
38 #endif
39
40 /* This array holds the chars that only start a comment at the beginning of
41    a line.  If the line seems to have the form '# 123 filename'
42    .line and .file directives will appear in the pre-processed output */
43 /* Note that input_file.c hand checks for '#' at the beginning of the
44    first line of the input file.  This is because the compiler outputs
45    #NO_APP at the beginning of its output. */
46 /* Also note that comments like this one will always work. */
47 const char line_comment_chars[] = "#";
48
49 const char line_separator_chars[] = "";
50
51 /* Chars that can be used to separate mant from exp in floating point nums */
52 CONST char EXP_CHARS[] = "eE";
53
54 /* Chars that mean this number is a floating point constant, as
55    in "0f12.456" or "0d1.2345e12".  */
56
57 CONST char FLT_CHARS[] = "rRsSfFdDxXeEpP";
58
59 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
60    changed in read.c .  Ideally it shouldn't have to know about it at all,
61    but nothing is ideal around here.  */
62
63 const int md_reloc_size = 8;    /* Size of relocation record */
64
65 /* Are we trying to generate PIC code?  If so, absolute references
66    ought to be made into linkage table references or pc-relative
67    references.  Not implemented.  For ELF there are other means 
68    to denote pic relocations.  */
69 int flag_want_pic;
70
71 static int flag_short_refs;     /* -l option */
72 static int flag_long_jumps;     /* -S option */
73
74 #ifdef REGISTER_PREFIX_OPTIONAL
75 int flag_reg_prefix_optional = REGISTER_PREFIX_OPTIONAL;
76 #else
77 int flag_reg_prefix_optional;
78 #endif
79
80 /* Whether --register-prefix-optional was used on the command line.  */
81 static int reg_prefix_optional_seen;
82
83 /* The floating point coprocessor to use by default.  */
84 static enum m68k_register m68k_float_copnum = COP1;
85
86 /* If this is non-zero, then references to number(%pc) will be taken
87    to refer to number, rather than to %pc + number.  */
88 static int m68k_abspcadd;
89
90 /* If this is non-zero, then the quick forms of the move, add, and sub
91    instructions are used when possible.  */
92 static int m68k_quick = 1;
93
94 /* If this is non-zero, then if the size is not specified for a base
95    or outer displacement, the assembler assumes that the size should
96    be 32 bits.  */
97 static int m68k_rel32 = 1;
98
99 /* The default width to use for an index register when using a base
100    displacement.  */
101 static enum m68k_size m68k_index_width_default = SIZE_LONG;
102
103 /* Its an arbitrary name:  This means I don't approve of it */
104 /* See flames below */
105 static struct obstack robyn;
106
107 #define TAB(x,y)        (((x)<<2)+(y))
108 #define TABTYPE(xy)     ((xy) >> 2)
109 #define BYTE            0
110 #define SHORT           1
111 #define LONG            2
112 #define SZ_UNDEF        3
113 #undef BRANCH
114 /* Case `g' except when BCC68000 is applicable.  */
115 #define ABRANCH         1
116 /* Coprocessor branches.  */
117 #define FBRANCH         2
118 /* Mode 7.2 -- program counter indirect with (16-bit) displacement,
119    supported on all cpus.  Widens to 32-bit absolute.  */
120 #define PCREL           3
121 /* For inserting an extra jmp instruction with long offset on 68000,
122    for expanding conditional branches.  (Not bsr or bra.)  Since the
123    68000 doesn't support 32-bit displacements for conditional
124    branches, we fake it by reversing the condition and branching
125    around a jmp with an absolute long operand.  */
126 #define BCC68000        4
127 /* For the DBcc "instructions".  If the displacement requires 32 bits,
128    the branch-around-a-jump game is played here too.  */
129 #define DBCC            5
130 /* Not currently used?  */
131 #define PCLEA           6
132 /* Mode AINDX (apc-relative) using PC, with variable target, might fit
133    in 16 or 8 bits.  */
134 #define PCINDEX         7
135
136 struct m68k_incant
137   {
138     const char *m_operands;
139     unsigned long m_opcode;
140     short m_opnum;
141     short m_codenum;
142     int m_arch;
143     struct m68k_incant *m_next;
144   };
145
146 #define getone(x)       ((((x)->m_opcode)>>16)&0xffff)
147 #define gettwo(x)       (((x)->m_opcode)&0xffff)
148
149 static const enum m68k_register m68000_control_regs[] = { 0 };
150 static const enum m68k_register m68010_control_regs[] = {
151   SFC, DFC, USP, VBR,
152   0
153 };
154 static const enum m68k_register m68020_control_regs[] = {
155   SFC, DFC, USP, VBR, CACR, CAAR, MSP, ISP,
156   0
157 };
158 static const enum m68k_register m68040_control_regs[] = {
159   SFC, DFC, CACR, TC, ITT0, ITT1, DTT0, DTT1,
160   USP, VBR, MSP, ISP, MMUSR, URP, SRP,
161   0
162 };
163 static const enum m68k_register m68060_control_regs[] = {
164   SFC, DFC, CACR, TC, ITT0, ITT1, DTT0, DTT1, BUSCR,
165   USP, VBR, URP, SRP, PCR,
166   0
167 };
168 static const enum m68k_register mcf5200_control_regs[] = {
169   CACR, TC, ITT0, ITT1, DTT0, DTT1, VBR, ROMBAR, 
170   RAMBAR0, RAMBAR1, MBAR,
171   0
172 };
173 #define cpu32_control_regs m68010_control_regs
174
175 static const enum m68k_register *control_regs;
176
177 /* internal form of a 68020 instruction */
178 struct m68k_it
179 {
180   const char *error;
181   const char *args;             /* list of opcode info */
182   int numargs;
183
184   int numo;                     /* Number of shorts in opcode */
185   short opcode[11];
186
187   struct m68k_op operands[6];
188
189   int nexp;                     /* number of exprs in use */
190   struct m68k_exp exprs[4];
191
192   int nfrag;                    /* Number of frags we have to produce */
193   struct
194     {
195       int fragoff;              /* Where in the current opcode the frag ends */
196       symbolS *fadd;
197       long foff;
198       int fragty;
199     }
200   fragb[4];
201
202   int nrel;                     /* Num of reloc strucs in use */
203   struct
204     {
205       int n;
206       expressionS exp;
207       char wid;
208       char pcrel;
209       /* In a pc relative address the difference between the address
210          of the offset and the address that the offset is relative
211          to.  This depends on the addressing mode.  Basically this
212          is the value to put in the offset field to address the
213          first byte of the offset, without regarding the special
214          significance of some values (in the branch instruction, for
215          example).  */
216       int pcrel_fix;
217 #ifdef OBJ_ELF
218       /* Whether this expression needs special pic relocation, and if
219          so, which.  */
220       enum pic_relocation pic_reloc;
221 #endif
222     }
223   reloc[5];                     /* Five is enough??? */
224 };
225
226 #define cpu_of_arch(x)          ((x) & (m68000up|mcf5200))
227 #define float_of_arch(x)        ((x) & mfloat)
228 #define mmu_of_arch(x)          ((x) & mmmu)
229
230 static struct m68k_it the_ins;  /* the instruction being assembled */
231
232 #define op(ex)          ((ex)->exp.X_op)
233 #define adds(ex)        ((ex)->exp.X_add_symbol)
234 #define subs(ex)        ((ex)->exp.X_op_symbol)
235 #define offs(ex)        ((ex)->exp.X_add_number)
236
237 /* Macros for adding things to the m68k_it struct */
238
239 #define addword(w)      the_ins.opcode[the_ins.numo++]=(w)
240
241 /* Like addword, but goes BEFORE general operands */
242 static void
243 insop (w, opcode)
244      int w;
245      struct m68k_incant *opcode;
246 {
247   int z;
248   for(z=the_ins.numo;z>opcode->m_codenum;--z)
249     the_ins.opcode[z]=the_ins.opcode[z-1];
250   for(z=0;z<the_ins.nrel;z++)
251     the_ins.reloc[z].n+=2;
252   for (z = 0; z < the_ins.nfrag; z++)
253     the_ins.fragb[z].fragoff++;
254   the_ins.opcode[opcode->m_codenum]=w;
255   the_ins.numo++;
256 }
257
258 /* The numo+1 kludge is so we can hit the low order byte of the prev word.
259    Blecch.  */
260 static void
261 add_fix (width, exp, pc_rel, pc_fix)
262      char width;
263      struct m68k_exp *exp;
264      int pc_rel;
265      int pc_fix;
266 {
267   the_ins.reloc[the_ins.nrel].n = ((width == 'B' || width == '3')
268                                    ? (the_ins.numo*2-1)
269                                    : (((width)=='b')
270                                       ? (the_ins.numo*2+1)
271                                       : (the_ins.numo*2)));
272   the_ins.reloc[the_ins.nrel].exp = exp->exp;
273   the_ins.reloc[the_ins.nrel].wid = width;
274   the_ins.reloc[the_ins.nrel].pcrel_fix = pc_fix;
275 #ifdef OBJ_ELF
276   the_ins.reloc[the_ins.nrel].pic_reloc = exp->pic_reloc;
277 #endif
278   the_ins.reloc[the_ins.nrel++].pcrel = pc_rel;
279 }
280
281 /* Cause an extra frag to be generated here, inserting up to 10 bytes
282    (that value is chosen in the frag_var call in md_assemble).  TYPE
283    is the subtype of the frag to be generated; its primary type is
284    rs_machine_dependent.
285
286    The TYPE parameter is also used by md_convert_frag_1 and
287    md_estimate_size_before_relax.  The appropriate type of fixup will
288    be emitted by md_convert_frag_1.
289
290    ADD becomes the FR_SYMBOL field of the frag, and OFF the FR_OFFSET.  */
291 static void
292 add_frag(add,off,type)
293      symbolS *add;
294      long off;
295      int type;
296 {
297   the_ins.fragb[the_ins.nfrag].fragoff=the_ins.numo;
298   the_ins.fragb[the_ins.nfrag].fadd=add;
299   the_ins.fragb[the_ins.nfrag].foff=off;
300   the_ins.fragb[the_ins.nfrag++].fragty=type;
301 }
302
303 #define isvar(ex) \
304   (op (ex) != O_constant && op (ex) != O_big)
305
306 static char *crack_operand PARAMS ((char *str, struct m68k_op *opP));
307 static int get_num PARAMS ((struct m68k_exp *exp, int ok));
308 static int reverse_16_bits PARAMS ((int in));
309 static int reverse_8_bits PARAMS ((int in));
310 static void install_gen_operand PARAMS ((int mode, int val));
311 static void install_operand PARAMS ((int mode, int val));
312 static void s_bss PARAMS ((int));
313 static void s_data1 PARAMS ((int));
314 static void s_data2 PARAMS ((int));
315 static void s_even PARAMS ((int));
316 static void s_proc PARAMS ((int));
317 static void mri_chip PARAMS ((void));
318 static void s_chip PARAMS ((int));
319 static void s_fopt PARAMS ((int));
320 static void s_opt PARAMS ((int));
321 static void s_reg PARAMS ((int));
322 static void s_restore PARAMS ((int));
323 static void s_save PARAMS ((int));
324 static void s_mri_if PARAMS ((int));
325 static void s_mri_else PARAMS ((int));
326 static void s_mri_endi PARAMS ((int));
327 static void s_mri_break PARAMS ((int));
328 static void s_mri_next PARAMS ((int));
329 static void s_mri_for PARAMS ((int));
330 static void s_mri_endf PARAMS ((int));
331 static void s_mri_repeat PARAMS ((int));
332 static void s_mri_until PARAMS ((int));
333 static void s_mri_while PARAMS ((int));
334 static void s_mri_endw PARAMS ((int));
335
336 static int current_architecture;
337
338 struct m68k_cpu {
339   unsigned long arch;
340   const char *name;
341   int alias;
342 };
343
344 static const struct m68k_cpu archs[] = {
345   { m68000, "68000", 0 },
346   { m68010, "68010", 0 },
347   { m68020, "68020", 0 },
348   { m68030, "68030", 0 },
349   { m68040, "68040", 0 },
350   { m68060, "68060", 0 },
351   { cpu32,  "cpu32", 0 },
352   { m68881, "68881", 0 },
353   { m68851, "68851", 0 },
354   { mcf5200, "5200", 0 },
355   /* Aliases (effectively, so far as gas is concerned) for the above
356      cpus.  */
357   { m68020, "68k", 1 },
358   { m68000, "68302", 1 },
359   { m68000, "68008", 1 },
360   { m68000, "68ec000", 1 },
361   { m68000, "68hc000", 1 },
362   { m68000, "68hc001", 1 },
363   { m68020, "68ec020", 1 },
364   { m68030, "68ec030", 1 },
365   { m68040, "68ec040", 1 },
366   { m68060, "68ec060", 1 },
367   { cpu32,  "68330", 1 },
368   { cpu32,  "68331", 1 },
369   { cpu32,  "68332", 1 },
370   { cpu32,  "68333", 1 },
371   { cpu32,  "68340", 1 },
372   { cpu32,  "68360", 1 },
373   { m68881, "68882", 1 },
374 };
375
376 static const int n_archs = sizeof (archs) / sizeof (archs[0]);
377
378 /* BCC68000 is for patching in an extra jmp instruction for long offsets
379    on the 68000.  The 68000 doesn't support long branches with branchs */
380
381 /* This table desribes how you change sizes for the various types of variable
382    size expressions.  This version only supports two kinds. */
383
384 /* Note that calls to frag_var need to specify the maximum expansion
385    needed; this is currently 10 bytes for DBCC.  */
386
387 /* The fields are:
388    How far Forward this mode will reach:
389    How far Backward this mode will reach:
390    How many bytes this mode will add to the size of the frag
391    Which mode to go to if the offset won't fit in this one
392    */
393 relax_typeS md_relax_table[] =
394 {
395   {1, 1, 0, 0},                 /* First entries aren't used */
396   {1, 1, 0, 0},                 /* For no good reason except */
397   {1, 1, 0, 0},                 /* that the VAX doesn't either */
398   {1, 1, 0, 0},
399
400   {(127), (-128), 0, TAB (ABRANCH, SHORT)},
401   {(32767), (-32768), 2, TAB (ABRANCH, LONG)},
402   {0, 0, 4, 0},
403   {1, 1, 0, 0},
404
405   {1, 1, 0, 0},                 /* FBRANCH doesn't come BYTE */
406   {(32767), (-32768), 2, TAB (FBRANCH, LONG)},
407   {0, 0, 4, 0},
408   {1, 1, 0, 0},
409
410   {1, 1, 0, 0},                 /* PCREL doesn't come BYTE */
411   {(32767), (-32768), 2, TAB (PCREL, LONG)},
412   {0, 0, 4, 0},
413   {1, 1, 0, 0},
414
415   {(127), (-128), 0, TAB (BCC68000, SHORT)},
416   {(32767), (-32768), 2, TAB (BCC68000, LONG)},
417   {0, 0, 6, 0},                 /* jmp long space */
418   {1, 1, 0, 0},
419
420   {1, 1, 0, 0},                 /* DBCC doesn't come BYTE */
421   {(32767), (-32768), 2, TAB (DBCC, LONG)},
422   {0, 0, 10, 0},                /* bra/jmp long space */
423   {1, 1, 0, 0},
424
425   {1, 1, 0, 0},                 /* PCLEA doesn't come BYTE */
426   {32767, -32768, 2, TAB (PCLEA, LONG)},
427   {0, 0, 6, 0},
428   {1, 1, 0, 0},
429
430   /* For, e.g., jmp pcrel indexed.  */
431   {125, -130, 0, TAB (PCINDEX, SHORT)},
432   {32765, -32770, 2, TAB (PCINDEX, LONG)},
433   {0, 0, 4, 0},
434   {1, 1, 0, 0},
435 };
436
437 /* These are the machine dependent pseudo-ops.  These are included so
438    the assembler can work on the output from the SUN C compiler, which
439    generates these.
440    */
441
442 /* This table describes all the machine specific pseudo-ops the assembler
443    has to support.  The fields are:
444    pseudo-op name without dot
445    function to call to execute this pseudo-op
446    Integer arg to pass to the function
447    */
448 const pseudo_typeS md_pseudo_table[] =
449 {
450   {"data1", s_data1, 0},
451   {"data2", s_data2, 0},
452   {"bss", s_bss, 0},
453   {"even", s_even, 0},
454   {"skip", s_space, 0},
455   {"proc", s_proc, 0},
456 #if defined (TE_SUN3) || defined (OBJ_ELF)
457   {"align", s_align_bytes, 0},
458 #endif
459 #ifdef OBJ_ELF
460   {"swbeg", s_ignore, 0},
461 #endif
462   {"extend", float_cons, 'x'},
463   {"ldouble", float_cons, 'x'},
464
465   /* The following pseudo-ops are supported for MRI compatibility.  */
466   {"chip", s_chip, 0},
467   {"comline", s_space, 1},
468   {"fopt", s_fopt, 0},
469   {"mask2", s_ignore, 0},
470   {"opt", s_opt, 0},
471   {"reg", s_reg, 0},
472   {"restore", s_restore, 0},
473   {"save", s_save, 0},
474
475   {"if", s_mri_if, 0},
476   {"if.b", s_mri_if, 'b'},
477   {"if.w", s_mri_if, 'w'},
478   {"if.l", s_mri_if, 'l'},
479   {"else", s_mri_else, 0},
480   {"else.s", s_mri_else, 's'},
481   {"else.l", s_mri_else, 'l'},
482   {"endi", s_mri_endi, 0},
483   {"break", s_mri_break, 0},
484   {"break.s", s_mri_break, 's'},
485   {"break.l", s_mri_break, 'l'},
486   {"next", s_mri_next, 0},
487   {"next.s", s_mri_next, 's'},
488   {"next.l", s_mri_next, 'l'},
489   {"for", s_mri_for, 0},
490   {"for.b", s_mri_for, 'b'},
491   {"for.w", s_mri_for, 'w'},
492   {"for.l", s_mri_for, 'l'},
493   {"endf", s_mri_endf, 0},
494   {"repeat", s_mri_repeat, 0},
495   {"until", s_mri_until, 0},
496   {"until.b", s_mri_until, 'b'},
497   {"until.w", s_mri_until, 'w'},
498   {"until.l", s_mri_until, 'l'},
499   {"while", s_mri_while, 0},
500   {"while.b", s_mri_while, 'b'},
501   {"while.w", s_mri_while, 'w'},
502   {"while.l", s_mri_while, 'l'},
503   {"endw", s_mri_endw, 0},
504
505   {0, 0, 0}
506 };
507
508
509 /* The mote pseudo ops are put into the opcode table, since they
510    don't start with a . they look like opcodes to gas.
511    */
512 extern void obj_coff_section ();
513
514 CONST pseudo_typeS mote_pseudo_table[] =
515 {
516
517   {"dcl", cons, 4},
518   {"dc", cons, 2},
519   {"dcw", cons, 2},
520   {"dcb", cons, 1},
521
522   {"dsl", s_space, 4},
523   {"ds", s_space, 2},
524   {"dsw", s_space, 2},
525   {"dsb", s_space, 1},
526
527   {"xdef", s_globl, 0},
528 #ifdef OBJ_ELF
529   {"align", s_align_bytes, 0},
530 #else
531   {"align", s_align_ptwo, 0},
532 #endif
533 #ifdef M68KCOFF
534   {"sect", obj_coff_section, 0},
535   {"section", obj_coff_section, 0},
536 #endif
537   {0, 0, 0}
538 };
539
540 #define issbyte(x)      ((x)>=-128 && (x)<=127)
541 #define isubyte(x)      ((x)>=0 && (x)<=255)
542 #define issword(x)      ((x)>=-32768 && (x)<=32767)
543 #define isuword(x)      ((x)>=0 && (x)<=65535)
544
545 #define isbyte(x)       ((x)>= -255 && (x)<=255)
546 #define isword(x)       ((x)>=-65536 && (x)<=65535)
547 #define islong(x)       (1)
548
549 extern char *input_line_pointer;
550
551 static char mklower_table[256];
552 #define mklower(c) (mklower_table[(unsigned char)(c)])
553 static char notend_table[256];
554 static char alt_notend_table[256];
555 #define notend(s)                                               \
556   (! (notend_table[(unsigned char) *s]                          \
557       || (*s == ':'                                             \
558           && alt_notend_table[(unsigned char) s[1]])))
559
560 #if defined (M68KCOFF) && !defined (BFD_ASSEMBLER)
561
562 #ifdef NO_PCREL_RELOCS
563
564 int
565 make_pcrel_absolute(fixP, add_number)
566     fixS *fixP;
567     long *add_number;
568 {
569   register unsigned char *opcode = fixP->fx_frag->fr_opcode;
570
571   /* rewrite the PC relative instructions to absolute address ones.
572    * these are rumoured to be faster, and the apollo linker refuses
573    * to deal with the PC relative relocations.
574    */
575   if (opcode[0] == 0x60 && opcode[1] == 0xff) /* BRA -> JMP */
576     {
577       opcode[0] = 0x4e;
578       opcode[1] = 0xf9;
579     }
580   else if (opcode[0] == 0x61 && opcode[1] == 0xff) /* BSR -> JSR */
581     {
582       opcode[0] = 0x4e;
583       opcode[1] = 0xb9;
584     }
585   else
586     as_fatal ("Unknown PC relative instruction");
587   *add_number -= 4;
588   return 0;
589 }
590
591 #endif /* NO_PCREL_RELOCS */
592
593 short
594 tc_coff_fix2rtype (fixP)
595      fixS *fixP;
596 {
597   if (fixP->fx_tcbit && fixP->fx_size == 4)
598     return R_RELLONG_NEG;
599 #ifdef NO_PCREL_RELOCS
600   know (fixP->fx_pcrel == 0);
601   return (fixP->fx_size == 1 ? R_RELBYTE
602           : fixP->fx_size == 2 ? R_DIR16
603           : R_DIR32);
604 #else
605   return (fixP->fx_pcrel ?
606           (fixP->fx_size == 1 ? R_PCRBYTE :
607            fixP->fx_size == 2 ? R_PCRWORD :
608            R_PCRLONG) :
609           (fixP->fx_size == 1 ? R_RELBYTE :
610            fixP->fx_size == 2 ? R_RELWORD :
611            R_RELLONG));
612 #endif
613 }
614
615 #endif
616
617 #ifdef OBJ_ELF
618
619 /* Compute the relocation code for a fixup of SIZE bytes, using pc
620    relative relocation if PCREL is non-zero.  PIC says whether a special
621    pic relocation was requested.  */
622
623 static bfd_reloc_code_real_type get_reloc_code
624   PARAMS ((int, int, enum pic_relocation));
625
626 static bfd_reloc_code_real_type
627 get_reloc_code (size, pcrel, pic)
628      int size;
629      int pcrel;
630      enum pic_relocation pic;
631 {
632   switch (pic)
633     {
634     case pic_got_pcrel:
635       switch (size)
636         {
637         case 1:
638           return BFD_RELOC_8_GOT_PCREL;
639         case 2:
640           return BFD_RELOC_16_GOT_PCREL;
641         case 4:
642           return BFD_RELOC_32_GOT_PCREL;
643         }
644       break;
645
646     case pic_got_off:
647       switch (size)
648         {
649         case 1:
650           return BFD_RELOC_8_GOTOFF;
651         case 2:
652           return BFD_RELOC_16_GOTOFF;
653         case 4:
654           return BFD_RELOC_32_GOTOFF;
655         }
656       break;
657
658     case pic_plt_pcrel:
659       switch (size)
660         {
661         case 1:
662           return BFD_RELOC_8_PLT_PCREL;
663         case 2:
664           return BFD_RELOC_16_PLT_PCREL;
665         case 4:
666           return BFD_RELOC_32_PLT_PCREL;
667         }
668       break;
669
670     case pic_plt_off:
671       switch (size)
672         {
673         case 1:
674           return BFD_RELOC_8_PLTOFF;
675         case 2:
676           return BFD_RELOC_16_PLTOFF;
677         case 4:
678           return BFD_RELOC_32_PLTOFF;
679         }
680       break;
681
682     case pic_none:
683       if (pcrel)
684         {
685           switch (size)
686             {
687             case 1:
688               return BFD_RELOC_8_PCREL;
689             case 2:
690               return BFD_RELOC_16_PCREL;
691             case 4:
692               return BFD_RELOC_32_PCREL;
693             }
694         }
695       else
696         {
697           switch (size)
698             {
699             case 1:
700               return BFD_RELOC_8;
701             case 2:
702               return BFD_RELOC_16;
703             case 4:
704               return BFD_RELOC_32;
705             }
706         }
707     }
708
709   as_bad ("Can not do %d byte %s%srelocation", size,
710           pcrel ? "pc-relative " : "",
711           pic == pic_none ? "" : "pic ");
712   return BFD_RELOC_NONE;
713 }
714
715 /* Here we decide which fixups can be adjusted to make them relative
716    to the beginning of the section instead of the symbol.  Basically
717    we need to make sure that the dynamic relocations are done
718    correctly, so in some cases we force the original symbol to be
719    used.  */
720 int
721 tc_m68k_fix_adjustable (fixP)
722      fixS *fixP;
723 {
724   /* Prevent all adjustments to global symbols. */
725   if (S_IS_EXTERNAL (fixP->fx_addsy))
726     return 0;
727
728   /* adjust_reloc_syms doesn't know about the GOT */
729   switch (fixP->fx_r_type)
730     {
731     case BFD_RELOC_8_GOT_PCREL:
732     case BFD_RELOC_16_GOT_PCREL:
733     case BFD_RELOC_32_GOT_PCREL:
734     case BFD_RELOC_8_GOTOFF:
735     case BFD_RELOC_16_GOTOFF:
736     case BFD_RELOC_32_GOTOFF:
737     case BFD_RELOC_8_PLT_PCREL:
738     case BFD_RELOC_16_PLT_PCREL:
739     case BFD_RELOC_32_PLT_PCREL:
740     case BFD_RELOC_8_PLTOFF:
741     case BFD_RELOC_16_PLTOFF:
742     case BFD_RELOC_32_PLTOFF:
743       return 0;
744
745     default:
746       return 1;
747     }
748 }
749
750 #else /* !OBJ_ELF */
751
752 #define get_reloc_code(SIZE,PCREL,OTHER) NO_RELOC
753
754 #endif /* OBJ_ELF */
755
756 #ifdef BFD_ASSEMBLER
757
758 arelent *
759 tc_gen_reloc (section, fixp)
760      asection *section;
761      fixS *fixp;
762 {
763   arelent *reloc;
764   bfd_reloc_code_real_type code;
765
766   if (fixp->fx_tcbit)
767     abort ();
768
769   if (fixp->fx_r_type != BFD_RELOC_NONE)
770     {
771       code = fixp->fx_r_type;
772
773       /* Since DIFF_EXPR_OK is defined in tc-m68k.h, it is possible
774          that fixup_segment converted a non-PC relative reloc into a
775          PC relative reloc.  In such a case, we need to convert the
776          reloc code.  */
777       if (fixp->fx_pcrel)
778         {
779           switch (code)
780             {
781             case BFD_RELOC_8:
782               code = BFD_RELOC_8_PCREL;
783               break;
784             case BFD_RELOC_16:
785               code = BFD_RELOC_16_PCREL;
786               break;
787             case BFD_RELOC_32:
788               code = BFD_RELOC_32_PCREL;
789               break;
790             case BFD_RELOC_8_PCREL:
791             case BFD_RELOC_16_PCREL:
792             case BFD_RELOC_32_PCREL:
793             case BFD_RELOC_8_GOT_PCREL:
794             case BFD_RELOC_16_GOT_PCREL:
795             case BFD_RELOC_32_GOT_PCREL:
796             case BFD_RELOC_8_GOTOFF:
797             case BFD_RELOC_16_GOTOFF:
798             case BFD_RELOC_32_GOTOFF:
799             case BFD_RELOC_8_PLT_PCREL:
800             case BFD_RELOC_16_PLT_PCREL:
801             case BFD_RELOC_32_PLT_PCREL:
802             case BFD_RELOC_8_PLTOFF:
803             case BFD_RELOC_16_PLTOFF:
804             case BFD_RELOC_32_PLTOFF:
805               break;
806             default:
807               as_bad_where (fixp->fx_file, fixp->fx_line,
808                             "Cannot make %s relocation PC relative",
809                             bfd_get_reloc_code_name (code));
810             }
811         }
812     }
813   else
814     {
815 #define F(SZ,PCREL)             (((SZ) << 1) + (PCREL))
816       switch (F (fixp->fx_size, fixp->fx_pcrel))
817         {
818 #define MAP(SZ,PCREL,TYPE)      case F(SZ,PCREL): code = (TYPE); break
819           MAP (1, 0, BFD_RELOC_8);
820           MAP (2, 0, BFD_RELOC_16);
821           MAP (4, 0, BFD_RELOC_32);
822           MAP (1, 1, BFD_RELOC_8_PCREL);
823           MAP (2, 1, BFD_RELOC_16_PCREL);
824           MAP (4, 1, BFD_RELOC_32_PCREL);
825         default:
826           abort ();
827         }
828     }
829 #undef F
830 #undef MAP
831
832   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
833   assert (reloc != 0);
834   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
835   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
836 #ifndef OBJ_ELF
837   if (fixp->fx_pcrel)
838     reloc->addend = fixp->fx_addnumber;
839   else
840     reloc->addend = 0;
841 #else
842   if (!fixp->fx_pcrel)
843     reloc->addend = fixp->fx_addnumber;
844   else
845     reloc->addend = (section->vma
846                      + (fixp->fx_pcrel_adjust == 64
847                         ? -1 : fixp->fx_pcrel_adjust)
848                      + fixp->fx_addnumber
849                      + md_pcrel_from (fixp));
850 #endif
851
852   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
853   assert (reloc->howto != 0);
854
855   return reloc;
856 }
857
858 #endif /* BFD_ASSEMBLER */
859
860 /* Handle of the OPCODE hash table.  NULL means any use before
861    m68k_ip_begin() will crash.  */
862 static struct hash_control *op_hash;
863 \f
864 /* Assemble an m68k instruction.  */
865
866 void
867 m68k_ip (instring)
868      char *instring;
869 {
870   register char *p;
871   register struct m68k_op *opP;
872   register struct m68k_incant *opcode;
873   register const char *s;
874   register int tmpreg = 0, baseo = 0, outro = 0, nextword;
875   char *pdot, *pdotmove;
876   enum m68k_size siz1, siz2;
877   char c;
878   int losing;
879   int opsfound;
880   char *crack_operand ();
881   LITTLENUM_TYPE words[6];
882   LITTLENUM_TYPE *wordp;
883   unsigned long ok_arch = 0;
884
885   if (*instring == ' ')
886     instring++;                 /* skip leading whitespace */
887
888   /* Scan up to end of operation-code, which MUST end in end-of-string
889      or exactly 1 space. */
890   pdot = 0;
891   for (p = instring; *p != '\0'; p++)
892     {
893       if (*p == ' ')
894         break;
895       if (*p == '.')
896         pdot = p;
897     }
898
899   if (p == instring)
900     {
901       the_ins.error = "No operator";
902       return;
903     }
904
905   /* p now points to the end of the opcode name, probably whitespace.
906      Make sure the name is null terminated by clobbering the
907      whitespace, look it up in the hash table, then fix it back.
908      Remove a dot, first, since the opcode tables have none.  */
909   if (pdot != NULL)
910     {
911       for (pdotmove = pdot; pdotmove < p; pdotmove++)
912         *pdotmove = pdotmove[1];
913       p--;
914     }
915
916   c = *p;
917   *p = '\0';
918   opcode = (struct m68k_incant *) hash_find (op_hash, instring);
919   *p = c;
920
921   if (pdot != NULL)
922     {
923       for (pdotmove = p; pdotmove > pdot; pdotmove--)
924         *pdotmove = pdotmove[-1];
925       *pdot = '.';
926       ++p;
927     }
928
929   if (opcode == NULL)
930     {
931       the_ins.error = "Unknown operator";
932       return;
933     }
934
935   /* found a legitimate opcode, start matching operands */
936   while (*p == ' ')
937     ++p;
938
939   if (opcode->m_operands == 0)
940     {
941       char *old = input_line_pointer;
942       *old = '\n';
943       input_line_pointer = p;
944       /* Ahh - it's a motorola style psuedo op */
945       mote_pseudo_table[opcode->m_opnum].poc_handler
946         (mote_pseudo_table[opcode->m_opnum].poc_val);
947       input_line_pointer = old;
948       *old = 0;
949
950       return;
951     }
952
953   if (flag_mri && opcode->m_opnum == 0)
954     {
955       /* In MRI mode, random garbage is allowed after an instruction
956          which accepts no operands.  */
957       the_ins.args = opcode->m_operands;
958       the_ins.numargs = opcode->m_opnum;
959       the_ins.numo = opcode->m_codenum;
960       the_ins.opcode[0] = getone (opcode);
961       the_ins.opcode[1] = gettwo (opcode);
962       return;
963     }
964
965   for (opP = &the_ins.operands[0]; *p; opP++)
966     {
967       p = crack_operand (p, opP);
968
969       if (opP->error)
970         {
971           the_ins.error = opP->error;
972           return;
973         }
974     }
975
976   opsfound = opP - &the_ins.operands[0];
977
978   /* This ugly hack is to support the floating pt opcodes in their
979      standard form.  Essentially, we fake a first enty of type COP#1 */
980   if (opcode->m_operands[0] == 'I')
981     {
982       int n;
983
984       for (n = opsfound; n > 0; --n)
985         the_ins.operands[n] = the_ins.operands[n - 1];
986
987       memset ((char *) (&the_ins.operands[0]), '\0',
988               sizeof (the_ins.operands[0]));
989       the_ins.operands[0].mode = CONTROL;
990       the_ins.operands[0].reg = m68k_float_copnum;
991       opsfound++;
992     }
993
994   /* We've got the operands.  Find an opcode that'll accept them */
995   for (losing = 0;;)
996     {
997       /* If we didn't get the right number of ops, or we have no
998          common model with this pattern then reject this pattern. */
999
1000       ok_arch |= opcode->m_arch;
1001       if (opsfound != opcode->m_opnum
1002           || ((opcode->m_arch & current_architecture) == 0))
1003         ++losing;
1004       else
1005         {
1006           for (s = opcode->m_operands, opP = &the_ins.operands[0];
1007                *s && !losing;
1008                s += 2, opP++)
1009             {
1010               /* Warning: this switch is huge! */
1011               /* I've tried to organize the cases into this order:
1012                  non-alpha first, then alpha by letter.  Lower-case
1013                  goes directly before uppercase counterpart.  */
1014               /* Code with multiple case ...: gets sorted by the lowest
1015                  case ... it belongs to.  I hope this makes sense.  */
1016               switch (*s)
1017                 {
1018                 case '!':
1019                   switch (opP->mode)
1020                     {
1021                     case IMMED:
1022                     case DREG:
1023                     case AREG:
1024                     case FPREG:
1025                     case CONTROL:
1026                     case AINC:
1027                     case ADEC:
1028                     case REGLST:
1029                       losing++;
1030                       break;
1031                     default:
1032                       break;
1033                     }
1034                   break;
1035
1036                 case '`':
1037                   switch (opP->mode)
1038                     {
1039                     case IMMED:
1040                     case DREG:
1041                     case AREG:
1042                     case FPREG:
1043                     case CONTROL:
1044                     case AINC:
1045                     case REGLST:
1046                     case AINDR:
1047                       losing++;
1048                       break;
1049                     default:
1050                       break;
1051                     }
1052                   break;
1053
1054                 case '<':
1055                   switch (opP->mode)
1056                     {
1057                     case DREG:
1058                     case AREG:
1059                     case FPREG:
1060                     case CONTROL:
1061                     case IMMED:
1062                     case ADEC:
1063                     case REGLST:
1064                       losing++;
1065                       break;
1066                     default:
1067                       break;
1068                     }
1069                   break;
1070
1071                 case '>':
1072                   switch (opP->mode)
1073                     {
1074                     case DREG:
1075                     case AREG:
1076                     case FPREG:
1077                     case CONTROL:
1078                     case IMMED:
1079                     case AINC:
1080                     case REGLST:
1081                       losing++;
1082                       break;
1083                     case ABSL:
1084                       break;
1085                     default:
1086                       if (opP->reg == PC
1087                           || opP->reg == ZPC)
1088                         losing++;
1089                       break;
1090                     }
1091                   break;
1092
1093                 case 'm':
1094                   switch (opP->mode)
1095                     {
1096                     case DREG:
1097                     case AREG:
1098                     case AINDR:
1099                     case AINC:
1100                     case ADEC:
1101                       break;
1102                     default:
1103                       losing++;
1104                     }
1105                   break;
1106
1107                 case 'n':
1108                   switch (opP->mode)
1109                     {
1110                     case DISP:
1111                       break;
1112                     default:
1113                       losing++;
1114                     }
1115                   break;
1116
1117                 case 'o':
1118                   switch (opP->mode)
1119                     {
1120                     case BASE:
1121                     case ABSL:
1122                     case IMMED:
1123                       break;
1124                     default:
1125                       losing++;
1126                     }
1127                   break;
1128
1129                 case 'p':
1130                   switch (opP->mode)
1131                     {
1132                     case DREG:
1133                     case AREG:
1134                     case AINDR:
1135                     case AINC:
1136                     case ADEC:
1137                     case DISP:
1138                       break;
1139                     default:
1140                       losing++;
1141                     }
1142                   break;
1143
1144                 case '#':
1145                   if (opP->mode != IMMED)
1146                     losing++;
1147                   else if (s[1] == 'b'
1148                            && ! isvar (&opP->disp)
1149                            && (opP->disp.exp.X_op != O_constant
1150                                || ! isbyte (opP->disp.exp.X_add_number)))
1151                     losing++;
1152                   else if (s[1] == 'w'
1153                            && ! isvar (&opP->disp)
1154                            && (opP->disp.exp.X_op != O_constant
1155                                || ! isword (opP->disp.exp.X_add_number)))
1156                     losing++;
1157                   else if (s[1] == 'W'
1158                            && ! isvar (&opP->disp)
1159                            && (opP->disp.exp.X_op != O_constant
1160                                || ! issword (opP->disp.exp.X_add_number)))
1161                     losing++;
1162                   break;
1163
1164                 case '^':
1165                 case 'T':
1166                   if (opP->mode != IMMED)
1167                     losing++;
1168                   break;
1169
1170                 case '$':
1171                   if (opP->mode == AREG
1172                       || opP->mode == CONTROL
1173                       || opP->mode == FPREG
1174                       || opP->mode == IMMED
1175                       || opP->mode == REGLST
1176                       || (opP->mode != ABSL
1177                           && (opP->reg == PC
1178                               || opP->reg == ZPC)))
1179                     losing++;
1180                   break;
1181
1182                 case '%':
1183                   if (opP->mode == CONTROL
1184                       || opP->mode == FPREG
1185                       || opP->mode == REGLST
1186                       || opP->mode == IMMED
1187                       || (opP->mode != ABSL
1188                           && (opP->reg == PC
1189                               || opP->reg == ZPC)))
1190                     losing++;
1191                   break;
1192
1193                 case '&':
1194                   switch (opP->mode)
1195                     {
1196                     case DREG:
1197                     case AREG:
1198                     case FPREG:
1199                     case CONTROL:
1200                     case IMMED:
1201                     case AINC:
1202                     case ADEC:
1203                     case REGLST:
1204                       losing++;
1205                       break;
1206                     case ABSL:
1207                       break;
1208                     default:
1209                       if (opP->reg == PC
1210                           || opP->reg == ZPC)
1211                         losing++;
1212                       break;
1213                     }
1214                   break;
1215
1216                 case '*':
1217                   if (opP->mode == CONTROL
1218                       || opP->mode == FPREG
1219                       || opP->mode == REGLST)
1220                     losing++;
1221                   break;
1222
1223                 case '+':
1224                   if (opP->mode != AINC)
1225                     losing++;
1226                   break;
1227
1228                 case '-':
1229                   if (opP->mode != ADEC)
1230                     losing++;
1231                   break;
1232
1233                 case '/':
1234                   switch (opP->mode)
1235                     {
1236                     case AREG:
1237                     case CONTROL:
1238                     case FPREG:
1239                     case AINC:
1240                     case ADEC:
1241                     case IMMED:
1242                     case REGLST:
1243                       losing++;
1244                       break;
1245                     default:
1246                       break;
1247                     }
1248                   break;
1249
1250                 case ';':
1251                   switch (opP->mode)
1252                     {
1253                     case AREG:
1254                     case CONTROL:
1255                     case FPREG:
1256                     case REGLST:
1257                       losing++;
1258                       break;
1259                     default:
1260                       break;
1261                     }
1262                   break;
1263
1264                 case '?':
1265                   switch (opP->mode)
1266                     {
1267                     case AREG:
1268                     case CONTROL:
1269                     case FPREG:
1270                     case AINC:
1271                     case ADEC:
1272                     case IMMED:
1273                     case REGLST:
1274                       losing++;
1275                       break;
1276                     case ABSL:
1277                       break;
1278                     default:
1279                       if (opP->reg == PC || opP->reg == ZPC)
1280                         losing++;
1281                       break;
1282                     }
1283                   break;
1284
1285                 case '@':
1286                   switch (opP->mode)
1287                     {
1288                     case AREG:
1289                     case CONTROL:
1290                     case FPREG:
1291                     case IMMED:
1292                     case REGLST:
1293                       losing++;
1294                       break;
1295                     default:
1296                       break;
1297                     }
1298                   break;
1299
1300                 case '~':       /* For now! (JF FOO is this right?) */
1301                   switch (opP->mode)
1302                     {
1303                     case DREG:
1304                     case AREG:
1305                     case CONTROL:
1306                     case FPREG:
1307                     case IMMED:
1308                     case REGLST:
1309                       losing++;
1310                       break;
1311                     case ABSL:
1312                       break;
1313                     default:
1314                       if (opP->reg == PC
1315                           || opP->reg == ZPC)
1316                         losing++;
1317                       break;
1318                     }
1319                   break;
1320
1321                 case '3':
1322                   if (opP->mode != CONTROL
1323                       || (opP->reg != TT0 && opP->reg != TT1))
1324                     losing++;
1325                   break;
1326
1327                 case 'A':
1328                   if (opP->mode != AREG)
1329                     losing++;
1330                   break;
1331
1332                 case 'a':
1333                   if (opP->mode != AINDR)
1334                     ++losing;
1335                   break;
1336
1337                 case 'B':       /* FOO */
1338                   if (opP->mode != ABSL
1339                       || (flag_long_jumps
1340                           && strncmp (instring, "jbsr", 4) == 0))
1341                     losing++;
1342                   break;
1343
1344                 case 'C':
1345                   if (opP->mode != CONTROL || opP->reg != CCR)
1346                     losing++;
1347                   break;
1348
1349                 case 'd':
1350                   if (opP->mode != DISP
1351                       || opP->reg < ADDR0
1352                       || opP->reg > ADDR7)
1353                     losing++;
1354                   break;
1355
1356                 case 'D':
1357                   if (opP->mode != DREG)
1358                     losing++;
1359                   break;
1360
1361                 case 'F':
1362                   if (opP->mode != FPREG)
1363                     losing++;
1364                   break;
1365
1366                 case 'I':
1367                   if (opP->mode != CONTROL
1368                       || opP->reg < COP0
1369                       || opP->reg > COP7)
1370                     losing++;
1371                   break;
1372
1373                 case 'J':
1374                   if (opP->mode != CONTROL
1375                       || opP->reg < USP
1376                       || opP->reg > last_movec_reg)
1377                     losing++;
1378                   else
1379                     {
1380                       const enum m68k_register *rp;
1381                       for (rp = control_regs; *rp; rp++)
1382                         if (*rp == opP->reg)
1383                           break;
1384                       if (*rp == 0)
1385                         losing++;
1386                     }
1387                   break;
1388
1389                 case 'k':
1390                   if (opP->mode != IMMED)
1391                     losing++;
1392                   break;
1393
1394                 case 'l':
1395                 case 'L':
1396                   if (opP->mode == DREG
1397                       || opP->mode == AREG
1398                       || opP->mode == FPREG)
1399                     {
1400                       if (s[1] == '8')
1401                         losing++;
1402                       else
1403                         {
1404                           switch (opP->mode)
1405                             {
1406                             case DREG:
1407                               opP->mask = 1 << (opP->reg - DATA0);
1408                               break;
1409                             case AREG:
1410                               opP->mask = 1 << (opP->reg - ADDR0 + 8);
1411                               break;
1412                             case FPREG:
1413                               opP->mask = 1 << (opP->reg - FP0 + 16);
1414                               break;
1415                             default:
1416                               abort ();
1417                             }
1418                           opP->mode = REGLST;
1419                         }
1420                     }
1421                   else if (opP->mode == CONTROL)
1422                     {
1423                       if (s[1] != '8')
1424                         losing++;
1425                       else
1426                         {
1427                           switch (opP->reg)
1428                             {
1429                             case FPI:
1430                               opP->mask = 1 << 24;
1431                               break;
1432                             case FPS:
1433                               opP->mask = 1 << 25;
1434                               break;
1435                             case FPC:
1436                               opP->mask = 1 << 26;
1437                               break;
1438                             default:
1439                               losing++;
1440                               break;
1441                             }
1442                           opP->mode = REGLST;
1443                         }
1444                     }
1445                   else if (opP->mode == ABSL
1446                            && opP->disp.size == SIZE_UNSPEC
1447                            && opP->disp.exp.X_op == O_constant)
1448                     {
1449                       /* This is what the MRI REG pseudo-op generates.  */
1450                       opP->mode = REGLST;
1451                       opP->mask = opP->disp.exp.X_add_number;
1452                     }
1453                   else if (opP->mode != REGLST)
1454                     losing++;
1455                   else if (s[1] == '8' && (opP->mask & 0x0ffffff) != 0)
1456                     losing++;
1457                   else if (s[1] == '3' && (opP->mask & 0x7000000) != 0)
1458                     losing++;
1459                   break;
1460
1461                 case 'M':
1462                   if (opP->mode != IMMED)
1463                     losing++;
1464                   else if (opP->disp.exp.X_op != O_constant
1465                            || ! issbyte (opP->disp.exp.X_add_number))
1466                     losing++;
1467                   else if (! m68k_quick
1468                            && instring[3] != 'q'
1469                            && instring[4] != 'q')
1470                     losing++;
1471                   break;
1472
1473                 case 'O':
1474                   if (opP->mode != DREG
1475                       && opP->mode != IMMED
1476                       && opP->mode != ABSL)
1477                     losing++;
1478                   break;
1479
1480                 case 'Q':
1481                   if (opP->mode != IMMED)
1482                     losing++;
1483                   else if (opP->disp.exp.X_op != O_constant
1484                            || opP->disp.exp.X_add_number < 1
1485                            || opP->disp.exp.X_add_number > 8)
1486                     losing++;
1487                   else if (! m68k_quick
1488                            && (strncmp (instring, "add", 3) == 0
1489                                || strncmp (instring, "sub", 3) == 0)
1490                            && instring[3] != 'q')
1491                     losing++;
1492                   break;
1493
1494                 case 'R':
1495                   if (opP->mode != DREG && opP->mode != AREG)
1496                     losing++;
1497                   break;
1498
1499                 case 'r':
1500                   if (opP->mode != AINDR
1501                       && (opP->mode != BASE
1502                           || (opP->reg != 0
1503                               && opP->reg != ZADDR0)
1504                           || opP->disp.exp.X_op != O_absent
1505                           || ((opP->index.reg < DATA0
1506                                || opP->index.reg > DATA7)
1507                               && (opP->index.reg < ADDR0
1508                                   || opP->index.reg > ADDR7))
1509                           || opP->index.size != SIZE_UNSPEC
1510                           || opP->index.scale != 1))
1511                     losing++;
1512                   break;
1513
1514                 case 's':
1515                   if (opP->mode != CONTROL
1516                       || ! (opP->reg == FPI
1517                             || opP->reg == FPS
1518                             || opP->reg == FPC))
1519                     losing++;
1520                   break;
1521
1522                 case 'S':
1523                   if (opP->mode != CONTROL || opP->reg != SR)
1524                     losing++;
1525                   break;
1526
1527                 case 't':
1528                   if (opP->mode != IMMED)
1529                     losing++;
1530                   else if (opP->disp.exp.X_op != O_constant
1531                            || opP->disp.exp.X_add_number < 0
1532                            || opP->disp.exp.X_add_number > 7)
1533                     losing++;
1534                   break;
1535
1536                 case 'U':
1537                   if (opP->mode != CONTROL || opP->reg != USP)
1538                     losing++;
1539                   break;
1540
1541                   /* JF these are out of order.  We could put them
1542                      in order if we were willing to put up with
1543                      bunches of #ifdef m68851s in the code.
1544
1545                      Don't forget that you need these operands
1546                      to use 68030 MMU instructions.  */
1547 #ifndef NO_68851
1548                   /* Memory addressing mode used by pflushr */
1549                 case '|':
1550                   if (opP->mode == CONTROL
1551                       || opP->mode == FPREG
1552                       || opP->mode == DREG
1553                       || opP->mode == AREG
1554                       || opP->mode == REGLST)
1555                     losing++;
1556                   /* We should accept immediate operands, but they
1557                      supposedly have to be quad word, and we don't
1558                      handle that.  I would like to see what a Motorola
1559                      assembler does before doing something here.  */
1560                   if (opP->mode == IMMED)
1561                     losing++;
1562                   break;
1563
1564                 case 'f':
1565                   if (opP->mode != CONTROL
1566                       || (opP->reg != SFC && opP->reg != DFC))
1567                     losing++;
1568                   break;
1569
1570                 case '0':
1571                   if (opP->mode != CONTROL || opP->reg != TC)
1572                     losing++;
1573                   break;
1574
1575                 case '1':
1576                   if (opP->mode != CONTROL || opP->reg != AC)
1577                     losing++;
1578                   break;
1579
1580                 case '2':
1581                   if (opP->mode != CONTROL
1582                       || (opP->reg != CAL
1583                           && opP->reg != VAL
1584                           && opP->reg != SCC))
1585                     losing++;
1586                   break;
1587
1588                 case 'V':
1589                   if (opP->mode != CONTROL
1590                       || opP->reg != VAL)
1591                     losing++;
1592                   break;
1593
1594                 case 'W':
1595                   if (opP->mode != CONTROL
1596                       || (opP->reg != DRP
1597                           && opP->reg != SRP
1598                           && opP->reg != CRP))
1599                     losing++;
1600                   break;
1601
1602                 case 'X':
1603                   if (opP->mode != CONTROL
1604                       || (!(opP->reg >= BAD && opP->reg <= BAD + 7)
1605                           && !(opP->reg >= BAC && opP->reg <= BAC + 7)))
1606                     losing++;
1607                   break;
1608
1609                 case 'Y':
1610                   if (opP->mode != CONTROL || opP->reg != PSR)
1611                     losing++;
1612                   break;
1613
1614                 case 'Z':
1615                   if (opP->mode != CONTROL || opP->reg != PCSR)
1616                     losing++;
1617                   break;
1618 #endif
1619                 case 'c':
1620                   if (opP->mode != CONTROL
1621                       || (opP->reg != NC
1622                           && opP->reg != IC
1623                           && opP->reg != DC
1624                           && opP->reg != BC))
1625                     {
1626                       losing++;
1627                     }           /* not a cache specifier. */
1628                   break;
1629
1630                 case '_':
1631                   if (opP->mode != ABSL)
1632                     ++losing;
1633                   break;
1634
1635                 default:
1636                   abort ();
1637                 }               /* switch on type of operand */
1638
1639               if (losing)
1640                 break;
1641             }                   /* for each operand */
1642         }                       /* if immediately wrong */
1643
1644       if (!losing)
1645         {
1646           break;
1647         }                       /* got it. */
1648
1649       opcode = opcode->m_next;
1650
1651       if (!opcode)
1652         {
1653           if (ok_arch
1654               && !(ok_arch & current_architecture))
1655             {
1656               char buf[200], *cp;
1657
1658               strcpy (buf,
1659                       "invalid instruction for this architecture; needs ");
1660               cp = buf + strlen (buf);
1661               switch (ok_arch)
1662                 {
1663                 case mfloat:
1664                   strcpy (cp, "fpu (68040, 68060 or 68881/68882)");
1665                   break;
1666                 case mmmu:
1667                   strcpy (cp, "mmu (68030 or 68851)");
1668                   break;
1669                 case m68020up:
1670                   strcpy (cp, "68020 or higher");
1671                   break;
1672                 case m68000up:
1673                   strcpy (cp, "68000 or higher");
1674                   break;
1675                 case m68010up:
1676                   strcpy (cp, "68010 or higher");
1677                   break;
1678                 default:
1679                   {
1680                     int got_one = 0, idx;
1681                     for (idx = 0; idx < sizeof (archs) / sizeof (archs[0]);
1682                          idx++)
1683                       {
1684                         if ((archs[idx].arch & ok_arch)
1685                             && ! archs[idx].alias)
1686                           {
1687                             if (got_one)
1688                               {
1689                                 strcpy (cp, " or ");
1690                                 cp += strlen (cp);
1691                               }
1692                             got_one = 1;
1693                             strcpy (cp, archs[idx].name);
1694                             cp += strlen (cp);
1695                           }
1696                       }
1697                   }
1698                 }
1699               cp = xmalloc (strlen (buf) + 1);
1700               strcpy (cp, buf);
1701               the_ins.error = cp;
1702             }
1703           else
1704             the_ins.error = "operands mismatch";
1705           return;
1706         }                       /* Fell off the end */
1707
1708       losing = 0;
1709     }
1710
1711   /* now assemble it */
1712
1713   the_ins.args = opcode->m_operands;
1714   the_ins.numargs = opcode->m_opnum;
1715   the_ins.numo = opcode->m_codenum;
1716   the_ins.opcode[0] = getone (opcode);
1717   the_ins.opcode[1] = gettwo (opcode);
1718
1719   for (s = the_ins.args, opP = &the_ins.operands[0]; *s; s += 2, opP++)
1720     {
1721       /* This switch is a doozy.
1722        Watch the first step; its a big one! */
1723       switch (s[0])
1724         {
1725
1726         case '*':
1727         case '~':
1728         case '%':
1729         case ';':
1730         case '@':
1731         case '!':
1732         case '&':
1733         case '$':
1734         case '?':
1735         case '/':
1736         case '`':
1737         case '<':
1738         case '>':
1739         case 'm':
1740         case 'n':
1741         case 'o':
1742         case 'p':
1743 #ifndef NO_68851
1744         case '|':
1745 #endif
1746           switch (opP->mode)
1747             {
1748             case IMMED:
1749               tmpreg = 0x3c;    /* 7.4 */
1750               if (strchr ("bwl", s[1]))
1751                 nextword = get_num (&opP->disp, 80);
1752               else
1753                 nextword = get_num (&opP->disp, 0);
1754               if (isvar (&opP->disp))
1755                 add_fix (s[1], &opP->disp, 0, 0);
1756               switch (s[1])
1757                 {
1758                 case 'b':
1759                   if (!isbyte (nextword))
1760                     opP->error = "operand out of range";
1761                   addword (nextword);
1762                   baseo = 0;
1763                   break;
1764                 case 'w':
1765                   if (!isword (nextword))
1766                     opP->error = "operand out of range";
1767                   addword (nextword);
1768                   baseo = 0;
1769                   break;
1770                 case 'W':
1771                   if (!issword (nextword))
1772                     opP->error = "operand out of range";
1773                   addword (nextword);
1774                   baseo = 0;
1775                   break;
1776                 case 'l':
1777                   addword (nextword >> 16);
1778                   addword (nextword);
1779                   baseo = 0;
1780                   break;
1781
1782                 case 'f':
1783                   baseo = 2;
1784                   outro = 8;
1785                   break;
1786                 case 'F':
1787                   baseo = 4;
1788                   outro = 11;
1789                   break;
1790                 case 'x':
1791                   baseo = 6;
1792                   outro = 15;
1793                   break;
1794                 case 'p':
1795                   baseo = 6;
1796                   outro = -1;
1797                   break;
1798                 default:
1799                   abort ();
1800                 }
1801               if (!baseo)
1802                 break;
1803
1804               /* We gotta put out some float */
1805               if (op (&opP->disp) != O_big)
1806                 {
1807                   valueT val;
1808                   int gencnt;
1809
1810                   /* Can other cases happen here?  */
1811                   if (op (&opP->disp) != O_constant)
1812                     abort ();
1813
1814                   val = (valueT) offs (&opP->disp);
1815                   gencnt = 0;
1816                   do
1817                     {
1818                       generic_bignum[gencnt] = (LITTLENUM_TYPE) val;
1819                       val >>= LITTLENUM_NUMBER_OF_BITS;
1820                       ++gencnt;
1821                     }
1822                   while (val != 0);
1823                   offs (&opP->disp) = gencnt;
1824                 }
1825               if (offs (&opP->disp) > 0)
1826                 {
1827                   if (offs (&opP->disp) > baseo)
1828                     {
1829                       as_warn ("Bignum too big for %c format; truncated",
1830                                s[1]);
1831                       offs (&opP->disp) = baseo;
1832                     }
1833                   baseo -= offs (&opP->disp);
1834                   while (baseo--)
1835                     addword (0);
1836                   for (wordp = generic_bignum + offs (&opP->disp) - 1;
1837                        offs (&opP->disp)--;
1838                        --wordp)
1839                     addword (*wordp);
1840                   break;
1841                 }
1842               gen_to_words (words, baseo, (long) outro);
1843               for (wordp = words; baseo--; wordp++)
1844                 addword (*wordp);
1845               break;
1846             case DREG:
1847               tmpreg = opP->reg - DATA; /* 0.dreg */
1848               break;
1849             case AREG:
1850               tmpreg = 0x08 + opP->reg - ADDR;  /* 1.areg */
1851               break;
1852             case AINDR:
1853               tmpreg = 0x10 + opP->reg - ADDR;  /* 2.areg */
1854               break;
1855             case ADEC:
1856               tmpreg = 0x20 + opP->reg - ADDR;  /* 4.areg */
1857               break;
1858             case AINC:
1859               tmpreg = 0x18 + opP->reg - ADDR;  /* 3.areg */
1860               break;
1861             case DISP:
1862
1863               nextword = get_num (&opP->disp, 80);
1864
1865               if (opP->reg == PC
1866                   && ! isvar (&opP->disp)
1867                   && m68k_abspcadd)
1868                 {
1869                   opP->disp.exp.X_op = O_symbol;
1870 #ifndef BFD_ASSEMBLER
1871                   opP->disp.exp.X_add_symbol = &abs_symbol;
1872 #else
1873                   opP->disp.exp.X_add_symbol =
1874                     section_symbol (absolute_section);
1875 #endif
1876                 }
1877
1878               /* Force into index mode.  Hope this works */
1879
1880               /* We do the first bit for 32-bit displacements, and the
1881                  second bit for 16 bit ones.  It is possible that we
1882                  should make the default be WORD instead of LONG, but
1883                  I think that'd break GCC, so we put up with a little
1884                  inefficiency for the sake of working output.  */
1885
1886               if (!issword (nextword)
1887                   || (isvar (&opP->disp)
1888                       && ((opP->disp.size == SIZE_UNSPEC
1889                            && flag_short_refs == 0
1890                            && cpu_of_arch (current_architecture) >= m68020)
1891                           || opP->disp.size == SIZE_LONG)))
1892                 {
1893                   if (cpu_of_arch (current_architecture) < m68020)
1894                     opP->error =
1895                       "displacement too large for this architecture; needs 68020 or higher";
1896                   if (opP->reg == PC)
1897                     tmpreg = 0x3B;      /* 7.3 */
1898                   else
1899                     tmpreg = 0x30 + opP->reg - ADDR;    /* 6.areg */
1900                   if (isvar (&opP->disp))
1901                     {
1902                       if (opP->reg == PC)
1903                         {
1904                           if (opP->disp.size == SIZE_LONG
1905 #ifdef OBJ_ELF
1906                               /* If the displacement needs pic
1907                                  relocation it cannot be relaxed.  */
1908                               || opP->disp.pic_reloc != pic_none
1909 #endif
1910                               )
1911                             {
1912                               addword (0x0170);
1913                               add_fix ('l', &opP->disp, 1, 2);
1914                             }
1915                           else
1916                             {
1917                               add_frag (adds (&opP->disp),
1918                                         offs (&opP->disp),
1919                                         TAB (PCLEA, SZ_UNDEF));
1920                               break;
1921                             }
1922                         }
1923                       else
1924                         {
1925                           addword (0x0170);
1926                           add_fix ('l', &opP->disp, 0, 0);
1927                         }
1928                     }
1929                   else
1930                     addword (0x0170);
1931                   addword (nextword >> 16);
1932                 }
1933               else
1934                 {
1935                   if (opP->reg == PC)
1936                     tmpreg = 0x3A;      /* 7.2 */
1937                   else
1938                     tmpreg = 0x28 + opP->reg - ADDR;    /* 5.areg */
1939
1940                   if (isvar (&opP->disp))
1941                     {
1942                       if (opP->reg == PC)
1943                         {
1944                           add_fix ('w', &opP->disp, 1, 0);
1945                         }
1946                       else
1947                         add_fix ('w', &opP->disp, 0, 0);
1948                     }
1949                 }
1950               addword (nextword);
1951               break;
1952
1953             case POST:
1954             case PRE:
1955             case BASE:
1956               nextword = 0;
1957               baseo = get_num (&opP->disp, 80);
1958               if (opP->mode == POST || opP->mode == PRE)
1959                 outro = get_num (&opP->odisp, 80);
1960               /* Figure out the `addressing mode'.
1961                  Also turn on the BASE_DISABLE bit, if needed.  */
1962               if (opP->reg == PC || opP->reg == ZPC)
1963                 {
1964                   tmpreg = 0x3b;        /* 7.3 */
1965                   if (opP->reg == ZPC)
1966                     nextword |= 0x80;
1967                 }
1968               else if (opP->reg == 0)
1969                 {
1970                   nextword |= 0x80;
1971                   tmpreg = 0x30;        /* 6.garbage */
1972                 }
1973               else if (opP->reg >= ZADDR0 && opP->reg <= ZADDR7)
1974                 {
1975                   nextword |= 0x80;
1976                   tmpreg = 0x30 + opP->reg - ZADDR0;
1977                 }
1978               else
1979                 tmpreg = 0x30 + opP->reg - ADDR;        /* 6.areg */
1980
1981               siz1 = opP->disp.size;
1982               if (opP->mode == POST || opP->mode == PRE)
1983                 siz2 = opP->odisp.size;
1984               else
1985                 siz2 = SIZE_UNSPEC;
1986
1987               /* Index register stuff */
1988               if (opP->index.reg != 0
1989                   && opP->index.reg >= DATA
1990                   && opP->index.reg <= ADDR7)
1991                 {
1992                   nextword |= (opP->index.reg - DATA) << 12;
1993
1994                   if (opP->index.size == SIZE_LONG
1995                       || (opP->index.size == SIZE_UNSPEC
1996                           && m68k_index_width_default == SIZE_LONG))
1997                     nextword |= 0x800;
1998
1999                   if ((opP->index.scale != 1 
2000                        && cpu_of_arch (current_architecture) < m68020)
2001                       || (opP->index.scale == 8 
2002                           && current_architecture == mcf5200))
2003                     {
2004                       opP->error =
2005                         "scale factor invalid on this architecture; needs cpu32 or 68020 or higher";
2006                     }
2007
2008                   switch (opP->index.scale)
2009                     {
2010                     case 1:
2011                       break;
2012                     case 2:
2013                       nextword |= 0x200;
2014                       break;
2015                     case 4:
2016                       nextword |= 0x400;
2017                       break;
2018                     case 8:
2019                       nextword |= 0x600;
2020                       break;
2021                     default:
2022                       abort ();
2023                     }
2024                   /* IF its simple,
2025                      GET US OUT OF HERE! */
2026
2027                   /* Must be INDEX, with an index register.  Address
2028                      register cannot be ZERO-PC, and either :b was
2029                      forced, or we know it will fit.  For a 68000 or
2030                      68010, force this mode anyways, because the
2031                      larger modes aren't supported.  */
2032                   if (opP->mode == BASE
2033                       && ((opP->reg >= ADDR0
2034                            && opP->reg <= ADDR7)
2035                           || opP->reg == PC))
2036                     {
2037                       if (siz1 == SIZE_BYTE
2038                           || cpu_of_arch (current_architecture) < m68020
2039                           || (siz1 == SIZE_UNSPEC
2040                               && ! isvar (&opP->disp)
2041                               && issbyte (baseo)))
2042                         {
2043                           nextword += baseo & 0xff;
2044                           addword (nextword);
2045                           if (isvar (&opP->disp))
2046                             {
2047                               /* Do a byte relocation.  If it doesn't
2048                                  fit (possible on m68000) let the
2049                                  fixup processing complain later.  */
2050                               if (opP->reg == PC)
2051                                 add_fix ('B', &opP->disp, 1, 1);
2052                               else
2053                                 add_fix ('B', &opP->disp, 0, 0);
2054                             }
2055                           else if (siz1 != SIZE_BYTE)
2056                             {
2057                               if (siz1 != SIZE_UNSPEC)
2058                                 as_warn ("Forcing byte displacement");
2059                               if (! issbyte (baseo))
2060                                 opP->error = "byte displacement out of range";
2061                             }
2062
2063                           break;
2064                         }
2065                       else if (siz1 == SIZE_UNSPEC
2066                                && opP->reg == PC
2067                                && isvar (&opP->disp)
2068                                && subs (&opP->disp) == NULL
2069 #ifdef OBJ_ELF
2070                                /* If the displacement needs pic
2071                                   relocation it cannot be relaxed.  */
2072                                && opP->disp.pic_reloc == pic_none
2073 #endif
2074                                )
2075                         {
2076                           nextword += baseo & 0xff;
2077                           addword (nextword);
2078                           add_frag (adds (&opP->disp), offs (&opP->disp),
2079                                     TAB (PCINDEX, SZ_UNDEF));
2080
2081                           break;
2082                         }
2083                     }
2084                 }
2085               else
2086                 {
2087                   nextword |= 0x40;     /* No index reg */
2088                   if (opP->index.reg >= ZDATA0
2089                       && opP->index.reg <= ZDATA7)
2090                     nextword |= (opP->index.reg - ZDATA0) << 12;
2091                   else if (opP->index.reg >= ZADDR0
2092                            || opP->index.reg <= ZADDR7)
2093                     nextword |= (opP->index.reg - ZADDR0 + 8) << 12;
2094                 }
2095
2096               /* It isn't simple.  */
2097
2098               if (cpu_of_arch (current_architecture) < m68020)
2099                 opP->error =
2100                   "invalid operand mode for this architecture; needs 68020 or higher";
2101
2102               nextword |= 0x100;
2103               /* If the guy specified a width, we assume that it is
2104                  wide enough.  Maybe it isn't.  If so, we lose.  */
2105               switch (siz1)
2106                 {
2107                 case SIZE_UNSPEC:
2108                   if (isvar (&opP->disp)
2109                       ? m68k_rel32
2110                       : ! issword (baseo))
2111                     {
2112                       siz1 = SIZE_LONG;
2113                       nextword |= 0x30;
2114                     }
2115                   else if (! isvar (&opP->disp) && baseo == 0)
2116                     nextword |= 0x10;
2117                   else
2118                     {
2119                       nextword |= 0x20;
2120                       siz1 = SIZE_WORD;
2121                     }
2122                   break;
2123                 case SIZE_BYTE:
2124                   as_warn (":b not permitted; defaulting to :w");
2125                   /* Fall through.  */
2126                 case SIZE_WORD:
2127                   nextword |= 0x20;
2128                   break;
2129                 case SIZE_LONG:
2130                   nextword |= 0x30;
2131                   break;
2132                 }
2133
2134               /* Figure out innner displacement stuff */
2135               if (opP->mode == POST || opP->mode == PRE)
2136                 {
2137                   if (cpu_of_arch (current_architecture) & cpu32)
2138                     opP->error = "invalid operand mode for this architecture; needs 68020 or higher";
2139                   switch (siz2)
2140                     {
2141                     case SIZE_UNSPEC:
2142                       if (isvar (&opP->odisp)
2143                           ? m68k_rel32
2144                           : ! issword (outro))
2145                         {
2146                           siz2 = SIZE_LONG;
2147                           nextword |= 0x3;
2148                         }
2149                       else if (! isvar (&opP->odisp) && outro == 0)
2150                         nextword |= 0x1;
2151                       else
2152                         {
2153                           nextword |= 0x2;
2154                           siz2 = SIZE_WORD;
2155                         }
2156                       break;
2157                     case 1:
2158                       as_warn (":b not permitted; defaulting to :w");
2159                       /* Fall through.  */
2160                     case 2:
2161                       nextword |= 0x2;
2162                       break;
2163                     case 3:
2164                       nextword |= 0x3;
2165                       break;
2166                     }
2167                   if (opP->mode == POST
2168                       && (nextword & 0x40) == 0)
2169                     nextword |= 0x04;
2170                 }
2171               addword (nextword);
2172
2173               if (siz1 != SIZE_UNSPEC && isvar (&opP->disp))
2174                 {
2175                   if (opP->reg == PC || opP->reg == ZPC)
2176                     add_fix (siz1 == SIZE_LONG ? 'l' : 'w', &opP->disp, 1, 2);
2177                   else
2178                     add_fix (siz1 == SIZE_LONG ? 'l' : 'w', &opP->disp, 0, 0);
2179                 }
2180               if (siz1 == SIZE_LONG)
2181                 addword (baseo >> 16);
2182               if (siz1 != SIZE_UNSPEC)
2183                 addword (baseo);
2184
2185               if (siz2 != SIZE_UNSPEC && isvar (&opP->odisp))
2186                 add_fix (siz2 == SIZE_LONG ? 'l' : 'w', &opP->odisp, 0, 0);
2187               if (siz2 == SIZE_LONG)
2188                 addword (outro >> 16);
2189               if (siz2 != SIZE_UNSPEC)
2190                 addword (outro);
2191
2192               break;
2193
2194             case ABSL:
2195               nextword = get_num (&opP->disp, 80);
2196               switch (opP->disp.size)
2197                 {
2198                 default:
2199                   abort ();
2200                 case SIZE_UNSPEC:
2201                   if (!isvar (&opP->disp) && issword (offs (&opP->disp)))
2202                     {
2203                       tmpreg = 0x38;    /* 7.0 */
2204                       addword (nextword);
2205                       break;
2206                     }
2207                   /* Don't generate pc relative code on 68010 and
2208                      68000.  */
2209                   if (isvar (&opP->disp)
2210                       && !subs (&opP->disp)
2211                       && adds (&opP->disp)
2212 #ifdef OBJ_ELF
2213                       /* If the displacement needs pic relocation it
2214                          cannot be relaxed.  */
2215                       && opP->disp.pic_reloc == pic_none
2216 #endif
2217                       && S_GET_SEGMENT (adds (&opP->disp)) == now_seg
2218                       && cpu_of_arch (current_architecture) >= m68020
2219                       && !flag_long_jumps
2220                       && !strchr ("~%&$?", s[0]))
2221                     {
2222                       tmpreg = 0x3A;    /* 7.2 */
2223                       add_frag (adds (&opP->disp),
2224                                 offs (&opP->disp),
2225                                 TAB (PCREL, SZ_UNDEF));
2226                       break;
2227                     }
2228                   /* Fall through into long */
2229                 case SIZE_LONG:
2230                   if (isvar (&opP->disp))
2231                     add_fix ('l', &opP->disp, 0, 0);
2232
2233                   tmpreg = 0x39;/* 7.1 mode */
2234                   addword (nextword >> 16);
2235                   addword (nextword);
2236                   break;
2237
2238                 case SIZE_WORD: /* Word */
2239                   if (isvar (&opP->disp))
2240                     add_fix ('w', &opP->disp, 0, 0);
2241
2242                   tmpreg = 0x38;/* 7.0 mode */
2243                   addword (nextword);
2244                   break;
2245                 }
2246               break;
2247             case CONTROL:
2248             case FPREG:
2249             default:
2250               as_bad ("unknown/incorrect operand");
2251               /* abort(); */
2252             }
2253           install_gen_operand (s[1], tmpreg);
2254           break;
2255
2256         case '#':
2257         case '^':
2258           switch (s[1])
2259             {                   /* JF: I hate floating point! */
2260             case 'j':
2261               tmpreg = 70;
2262               break;
2263             case '8':
2264               tmpreg = 20;
2265               break;
2266             case 'C':
2267               tmpreg = 50;
2268               break;
2269             case '3':
2270             default:
2271               tmpreg = 80;
2272               break;
2273             }
2274           tmpreg = get_num (&opP->disp, tmpreg);
2275           if (isvar (&opP->disp))
2276             add_fix (s[1], &opP->disp, 0, 0);
2277           switch (s[1])
2278             {
2279             case 'b':           /* Danger:  These do no check for
2280                                    certain types of overflow.
2281                                    user beware! */
2282               if (!isbyte (tmpreg))
2283                 opP->error = "out of range";
2284               insop (tmpreg, opcode);
2285               if (isvar (&opP->disp))
2286                 the_ins.reloc[the_ins.nrel - 1].n =
2287                   (opcode->m_codenum) * 2 + 1;
2288               break;
2289             case 'w':
2290               if (!isword (tmpreg))
2291                 opP->error = "out of range";
2292               insop (tmpreg, opcode);
2293               if (isvar (&opP->disp))
2294                 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
2295               break;
2296             case 'W':
2297               if (!issword (tmpreg))
2298                 opP->error = "out of range";
2299               insop (tmpreg, opcode);
2300               if (isvar (&opP->disp))
2301                 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
2302               break;
2303             case 'l':
2304               /* Because of the way insop works, we put these two out
2305                  backwards.  */
2306               insop (tmpreg, opcode);
2307               insop (tmpreg >> 16, opcode);
2308               if (isvar (&opP->disp))
2309                 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
2310               break;
2311             case '3':
2312               tmpreg &= 0xFF;
2313             case '8':
2314             case 'C':
2315               install_operand (s[1], tmpreg);
2316               break;
2317             default:
2318               abort ();
2319             }
2320           break;
2321
2322         case '+':
2323         case '-':
2324         case 'A':
2325         case 'a':
2326           install_operand (s[1], opP->reg - ADDR);
2327           break;
2328
2329         case 'B':
2330           tmpreg = get_num (&opP->disp, 80);
2331           switch (s[1])
2332             {
2333             case 'B':
2334               /* The pc_fix argument winds up in fx_pcrel_adjust,
2335                  which is a char, and may therefore be unsigned.  We
2336                  want to pass -1, but we pass 64 instead, and convert
2337                  back in md_pcrel_from.  */
2338               add_fix ('B', &opP->disp, 1, 64);
2339               break;
2340             case 'W':
2341               add_fix ('w', &opP->disp, 1, 0);
2342               addword (0);
2343               break;
2344             case 'L':
2345             long_branch:
2346               if (cpu_of_arch (current_architecture) < m68020)
2347                 as_warn ("Can't use long branches on 68000/68010");
2348               the_ins.opcode[the_ins.numo - 1] |= 0xff;
2349               add_fix ('l', &opP->disp, 1, 0);
2350               addword (0);
2351               addword (0);
2352               break;
2353             case 'g':
2354               if (subs (&opP->disp))    /* We can't relax it */
2355                 goto long_branch;
2356
2357 #ifdef OBJ_ELF
2358               /* If the displacement needs pic relocation it cannot be
2359                  relaxed.  */
2360               if (opP->disp.pic_reloc != pic_none)
2361                 goto long_branch;
2362 #endif
2363
2364               /* This could either be a symbol, or an absolute
2365                  address.  No matter, the frag hacking will finger it
2366                  out.  Not quite: it can't switch from BRANCH to
2367                  BCC68000 for the case where opnd is absolute (it
2368                  needs to use the 68000 hack since no conditional abs
2369                  jumps).  */
2370               if (((cpu_of_arch (current_architecture) < m68020)
2371                    || (0 == adds (&opP->disp)))
2372                   && (the_ins.opcode[0] >= 0x6200)
2373                   && (the_ins.opcode[0] <= 0x6f00))
2374                 add_frag (adds (&opP->disp), offs (&opP->disp),
2375                           TAB (BCC68000, SZ_UNDEF));
2376               else
2377                 add_frag (adds (&opP->disp), offs (&opP->disp),
2378                           TAB (ABRANCH, SZ_UNDEF));
2379               break;
2380             case 'w':
2381               if (isvar (&opP->disp))
2382                 {
2383 #if 1
2384                   /* check for DBcc instruction */
2385                   if ((the_ins.opcode[0] & 0xf0f8) == 0x50c8)
2386                     {
2387                       /* size varies if patch */
2388                       /* needed for long form */
2389                       add_frag (adds (&opP->disp), offs (&opP->disp),
2390                                 TAB (DBCC, SZ_UNDEF));
2391                       break;
2392                     }
2393 #endif
2394                   add_fix ('w', &opP->disp, 1, 0);
2395                 }
2396               addword (0);
2397               break;
2398             case 'C':           /* Fixed size LONG coproc branches */
2399               add_fix ('l', &opP->disp, 1, 0);
2400               addword (0);
2401               addword (0);
2402               break;
2403             case 'c':           /* Var size Coprocesssor branches */
2404               if (subs (&opP->disp))
2405                 {
2406                   add_fix ('l', &opP->disp, 1, 0);
2407                   add_frag ((symbolS *) 0, (long) 0, TAB (FBRANCH, LONG));
2408                 }
2409               else if (adds (&opP->disp))
2410                 add_frag (adds (&opP->disp), offs (&opP->disp),
2411                           TAB (FBRANCH, SZ_UNDEF));
2412               else
2413                 {
2414                   /* add_frag((symbolS *) 0, offs(&opP->disp),
2415                      TAB(FBRANCH,SHORT)); */
2416                   the_ins.opcode[the_ins.numo - 1] |= 0x40;
2417                   add_fix ('l', &opP->disp, 1, 0);
2418                   addword (0);
2419                   addword (0);
2420                 }
2421               break;
2422             default:
2423               abort ();
2424             }
2425           break;
2426
2427         case 'C':               /* Ignore it */
2428           break;
2429
2430         case 'd':               /* JF this is a kludge */
2431           install_operand ('s', opP->reg - ADDR);
2432           tmpreg = get_num (&opP->disp, 80);
2433           if (!issword (tmpreg))
2434             {
2435               as_warn ("Expression out of range, using 0");
2436               tmpreg = 0;
2437             }
2438           addword (tmpreg);
2439           break;
2440
2441         case 'D':
2442           install_operand (s[1], opP->reg - DATA);
2443           break;
2444
2445         case 'F':
2446           install_operand (s[1], opP->reg - FP0);
2447           break;
2448
2449         case 'I':
2450           tmpreg = opP->reg - COP0;
2451           install_operand (s[1], tmpreg);
2452           break;
2453
2454         case 'J':               /* JF foo */
2455           switch (opP->reg)
2456             {
2457             case SFC:
2458               tmpreg = 0x000;
2459               break;
2460             case DFC:
2461               tmpreg = 0x001;
2462               break;
2463             case CACR:
2464               tmpreg = 0x002;
2465               break;
2466             case TC:
2467               tmpreg = 0x003;
2468               break;
2469             case ITT0:
2470               tmpreg = 0x004;
2471               break;
2472             case ITT1:
2473               tmpreg = 0x005;
2474               break;
2475             case DTT0:
2476               tmpreg = 0x006;
2477               break;
2478             case DTT1:
2479               tmpreg = 0x007;
2480               break;
2481             case BUSCR:
2482               tmpreg = 0x008;
2483               break;
2484
2485             case USP:
2486               tmpreg = 0x800;
2487               break;
2488             case VBR:
2489               tmpreg = 0x801;
2490               break;
2491             case CAAR:
2492               tmpreg = 0x802;
2493               break;
2494             case MSP:
2495               tmpreg = 0x803;
2496               break;
2497             case ISP:
2498               tmpreg = 0x804;
2499               break;
2500             case MMUSR:
2501               tmpreg = 0x805;
2502               break;
2503             case URP:
2504               tmpreg = 0x806;
2505               break;
2506             case SRP:
2507               tmpreg = 0x807;
2508               break;
2509             case PCR:
2510               tmpreg = 0x808;
2511               break;
2512             case ROMBAR:
2513               tmpreg = 0xC00;
2514               break;
2515             case RAMBAR0:
2516               tmpreg = 0xC04;
2517               break;
2518             case RAMBAR1:
2519               tmpreg = 0xC05;
2520               break;
2521             case MBAR:
2522               tmpreg = 0xC0F;
2523               break;
2524             default:
2525               abort ();
2526             }
2527           install_operand (s[1], tmpreg);
2528           break;
2529
2530         case 'k':
2531           tmpreg = get_num (&opP->disp, 55);
2532           install_operand (s[1], tmpreg & 0x7f);
2533           break;
2534
2535         case 'l':
2536           tmpreg = opP->mask;
2537           if (s[1] == 'w')
2538             {
2539               if (tmpreg & 0x7FF0000)
2540                 as_bad ("Floating point register in register list");
2541               insop (reverse_16_bits (tmpreg), opcode);
2542             }
2543           else
2544             {
2545               if (tmpreg & 0x700FFFF)
2546                 as_bad ("Wrong register in floating-point reglist");
2547               install_operand (s[1], reverse_8_bits (tmpreg >> 16));
2548             }
2549           break;
2550
2551         case 'L':
2552           tmpreg = opP->mask;
2553           if (s[1] == 'w')
2554             {
2555               if (tmpreg & 0x7FF0000)
2556                 as_bad ("Floating point register in register list");
2557               insop (tmpreg, opcode);
2558             }
2559           else if (s[1] == '8')
2560             {
2561               if (tmpreg & 0x0FFFFFF)
2562                 as_bad ("incorrect register in reglist");
2563               install_operand (s[1], tmpreg >> 24);
2564             }
2565           else
2566             {
2567               if (tmpreg & 0x700FFFF)
2568                 as_bad ("wrong register in floating-point reglist");
2569               else
2570                 install_operand (s[1], tmpreg >> 16);
2571             }
2572           break;
2573
2574         case 'M':
2575           install_operand (s[1], get_num (&opP->disp, 60));
2576           break;
2577
2578         case 'O':
2579           tmpreg = ((opP->mode == DREG)
2580                     ? 0x20 + opP->reg - DATA
2581                     : (get_num (&opP->disp, 40) & 0x1F));
2582           install_operand (s[1], tmpreg);
2583           break;
2584
2585         case 'Q':
2586           tmpreg = get_num (&opP->disp, 10);
2587           if (tmpreg == 8)
2588             tmpreg = 0;
2589           install_operand (s[1], tmpreg);
2590           break;
2591
2592         case 'R':
2593           /* This depends on the fact that ADDR registers are eight
2594              more than their corresponding DATA regs, so the result
2595              will have the ADDR_REG bit set */
2596           install_operand (s[1], opP->reg - DATA);
2597           break;
2598
2599         case 'r':
2600           if (opP->mode == AINDR)
2601             install_operand (s[1], opP->reg - DATA);
2602           else
2603             install_operand (s[1], opP->index.reg - DATA);
2604           break;
2605
2606         case 's':
2607           if (opP->reg == FPI)
2608             tmpreg = 0x1;
2609           else if (opP->reg == FPS)
2610             tmpreg = 0x2;
2611           else if (opP->reg == FPC)
2612             tmpreg = 0x4;
2613           else
2614             abort ();
2615           install_operand (s[1], tmpreg);
2616           break;
2617
2618         case 'S':               /* Ignore it */
2619           break;
2620
2621         case 'T':
2622           install_operand (s[1], get_num (&opP->disp, 30));
2623           break;
2624
2625         case 'U':               /* Ignore it */
2626           break;
2627
2628         case 'c':
2629           switch (opP->reg)
2630             {
2631             case NC:
2632               tmpreg = 0;
2633               break;
2634             case DC:
2635               tmpreg = 1;
2636               break;
2637             case IC:
2638               tmpreg = 2;
2639               break;
2640             case BC:
2641               tmpreg = 3;
2642               break;
2643             default:
2644               as_fatal ("failed sanity check");
2645             }                   /* switch on cache token */
2646           install_operand (s[1], tmpreg);
2647           break;
2648 #ifndef NO_68851
2649           /* JF: These are out of order, I fear. */
2650         case 'f':
2651           switch (opP->reg)
2652             {
2653             case SFC:
2654               tmpreg = 0;
2655               break;
2656             case DFC:
2657               tmpreg = 1;
2658               break;
2659             default:
2660               abort ();
2661             }
2662           install_operand (s[1], tmpreg);
2663           break;
2664
2665         case '0':
2666         case '1':
2667         case '2':
2668           switch (opP->reg)
2669             {
2670             case TC:
2671               tmpreg = 0;
2672               break;
2673             case CAL:
2674               tmpreg = 4;
2675               break;
2676             case VAL:
2677               tmpreg = 5;
2678               break;
2679             case SCC:
2680               tmpreg = 6;
2681               break;
2682             case AC:
2683               tmpreg = 7;
2684               break;
2685             default:
2686               abort ();
2687             }
2688           install_operand (s[1], tmpreg);
2689           break;
2690
2691         case 'V':
2692           if (opP->reg == VAL)
2693             break;
2694           abort ();
2695
2696         case 'W':
2697           switch (opP->reg)
2698             {
2699             case DRP:
2700               tmpreg = 1;
2701               break;
2702             case SRP:
2703               tmpreg = 2;
2704               break;
2705             case CRP:
2706               tmpreg = 3;
2707               break;
2708             default:
2709               abort ();
2710             }
2711           install_operand (s[1], tmpreg);
2712           break;
2713
2714         case 'X':
2715           switch (opP->reg)
2716             {
2717             case BAD:
2718             case BAD + 1:
2719             case BAD + 2:
2720             case BAD + 3:
2721             case BAD + 4:
2722             case BAD + 5:
2723             case BAD + 6:
2724             case BAD + 7:
2725               tmpreg = (4 << 10) | ((opP->reg - BAD) << 2);
2726               break;
2727
2728             case BAC:
2729             case BAC + 1:
2730             case BAC + 2:
2731             case BAC + 3:
2732             case BAC + 4:
2733             case BAC + 5:
2734             case BAC + 6:
2735             case BAC + 7:
2736               tmpreg = (5 << 10) | ((opP->reg - BAC) << 2);
2737               break;
2738
2739             default:
2740               abort ();
2741             }
2742           install_operand (s[1], tmpreg);
2743           break;
2744         case 'Y':
2745           know (opP->reg == PSR);
2746           break;
2747         case 'Z':
2748           know (opP->reg == PCSR);
2749           break;
2750 #endif /* m68851 */
2751         case '3':
2752           switch (opP->reg)
2753             {
2754             case TT0:
2755               tmpreg = 2;
2756               break;
2757             case TT1:
2758               tmpreg = 3;
2759               break;
2760             default:
2761               abort ();
2762             }
2763           install_operand (s[1], tmpreg);
2764           break;
2765         case 't':
2766           tmpreg = get_num (&opP->disp, 20);
2767           install_operand (s[1], tmpreg);
2768           break;
2769         case '_':       /* used only for move16 absolute 32-bit address */
2770           tmpreg = get_num (&opP->disp, 80);
2771           addword (tmpreg >> 16);
2772           addword (tmpreg & 0xFFFF);
2773           break;
2774         default:
2775           abort ();
2776         }
2777     }
2778
2779   /* By the time whe get here (FINALLY) the_ins contains the complete
2780      instruction, ready to be emitted. . . */
2781 }
2782
2783 static int
2784 reverse_16_bits (in)
2785      int in;
2786 {
2787   int out = 0;
2788   int n;
2789
2790   static int mask[16] =
2791   {
2792     0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
2793     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000
2794   };
2795   for (n = 0; n < 16; n++)
2796     {
2797       if (in & mask[n])
2798         out |= mask[15 - n];
2799     }
2800   return out;
2801 }                               /* reverse_16_bits() */
2802
2803 static int
2804 reverse_8_bits (in)
2805      int in;
2806 {
2807   int out = 0;
2808   int n;
2809
2810   static int mask[8] =
2811   {
2812     0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
2813   };
2814
2815   for (n = 0; n < 8; n++)
2816     {
2817       if (in & mask[n])
2818         out |= mask[7 - n];
2819     }
2820   return out;
2821 }                               /* reverse_8_bits() */
2822
2823 /* Cause an extra frag to be generated here, inserting up to 10 bytes
2824    (that value is chosen in the frag_var call in md_assemble).  TYPE
2825    is the subtype of the frag to be generated; its primary type is
2826    rs_machine_dependent.
2827
2828    The TYPE parameter is also used by md_convert_frag_1 and
2829    md_estimate_size_before_relax.  The appropriate type of fixup will
2830    be emitted by md_convert_frag_1.
2831
2832    ADD becomes the FR_SYMBOL field of the frag, and OFF the FR_OFFSET.  */
2833 static void
2834 install_operand (mode, val)
2835      int mode;
2836      int val;
2837 {
2838   switch (mode)
2839     {
2840     case 's':
2841       the_ins.opcode[0] |= val & 0xFF;  /* JF FF is for M kludge */
2842       break;
2843     case 'd':
2844       the_ins.opcode[0] |= val << 9;
2845       break;
2846     case '1':
2847       the_ins.opcode[1] |= val << 12;
2848       break;
2849     case '2':
2850       the_ins.opcode[1] |= val << 6;
2851       break;
2852     case '3':
2853       the_ins.opcode[1] |= val;
2854       break;
2855     case '4':
2856       the_ins.opcode[2] |= val << 12;
2857       break;
2858     case '5':
2859       the_ins.opcode[2] |= val << 6;
2860       break;
2861     case '6':
2862       /* DANGER!  This is a hack to force cas2l and cas2w cmds to be
2863          three words long! */
2864       the_ins.numo++;
2865       the_ins.opcode[2] |= val;
2866       break;
2867     case '7':
2868       the_ins.opcode[1] |= val << 7;
2869       break;
2870     case '8':
2871       the_ins.opcode[1] |= val << 10;
2872       break;
2873 #ifndef NO_68851
2874     case '9':
2875       the_ins.opcode[1] |= val << 5;
2876       break;
2877 #endif
2878
2879     case 't':
2880       the_ins.opcode[1] |= (val << 10) | (val << 7);
2881       break;
2882     case 'D':
2883       the_ins.opcode[1] |= (val << 12) | val;
2884       break;
2885     case 'g':
2886       the_ins.opcode[0] |= val = 0xff;
2887       break;
2888     case 'i':
2889       the_ins.opcode[0] |= val << 9;
2890       break;
2891     case 'C':
2892       the_ins.opcode[1] |= val;
2893       break;
2894     case 'j':
2895       the_ins.opcode[1] |= val;
2896       the_ins.numo++;           /* What a hack */
2897       break;
2898     case 'k':
2899       the_ins.opcode[1] |= val << 4;
2900       break;
2901     case 'b':
2902     case 'w':
2903     case 'W':
2904     case 'l':
2905       break;
2906     case 'e':
2907       the_ins.opcode[0] |= (val << 6);
2908       break;
2909     case 'L':
2910       the_ins.opcode[1] = (val >> 16);
2911       the_ins.opcode[2] = val & 0xffff;
2912       break;
2913     case 'c':
2914     default:
2915       as_fatal ("failed sanity check.");
2916     }
2917 }                               /* install_operand() */
2918
2919 static void
2920 install_gen_operand (mode, val)
2921      int mode;
2922      int val;
2923 {
2924   switch (mode)
2925     {
2926     case 's':
2927       the_ins.opcode[0] |= val;
2928       break;
2929     case 'd':
2930       /* This is a kludge!!! */
2931       the_ins.opcode[0] |= (val & 0x07) << 9 | (val & 0x38) << 3;
2932       break;
2933     case 'b':
2934     case 'w':
2935     case 'l':
2936     case 'f':
2937     case 'F':
2938     case 'x':
2939     case 'p':
2940       the_ins.opcode[0] |= val;
2941       break;
2942       /* more stuff goes here */
2943     default:
2944       as_fatal ("failed sanity check.");
2945     }
2946 }                               /* install_gen_operand() */
2947
2948 /*
2949  * verify that we have some number of paren pairs, do m68k_ip_op(), and
2950  * then deal with the bitfield hack.
2951  */
2952
2953 static char *
2954 crack_operand (str, opP)
2955      register char *str;
2956      register struct m68k_op *opP;
2957 {
2958   register int parens;
2959   register int c;
2960   register char *beg_str;
2961   int inquote = 0;
2962
2963   if (!str)
2964     {
2965       return str;
2966     }
2967   beg_str = str;
2968   for (parens = 0; *str && (parens > 0 || inquote || notend (str)); str++)
2969     {
2970       if (! inquote)
2971         {
2972           if (*str == '(')
2973             parens++;
2974           else if (*str == ')')
2975             {
2976               if (!parens)
2977                 {                       /* ERROR */
2978                   opP->error = "Extra )";
2979                   return str;
2980                 }
2981               --parens;
2982             }
2983         }
2984       if (flag_mri && *str == '\'')
2985         inquote = ! inquote;
2986     }
2987   if (!*str && parens)
2988     {                           /* ERROR */
2989       opP->error = "Missing )";
2990       return str;
2991     }
2992   c = *str;
2993   *str = '\0';
2994   if (m68k_ip_op (beg_str, opP) != 0)
2995     {
2996       *str = c;
2997       return str;
2998     }
2999   *str = c;
3000   if (c == '}')
3001     c = *++str;                 /* JF bitfield hack */
3002   if (c)
3003     {
3004       c = *++str;
3005       if (!c)
3006         as_bad ("Missing operand");
3007     }
3008   return str;
3009 }
3010
3011 /* This is the guts of the machine-dependent assembler.  STR points to a
3012    machine dependent instruction.  This function is supposed to emit
3013    the frags/bytes it assembles to.
3014    */
3015
3016 void
3017 insert_reg (regname, regnum)
3018      char *regname;
3019      int regnum;
3020 {
3021   char buf[100];
3022   int i;
3023
3024 #ifdef REGISTER_PREFIX
3025   if (!flag_reg_prefix_optional)
3026     {
3027       buf[0] = REGISTER_PREFIX;
3028       strcpy (buf + 1, regname);
3029       regname = buf;
3030     }
3031 #endif
3032
3033   symbol_table_insert (symbol_new (regname, reg_section, regnum,
3034                                    &zero_address_frag));
3035
3036   for (i = 0; regname[i]; i++)
3037     buf[i] = islower (regname[i]) ? toupper (regname[i]) : regname[i];
3038   buf[i] = '\0';
3039
3040   symbol_table_insert (symbol_new (buf, reg_section, regnum,
3041                                    &zero_address_frag));
3042 }
3043
3044 struct init_entry
3045   {
3046     const char *name;
3047     int number;
3048   };
3049
3050 static const struct init_entry init_table[] =
3051 {
3052   { "d0", DATA0 },
3053   { "d1", DATA1 },
3054   { "d2", DATA2 },
3055   { "d3", DATA3 },
3056   { "d4", DATA4 },
3057   { "d5", DATA5 },
3058   { "d6", DATA6 },
3059   { "d7", DATA7 },
3060   { "a0", ADDR0 },
3061   { "a1", ADDR1 },
3062   { "a2", ADDR2 },
3063   { "a3", ADDR3 },
3064   { "a4", ADDR4 },
3065   { "a5", ADDR5 },
3066   { "a6", ADDR6 },
3067   { "fp", ADDR6 },
3068   { "a7", ADDR7 },
3069   { "sp", ADDR7 },
3070   { "ssp", ADDR7 },
3071   { "fp0", FP0 },
3072   { "fp1", FP1 },
3073   { "fp2", FP2 },
3074   { "fp3", FP3 },
3075   { "fp4", FP4 },
3076   { "fp5", FP5 },
3077   { "fp6", FP6 },
3078   { "fp7", FP7 },
3079   { "fpi", FPI },
3080   { "fpiar", FPI },
3081   { "fpc", FPI },
3082   { "fps", FPS },
3083   { "fpsr", FPS },
3084   { "fpc", FPC },
3085   { "fpcr", FPC },
3086   { "control", FPC },
3087   { "status", FPS },
3088   { "iaddr", FPI },
3089
3090   { "cop0", COP0 },
3091   { "cop1", COP1 },
3092   { "cop2", COP2 },
3093   { "cop3", COP3 },
3094   { "cop4", COP4 },
3095   { "cop5", COP5 },
3096   { "cop6", COP6 },
3097   { "cop7", COP7 },
3098   { "pc", PC },
3099   { "zpc", ZPC },
3100   { "sr", SR },
3101
3102   { "ccr", CCR },
3103   { "cc", CCR },
3104
3105   /* control registers */
3106   { "sfc", SFC },               /* Source Function Code */
3107   { "sfcr", SFC },
3108   { "dfc", DFC },               /* Destination Function Code */
3109   { "dfcr", DFC },
3110   { "cacr", CACR },             /* Cache Control Register */
3111   { "caar", CAAR },             /* Cache Address Register */
3112
3113   { "usp", USP },               /* User Stack Pointer */
3114   { "vbr", VBR },               /* Vector Base Register */
3115   { "msp", MSP },               /* Master Stack Pointer */
3116   { "isp", ISP },               /* Interrupt Stack Pointer */
3117
3118   { "itt0", ITT0 },             /* Instruction Transparent Translation Reg 0 */
3119   { "itt1", ITT1 },             /* Instruction Transparent Translation Reg 1 */
3120   { "dtt0", DTT0 },             /* Data Transparent Translation Register 0 */
3121   { "dtt1", DTT1 },             /* Data Transparent Translation Register 1 */
3122
3123   /* 68ec040 versions of same */
3124   { "iacr0", ITT0 },            /* Instruction Access Control Register 0 */
3125   { "iacr1", ITT1 },            /* Instruction Access Control Register 0 */
3126   { "dacr0", DTT0 },            /* Data Access Control Register 0 */
3127   { "dacr1", DTT1 },            /* Data Access Control Register 0 */
3128
3129   /* mcf5200 versions of same.  The ColdFire programmer's reference
3130      manual indicated that the order is 2,3,0,1, but Ken Rose
3131      <rose@netcom.com> says that 0,1,2,3 is the correct order.  */
3132   { "acr0", ITT0 },             /* Access Control Unit 0 */
3133   { "acr1", ITT1 },             /* Access Control Unit 1 */
3134   { "acr2", DTT0 },             /* Access Control Unit 2 */
3135   { "acr3", DTT1 },             /* Access Control Unit 3 */
3136
3137   { "tc", TC },                 /* MMU Translation Control Register */
3138   { "tcr", TC },
3139
3140   { "mmusr", MMUSR },           /* MMU Status Register */
3141   { "srp", SRP },               /* User Root Pointer */
3142   { "urp", URP },               /* Supervisor Root Pointer */
3143
3144   { "buscr", BUSCR },
3145   { "pcr", PCR },
3146
3147   { "rombar", ROMBAR },         /* ROM Base Address Register */
3148   { "rambar0", RAMBAR0 },       /* ROM Base Address Register */
3149   { "rambar1", RAMBAR1 },       /* ROM Base Address Register */
3150   { "mbar", MBAR },             /* Module Base Address Register */
3151   /* end of control registers */
3152
3153   { "ac", AC },
3154   { "bc", BC },
3155   { "cal", CAL },
3156   { "crp", CRP },
3157   { "drp", DRP },
3158   { "pcsr", PCSR },
3159   { "psr", PSR },
3160   { "scc", SCC },
3161   { "val", VAL },
3162   { "bad0", BAD0 },
3163   { "bad1", BAD1 },
3164   { "bad2", BAD2 },
3165   { "bad3", BAD3 },
3166   { "bad4", BAD4 },
3167   { "bad5", BAD5 },
3168   { "bad6", BAD6 },
3169   { "bad7", BAD7 },
3170   { "bac0", BAC0 },
3171   { "bac1", BAC1 },
3172   { "bac2", BAC2 },
3173   { "bac3", BAC3 },
3174   { "bac4", BAC4 },
3175   { "bac5", BAC5 },
3176   { "bac6", BAC6 },
3177   { "bac7", BAC7 },
3178
3179   { "ic", IC },
3180   { "dc", DC },
3181   { "nc", NC },
3182
3183   { "tt0", TT0 },
3184   { "tt1", TT1 },
3185   /* 68ec030 versions of same */
3186   { "ac0", TT0 },
3187   { "ac1", TT1 },
3188   /* 68ec030 access control unit, identical to 030 MMU status reg */
3189   { "acusr", PSR },
3190
3191   /* Suppressed data and address registers.  */
3192   { "zd0", ZDATA0 },
3193   { "zd1", ZDATA1 },
3194   { "zd2", ZDATA2 },
3195   { "zd3", ZDATA3 },
3196   { "zd4", ZDATA4 },
3197   { "zd5", ZDATA5 },
3198   { "zd6", ZDATA6 },
3199   { "zd7", ZDATA7 },
3200   { "za0", ZADDR0 },
3201   { "za1", ZADDR1 },
3202   { "za2", ZADDR2 },
3203   { "za3", ZADDR3 },
3204   { "za4", ZADDR4 },
3205   { "za5", ZADDR5 },
3206   { "za6", ZADDR6 },
3207   { "za7", ZADDR7 },
3208
3209   { 0, 0 }
3210 };
3211
3212 void
3213 init_regtable ()
3214 {
3215   int i;
3216   for (i = 0; init_table[i].name; i++)
3217     insert_reg (init_table[i].name, init_table[i].number);
3218 }
3219
3220 static int no_68851, no_68881;
3221
3222 #ifdef OBJ_AOUT
3223 /* a.out machine type.  Default to 68020.  */
3224 int m68k_aout_machtype = 2;
3225 #endif
3226
3227 void
3228 md_assemble (str)
3229      char *str;
3230 {
3231   const char *er;
3232   short *fromP;
3233   char *toP = NULL;
3234   int m, n = 0;
3235   char *to_beg_P;
3236   int shorts_this_frag;
3237   fixS *fixP;
3238
3239   /* In MRI mode, the instruction and operands are separated by a
3240      space.  Anything following the operands is a comment.  The label
3241      has already been removed.  */
3242   if (flag_mri)
3243     {
3244       char *s;
3245       int fields = 0;
3246       int infield = 0;
3247       int inquote = 0;
3248
3249       for (s = str; *s != '\0'; s++)
3250         {
3251           if ((*s == ' ' || *s == '\t') && ! inquote)
3252             {
3253               if (infield)
3254                 {
3255                   ++fields;
3256                   if (fields >= 2)
3257                     {
3258                       *s = '\0';
3259                       break;
3260                     }
3261                   infield = 0;
3262                 }
3263             }
3264           else
3265             {
3266               if (! infield)
3267                 infield = 1;
3268               if (*s == '\'')
3269                 inquote = ! inquote;
3270             }
3271         }
3272     }
3273
3274   memset ((char *) (&the_ins), '\0', sizeof (the_ins));
3275   m68k_ip (str);
3276   er = the_ins.error;
3277   if (!er)
3278     {
3279       for (n = 0; n < the_ins.numargs; n++)
3280         if (the_ins.operands[n].error)
3281           {
3282             er = the_ins.operands[n].error;
3283             break;
3284           }
3285     }
3286   if (er)
3287     {
3288       as_bad ("%s -- statement `%s' ignored", er, str);
3289       return;
3290     }
3291
3292   if (the_ins.nfrag == 0)
3293     {
3294       /* No frag hacking involved; just put it out */
3295       toP = frag_more (2 * the_ins.numo);
3296       fromP = &the_ins.opcode[0];
3297       for (m = the_ins.numo; m; --m)
3298         {
3299           md_number_to_chars (toP, (long) (*fromP), 2);
3300           toP += 2;
3301           fromP++;
3302         }
3303       /* put out symbol-dependent info */
3304       for (m = 0; m < the_ins.nrel; m++)
3305         {
3306           switch (the_ins.reloc[m].wid)
3307             {
3308             case 'B':
3309               n = 1;
3310               break;
3311             case 'b':
3312               n = 1;
3313               break;
3314             case '3':
3315               n = 1;
3316               break;
3317             case 'w':
3318               n = 2;
3319               break;
3320             case 'l':
3321               n = 4;
3322               break;
3323             default:
3324               as_fatal ("Don't know how to figure width of %c in md_assemble()",
3325                         the_ins.reloc[m].wid);
3326             }
3327
3328           fixP = fix_new_exp (frag_now,
3329                               ((toP - frag_now->fr_literal)
3330                                - the_ins.numo * 2 + the_ins.reloc[m].n),
3331                               n,
3332                               &the_ins.reloc[m].exp,
3333                               the_ins.reloc[m].pcrel,
3334                               get_reloc_code (n, the_ins.reloc[m].pcrel,
3335                                               the_ins.reloc[m].pic_reloc));
3336           fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
3337         }
3338       return;
3339     }
3340
3341   /* There's some frag hacking */
3342   for (n = 0, fromP = &the_ins.opcode[0]; n < the_ins.nfrag; n++)
3343     {
3344       int wid;
3345
3346       if (n == 0)
3347         wid = 2 * the_ins.fragb[n].fragoff;
3348       else
3349         wid = 2 * (the_ins.numo - the_ins.fragb[n - 1].fragoff);
3350       toP = frag_more (wid);
3351       to_beg_P = toP;
3352       shorts_this_frag = 0;
3353       for (m = wid / 2; m; --m)
3354         {
3355           md_number_to_chars (toP, (long) (*fromP), 2);
3356           toP += 2;
3357           fromP++;
3358           shorts_this_frag++;
3359         }
3360       for (m = 0; m < the_ins.nrel; m++)
3361         {
3362           if ((the_ins.reloc[m].n) >= 2 * shorts_this_frag)
3363             {
3364               the_ins.reloc[m].n -= 2 * shorts_this_frag;
3365               break;
3366             }
3367           wid = the_ins.reloc[m].wid;
3368           if (wid == 0)
3369             continue;
3370           the_ins.reloc[m].wid = 0;
3371           wid = (wid == 'b') ? 1 : (wid == 'w') ? 2 : (wid == 'l') ? 4 : 4000;
3372
3373           fixP = fix_new_exp (frag_now,
3374                               ((toP - frag_now->fr_literal)
3375                                - the_ins.numo * 2 + the_ins.reloc[m].n),
3376                               wid,
3377                               &the_ins.reloc[m].exp,
3378                               the_ins.reloc[m].pcrel,
3379                               get_reloc_code (wid, the_ins.reloc[m].pcrel,
3380                                               the_ins.reloc[m].pic_reloc));
3381           fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
3382         }
3383       (void) frag_var (rs_machine_dependent, 10, 0,
3384                        (relax_substateT) (the_ins.fragb[n].fragty),
3385                        the_ins.fragb[n].fadd, the_ins.fragb[n].foff, to_beg_P);
3386     }
3387   n = (the_ins.numo - the_ins.fragb[n - 1].fragoff);
3388   shorts_this_frag = 0;
3389   if (n)
3390     {
3391       toP = frag_more (n * sizeof (short));
3392       while (n--)
3393         {
3394           md_number_to_chars (toP, (long) (*fromP), 2);
3395           toP += 2;
3396           fromP++;
3397           shorts_this_frag++;
3398         }
3399     }
3400   for (m = 0; m < the_ins.nrel; m++)
3401     {
3402       int wid;
3403
3404       wid = the_ins.reloc[m].wid;
3405       if (wid == 0)
3406         continue;
3407       the_ins.reloc[m].wid = 0;
3408       wid = (wid == 'b') ? 1 : (wid == 'w') ? 2 : (wid == 'l') ? 4 : 4000;
3409
3410       fixP = fix_new_exp (frag_now,
3411                           ((the_ins.reloc[m].n + toP - frag_now->fr_literal)
3412                            - shorts_this_frag * 2),
3413                           wid,
3414                           &the_ins.reloc[m].exp,
3415                           the_ins.reloc[m].pcrel,
3416                           get_reloc_code (wid, the_ins.reloc[m].pcrel,
3417                                           the_ins.reloc[m].pic_reloc));
3418       fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
3419     }
3420 }
3421
3422 void
3423 md_begin ()
3424 {
3425   /*
3426    * md_begin -- set up hash tables with 68000 instructions.
3427    * similar to what the vax assembler does.  ---phr
3428    */
3429   /* RMS claims the thing to do is take the m68k-opcode.h table, and make
3430      a copy of it at runtime, adding in the information we want but isn't
3431      there.  I think it'd be better to have an awk script hack the table
3432      at compile time.  Or even just xstr the table and use it as-is.  But
3433      my lord ghod hath spoken, so we do it this way.  Excuse the ugly var
3434      names.  */
3435
3436   register const struct m68k_opcode *ins;
3437   register struct m68k_incant *hack, *slak;
3438   register const char *retval = 0;      /* empty string, or error msg text */
3439   register unsigned int i;
3440   register char c;
3441
3442   if (flag_mri)
3443     {
3444       flag_reg_prefix_optional = 1;
3445       m68k_abspcadd = 1;
3446       m68k_rel32 = 0;
3447     }
3448
3449   op_hash = hash_new ();
3450
3451   obstack_begin (&robyn, 4000);
3452   for (i = 0; i < m68k_numopcodes; i++)
3453     {
3454       hack = slak = (struct m68k_incant *) obstack_alloc (&robyn, sizeof (struct m68k_incant));
3455       do
3456         {
3457           ins = &m68k_opcodes[i];
3458           /* We *could* ignore insns that don't match our arch here
3459              but just leaving them out of the hash. */
3460           slak->m_operands = ins->args;
3461           slak->m_opnum = strlen (slak->m_operands) / 2;
3462           slak->m_arch = ins->arch;
3463           slak->m_opcode = ins->opcode;
3464           /* This is kludgey */
3465           slak->m_codenum = ((ins->match) & 0xffffL) ? 2 : 1;
3466           if (i + 1 != m68k_numopcodes
3467               && !strcmp (ins->name, m68k_opcodes[i + 1].name))
3468             {
3469               slak->m_next = (struct m68k_incant *) obstack_alloc (&robyn, sizeof (struct m68k_incant));
3470               i++;
3471             }
3472           else
3473             slak->m_next = 0;
3474           slak = slak->m_next;
3475         }
3476       while (slak);
3477
3478       retval = hash_insert (op_hash, ins->name, (char *) hack);
3479       if (retval)
3480         as_fatal ("Internal Error:  Can't hash %s: %s", ins->name, retval);
3481     }
3482
3483   for (i = 0; i < m68k_numaliases; i++)
3484     {
3485       const char *name = m68k_opcode_aliases[i].primary;
3486       const char *alias = m68k_opcode_aliases[i].alias;
3487       PTR val = hash_find (op_hash, name);
3488       if (!val)
3489         as_fatal ("Internal Error: Can't find %s in hash table", name);
3490       retval = hash_insert (op_hash, alias, val);
3491       if (retval)
3492         as_fatal ("Internal Error: Can't hash %s: %s", alias, retval);
3493     }
3494
3495   /* In MRI mode, all unsized branches are variable sized.  Normally,
3496      they are word sized.  */
3497   if (flag_mri)
3498     {
3499       static struct m68k_opcode_alias mri_aliases[] =
3500         {
3501           { "bhi",      "jhi", },
3502           { "bls",      "jls", },
3503           { "bcc",      "jcc", },
3504           { "bcs",      "jcs", },
3505           { "bne",      "jne", },
3506           { "beq",      "jeq", },
3507           { "bvc",      "jvc", },
3508           { "bvs",      "jvs", },
3509           { "bpl",      "jpl", },
3510           { "bmi",      "jmi", },
3511           { "bge",      "jge", },
3512           { "blt",      "jlt", },
3513           { "bgt",      "jgt", },
3514           { "ble",      "jle", },
3515           { "bra",      "jra", },
3516           { "bsr",      "jbsr", },
3517         };
3518
3519       for (i = 0; i < sizeof mri_aliases / sizeof mri_aliases[0]; i++)
3520         {
3521           const char *name = mri_aliases[i].primary;
3522           const char *alias = mri_aliases[i].alias;
3523           PTR val = hash_find (op_hash, name);
3524           if (!val)
3525             as_fatal ("Internal Error: Can't find %s in hash table", name);
3526           retval = hash_jam (op_hash, alias, val);
3527           if (retval)
3528             as_fatal ("Internal Error: Can't hash %s: %s", alias, retval);
3529         }
3530     }
3531
3532   for (i = 0; i < sizeof (mklower_table); i++)
3533     mklower_table[i] = (isupper (c = (char) i)) ? tolower (c) : c;
3534
3535   for (i = 0; i < sizeof (notend_table); i++)
3536     {
3537       notend_table[i] = 0;
3538       alt_notend_table[i] = 0;
3539     }
3540   notend_table[','] = 1;
3541   notend_table['{'] = 1;
3542   notend_table['}'] = 1;
3543   alt_notend_table['a'] = 1;
3544   alt_notend_table['A'] = 1;
3545   alt_notend_table['d'] = 1;
3546   alt_notend_table['D'] = 1;
3547   alt_notend_table['#'] = 1;
3548   alt_notend_table['&'] = 1;
3549   alt_notend_table['f'] = 1;
3550   alt_notend_table['F'] = 1;
3551 #ifdef REGISTER_PREFIX
3552   alt_notend_table[REGISTER_PREFIX] = 1;
3553 #endif
3554
3555   /* We need to put '(' in alt_notend_table to handle
3556        cas2 %d0:%d2,%d3:%d4,(%a0):(%a1)
3557      */
3558   alt_notend_table['('] = 1;
3559
3560   /* We need to put '@' in alt_notend_table to handle
3561        cas2 %d0:%d2,%d3:%d4,@(%d0):@(%d1)
3562      */
3563   alt_notend_table['@'] = 1;
3564
3565   /* We need to put digits in alt_notend_table to handle
3566        bfextu %d0{24:1},%d0
3567      */
3568   alt_notend_table['0'] = 1;
3569   alt_notend_table['1'] = 1;
3570   alt_notend_table['2'] = 1;
3571   alt_notend_table['3'] = 1;
3572   alt_notend_table['4'] = 1;
3573   alt_notend_table['5'] = 1;
3574   alt_notend_table['6'] = 1;
3575   alt_notend_table['7'] = 1;
3576   alt_notend_table['8'] = 1;
3577   alt_notend_table['9'] = 1;
3578
3579 #ifndef MIT_SYNTAX_ONLY
3580   /* Insert pseudo ops, these have to go into the opcode table since
3581      gas expects pseudo ops to start with a dot */
3582   {
3583     int n = 0;
3584     while (mote_pseudo_table[n].poc_name)
3585       {
3586         hack = (struct m68k_incant *)
3587           obstack_alloc (&robyn, sizeof (struct m68k_incant));
3588         hash_insert (op_hash,
3589                      mote_pseudo_table[n].poc_name, (char *) hack);
3590         hack->m_operands = 0;
3591         hack->m_opnum = n;
3592         n++;
3593       }
3594   }
3595 #endif
3596
3597   init_regtable ();
3598
3599 #ifdef OBJ_ELF
3600   record_alignment (text_section, 2);
3601   record_alignment (data_section, 2);
3602   record_alignment (bss_section, 2);
3603 #endif
3604 }
3605
3606 static void
3607 select_control_regs ()
3608 {
3609   /* Note which set of "movec" control registers is available.  */
3610   switch (cpu_of_arch (current_architecture))
3611     {
3612     case m68000:
3613       control_regs = m68000_control_regs;
3614       break;
3615     case m68010:
3616       control_regs = m68010_control_regs;
3617       break;
3618     case m68020:
3619     case m68030:
3620       control_regs = m68020_control_regs;
3621       break;
3622     case m68040:
3623       control_regs = m68040_control_regs;
3624       break;
3625     case m68060:
3626       control_regs = m68060_control_regs;
3627       break;
3628     case cpu32:
3629       control_regs = cpu32_control_regs;
3630       break;
3631     case mcf5200:
3632       control_regs = mcf5200_control_regs;
3633       break;
3634     default:
3635       abort ();
3636     }
3637 }
3638
3639 void
3640 m68k_init_after_args ()
3641 {
3642   if (cpu_of_arch (current_architecture) == 0)
3643     {
3644       int i;
3645       const char *default_cpu = TARGET_CPU;
3646
3647       if (*default_cpu == 'm')
3648         default_cpu++;
3649       for (i = 0; i < n_archs; i++)
3650         if (strcasecmp (default_cpu, archs[i].name) == 0)
3651           break;
3652       if (i == n_archs)
3653         {
3654           as_bad ("unrecognized default cpu `%s' ???", TARGET_CPU);
3655           current_architecture |= m68020;
3656         }
3657       else
3658         current_architecture |= archs[i].arch;
3659     }
3660   /* Permit m68881 specification with all cpus; those that can't work
3661      with a coprocessor could be doing emulation.  */
3662   if (current_architecture & m68851)
3663     {
3664       if (current_architecture & m68040)
3665         {
3666           as_warn ("68040 and 68851 specified; mmu instructions may assemble incorrectly");
3667         }
3668     }
3669   /* What other incompatibilities could we check for?  */
3670
3671   /* Toss in some default assumptions about coprocessors.  */
3672   if (!no_68881
3673       && (cpu_of_arch (current_architecture)
3674           /* Can CPU32 have a 68881 coprocessor??  */
3675           & (m68020 | m68030 | cpu32)))
3676     {
3677       current_architecture |= m68881;
3678     }
3679   if (!no_68851
3680       && (cpu_of_arch (current_architecture) & m68020up) != 0
3681       && (cpu_of_arch (current_architecture) & m68040up) == 0)
3682     {
3683       current_architecture |= m68851;
3684     }
3685   if (no_68881 && (current_architecture & m68881))
3686     as_bad ("options for 68881 and no-68881 both given");
3687   if (no_68851 && (current_architecture & m68851))
3688     as_bad ("options for 68851 and no-68851 both given");
3689
3690 #ifdef OBJ_AOUT
3691   /* Work out the magic number.  This isn't very general.  */
3692   if (current_architecture & m68000)
3693     m68k_aout_machtype = 0;
3694   else if (current_architecture & m68010)
3695     m68k_aout_machtype = 1;
3696   else if (current_architecture & m68020)
3697     m68k_aout_machtype = 2;
3698   else
3699     m68k_aout_machtype = 2;
3700 #endif
3701
3702   /* Note which set of "movec" control registers is available.  */
3703   select_control_regs ();
3704
3705   if (cpu_of_arch (current_architecture) < m68020)
3706     md_relax_table[TAB (PCINDEX, BYTE)].rlx_more = 0;
3707 }
3708
3709 /* This is called if we go in or out of MRI mode because of the .mri
3710    pseudo-op.  */
3711
3712 void
3713 m68k_mri_mode_change (on)
3714      int on;
3715 {
3716   if (on)
3717     {
3718       if (! flag_reg_prefix_optional)
3719         {
3720           flag_reg_prefix_optional = 1;
3721 #ifdef REGISTER_PREFIX
3722           init_regtable ();
3723 #endif
3724         }
3725       m68k_abspcadd = 1;
3726       m68k_rel32 = 0;
3727     }
3728   else
3729     {
3730       if (! reg_prefix_optional_seen)
3731         {
3732 #ifdef REGISTER_PREFIX_OPTIONAL
3733           flag_reg_prefix_optional = REGISTER_PREFIX_OPTIONAL;
3734 #else
3735           flag_reg_prefix_optional = 0;
3736 #endif
3737 #ifdef REGISTER_PREFIX
3738           init_regtable ();
3739 #endif
3740         }
3741       m68k_abspcadd = 0;
3742       m68k_rel32 = 1;
3743     }
3744 }
3745
3746 /* Equal to MAX_PRECISION in atof-ieee.c */
3747 #define MAX_LITTLENUMS 6
3748
3749 /* Turn a string in input_line_pointer into a floating point constant
3750    of type type, and store the appropriate bytes in *litP.  The number
3751    of LITTLENUMS emitted is stored in *sizeP .  An error message is
3752    returned, or NULL on OK.  */
3753
3754 char *
3755 md_atof (type, litP, sizeP)
3756      char type;
3757      char *litP;
3758      int *sizeP;
3759 {
3760   int prec;
3761   LITTLENUM_TYPE words[MAX_LITTLENUMS];
3762   LITTLENUM_TYPE *wordP;
3763   char *t;
3764   char *atof_ieee ();
3765
3766   switch (type)
3767     {
3768     case 'f':
3769     case 'F':
3770     case 's':
3771     case 'S':
3772       prec = 2;
3773       break;
3774
3775     case 'd':
3776     case 'D':
3777     case 'r':
3778     case 'R':
3779       prec = 4;
3780       break;
3781
3782     case 'x':
3783     case 'X':
3784       prec = 6;
3785       break;
3786
3787     case 'p':
3788     case 'P':
3789       prec = 6;
3790       break;
3791
3792     default:
3793       *sizeP = 0;
3794       return "Bad call to MD_ATOF()";
3795     }
3796   t = atof_ieee (input_line_pointer, type, words);
3797   if (t)
3798     input_line_pointer = t;
3799
3800   *sizeP = prec * sizeof (LITTLENUM_TYPE);
3801   for (wordP = words; prec--;)
3802     {
3803       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
3804       litP += sizeof (LITTLENUM_TYPE);
3805     }
3806   return 0;
3807 }
3808
3809 void
3810 md_number_to_chars (buf, val, n)
3811      char *buf;
3812      valueT val;
3813      int n;
3814 {
3815   number_to_chars_bigendian (buf, val, n);
3816 }
3817
3818 static void
3819 md_apply_fix_2 (fixP, val)
3820      fixS *fixP;
3821      offsetT val;
3822 {
3823   addressT upper_limit;
3824   offsetT lower_limit;
3825
3826   /* This is unnecessary but it convinces the native rs6000 compiler
3827      to generate the code we want.  */
3828   char *buf = fixP->fx_frag->fr_literal;
3829   buf += fixP->fx_where;
3830   /* end ibm compiler workaround */
3831
3832   if (val & 0x80000000)
3833     val |= ~(addressT)0x7fffffff;
3834   else
3835     val &= 0x7fffffff;
3836
3837 #ifdef OBJ_ELF
3838   if (fixP->fx_addsy)
3839     {
3840       memset (buf, 0, fixP->fx_size);
3841       fixP->fx_addnumber = val; /* Remember value for emit_reloc */
3842       return;
3843     }
3844 #endif
3845
3846   switch (fixP->fx_size)
3847     {
3848       /* The cast to offsetT below are necessary to make code correct for
3849          machines where ints are smaller than offsetT */
3850     case 1:
3851       *buf++ = val;
3852       upper_limit = 0x7f;
3853       lower_limit = - (offsetT) 0x80;
3854       break;
3855     case 2:
3856       *buf++ = (val >> 8);
3857       *buf++ = val;
3858       upper_limit = 0x7fff;
3859       lower_limit = - (offsetT) 0x8000;
3860       break;
3861     case 4:
3862       *buf++ = (val >> 24);
3863       *buf++ = (val >> 16);
3864       *buf++ = (val >> 8);
3865       *buf++ = val;
3866       upper_limit = 0x7fffffff;
3867       lower_limit = - (offsetT) 0x7fffffff - 1; /* avoid constant overflow */
3868       break;
3869     default:
3870       BAD_CASE (fixP->fx_size);
3871     }
3872
3873   /* Fix up a negative reloc.  */
3874   if (fixP->fx_addsy == NULL && fixP->fx_subsy != NULL)
3875     {
3876       fixP->fx_addsy = fixP->fx_subsy;
3877       fixP->fx_subsy = NULL;
3878       fixP->fx_tcbit = 1;
3879     }
3880
3881   /* For non-pc-relative values, it's conceivable we might get something
3882      like "0xff" for a byte field.  So extend the upper part of the range
3883      to accept such numbers.  We arbitrarily disallow "-0xff" or "0xff+0xff",
3884      so that we can do any range checking at all.  */
3885   if (!fixP->fx_pcrel)
3886     upper_limit = upper_limit * 2 + 1;
3887
3888   if ((addressT) val > upper_limit
3889       && (val > 0 || val < lower_limit))
3890     as_bad_where (fixP->fx_file, fixP->fx_line, "value out of range");
3891
3892   /* A one byte PC-relative reloc means a short branch.  We can't use
3893      a short branch with a value of 0 or -1, because those indicate
3894      different opcodes (branches with longer offsets).  */
3895   if (fixP->fx_pcrel
3896       && fixP->fx_size == 1
3897       && (fixP->fx_addsy == NULL
3898           || S_IS_DEFINED (fixP->fx_addsy))
3899       && (val == 0 || val == -1))
3900     as_bad_where (fixP->fx_file, fixP->fx_line, "invalid byte branch offset");
3901 }
3902
3903 #ifdef BFD_ASSEMBLER
3904 int
3905 md_apply_fix (fixP, valp)
3906      fixS *fixP;
3907      valueT *valp;
3908 {
3909   md_apply_fix_2 (fixP, (addressT) *valp);
3910   return 1;
3911 }
3912 #else
3913 void md_apply_fix (fixP, val)
3914      fixS *fixP;
3915      long val;
3916 {
3917   md_apply_fix_2 (fixP, (addressT) val);
3918 }
3919 #endif
3920
3921 /* *fragP has been relaxed to its final size, and now needs to have
3922    the bytes inside it modified to conform to the new size  There is UGLY
3923    MAGIC here. ..
3924    */
3925 void
3926 md_convert_frag_1 (fragP)
3927      register fragS *fragP;
3928 {
3929   long disp;
3930   long ext = 0;
3931   fixS *fixP;
3932
3933   /* Address in object code of the displacement.  */
3934   register int object_address = fragP->fr_fix + fragP->fr_address;
3935
3936   /* Address in gas core of the place to store the displacement.  */
3937   /* This convinces the native rs6000 compiler to generate the code we
3938      want. */
3939   register char *buffer_address = fragP->fr_literal;
3940   buffer_address += fragP->fr_fix;
3941   /* end ibm compiler workaround */
3942
3943   /* The displacement of the address, from current location.  */
3944   disp = fragP->fr_symbol ? S_GET_VALUE (fragP->fr_symbol) : 0;
3945   disp = (disp + fragP->fr_offset) - object_address;
3946
3947 #ifdef BFD_ASSEMBLER
3948   disp += fragP->fr_symbol->sy_frag->fr_address;
3949 #endif
3950
3951   switch (fragP->fr_subtype)
3952     {
3953     case TAB (BCC68000, BYTE):
3954     case TAB (ABRANCH, BYTE):
3955       know (issbyte (disp));
3956       if (disp == 0)
3957         as_bad ("short branch with zero offset: use :w");
3958       fragP->fr_opcode[1] = disp;
3959       ext = 0;
3960       break;
3961     case TAB (DBCC, SHORT):
3962       know (issword (disp));
3963       ext = 2;
3964       break;
3965     case TAB (BCC68000, SHORT):
3966     case TAB (ABRANCH, SHORT):
3967       know (issword (disp));
3968       fragP->fr_opcode[1] = 0x00;
3969       ext = 2;
3970       break;
3971     case TAB (ABRANCH, LONG):
3972       if (cpu_of_arch (current_architecture) < m68020)
3973         {
3974           if (fragP->fr_opcode[0] == 0x61)
3975             /* BSR */
3976             {
3977               fragP->fr_opcode[0] = 0x4E;
3978               fragP->fr_opcode[1] = (char) 0xB9; /* JBSR with ABSL LONG offset */
3979
3980               fix_new (fragP,
3981                        fragP->fr_fix,
3982                        4,
3983                        fragP->fr_symbol,
3984                        fragP->fr_offset,
3985                        0,
3986                        NO_RELOC);
3987
3988               fragP->fr_fix += 4;
3989               ext = 0;
3990             }
3991           /* BRA */
3992           else if (fragP->fr_opcode[0] == 0x60)
3993             {
3994               fragP->fr_opcode[0] = 0x4E;
3995               fragP->fr_opcode[1] = (char) 0xF9; /* JMP  with ABSL LONG offset */
3996               fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3997                        fragP->fr_offset, 0, NO_RELOC);
3998               fragP->fr_fix += 4;
3999               ext = 0;
4000             }
4001           else
4002             {
4003               as_bad ("Long branch offset not supported.");
4004             }
4005         }
4006       else
4007         {
4008           fragP->fr_opcode[1] = (char) 0xff;
4009           ext = 4;
4010         }
4011       break;
4012     case TAB (BCC68000, LONG):
4013       /* only Bcc 68000 instructions can come here */
4014       /* change bcc into b!cc/jmp absl long */
4015       fragP->fr_opcode[0] ^= 0x01;      /* invert bcc */
4016       fragP->fr_opcode[1] = 0x6;/* branch offset = 6 */
4017
4018       /* JF: these used to be fr_opcode[2,3], but they may be in a
4019            different frag, in which case refering to them is a no-no.
4020            Only fr_opcode[0,1] are guaranteed to work. */
4021       *buffer_address++ = 0x4e; /* put in jmp long (0x4ef9) */
4022       *buffer_address++ = (char) 0xf9;
4023       fragP->fr_fix += 2;       /* account for jmp instruction */
4024       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
4025                fragP->fr_offset, 0, NO_RELOC);
4026       fragP->fr_fix += 4;
4027       ext = 0;
4028       break;
4029     case TAB (DBCC, LONG):
4030       /* only DBcc 68000 instructions can come here */
4031       /* change dbcc into dbcc/jmp absl long */
4032       /* JF: these used to be fr_opcode[2-7], but that's wrong */
4033       *buffer_address++ = 0x00; /* branch offset = 4 */
4034       *buffer_address++ = 0x04;
4035       *buffer_address++ = 0x60; /* put in bra pc+6 */
4036       *buffer_address++ = 0x06;
4037       *buffer_address++ = 0x4e; /* put in jmp long (0x4ef9) */
4038       *buffer_address++ = (char) 0xf9;
4039
4040       fragP->fr_fix += 6;       /* account for bra/jmp instructions */
4041       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
4042                fragP->fr_offset, 0, NO_RELOC);
4043       fragP->fr_fix += 4;
4044       ext = 0;
4045       break;
4046     case TAB (FBRANCH, SHORT):
4047       know ((fragP->fr_opcode[1] & 0x40) == 0);
4048       ext = 2;
4049       break;
4050     case TAB (FBRANCH, LONG):
4051       fragP->fr_opcode[1] |= 0x40;      /* Turn on LONG bit */
4052       ext = 4;
4053       break;
4054     case TAB (PCREL, SHORT):
4055       ext = 2;
4056       break;
4057     case TAB (PCREL, LONG):
4058       /* The thing to do here is force it to ABSOLUTE LONG, since
4059         PCREL is really trying to shorten an ABSOLUTE address anyway */
4060       /* JF FOO This code has not been tested */
4061       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol, fragP->fr_offset,
4062                0, NO_RELOC);
4063       if ((fragP->fr_opcode[1] & 0x3F) != 0x3A)
4064         as_bad ("Internal error (long PC-relative operand) for insn 0x%04x at 0x%lx",
4065                 (unsigned) fragP->fr_opcode[0],
4066                 (unsigned long) fragP->fr_address);
4067       fragP->fr_opcode[1] &= ~0x3F;
4068       fragP->fr_opcode[1] |= 0x39;      /* Mode 7.1 */
4069       fragP->fr_fix += 4;
4070       ext = 0;
4071       break;
4072     case TAB (PCLEA, SHORT):
4073       fix_new (fragP, (int) (fragP->fr_fix), 2, fragP->fr_symbol,
4074                fragP->fr_offset, 1, NO_RELOC);
4075       fragP->fr_opcode[1] &= ~0x3F;
4076       fragP->fr_opcode[1] |= 0x3A; /* 072 - mode 7.2 */
4077       ext = 2;
4078       break;
4079     case TAB (PCLEA, LONG):
4080       fixP = fix_new (fragP, (int) (fragP->fr_fix) + 2, 4, fragP->fr_symbol,
4081                       fragP->fr_offset, 1, NO_RELOC);
4082       fixP->fx_pcrel_adjust = 2;
4083       /* Already set to mode 7.3; this indicates: PC indirect with
4084          suppressed index, 32-bit displacement.  */
4085       *buffer_address++ = 0x01;
4086       *buffer_address++ = 0x70;
4087       fragP->fr_fix += 2;
4088       ext = 4;
4089       break;
4090
4091     case TAB (PCINDEX, BYTE):
4092       disp += 2;
4093       if (!issbyte (disp))
4094         {
4095           as_bad ("displacement doesn't fit in one byte");
4096           disp = 0;
4097         }
4098       assert (fragP->fr_fix >= 2);
4099       buffer_address[-2] &= ~1;
4100       buffer_address[-1] = disp;
4101       ext = 0;
4102       break;
4103     case TAB (PCINDEX, SHORT):
4104       disp += 2;
4105       assert (issword (disp));
4106       assert (fragP->fr_fix >= 2);
4107       buffer_address[-2] |= 0x1;
4108       buffer_address[-1] = 0x20;
4109       fixP = fix_new (fragP, (int) (fragP->fr_fix), 2, fragP->fr_symbol,
4110                       fragP->fr_offset, (fragP->fr_opcode[1] & 077) == 073,
4111                       NO_RELOC);
4112       fixP->fx_pcrel_adjust = 2;
4113       ext = 2;
4114       break;
4115     case TAB (PCINDEX, LONG):
4116       disp += 2;
4117       fixP = fix_new (fragP, (int) (fragP->fr_fix), 4, fragP->fr_symbol,
4118                       fragP->fr_offset, (fragP->fr_opcode[1] & 077) == 073,
4119                       NO_RELOC);
4120       fixP->fx_pcrel_adjust = 2;
4121       assert (fragP->fr_fix >= 2);
4122       buffer_address[-2] |= 0x1;
4123       buffer_address[-1] = 0x30;
4124       ext = 4;
4125       break;
4126     }
4127
4128   if (ext)
4129     {
4130       md_number_to_chars (buffer_address, (long) disp, (int) ext);
4131       fragP->fr_fix += ext;
4132     }
4133 }
4134
4135 #ifndef BFD_ASSEMBLER
4136
4137 void
4138 md_convert_frag (headers, sec, fragP)
4139      object_headers *headers;
4140      segT sec;
4141      fragS *fragP;
4142 {
4143   md_convert_frag_1 (fragP);
4144 }
4145
4146 #else
4147
4148 void
4149 md_convert_frag (abfd, sec, fragP)
4150      bfd *abfd;
4151      segT sec;
4152      fragS *fragP;
4153 {
4154   md_convert_frag_1 (fragP);
4155 }
4156 #endif
4157
4158 /* Force truly undefined symbols to their maximum size, and generally set up
4159    the frag list to be relaxed
4160    */
4161 int
4162 md_estimate_size_before_relax (fragP, segment)
4163      register fragS *fragP;
4164      segT segment;
4165 {
4166   int old_fix;
4167   register char *buffer_address = fragP->fr_fix + fragP->fr_literal;
4168
4169   old_fix = fragP->fr_fix;
4170
4171   /* handle SZ_UNDEF first, it can be changed to BYTE or SHORT */
4172   switch (fragP->fr_subtype)
4173     {
4174
4175     case TAB (ABRANCH, SZ_UNDEF):
4176       {
4177         if ((fragP->fr_symbol != NULL)  /* Not absolute */
4178             && S_GET_SEGMENT (fragP->fr_symbol) == segment)
4179           {
4180             fragP->fr_subtype = TAB (TABTYPE (fragP->fr_subtype), BYTE);
4181             break;
4182           }
4183         else if ((fragP->fr_symbol == 0) || (cpu_of_arch (current_architecture) < m68020))
4184           {
4185             /* On 68000, or for absolute value, switch to abs long */
4186             /* FIXME, we should check abs val, pick short or long */
4187             if (fragP->fr_opcode[0] == 0x61)
4188               {
4189                 fragP->fr_opcode[0] = 0x4E;
4190                 fragP->fr_opcode[1] = (char) 0xB9; /* JBSR with ABSL LONG offset */
4191                 fix_new (fragP, fragP->fr_fix, 4,
4192                          fragP->fr_symbol, fragP->fr_offset, 0, NO_RELOC);
4193                 fragP->fr_fix += 4;
4194                 frag_wane (fragP);
4195               }
4196             else if (fragP->fr_opcode[0] == 0x60)
4197               {
4198                 fragP->fr_opcode[0] = 0x4E;
4199                 fragP->fr_opcode[1] = (char) 0xF9; /* JMP  with ABSL LONG offset */
4200                 fix_new (fragP, fragP->fr_fix, 4,
4201                          fragP->fr_symbol, fragP->fr_offset, 0, NO_RELOC);
4202                 fragP->fr_fix += 4;
4203                 frag_wane (fragP);
4204               }
4205             else
4206               {
4207                 as_warn ("Long branch offset to extern symbol not supported.");
4208               }
4209           }
4210         else
4211           {                     /* Symbol is still undefined.  Make it simple */
4212             fix_new (fragP, (int) (fragP->fr_fix), 4, fragP->fr_symbol,
4213                      fragP->fr_offset, 1, NO_RELOC);
4214             fragP->fr_fix += 4;
4215             fragP->fr_opcode[1] = (char) 0xff;
4216             frag_wane (fragP);
4217             break;
4218           }
4219
4220         break;
4221       }                         /* case TAB(ABRANCH,SZ_UNDEF) */
4222
4223     case TAB (FBRANCH, SZ_UNDEF):
4224       {
4225         if (S_GET_SEGMENT (fragP->fr_symbol) == segment || flag_short_refs)
4226           {
4227             fragP->fr_subtype = TAB (FBRANCH, SHORT);
4228             fragP->fr_var += 2;
4229           }
4230         else
4231           {
4232             fix_new (fragP, (int) fragP->fr_fix, 4, fragP->fr_symbol,
4233                      fragP->fr_offset, 1, NO_RELOC);
4234             fragP->fr_fix += 4;
4235             fragP->fr_opcode[1] |= 0x40; /* Turn on LONG bit */
4236             frag_wane (fragP);
4237           }
4238         break;
4239       }                         /* TAB(FBRANCH,SZ_UNDEF) */
4240
4241     case TAB (PCREL, SZ_UNDEF):
4242       {
4243         if (S_GET_SEGMENT (fragP->fr_symbol) == segment
4244             || flag_short_refs
4245             || cpu_of_arch (current_architecture) < m68020)
4246           {
4247             fragP->fr_subtype = TAB (PCREL, SHORT);
4248             fragP->fr_var += 2;
4249           }
4250         else
4251           {
4252             fragP->fr_subtype = TAB (PCREL, LONG);
4253             fragP->fr_var += 4;
4254           }
4255         break;
4256       }                         /* TAB(PCREL,SZ_UNDEF) */
4257
4258     case TAB (BCC68000, SZ_UNDEF):
4259       {
4260         if ((fragP->fr_symbol != NULL)
4261             && S_GET_SEGMENT (fragP->fr_symbol) == segment)
4262           {
4263             fragP->fr_subtype = TAB (BCC68000, BYTE);
4264             break;
4265           }
4266         /* only Bcc 68000 instructions can come here */
4267         /* change bcc into b!cc/jmp absl long */
4268         fragP->fr_opcode[0] ^= 0x01;    /* invert bcc */
4269         if (flag_short_refs)
4270           {
4271             fragP->fr_opcode[1] = 0x04; /* branch offset = 6 */
4272             /* JF: these were fr_opcode[2,3] */
4273             buffer_address[0] = 0x4e;   /* put in jmp long (0x4ef9) */
4274             buffer_address[1] = (char) 0xf8;
4275             fragP->fr_fix += 2; /* account for jmp instruction */
4276             fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
4277                      fragP->fr_offset, 0, NO_RELOC);
4278             fragP->fr_fix += 2;
4279           }
4280         else
4281           {
4282             fragP->fr_opcode[1] = 0x06; /* branch offset = 6 */
4283             /* JF: these were fr_opcode[2,3] */
4284             buffer_address[0] = 0x4e;   /* put in jmp long (0x4ef9) */
4285             buffer_address[1] = (char) 0xf9;
4286             fragP->fr_fix += 2; /* account for jmp instruction */
4287             fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
4288                      fragP->fr_offset, 0, NO_RELOC);
4289             fragP->fr_fix += 4;
4290           }
4291         frag_wane (fragP);
4292         break;
4293       }                         /* case TAB(BCC68000,SZ_UNDEF) */
4294
4295     case TAB (DBCC, SZ_UNDEF):
4296       {
4297         if (fragP->fr_symbol != NULL && S_GET_SEGMENT (fragP->fr_symbol) == segment)
4298           {
4299             fragP->fr_subtype = TAB (DBCC, SHORT);
4300             fragP->fr_var += 2;
4301             break;
4302           }
4303         /* only DBcc 68000 instructions can come here */
4304         /* change dbcc into dbcc/jmp absl long */
4305         /* JF: these used to be fr_opcode[2-4], which is wrong. */
4306         buffer_address[0] = 0x00;       /* branch offset = 4 */
4307         buffer_address[1] = 0x04;
4308         buffer_address[2] = 0x60;       /* put in bra pc + ... */
4309
4310         if (flag_short_refs)
4311           {
4312             /* JF: these were fr_opcode[5-7] */
4313             buffer_address[3] = 0x04;   /* plus 4 */
4314             buffer_address[4] = 0x4e;   /* Put in Jump Word */
4315             buffer_address[5] = (char) 0xf8;
4316             fragP->fr_fix += 6; /* account for bra/jmp instruction */
4317             fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
4318                      fragP->fr_offset, 0, NO_RELOC);
4319             fragP->fr_fix += 2;
4320           }
4321         else
4322           {
4323             /* JF: these were fr_opcode[5-7] */
4324             buffer_address[3] = 0x06;   /* Plus 6 */
4325             buffer_address[4] = 0x4e;   /* put in jmp long (0x4ef9) */
4326             buffer_address[5] = (char) 0xf9;
4327             fragP->fr_fix += 6; /* account for bra/jmp instruction */
4328             fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
4329                      fragP->fr_offset, 0, NO_RELOC);
4330             fragP->fr_fix += 4;
4331           }
4332
4333         frag_wane (fragP);
4334         break;
4335       }                         /* case TAB(DBCC,SZ_UNDEF) */
4336
4337     case TAB (PCLEA, SZ_UNDEF):
4338       {
4339         if ((S_GET_SEGMENT (fragP->fr_symbol)) == segment
4340             || flag_short_refs
4341             || cpu_of_arch (current_architecture) < m68020)
4342           {
4343             fragP->fr_subtype = TAB (PCLEA, SHORT);
4344             fragP->fr_var += 2;
4345           }
4346         else
4347           {
4348             fragP->fr_subtype = TAB (PCLEA, LONG);
4349             fragP->fr_var += 6;
4350           }
4351         break;
4352       }                         /* TAB(PCLEA,SZ_UNDEF) */
4353
4354     case TAB (PCINDEX, SZ_UNDEF):
4355       if (S_GET_SEGMENT (fragP->fr_symbol) == segment
4356           || cpu_of_arch (current_architecture) < m68020)
4357         {
4358           fragP->fr_subtype = TAB (PCINDEX, BYTE);
4359         }
4360       else
4361         {
4362           fragP->fr_subtype = TAB (PCINDEX, LONG);
4363           fragP->fr_var += 4;
4364         }
4365       break;
4366
4367     default:
4368       break;
4369     }
4370
4371   /* now that SZ_UNDEF are taken care of, check others */
4372   switch (fragP->fr_subtype)
4373     {
4374     case TAB (BCC68000, BYTE):
4375     case TAB (ABRANCH, BYTE):
4376       /* We can't do a short jump to the next instruction, so in that
4377          case we force word mode.  At this point S_GET_VALUE should
4378          return the offset of the symbol within its frag.  If the
4379          symbol is at the start of a frag, and it is the next frag
4380          with any data in it (usually this is just the next frag, but
4381          assembler listings may introduce empty frags), we must use
4382          word mode.  */
4383       if (fragP->fr_symbol && S_GET_VALUE (fragP->fr_symbol) == 0)
4384         {
4385           fragS *l;
4386
4387           for (l = fragP->fr_next;
4388                l != fragP->fr_symbol->sy_frag;
4389                l = l->fr_next)
4390             if (l->fr_fix + l->fr_var != 0)
4391               break;
4392           if (l == fragP->fr_symbol->sy_frag)
4393             {
4394               fragP->fr_subtype = TAB (TABTYPE (fragP->fr_subtype), SHORT);
4395               fragP->fr_var += 2;
4396             }
4397         }
4398       break;
4399     default:
4400       break;
4401     }
4402   return fragP->fr_var + fragP->fr_fix - old_fix;
4403 }
4404
4405 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
4406 /* the bit-field entries in the relocation_info struct plays hell
4407    with the byte-order problems of cross-assembly.  So as a hack,
4408    I added this mach. dependent ri twiddler.  Ugly, but it gets
4409    you there. -KWK */
4410 /* on m68k: first 4 bytes are normal unsigned long, next three bytes
4411    are symbolnum, most sig. byte first.  Last byte is broken up with
4412    bit 7 as pcrel, bits 6 & 5 as length, bit 4 as pcrel, and the lower
4413    nibble as nuthin. (on Sun 3 at least) */
4414 /* Translate the internal relocation information into target-specific
4415    format. */
4416 #ifdef comment
4417 void
4418 md_ri_to_chars (the_bytes, ri)
4419      char *the_bytes;
4420      struct reloc_info_generic *ri;
4421 {
4422   /* this is easy */
4423   md_number_to_chars (the_bytes, ri->r_address, 4);
4424   /* now the fun stuff */
4425   the_bytes[4] = (ri->r_symbolnum >> 16) & 0x0ff;
4426   the_bytes[5] = (ri->r_symbolnum >> 8) & 0x0ff;
4427   the_bytes[6] = ri->r_symbolnum & 0x0ff;
4428   the_bytes[7] = (((ri->r_pcrel << 7) & 0x80) | ((ri->r_length << 5) & 0x60) |
4429                   ((ri->r_extern << 4) & 0x10));
4430 }
4431
4432 #endif /* comment */
4433
4434 #ifndef BFD_ASSEMBLER
4435 void
4436 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
4437      char *where;
4438      fixS *fixP;
4439      relax_addressT segment_address_in_file;
4440 {
4441   /*
4442    * In: length of relocation (or of address) in chars: 1, 2 or 4.
4443    * Out: GNU LD relocation length code: 0, 1, or 2.
4444    */
4445
4446   static CONST unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
4447   long r_symbolnum;
4448
4449   know (fixP->fx_addsy != NULL);
4450
4451   md_number_to_chars (where,
4452        fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
4453                       4);
4454
4455   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
4456                  ? S_GET_TYPE (fixP->fx_addsy)
4457                  : fixP->fx_addsy->sy_number);
4458
4459   where[4] = (r_symbolnum >> 16) & 0x0ff;
4460   where[5] = (r_symbolnum >> 8) & 0x0ff;
4461   where[6] = r_symbolnum & 0x0ff;
4462   where[7] = (((fixP->fx_pcrel << 7) & 0x80) | ((nbytes_r_length[fixP->fx_size] << 5) & 0x60) |
4463               (((!S_IS_DEFINED (fixP->fx_addsy)) << 4) & 0x10));
4464 }
4465 #endif
4466
4467 #endif /* OBJ_AOUT or OBJ_BOUT */
4468
4469 #ifndef WORKING_DOT_WORD
4470 CONST int md_short_jump_size = 4;
4471 CONST int md_long_jump_size = 6;
4472
4473 void
4474 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
4475      char *ptr;
4476      addressT from_addr, to_addr;
4477      fragS *frag;
4478      symbolS *to_symbol;
4479 {
4480   valueT offset;
4481
4482   offset = to_addr - (from_addr + 2);
4483
4484   md_number_to_chars (ptr, (valueT) 0x6000, 2);
4485   md_number_to_chars (ptr + 2, (valueT) offset, 2);
4486 }
4487
4488 void
4489 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
4490      char *ptr;
4491      addressT from_addr, to_addr;
4492      fragS *frag;
4493      symbolS *to_symbol;
4494 {
4495   valueT offset;
4496
4497   if (cpu_of_arch (current_architecture) < m68020)
4498     {
4499       offset = to_addr - S_GET_VALUE (to_symbol);
4500       md_number_to_chars (ptr, (valueT) 0x4EF9, 2);
4501       md_number_to_chars (ptr + 2, (valueT) offset, 4);
4502       fix_new (frag, (ptr + 2) - frag->fr_literal, 4, to_symbol, (offsetT) 0,
4503                0, NO_RELOC);
4504     }
4505   else
4506     {
4507       offset = to_addr - (from_addr + 2);
4508       md_number_to_chars (ptr, (valueT) 0x60ff, 2);
4509       md_number_to_chars (ptr + 2, (valueT) offset, 4);
4510     }
4511 }
4512
4513 #endif
4514
4515 /* Different values of OK tell what its OK to return.  Things that
4516    aren't OK are an error (what a shock, no?)
4517
4518    0:  Everything is OK
4519    10:  Absolute 1:8    only
4520    20:  Absolute 0:7    only
4521    30:  absolute 0:15   only
4522    40:  Absolute 0:31   only
4523    50:  absolute 0:127  only
4524    55:  absolute -64:63    only
4525    60:  absolute -128:127       only
4526    70:  absolute 0:4095 only
4527    80:  No bignums
4528
4529    */
4530
4531 static int
4532 get_num (exp, ok)
4533      struct m68k_exp *exp;
4534      int ok;
4535 {
4536   if (exp->exp.X_op == O_absent)
4537     {
4538       /* Do the same thing the VAX asm does */
4539       op (exp) = O_constant;
4540       adds (exp) = 0;
4541       subs (exp) = 0;
4542       offs (exp) = 0;
4543       if (ok == 10)
4544         {
4545           as_warn ("expression out of range: defaulting to 1");
4546           offs (exp) = 1;
4547         }
4548     }
4549   else if (exp->exp.X_op == O_constant)
4550     {
4551       switch (ok)
4552         {
4553         case 10:
4554           if (offs (exp) < 1 || offs (exp) > 8)
4555             {
4556               as_warn ("expression out of range: defaulting to 1");
4557               offs (exp) = 1;
4558             }
4559           break;
4560         case 20:
4561           if (offs (exp) < 0 || offs (exp) > 7)
4562             goto outrange;
4563           break;
4564         case 30:
4565           if (offs (exp) < 0 || offs (exp) > 15)
4566             goto outrange;
4567           break;
4568         case 40:
4569           if (offs (exp) < 0 || offs (exp) > 32)
4570             goto outrange;
4571           break;
4572         case 50:
4573           if (offs (exp) < 0 || offs (exp) > 127)
4574             goto outrange;
4575           break;
4576         case 55:
4577           if (offs (exp) < -64 || offs (exp) > 63)
4578             goto outrange;
4579           break;
4580         case 60:
4581           if (offs (exp) < -128 || offs (exp) > 127)
4582             goto outrange;
4583           break;
4584         case 70:
4585           if (offs (exp) < 0 || offs (exp) > 4095)
4586             {
4587             outrange:
4588               as_warn ("expression out of range: defaulting to 0");
4589               offs (exp) = 0;
4590             }
4591           break;
4592         default:
4593           break;
4594         }
4595     }
4596   else if (exp->exp.X_op == O_big)
4597     {
4598       if (offs (exp) <= 0       /* flonum */
4599           && (ok == 80          /* no bignums */
4600               || (ok > 10       /* small-int ranges including 0 ok */
4601                   /* If we have a flonum zero, a zero integer should
4602                      do as well (e.g., in moveq).  */
4603                   && generic_floating_point_number.exponent == 0
4604                   && generic_floating_point_number.low[0] == 0)))
4605         {
4606           /* HACK! Turn it into a long */
4607           LITTLENUM_TYPE words[6];
4608
4609           gen_to_words (words, 2, 8L);  /* These numbers are magic! */
4610           op (exp) = O_constant;
4611           adds (exp) = 0;
4612           subs (exp) = 0;
4613           offs (exp) = words[1] | (words[0] << 16);
4614         }
4615       else if (ok != 0)
4616         {
4617           op (exp) = O_constant;
4618           adds (exp) = 0;
4619           subs (exp) = 0;
4620           offs (exp) = (ok == 10) ? 1 : 0;
4621           as_warn ("Can't deal with expression; defaulting to %ld",
4622                    offs (exp));
4623         }
4624     }
4625   else
4626     {
4627       if (ok >= 10 && ok <= 70)
4628         {
4629           op (exp) = O_constant;
4630           adds (exp) = 0;
4631           subs (exp) = 0;
4632           offs (exp) = (ok == 10) ? 1 : 0;
4633           as_warn ("Can't deal with expression; defaulting to %ld",
4634                    offs (exp));
4635         }
4636     }
4637
4638   if (exp->size != SIZE_UNSPEC)
4639     {
4640       switch (exp->size)
4641         {
4642         case SIZE_UNSPEC:
4643         case SIZE_LONG:
4644           break;
4645         case SIZE_BYTE:
4646           if (!isbyte (offs (exp)))
4647             as_warn ("expression doesn't fit in BYTE");
4648           break;
4649         case SIZE_WORD:
4650           if (!isword (offs (exp)))
4651             as_warn ("expression doesn't fit in WORD");
4652           break;
4653         }
4654     }
4655
4656   return offs (exp);
4657 }
4658
4659 /* These are the back-ends for the various machine dependent pseudo-ops.  */
4660 void demand_empty_rest_of_line ();      /* Hate those extra verbose names */
4661
4662 static void
4663 s_data1 (ignore)
4664      int ignore;
4665 {
4666   subseg_set (data_section, 1);
4667   demand_empty_rest_of_line ();
4668 }
4669
4670 static void
4671 s_data2 (ignore)
4672      int ignore;
4673 {
4674   subseg_set (data_section, 2);
4675   demand_empty_rest_of_line ();
4676 }
4677
4678 static void
4679 s_bss (ignore)
4680      int ignore;
4681 {
4682   /* We don't support putting frags in the BSS segment, we fake it
4683      by marking in_bss, then looking at s_skip for clues.  */
4684
4685   subseg_set (bss_section, 0);
4686   demand_empty_rest_of_line ();
4687 }
4688
4689 static void
4690 s_even (ignore)
4691      int ignore;
4692 {
4693   register int temp;
4694   register long temp_fill;
4695
4696   temp = 1;                     /* JF should be 2? */
4697   temp_fill = get_absolute_expression ();
4698   if (!need_pass_2)             /* Never make frag if expect extra pass. */
4699     frag_align (temp, (int) temp_fill);
4700   demand_empty_rest_of_line ();
4701 }
4702
4703 static void
4704 s_proc (ignore)
4705      int ignore;
4706 {
4707   demand_empty_rest_of_line ();
4708 }
4709 \f
4710 /* Pseudo-ops handled for MRI compatibility.  */
4711
4712 /* Handle an MRI style chip specification.  */
4713
4714 static void
4715 mri_chip ()
4716 {
4717   char *s;
4718   char c;
4719   int i;
4720
4721   s = input_line_pointer;
4722   c = get_symbol_end ();
4723   for (i = 0; i < n_archs; i++)
4724     if (strcasecmp (s, archs[i].name) == 0)
4725       break;
4726   if (i >= n_archs)
4727     {
4728       as_bad ("%s: unrecognized processor name", s);
4729       *input_line_pointer = c;
4730       ignore_rest_of_line ();
4731       return;
4732     }
4733   *input_line_pointer = c;
4734
4735   if (*input_line_pointer == '/')
4736     current_architecture = 0;
4737   else
4738     current_architecture &= m68881 | m68851;
4739   current_architecture |= archs[i].arch;
4740
4741   while (*input_line_pointer == '/')
4742     {
4743       ++input_line_pointer;
4744       s = input_line_pointer;
4745       c = get_symbol_end ();
4746       if (strcmp (s, "68881") == 0)
4747         current_architecture |= m68881;
4748       else if (strcmp (s, "68851") == 0)
4749         current_architecture |= m68851;
4750       *input_line_pointer = c;
4751     }
4752
4753   /* Update info about available control registers.  */
4754   select_control_regs ();
4755 }
4756
4757 /* The MRI CHIP pseudo-op.  */
4758
4759 static void
4760 s_chip (ignore)
4761      int ignore;
4762 {
4763   char *stop = NULL;
4764   char stopc;
4765
4766   if (flag_mri)
4767     stop = mri_comment_field (&stopc);
4768   mri_chip ();
4769   if (flag_mri)
4770     mri_comment_end (stop, stopc);
4771   demand_empty_rest_of_line ();
4772 }
4773
4774 /* The MRI FOPT pseudo-op.  */
4775
4776 static void
4777 s_fopt (ignore)
4778      int ignore;
4779 {
4780   SKIP_WHITESPACE ();
4781
4782   if (strncasecmp (input_line_pointer, "ID=", 3) == 0)
4783     {
4784       int temp;
4785
4786       input_line_pointer += 3;
4787       temp = get_absolute_expression ();
4788       if (temp < 0 || temp > 7)
4789         as_bad ("bad coprocessor id");
4790       else
4791         m68k_float_copnum = COP0 + temp;
4792     }
4793   else
4794     {
4795       as_bad ("unrecognized fopt option");
4796       ignore_rest_of_line ();
4797       return;
4798     }
4799
4800   demand_empty_rest_of_line ();
4801 }
4802
4803 /* The structure used to handle the MRI OPT pseudo-op.  */
4804
4805 struct opt_action
4806 {
4807   /* The name of the option.  */
4808   const char *name;
4809
4810   /* If this is not NULL, just call this function.  The first argument
4811      is the ARG field of this structure, the second argument is
4812      whether the option was negated.  */
4813   void (*pfn) PARAMS ((int arg, int on));
4814
4815   /* If this is not NULL, and the PFN field is NULL, set the variable
4816      this points to.  Set it to the ARG field if the option was not
4817      negated, and the NOTARG field otherwise.  */
4818   int *pvar;
4819
4820   /* The value to pass to PFN or to assign to *PVAR.  */
4821   int arg;
4822
4823   /* The value to assign to *PVAR if the option is negated.  If PFN is
4824      NULL, and PVAR is not NULL, and ARG and NOTARG are the same, then
4825      the option may not be negated.  */
4826   int notarg;
4827 };
4828
4829 /* The table used to handle the MRI OPT pseudo-op.  */
4830
4831 static void skip_to_comma PARAMS ((int, int));
4832 static void opt_nest PARAMS ((int, int));
4833 static void opt_chip PARAMS ((int, int));
4834 static void opt_list PARAMS ((int, int));
4835 static void opt_list_symbols PARAMS ((int, int));
4836
4837 static const struct opt_action opt_table[] =
4838 {
4839   { "abspcadd", 0, &m68k_abspcadd, 1, 0 },
4840
4841   /* We do relaxing, so there is little use for these options.  */
4842   { "b", 0, 0, 0, 0 },
4843   { "brs", 0, 0, 0, 0 },
4844   { "brb", 0, 0, 0, 0 },
4845   { "brl", 0, 0, 0, 0 },
4846   { "brw", 0, 0, 0, 0 },
4847
4848   { "c", 0, 0, 0, 0 },
4849   { "cex", 0, 0, 0, 0 },
4850   { "case", 0, &symbols_case_sensitive, 1, 0 },
4851   { "cl", 0, 0, 0, 0 },
4852   { "cre", 0, 0, 0, 0 },
4853   { "d", 0, &flag_keep_locals, 1, 0 },
4854   { "e", 0, 0, 0, 0 },
4855   { "f", 0, &flag_short_refs, 1, 0 },
4856   { "frs", 0, &flag_short_refs, 1, 0 },
4857   { "frl", 0, &flag_short_refs, 0, 1 },
4858   { "g", 0, 0, 0, 0 },
4859   { "i", 0, 0, 0, 0 },
4860   { "m", 0, 0, 0, 0 },
4861   { "mex", 0, 0, 0, 0 },
4862   { "mc", 0, 0, 0, 0 },
4863   { "md", 0, 0, 0, 0 },
4864   { "nest", opt_nest, 0, 0, 0 },
4865   { "next", skip_to_comma, 0, 0, 0 },
4866   { "o", 0, 0, 0, 0 },
4867   { "old", 0, 0, 0, 0 },
4868   { "op", skip_to_comma, 0, 0, 0 },
4869   { "pco", 0, 0, 0, 0 },
4870   { "p", opt_chip, 0, 0, 0 },
4871   { "pcr", 0, 0, 0, 0 },
4872   { "pcs", 0, 0, 0, 0 },
4873   { "r", 0, 0, 0, 0 },
4874   { "quick", 0, &m68k_quick, 1, 0 },
4875   { "rel32", 0, &m68k_rel32, 1, 0 },
4876   { "s", opt_list, 0, 0, 0 },
4877   { "t", opt_list_symbols, 0, 0, 0 },
4878   { "w", 0, &flag_no_warnings, 0, 1 },
4879   { "x", 0, 0, 0, 0 }
4880 };
4881
4882 #define OPTCOUNT (sizeof opt_table / sizeof opt_table[0])
4883
4884 /* The MRI OPT pseudo-op.  */
4885
4886 static void
4887 s_opt (ignore)
4888      int ignore;
4889 {
4890   do
4891     {
4892       int t;
4893       char *s;
4894       char c;
4895       int i;
4896       const struct opt_action *o;
4897
4898       SKIP_WHITESPACE ();
4899
4900       t = 1;
4901       if (*input_line_pointer == '-')
4902         {
4903           ++input_line_pointer;
4904           t = 0;
4905         }
4906       else if (strncasecmp (input_line_pointer, "NO", 2) == 0)
4907         {
4908           input_line_pointer += 2;
4909           t = 0;
4910         }
4911
4912       s = input_line_pointer;
4913       c = get_symbol_end ();
4914
4915       for (i = 0, o = opt_table; i < OPTCOUNT; i++, o++)
4916         {
4917           if (strcasecmp (s, o->name) == 0)
4918             {
4919               if (o->pfn)
4920                 {
4921                   /* Restore input_line_pointer now in case the option
4922                      takes arguments.  */
4923                   *input_line_pointer = c;
4924                   (*o->pfn) (o->arg, t);
4925                 }
4926               else if (o->pvar != NULL)
4927                 {
4928                   if (! t && o->arg == o->notarg)
4929                     as_bad ("option `%s' may not be negated", s);
4930                   *input_line_pointer = c;
4931                   *o->pvar = t ? o->arg : o->notarg;
4932                 }
4933               else
4934                 *input_line_pointer = c;
4935               break;
4936             }
4937         }
4938       if (i >= OPTCOUNT)
4939         {
4940           as_bad ("option `%s' not recognized", s);
4941           *input_line_pointer = c;
4942         }
4943     }
4944   while (*input_line_pointer++ == ',');
4945
4946   /* Move back to terminating character.  */
4947   --input_line_pointer;
4948   demand_empty_rest_of_line ();
4949 }
4950
4951 /* Skip ahead to a comma.  This is used for OPT options which we do
4952    not suppor tand which take arguments.  */
4953
4954 static void
4955 skip_to_comma (arg, on)
4956      int arg;
4957      int on;
4958 {
4959   while (*input_line_pointer != ','
4960          && ! is_end_of_line[(unsigned char) *input_line_pointer])
4961     ++input_line_pointer;
4962 }
4963
4964 /* Handle the OPT NEST=depth option.  */
4965
4966 static void
4967 opt_nest (arg, on)
4968      int arg;
4969      int on;
4970 {
4971   if (*input_line_pointer != '=')
4972     {
4973       as_bad ("bad format of OPT NEST=depth");
4974       return;
4975     }
4976
4977   ++input_line_pointer;
4978   max_macro_nest = get_absolute_expression ();
4979 }
4980
4981 /* Handle the OPT P=chip option.  */
4982
4983 static void
4984 opt_chip (arg, on)
4985      int arg;
4986      int on;
4987 {
4988   if (*input_line_pointer != '=')
4989     {
4990       /* This is just OPT P, which we do not support.  */
4991       return;
4992     }
4993
4994   ++input_line_pointer;
4995   mri_chip ();
4996 }
4997
4998 /* Handle the OPT S option.  */
4999
5000 static void
5001 opt_list (arg, on)
5002      int arg;
5003      int on;
5004 {
5005   listing_list (on);
5006 }
5007
5008 /* Handle the OPT T option.  */
5009
5010 static void
5011 opt_list_symbols (arg, on)
5012      int arg;
5013      int on;
5014 {
5015   if (on)
5016     listing |= LISTING_SYMBOLS;
5017   else
5018     listing &=~ LISTING_SYMBOLS;
5019 }
5020
5021 /* Handle the MRI REG pseudo-op.  */
5022
5023 static void
5024 s_reg (ignore)
5025      int ignore;
5026 {
5027   char *s;
5028   int c;
5029   struct m68k_op rop;
5030   unsigned long mask;
5031   char *stop = NULL;
5032   char stopc;
5033
5034   if (line_label == NULL)
5035     {
5036       as_bad ("missing label");
5037       ignore_rest_of_line ();
5038       return;
5039     }
5040
5041   if (flag_mri)
5042     stop = mri_comment_field (&stopc);
5043
5044   SKIP_WHITESPACE ();
5045
5046   s = input_line_pointer;
5047   while (isalnum ((unsigned char) *input_line_pointer)
5048 #ifdef REGISTER_PREFIX
5049          || *input_line_pointer == REGISTER_PREFIX
5050 #endif
5051          || *input_line_pointer == '/'
5052          || *input_line_pointer == '-')
5053     ++input_line_pointer;
5054   c = *input_line_pointer;
5055   *input_line_pointer = '\0';
5056
5057   if (m68k_ip_op (s, &rop) != 0)
5058     {
5059       if (rop.error == NULL)
5060         as_bad ("bad register list");
5061       else
5062         as_bad ("bad register list: %s", rop.error);
5063       *input_line_pointer = c;
5064       ignore_rest_of_line ();
5065       return;
5066     }
5067
5068   *input_line_pointer = c;
5069
5070   if (rop.mode == REGLST)
5071     mask = rop.mask;
5072   else if (rop.mode == DREG)
5073     mask = 1 << (rop.reg - DATA0);
5074   else if (rop.mode == AREG)
5075     mask = 1 << (rop.reg - ADDR0 + 8);
5076   else if (rop.mode == FPREG)
5077     mask = 1 << (rop.reg - FP0 + 16);
5078   else if (rop.mode == CONTROL
5079            && rop.reg == FPI)
5080     mask = 1 << 24;
5081   else if (rop.mode == CONTROL
5082            && rop.reg == FPS)
5083     mask = 1 << 25;
5084   else if (rop.mode == CONTROL
5085            && rop.reg == FPC)
5086     mask = 1 << 26;
5087   else
5088     {
5089       as_bad ("bad register list");
5090       ignore_rest_of_line ();
5091       return;
5092     }
5093
5094   S_SET_SEGMENT (line_label, absolute_section);
5095   S_SET_VALUE (line_label, mask);
5096   line_label->sy_frag = &zero_address_frag;
5097
5098   if (flag_mri)
5099     mri_comment_end (stop, stopc);
5100
5101   demand_empty_rest_of_line ();
5102 }
5103
5104 /* This structure is used for the MRI SAVE and RESTORE pseudo-ops.  */
5105
5106 struct save_opts
5107 {
5108   struct save_opts *next;
5109   int abspcadd;
5110   int symbols_case_sensitive;
5111   int keep_locals;
5112   int short_refs;
5113   int architecture;
5114   int quick;
5115   int rel32;
5116   int listing;
5117   int no_warnings;
5118   /* FIXME: We don't save OPT S.  */
5119 };
5120
5121 /* This variable holds the stack of saved options.  */
5122
5123 static struct save_opts *save_stack;
5124
5125 /* The MRI SAVE pseudo-op.  */
5126
5127 static void
5128 s_save (ignore)
5129      int ignore;
5130 {
5131   struct save_opts *s;
5132
5133   s = (struct save_opts *) xmalloc (sizeof (struct save_opts));
5134   s->abspcadd = m68k_abspcadd;
5135   s->symbols_case_sensitive = symbols_case_sensitive;
5136   s->keep_locals = flag_keep_locals;
5137   s->short_refs = flag_short_refs;
5138   s->architecture = current_architecture;
5139   s->quick = m68k_quick;
5140   s->rel32 = m68k_rel32;
5141   s->listing = listing;
5142   s->no_warnings = flag_no_warnings;
5143
5144   s->next = save_stack;
5145   save_stack = s;
5146
5147   demand_empty_rest_of_line ();
5148 }
5149
5150 /* The MRI RESTORE pseudo-op.  */
5151
5152 static void
5153 s_restore (ignore)
5154      int ignore;
5155 {
5156   struct save_opts *s;
5157
5158   if (save_stack == NULL)
5159     {
5160       as_bad ("restore without save");
5161       ignore_rest_of_line ();
5162       return;
5163     }
5164
5165   s = save_stack;
5166   save_stack = s->next;
5167
5168   m68k_abspcadd = s->abspcadd;
5169   symbols_case_sensitive = s->symbols_case_sensitive;
5170   flag_keep_locals = s->keep_locals;
5171   flag_short_refs = s->short_refs;
5172   current_architecture = s->architecture;
5173   m68k_quick = s->quick;
5174   m68k_rel32 = s->rel32;
5175   listing = s->listing;
5176   flag_no_warnings = s->no_warnings;
5177
5178   free (s);
5179
5180   demand_empty_rest_of_line ();
5181 }
5182
5183 /* Types of MRI structured control directives.  */
5184
5185 enum mri_control_type
5186 {
5187   mri_for,
5188   mri_if,
5189   mri_repeat,
5190   mri_while
5191 };
5192
5193 /* This structure is used to stack the MRI structured control
5194    directives.  */
5195
5196 struct mri_control_info
5197 {
5198   /* The directive within which this one is enclosed.  */
5199   struct mri_control_info *outer;
5200
5201   /* The type of directive.  */
5202   enum mri_control_type type;
5203
5204   /* Whether an ELSE has been in an IF.  */
5205   int else_seen;
5206
5207   /* The add or sub statement at the end of a FOR.  */
5208   char *incr;
5209
5210   /* The label of the top of a FOR or REPEAT loop.  */
5211   char *top;
5212
5213   /* The label to jump to for the next iteration, or the else
5214      expression of a conditional.  */
5215   char *next;
5216
5217   /* The label to jump to to break out of the loop, or the label past
5218      the end of a conditional.  */
5219   char *bottom;
5220 };
5221
5222 /* The stack of MRI structured control directives.  */
5223
5224 static struct mri_control_info *mri_control_stack;
5225
5226 /* The current MRI structured control directive index number, used to
5227    generate label names.  */
5228
5229 static int mri_control_index;
5230
5231 /* Some function prototypes.  */
5232
5233 static char *mri_control_label PARAMS ((void));
5234 static struct mri_control_info *push_mri_control
5235   PARAMS ((enum mri_control_type));
5236 static void pop_mri_control PARAMS ((void));
5237 static int parse_mri_condition PARAMS ((int *));
5238 static int parse_mri_control_operand
5239   PARAMS ((int *, char **, char **, char **, char **));
5240 static int swap_mri_condition PARAMS ((int));
5241 static int reverse_mri_condition PARAMS ((int));
5242 static void build_mri_control_operand
5243   PARAMS ((int, int, char *, char *, char *, char *, const char *,
5244            const char *, int));
5245 static void parse_mri_control_expression
5246   PARAMS ((char *, int, const char *, const char *, int));
5247
5248 /* Generate a new MRI label structured control directive label name.  */
5249
5250 static char *
5251 mri_control_label ()
5252 {
5253   char *n;
5254
5255   n = (char *) xmalloc (20);
5256   sprintf (n, "%smc%d", FAKE_LABEL_NAME, mri_control_index);
5257   ++mri_control_index;
5258   return n;
5259 }
5260
5261 /* Create a new MRI structured control directive.  */
5262
5263 static struct mri_control_info *
5264 push_mri_control (type)
5265      enum mri_control_type type;
5266 {
5267   struct mri_control_info *n;
5268
5269   n = (struct mri_control_info *) xmalloc (sizeof (struct mri_control_info));
5270
5271   n->type = type;
5272   n->else_seen = 0;
5273   if (type == mri_if || type == mri_while)
5274     n->top = NULL;
5275   else
5276     n->top = mri_control_label ();
5277   n->next = mri_control_label ();
5278   n->bottom = mri_control_label ();
5279
5280   n->outer = mri_control_stack;
5281   mri_control_stack = n;
5282
5283   return n;
5284 }
5285
5286 /* Pop off the stack of MRI structured control directives.  */
5287
5288 static void
5289 pop_mri_control ()
5290 {
5291   struct mri_control_info *n;
5292
5293   n = mri_control_stack;
5294   mri_control_stack = n->outer;
5295   if (n->top != NULL)
5296     free (n->top);
5297   free (n->next);
5298   free (n->bottom);
5299   free (n);
5300 }
5301
5302 /* Recognize a condition code in an MRI structured control expression.  */
5303
5304 static int
5305 parse_mri_condition (pcc)
5306      int *pcc;
5307 {
5308   char c1, c2;
5309
5310   know (*input_line_pointer == '<');
5311
5312   ++input_line_pointer;
5313   c1 = *input_line_pointer++;
5314   c2 = *input_line_pointer++;
5315
5316   if (*input_line_pointer != '>')
5317     {
5318       as_bad ("syntax error in structured control directive");
5319       return 0;
5320     }
5321
5322   ++input_line_pointer;
5323   SKIP_WHITESPACE ();
5324
5325   if (isupper (c1))
5326     c1 = tolower (c1);
5327   if (isupper (c2))
5328     c2 = tolower (c2);
5329
5330   *pcc = (c1 << 8) | c2;
5331
5332   return 1;
5333 }
5334
5335 /* Parse a single operand in an MRI structured control expression.  */
5336
5337 static int
5338 parse_mri_control_operand (pcc, leftstart, leftstop, rightstart, rightstop)
5339      int *pcc;
5340      char **leftstart;
5341      char **leftstop;
5342      char **rightstart;
5343      char **rightstop;
5344 {
5345   char *s;
5346
5347   SKIP_WHITESPACE ();
5348
5349   *pcc = -1;
5350   *leftstart = NULL;
5351   *leftstop = NULL;
5352   *rightstart = NULL;
5353   *rightstop = NULL;
5354
5355   if (*input_line_pointer == '<')
5356     {
5357       /* It's just a condition code.  */
5358       return parse_mri_condition (pcc);
5359     }
5360
5361   /* Look ahead for the condition code.  */
5362   for (s = input_line_pointer; *s != '\0'; ++s)
5363     {
5364       if (*s == '<' && s[1] != '\0' && s[2] != '\0' && s[3] == '>')
5365         break;
5366     }
5367   if (*s == '\0')
5368     {
5369       as_bad ("missing condition code in structured control directive");
5370       return 0;
5371     }
5372
5373   *leftstart = input_line_pointer;
5374   *leftstop = s;
5375   if (*leftstop > *leftstart
5376       && ((*leftstop)[-1] == ' ' || (*leftstop)[-1] == '\t'))
5377     --*leftstop;
5378
5379   input_line_pointer = s;
5380   if (! parse_mri_condition (pcc))
5381     return 0;
5382
5383   /* Look ahead for AND or OR or end of line.  */
5384   for (s = input_line_pointer; *s != '\0'; ++s)
5385     {
5386       if ((strncasecmp (s, "AND", 3) == 0
5387            && (s[3] == '.' || ! is_part_of_name (s[3])))
5388           || (strncasecmp (s, "OR", 2) == 0
5389               && (s[2] == '.' || ! is_part_of_name (s[2]))))
5390         break;
5391     }
5392
5393   *rightstart = input_line_pointer;
5394   *rightstop = s;
5395   if (*rightstop > *rightstart
5396       && ((*rightstop)[-1] == ' ' || (*rightstop)[-1] == '\t'))
5397     --*rightstop;
5398
5399   input_line_pointer = s;
5400
5401   return 1;
5402 }
5403
5404 #define MCC(b1, b2) (((b1) << 8) | (b2))
5405
5406 /* Swap the sense of a condition.  This changes the condition so that
5407    it generates the same result when the operands are swapped.  */
5408
5409 static int
5410 swap_mri_condition (cc)
5411      int cc;
5412 {
5413   switch (cc)
5414     {
5415     case MCC ('h', 'i'): return MCC ('c', 's');
5416     case MCC ('l', 's'): return MCC ('c', 'c');
5417     case MCC ('c', 'c'): return MCC ('l', 's');
5418     case MCC ('c', 's'): return MCC ('h', 'i');
5419     case MCC ('p', 'l'): return MCC ('m', 'i');
5420     case MCC ('m', 'i'): return MCC ('p', 'l');
5421     case MCC ('g', 'e'): return MCC ('l', 'e');
5422     case MCC ('l', 't'): return MCC ('g', 't');
5423     case MCC ('g', 't'): return MCC ('l', 't');
5424     case MCC ('l', 'e'): return MCC ('g', 'e');
5425     }
5426   return cc;
5427 }
5428
5429 /* Reverse the sense of a condition.  */
5430
5431 static int
5432 reverse_mri_condition (cc)
5433      int cc;
5434 {
5435   switch (cc)
5436     {
5437     case MCC ('h', 'i'): return MCC ('l', 's');
5438     case MCC ('l', 's'): return MCC ('h', 'i');
5439     case MCC ('c', 'c'): return MCC ('c', 's');
5440     case MCC ('c', 's'): return MCC ('c', 'c');
5441     case MCC ('n', 'e'): return MCC ('e', 'q');
5442     case MCC ('e', 'q'): return MCC ('n', 'e');
5443     case MCC ('v', 'c'): return MCC ('v', 's');
5444     case MCC ('v', 's'): return MCC ('v', 'c');
5445     case MCC ('p', 'l'): return MCC ('m', 'i');
5446     case MCC ('m', 'i'): return MCC ('p', 'l');
5447     case MCC ('g', 'e'): return MCC ('l', 't');
5448     case MCC ('l', 't'): return MCC ('g', 'e');
5449     case MCC ('g', 't'): return MCC ('l', 'e');
5450     case MCC ('l', 'e'): return MCC ('g', 't');
5451     }
5452   return cc;
5453 }
5454
5455 /* Build an MRI structured control expression.  This generates test
5456    and branch instructions.  It goes to TRUELAB if the condition is
5457    true, and to FALSELAB if the condition is false.  Exactly one of
5458    TRUELAB and FALSELAB will be NULL, meaning to fall through.  QUAL
5459    is the size qualifier for the expression.  EXTENT is the size to
5460    use for the branch.  */
5461
5462 static void
5463 build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5464                            rightstop, truelab, falselab, extent)
5465      int qual;
5466      int cc;
5467      char *leftstart;
5468      char *leftstop;
5469      char *rightstart;
5470      char *rightstop;
5471      const char *truelab;
5472      const char *falselab;
5473      int extent;
5474 {
5475   char *buf;
5476   char *s;
5477
5478   if (leftstart != NULL)
5479     {
5480       struct m68k_op leftop, rightop;
5481       char c;
5482
5483       /* Swap the compare operands, if necessary, to produce a legal
5484          m68k compare instruction.  Comparing a register operand with
5485          a non-register operand requires the register to be on the
5486          right (cmp, cmpa).  Comparing an immediate value with
5487          anything requires the immediate value to be on the left
5488          (cmpi).  */
5489
5490       c = *leftstop;
5491       *leftstop = '\0';
5492       (void) m68k_ip_op (leftstart, &leftop);
5493       *leftstop = c;
5494
5495       c = *rightstop;
5496       *rightstop = '\0';
5497       (void) m68k_ip_op (rightstart, &rightop);
5498       *rightstop = c;
5499
5500       if (rightop.mode == IMMED
5501           || ((leftop.mode == DREG || leftop.mode == AREG)
5502               && (rightop.mode != DREG && rightop.mode != AREG)))
5503         {
5504           char *temp;
5505
5506           cc = swap_mri_condition (cc);
5507           temp = leftstart;
5508           leftstart = rightstart;
5509           rightstart = temp;
5510           temp = leftstop;
5511           leftstop = rightstop;
5512           rightstop = temp;
5513         }
5514     }
5515
5516   if (truelab == NULL)
5517     {
5518       cc = reverse_mri_condition (cc);
5519       truelab = falselab;
5520     }
5521       
5522   if (leftstart != NULL)
5523     {
5524       buf = (char *) xmalloc (20
5525                               + (leftstop - leftstart)
5526                               + (rightstop - rightstart));
5527       s = buf;
5528       *s++ = 'c';
5529       *s++ = 'm';
5530       *s++ = 'p';
5531       if (qual != '\0')
5532         *s++ = qual;
5533       *s++ = ' ';
5534       memcpy (s, leftstart, leftstop - leftstart);
5535       s += leftstop - leftstart;
5536       *s++ = ',';
5537       memcpy (s, rightstart, rightstop - rightstart);
5538       s += rightstop - rightstart;
5539       *s = '\0';
5540       md_assemble (buf);
5541       free (buf);
5542     }
5543       
5544   buf = (char *) xmalloc (20 + strlen (truelab));
5545   s = buf;
5546   *s++ = 'b';
5547   *s++ = cc >> 8;
5548   *s++ = cc & 0xff;
5549   if (extent != '\0')
5550     *s++ = extent;
5551   *s++ = ' ';
5552   strcpy (s, truelab);
5553   md_assemble (buf);
5554   free (buf);
5555 }
5556
5557 /* Parse an MRI structured control expression.  This generates test
5558    and branch instructions.  STOP is where the expression ends.  It
5559    goes to TRUELAB if the condition is true, and to FALSELAB if the
5560    condition is false.  Exactly one of TRUELAB and FALSELAB will be
5561    NULL, meaning to fall through.  QUAL is the size qualifier for the
5562    expression.  EXTENT is the size to use for the branch.  */
5563
5564 static void
5565 parse_mri_control_expression (stop, qual, truelab, falselab, extent)
5566      char *stop;
5567      int qual;
5568      const char *truelab;
5569      const char *falselab;
5570      int extent;
5571 {
5572   int c;
5573   int cc;
5574   char *leftstart;
5575   char *leftstop;
5576   char *rightstart;
5577   char *rightstop;
5578
5579   c = *stop;
5580   *stop = '\0';
5581
5582   if (! parse_mri_control_operand (&cc, &leftstart, &leftstop,
5583                                    &rightstart, &rightstop))
5584     {
5585       *stop = c;
5586       return;
5587     }
5588
5589   if (strncasecmp (input_line_pointer, "AND", 3) == 0)
5590     {
5591       const char *flab;
5592
5593       if (falselab != NULL)
5594         flab = falselab;
5595       else
5596         flab = mri_control_label ();
5597
5598       build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5599                                  rightstop, (const char *) NULL, flab, extent);
5600
5601       input_line_pointer += 3;
5602       if (*input_line_pointer != '.'
5603           || input_line_pointer[1] == '\0')
5604         qual = '\0';
5605       else
5606         {
5607           qual = input_line_pointer[1];
5608           input_line_pointer += 2;
5609         }
5610
5611       if (! parse_mri_control_operand (&cc, &leftstart, &leftstop,
5612                                        &rightstart, &rightstop))
5613         {
5614           *stop = c;
5615           return;
5616         }
5617
5618       build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5619                                  rightstop, truelab, falselab, extent);
5620
5621       if (falselab == NULL)
5622         colon (flab);
5623     }
5624   else if (strncasecmp (input_line_pointer, "OR", 2) == 0)
5625     {
5626       const char *tlab;
5627
5628       if (truelab != NULL)
5629         tlab = truelab;
5630       else
5631         tlab = mri_control_label ();
5632
5633       build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5634                                  rightstop, tlab, (const char *) NULL, extent);
5635
5636       input_line_pointer += 2;
5637       if (*input_line_pointer != '.'
5638           || input_line_pointer[1] == '\0')
5639         qual = '\0';
5640       else
5641         {
5642           qual = input_line_pointer[1];
5643           input_line_pointer += 2;
5644         }
5645
5646       if (! parse_mri_control_operand (&cc, &leftstart, &leftstop,
5647                                        &rightstart, &rightstop))
5648         {
5649           *stop = c;
5650           return;
5651         }
5652
5653       build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5654                                  rightstop, truelab, falselab, extent);
5655
5656       if (truelab == NULL)
5657         colon (tlab);
5658     }
5659   else
5660     {
5661       build_mri_control_operand (qual, cc, leftstart, leftstop, rightstart,
5662                                  rightstop, truelab, falselab, extent);
5663     }
5664
5665   *stop = c;
5666   if (input_line_pointer != stop)
5667     as_bad ("syntax error in structured control directive");
5668 }
5669
5670 /* Handle the MRI IF pseudo-op.  This may be a structured control
5671    directive, or it may be a regular assembler conditional, depending
5672    on its operands.  */
5673
5674 static void
5675 s_mri_if (qual)
5676      int qual;
5677 {
5678   char *s;
5679   int c;
5680   struct mri_control_info *n;
5681
5682   /* A structured control directive must end with THEN with an
5683      optional qualifier.  */
5684   s = input_line_pointer;
5685   while (! is_end_of_line[(unsigned char) *s]
5686          && (! flag_mri || *s != '*'))
5687     ++s;
5688   --s;
5689   while (s > input_line_pointer && (*s == ' ' || *s == '\t'))
5690     --s;
5691
5692   if (s - input_line_pointer > 1
5693       && s[-1] == '.')
5694     s -= 2;
5695
5696   if (s - input_line_pointer < 3
5697       || strncasecmp (s - 3, "THEN", 4) != 0)
5698     {
5699       if (qual != '\0')
5700         {
5701           as_bad ("missing then");
5702           ignore_rest_of_line ();
5703           return;
5704         }
5705
5706       /* It's a conditional.  */
5707       s_if (O_ne);
5708       return;
5709     }
5710
5711   /* Since this might be a conditional if, this pseudo-op will be
5712      called even if we are supported to be ignoring input.  Double
5713      check now.  Clobber *input_line_pointer so that ignore_input
5714      thinks that this is not a special pseudo-op.  */
5715   c = *input_line_pointer;
5716   *input_line_pointer = 0;
5717   if (ignore_input ())
5718     {
5719       *input_line_pointer = c;
5720       while (! is_end_of_line[(unsigned char) *input_line_pointer])
5721         ++input_line_pointer;
5722       demand_empty_rest_of_line ();
5723       return;
5724     }
5725   *input_line_pointer = c;
5726
5727   n = push_mri_control (mri_if);
5728
5729   parse_mri_control_expression (s - 3, qual, (const char *) NULL,
5730                                 n->next, s[1] == '.' ? s[2] : '\0');
5731
5732   if (s[1] == '.')
5733     input_line_pointer = s + 3;
5734   else
5735     input_line_pointer = s + 1;
5736
5737   if (flag_mri)
5738     {
5739       while (! is_end_of_line[(unsigned char) *input_line_pointer])
5740         ++input_line_pointer;
5741     }
5742
5743   demand_empty_rest_of_line ();
5744 }
5745
5746 /* Handle the MRI else pseudo-op.  If we are currently doing an MRI
5747    structured IF, associate the ELSE with the IF.  Otherwise, assume
5748    it is a conditional else.  */
5749
5750 static void
5751 s_mri_else (qual)
5752      int qual;
5753 {
5754   int c;
5755   char *buf;
5756   char q[2];
5757
5758   if (qual == '\0'
5759       && (mri_control_stack == NULL
5760           || mri_control_stack->type != mri_if
5761           || mri_control_stack->else_seen))
5762     {
5763       s_else (0);
5764       return;
5765     }
5766
5767   c = *input_line_pointer;
5768   *input_line_pointer = 0;
5769   if (ignore_input ())
5770     {
5771       *input_line_pointer = c;
5772       while (! is_end_of_line[(unsigned char) *input_line_pointer])
5773         ++input_line_pointer;
5774       demand_empty_rest_of_line ();
5775       return;
5776     }
5777   *input_line_pointer = c;
5778
5779   if (mri_control_stack == NULL
5780       || mri_control_stack->type != mri_if
5781       || mri_control_stack->else_seen)
5782     {
5783       as_bad ("else without matching if");
5784       ignore_rest_of_line ();
5785       return;
5786     }
5787
5788   mri_control_stack->else_seen = 1;
5789
5790   buf = (char *) xmalloc (20 + strlen (mri_control_stack->bottom));
5791   q[0] = qual;
5792   q[1] = '\0';
5793   sprintf (buf, "bra%s %s", q, mri_control_stack->bottom);
5794   md_assemble (buf);
5795   free (buf);
5796
5797   colon (mri_control_stack->next);
5798
5799   if (flag_mri)
5800     {
5801       while (! is_end_of_line[(unsigned char) *input_line_pointer])
5802         ++input_line_pointer;
5803     }
5804
5805   demand_empty_rest_of_line ();
5806 }
5807
5808 /* Handle the MRI ENDI pseudo-op.  */
5809
5810 static void
5811 s_mri_endi (ignore)
5812      int ignore;
5813 {
5814   if (mri_control_stack == NULL
5815       || mri_control_stack->type != mri_if)
5816     {
5817       as_bad ("endi without matching if");
5818       ignore_rest_of_line ();
5819       return;
5820     }
5821
5822   /* ignore_input will not return true for ENDI, so we don't need to
5823      worry about checking it again here.  */
5824
5825   if (! mri_control_stack->else_seen)
5826     colon (mri_control_stack->next);
5827   colon (mri_control_stack->bottom);
5828
5829   pop_mri_control ();
5830
5831   if (flag_mri)
5832     {
5833       while (! is_end_of_line[(unsigned char) *input_line_pointer])
5834         ++input_line_pointer;
5835     }
5836
5837   demand_empty_rest_of_line ();
5838 }
5839
5840 /* Handle the MRI BREAK pseudo-op.  */
5841
5842 static void
5843 s_mri_break (extent)
5844      int extent;
5845 {
5846   struct mri_control_info *n;
5847   char *buf;
5848   char ex[2];
5849
5850   n = mri_control_stack;
5851   while (n != NULL
5852          && n->type != mri_for
5853          && n->type != mri_repeat
5854          && n->type != mri_while)
5855     n = n->outer;
5856   if (n == NULL)
5857     {
5858       as_bad ("break outside of structured loop");
5859       ignore_rest_of_line ();
5860       return;
5861     }
5862
5863   buf = (char *) xmalloc (20 + strlen (n->bottom));
5864   ex[0] = extent;
5865   ex[1] = '\0';
5866   sprintf (buf, "bra%s %s", ex, n->bottom);
5867   md_assemble (buf);
5868   free (buf);
5869
5870   if (flag_mri)
5871     {
5872       while (! is_end_of_line[(unsigned char) *input_line_pointer])
5873         ++input_line_pointer;
5874     }
5875
5876   demand_empty_rest_of_line ();
5877 }
5878
5879 /* Handle the MRI NEXT pseudo-op.  */
5880
5881 static void
5882 s_mri_next (extent)
5883      int extent;
5884 {
5885   struct mri_control_info *n;
5886   char *buf;
5887   char ex[2];
5888
5889   n = mri_control_stack;
5890   while (n != NULL
5891          && n->type != mri_for
5892          && n->type != mri_repeat
5893          && n->type != mri_while)
5894     n = n->outer;
5895   if (n == NULL)
5896     {
5897       as_bad ("next outside of structured loop");
5898       ignore_rest_of_line ();
5899       return;
5900     }
5901
5902   buf = (char *) xmalloc (20 + strlen (n->next));
5903   ex[0] = extent;
5904   ex[1] = '\0';
5905   sprintf (buf, "bra%s %s", ex, n->next);
5906   md_assemble (buf);
5907   free (buf);
5908
5909   if (flag_mri)
5910     {
5911       while (! is_end_of_line[(unsigned char) *input_line_pointer])
5912         ++input_line_pointer;
5913     }
5914
5915   demand_empty_rest_of_line ();
5916 }
5917
5918 /* Handle the MRI FOR pseudo-op.  */
5919
5920 static void
5921 s_mri_for (qual)
5922      int qual;
5923 {
5924   const char *varstart, *varstop;
5925   const char *initstart, *initstop;
5926   const char *endstart, *endstop;
5927   const char *bystart, *bystop;
5928   int up;
5929   int by;
5930   int extent;
5931   struct mri_control_info *n;
5932   char *buf;
5933   char *s;
5934   char ex[2];
5935
5936   /* The syntax is
5937        FOR.q var = init { TO | DOWNTO } end [ BY by ] DO.e
5938      */
5939
5940   SKIP_WHITESPACE ();
5941   varstart = input_line_pointer;
5942
5943   /* Look for the '='.  */
5944   while (! is_end_of_line[(unsigned char) *input_line_pointer]
5945          && *input_line_pointer != '=')
5946     ++input_line_pointer;
5947   if (*input_line_pointer != '=')
5948     {
5949       as_bad ("missing =");
5950       ignore_rest_of_line ();
5951       return;
5952     }
5953
5954   varstop = input_line_pointer;
5955   if (varstop > varstart
5956       && (varstop[-1] == ' ' || varstop[-1] == '\t'))
5957     --varstop;
5958
5959   ++input_line_pointer;
5960
5961   initstart = input_line_pointer;
5962
5963   /* Look for TO or DOWNTO.  */
5964   up = 1;
5965   initstop = NULL;
5966   while (! is_end_of_line[(unsigned char) *input_line_pointer])
5967     {
5968       if (strncasecmp (input_line_pointer, "TO", 2) == 0
5969           && ! is_part_of_name (input_line_pointer[2]))
5970         {
5971           initstop = input_line_pointer;
5972           input_line_pointer += 2;
5973           break;
5974         }
5975       if (strncasecmp (input_line_pointer, "DOWNTO", 6) == 0
5976           && ! is_part_of_name (input_line_pointer[6]))
5977         {
5978           initstop = input_line_pointer;
5979           up = 0;
5980           input_line_pointer += 6;
5981           break;
5982         }
5983       ++input_line_pointer;
5984     }
5985   if (initstop == NULL)
5986     {
5987       as_bad ("missing to or downto");
5988       ignore_rest_of_line ();
5989       return;
5990     }
5991   if (initstop > initstart
5992       && (initstop[-1] == ' ' || initstop[-1] == '\t'))
5993     --initstop;
5994
5995   SKIP_WHITESPACE ();
5996   endstart = input_line_pointer;
5997
5998   /* Look for BY or DO.  */
5999   by = 0;
6000   endstop = NULL;
6001   while (! is_end_of_line[(unsigned char) *input_line_pointer])
6002     {
6003       if (strncasecmp (input_line_pointer, "BY", 2) == 0
6004           && ! is_part_of_name (input_line_pointer[2]))
6005         {
6006           endstop = input_line_pointer;
6007           by = 1;
6008           input_line_pointer += 2;
6009           break;
6010         }
6011       if (strncasecmp (input_line_pointer, "DO", 2) == 0
6012           && (input_line_pointer[2] == '.'
6013               || ! is_part_of_name (input_line_pointer[2])))
6014         {
6015           endstop = input_line_pointer;
6016           input_line_pointer += 2;
6017           break;
6018         }
6019       ++input_line_pointer;
6020     }
6021   if (endstop == NULL)
6022     {
6023       as_bad ("missing do");
6024       ignore_rest_of_line ();
6025       return;
6026     }
6027   if (endstop > endstart
6028       && (endstop[-1] == ' ' || endstop[-1] == '\t'))
6029     --endstop;
6030
6031   if (! by)
6032     {
6033       bystart = "#1";
6034       bystop = bystart + 2;
6035     }
6036   else
6037     {
6038       SKIP_WHITESPACE ();
6039       bystart = input_line_pointer;
6040
6041       /* Look for DO.  */
6042       bystop = NULL;
6043       while (! is_end_of_line[(unsigned char) *input_line_pointer])
6044         {
6045           if (strncasecmp (input_line_pointer, "DO", 2) == 0
6046               && (input_line_pointer[2] == '.'
6047                   || ! is_part_of_name (input_line_pointer[2])))
6048             {
6049               bystop = input_line_pointer;
6050               input_line_pointer += 2;
6051               break;
6052             }
6053           ++input_line_pointer;
6054         }
6055       if (bystop == NULL)
6056         {
6057           as_bad ("missing do");
6058           ignore_rest_of_line ();
6059           return;
6060         }
6061       if (bystop > bystart
6062           && (bystop[-1] == ' ' || bystop[-1] == '\t'))
6063         --bystop;
6064     }
6065
6066   if (*input_line_pointer != '.')
6067     extent = '\0';
6068   else
6069     {
6070       extent = input_line_pointer[1];
6071       input_line_pointer += 2;
6072     }
6073
6074   /* We have fully parsed the FOR operands.  Now build the loop.  */
6075
6076   n = push_mri_control (mri_for);
6077
6078   buf = (char *) xmalloc (50 + (input_line_pointer - varstart));
6079
6080   /* move init,var */
6081   s = buf;
6082   *s++ = 'm';
6083   *s++ = 'o';
6084   *s++ = 'v';
6085   *s++ = 'e';
6086   if (qual != '\0')
6087     *s++ = qual;
6088   *s++ = ' ';
6089   memcpy (s, initstart, initstop - initstart);
6090   s += initstop - initstart;
6091   *s++ = ',';
6092   memcpy (s, varstart, varstop - varstart);
6093   s += varstop - varstart;
6094   *s = '\0';
6095   md_assemble (buf);
6096
6097   colon (n->top);
6098
6099   /* cmp end,var */
6100   s = buf;
6101   *s++ = 'c';
6102   *s++ = 'm';
6103   *s++ = 'p';
6104   if (qual != '\0')
6105     *s++ = qual;
6106   *s++ = ' ';
6107   memcpy (s, endstart, endstop - endstart);
6108   s += endstop - endstart;
6109   *s++ = ',';
6110   memcpy (s, varstart, varstop - varstart);
6111   s += varstop - varstart;
6112   *s = '\0';
6113   md_assemble (buf);
6114
6115   /* bcc bottom */
6116   ex[0] = extent;
6117   ex[1] = '\0';
6118   if (up)
6119     sprintf (buf, "blt%s %s", ex, n->bottom);
6120   else
6121     sprintf (buf, "bgt%s %s", ex, n->bottom);
6122   md_assemble (buf);
6123
6124   /* Put together the add or sub instruction used by ENDF.  */
6125   s = buf;
6126   if (up)
6127     strcpy (s, "add");
6128   else
6129     strcpy (s, "sub");
6130   s += 3;
6131   if (qual != '\0')
6132     *s++ = qual;
6133   *s++ = ' ';
6134   memcpy (s, bystart, bystop - bystart);
6135   s += bystop - bystart;
6136   *s++ = ',';
6137   memcpy (s, varstart, varstop - varstart);
6138   s += varstop - varstart;
6139   *s = '\0';
6140   n->incr = buf;
6141
6142   if (flag_mri)
6143     {
6144       while (! is_end_of_line[(unsigned char) *input_line_pointer])
6145         ++input_line_pointer;
6146     }
6147
6148   demand_empty_rest_of_line ();
6149 }
6150
6151 /* Handle the MRI ENDF pseudo-op.  */
6152
6153 static void
6154 s_mri_endf (ignore)
6155      int ignore;
6156 {
6157   if (mri_control_stack == NULL
6158       || mri_control_stack->type != mri_for)
6159     {
6160       as_bad ("endf without for");
6161       ignore_rest_of_line ();
6162       return;
6163     }
6164
6165   colon (mri_control_stack->next);
6166
6167   md_assemble (mri_control_stack->incr);
6168
6169   sprintf (mri_control_stack->incr, "bra %s", mri_control_stack->top);
6170   md_assemble (mri_control_stack->incr);
6171
6172   free (mri_control_stack->incr);
6173
6174   colon (mri_control_stack->bottom);
6175
6176   pop_mri_control ();
6177
6178   if (flag_mri)
6179     {
6180       while (! is_end_of_line[(unsigned char) *input_line_pointer])
6181         ++input_line_pointer;
6182     }
6183
6184   demand_empty_rest_of_line ();
6185 }
6186
6187 /* Handle the MRI REPEAT pseudo-op.  */
6188
6189 static void
6190 s_mri_repeat (ignore)
6191      int ignore;
6192 {
6193   struct mri_control_info *n;
6194
6195   n = push_mri_control (mri_repeat);
6196   colon (n->top);
6197   if (flag_mri)
6198     {
6199       while (! is_end_of_line[(unsigned char) *input_line_pointer])
6200         ++input_line_pointer;
6201     }
6202   demand_empty_rest_of_line ();
6203 }
6204
6205 /* Handle the MRI UNTIL pseudo-op.  */
6206
6207 static void
6208 s_mri_until (qual)
6209      int qual;
6210 {
6211   char *s;
6212
6213   if (mri_control_stack == NULL
6214       || mri_control_stack->type != mri_repeat)
6215     {
6216       as_bad ("until without repeat");
6217       ignore_rest_of_line ();
6218       return;
6219     }
6220
6221   colon (mri_control_stack->next);
6222
6223   for (s = input_line_pointer; ! is_end_of_line[(unsigned char) *s]; s++)
6224     ;
6225
6226   parse_mri_control_expression (s, qual, (const char *) NULL,
6227                                 mri_control_stack->top, '\0');
6228
6229   colon (mri_control_stack->bottom);
6230
6231   input_line_pointer = s;
6232
6233   pop_mri_control ();
6234
6235   if (flag_mri)
6236     {
6237       while (! is_end_of_line[(unsigned char) *input_line_pointer])
6238         ++input_line_pointer;
6239     }
6240
6241   demand_empty_rest_of_line ();
6242 }
6243
6244 /* Handle the MRI WHILE pseudo-op.  */
6245
6246 static void
6247 s_mri_while (qual)
6248      int qual;
6249 {
6250   char *s;
6251
6252   struct mri_control_info *n;
6253
6254   s = input_line_pointer;
6255   while (! is_end_of_line[(unsigned char) *s]
6256          && (! flag_mri || *s != '*'))
6257     s++;
6258   --s;
6259   while (*s == ' ' || *s == '\t')
6260     --s;
6261   if (s - input_line_pointer > 1
6262       && s[-1] == '.')
6263     s -= 2;
6264   if (s - input_line_pointer < 2
6265       || strncasecmp (s - 1, "DO", 2) != 0)
6266     {
6267       as_bad ("missing do");
6268       ignore_rest_of_line ();
6269       return;
6270     }
6271
6272   n = push_mri_control (mri_while);
6273
6274   colon (n->next);
6275
6276   parse_mri_control_expression (s - 1, qual, (const char *) NULL, n->bottom,
6277                                 s[1] == '.' ? s[2] : '\0');
6278
6279   input_line_pointer = s + 1;
6280   if (*input_line_pointer == '.')
6281     input_line_pointer += 2;
6282
6283   if (flag_mri)
6284     {
6285       while (! is_end_of_line[(unsigned char) *input_line_pointer])
6286         ++input_line_pointer;
6287     }
6288
6289   demand_empty_rest_of_line ();
6290 }
6291
6292 /* Handle the MRI ENDW pseudo-op.  */
6293
6294 static void
6295 s_mri_endw (ignore)
6296      int ignore;
6297 {
6298   char *buf;
6299
6300   if (mri_control_stack == NULL
6301       || mri_control_stack->type != mri_while)
6302     {
6303       as_bad ("endw without while");
6304       ignore_rest_of_line ();
6305       return;
6306     }
6307
6308   buf = (char *) xmalloc (20 + strlen (mri_control_stack->next));
6309   sprintf (buf, "bra %s", mri_control_stack->next);
6310   md_assemble (buf);
6311   free (buf);
6312
6313   colon (mri_control_stack->bottom);
6314
6315   pop_mri_control ();
6316
6317   if (flag_mri)
6318     {
6319       while (! is_end_of_line[(unsigned char) *input_line_pointer])
6320         ++input_line_pointer;
6321     }
6322
6323   demand_empty_rest_of_line ();
6324 }
6325 \f
6326 /*
6327  * md_parse_option
6328  *      Invocation line includes a switch not recognized by the base assembler.
6329  *      See if it's a processor-specific option.  These are:
6330  *
6331  *      -[A]m[c]68000, -[A]m[c]68008, -[A]m[c]68010, -[A]m[c]68020, -[A]m[c]68030, -[A]m[c]68040
6332  *      -[A]m[c]68881, -[A]m[c]68882, -[A]m[c]68851
6333  *              Select the architecture.  Instructions or features not
6334  *              supported by the selected architecture cause fatal
6335  *              errors.  More than one may be specified.  The default is
6336  *              -m68020 -m68851 -m68881.  Note that -m68008 is a synonym
6337  *              for -m68000, and -m68882 is a synonym for -m68881.
6338  *      -[A]m[c]no-68851, -[A]m[c]no-68881
6339  *              Don't accept 688?1 instructions.  (The "c" is kind of silly,
6340  *              so don't use or document it, but that's the way the parsing
6341  *              works).
6342  *
6343  *      -pic    Indicates PIC.
6344  *      -k      Indicates PIC.  (Sun 3 only.)
6345  *
6346  *      --bitwise-or
6347  *              Permit `|' to be used in expressions.
6348  *
6349  */
6350
6351 #ifdef OBJ_ELF
6352 CONST char *md_shortopts = "lSA:m:kQ:V";
6353 #else
6354 CONST char *md_shortopts = "lSA:m:k";
6355 #endif
6356
6357 struct option md_longopts[] = {
6358 #define OPTION_PIC (OPTION_MD_BASE)
6359   {"pic", no_argument, NULL, OPTION_PIC},
6360 #define OPTION_REGISTER_PREFIX_OPTIONAL (OPTION_MD_BASE + 1)
6361   {"register-prefix-optional", no_argument, NULL,
6362      OPTION_REGISTER_PREFIX_OPTIONAL},
6363 #define OPTION_BITWISE_OR (OPTION_MD_BASE + 2)
6364   {"bitwise-or", no_argument, NULL, OPTION_BITWISE_OR},
6365 #define OPTION_BASE_SIZE_DEFAULT_16 (OPTION_MD_BASE + 3)
6366   {"base-size-default-16", no_argument, NULL, OPTION_BASE_SIZE_DEFAULT_16},
6367 #define OPTION_BASE_SIZE_DEFAULT_32 (OPTION_MD_BASE + 4)
6368   {"base-size-default-32", no_argument, NULL, OPTION_BASE_SIZE_DEFAULT_32},
6369   {NULL, no_argument, NULL, 0}
6370 };
6371 size_t md_longopts_size = sizeof(md_longopts);
6372
6373 int
6374 md_parse_option (c, arg)
6375      int c;
6376      char *arg;
6377 {
6378   switch (c)
6379     {
6380     case 'l':                   /* -l means keep external to 2 bit offset
6381                                    rather than 16 bit one */
6382       flag_short_refs = 1;
6383       break;
6384
6385     case 'S':                   /* -S means that jbsr's always turn into
6386                                    jsr's.  */
6387       flag_long_jumps = 1;
6388       break;
6389
6390     case 'A':
6391       if (*arg == 'm')
6392         arg++;
6393       /* intentional fall-through */
6394     case 'm':
6395
6396       if (arg[0] == 'n' && arg[1] == 'o' && arg[2] == '-')
6397         {
6398           int i;
6399           unsigned long arch;
6400           const char *oarg = arg;
6401
6402           arg += 3;
6403           if (*arg == 'm')
6404             {
6405               arg++;
6406               if (arg[0] == 'c' && arg[1] == '6')
6407                 arg++;
6408             }
6409           for (i = 0; i < n_archs; i++)
6410             if (!strcmp (arg, archs[i].name))
6411               break;
6412           if (i == n_archs)
6413             {
6414             unknown:
6415               as_bad ("unrecognized option `%s'", oarg);
6416               return 0;
6417             }
6418           arch = archs[i].arch;
6419           if (arch == m68881)
6420             no_68881 = 1;
6421           else if (arch == m68851)
6422             no_68851 = 1;
6423           else
6424             goto unknown;
6425         }
6426       else
6427         {
6428           int i;
6429
6430           if (arg[0] == 'c' && arg[1] == '6')
6431             arg++;
6432
6433           for (i = 0; i < n_archs; i++)
6434             if (!strcmp (arg, archs[i].name))
6435               {
6436                 unsigned long arch = archs[i].arch;
6437                 if (cpu_of_arch (arch))
6438                   /* It's a cpu spec.  */
6439                   {
6440                     current_architecture &= ~m68000up;
6441                     current_architecture |= arch;
6442                   }
6443                 else if (arch == m68881)
6444                   {
6445                     current_architecture |= m68881;
6446                     no_68881 = 0;
6447                   }
6448                 else if (arch == m68851)
6449                   {
6450                     current_architecture |= m68851;
6451                     no_68851 = 0;
6452                   }
6453                 else
6454                   /* ??? */
6455                   abort ();
6456                 break;
6457               }
6458           if (i == n_archs)
6459             {
6460               as_bad ("unrecognized architecture specification `%s'", arg);
6461               return 0;
6462             }
6463         }
6464       break;
6465
6466     case OPTION_PIC:
6467     case 'k':
6468       flag_want_pic = 1;
6469       break;                    /* -pic, Position Independent Code */
6470
6471     case OPTION_REGISTER_PREFIX_OPTIONAL:
6472       flag_reg_prefix_optional = 1;
6473       reg_prefix_optional_seen = 1;
6474       break;
6475
6476       /* -V: SVR4 argument to print version ID.  */
6477     case 'V':
6478       print_version_id ();
6479       break;
6480
6481       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
6482          should be emitted or not.  FIXME: Not implemented.  */
6483     case 'Q':
6484       break;
6485
6486     case OPTION_BITWISE_OR:
6487       {
6488         char *n, *t;
6489         const char *s;
6490
6491         n = (char *) xmalloc (strlen (m68k_comment_chars) + 1);
6492         t = n;
6493         for (s = m68k_comment_chars; *s != '\0'; s++)
6494           if (*s != '|')
6495             *t++ = *s;
6496         *t = '\0';
6497         m68k_comment_chars = n;
6498       }
6499       break;
6500
6501     case OPTION_BASE_SIZE_DEFAULT_16:
6502       m68k_index_width_default = SIZE_WORD;
6503       break;
6504
6505     case OPTION_BASE_SIZE_DEFAULT_32:
6506       m68k_index_width_default = SIZE_LONG;
6507       break;
6508
6509     default:
6510       return 0;
6511     }
6512
6513   return 1;
6514 }
6515
6516 void
6517 md_show_usage (stream)
6518      FILE *stream;
6519 {
6520   fprintf(stream, "\
6521 680X0 options:\n\
6522 -l                      use 1 word for refs to undefined symbols [default 2]\n\
6523 -m68000 | -m68008 | -m68010 | -m68020 | -m68030 | -m68040 | -m68060\n\
6524  | -m68302 | -m68331 | -m68332 | -m68333 | -m68340 | -m68360\n\
6525  | -mcpu32 | -m5200\n\
6526                         specify variant of 680X0 architecture [default 68020]\n\
6527 -m68881 | -m68882 | -mno-68881 | -mno-68882\n\
6528                         target has/lacks floating-point coprocessor\n\
6529                         [default yes for 68020, 68030, and cpu32]\n");
6530   fprintf(stream, "\
6531 -m68851 | -mno-68851\n\
6532                         target has/lacks memory-management unit coprocessor\n\
6533                         [default yes for 68020 and up]\n\
6534 -pic, -k                generate position independent code\n\
6535 -S                      turn jbsr into jsr\n\
6536 --register-prefix-optional\n\
6537                         recognize register names without prefix character\n\
6538 --bitwise-or            do not treat `|' as a comment character\n");
6539   fprintf (stream, "\
6540 --base-size-default-16  base reg without size is 16 bits\n\
6541 --base-size-default-32  base reg without size is 32 bits (default)\n");
6542 }
6543 \f
6544 #ifdef TEST2
6545
6546 /* TEST2:  Test md_assemble() */
6547 /* Warning, this routine probably doesn't work anymore */
6548
6549 main ()
6550 {
6551   struct m68k_it the_ins;
6552   char buf[120];
6553   char *cp;
6554   int n;
6555
6556   m68k_ip_begin ();
6557   for (;;)
6558     {
6559       if (!gets (buf) || !*buf)
6560         break;
6561       if (buf[0] == '|' || buf[1] == '.')
6562         continue;
6563       for (cp = buf; *cp; cp++)
6564         if (*cp == '\t')
6565           *cp = ' ';
6566       if (is_label (buf))
6567         continue;
6568       memset (&the_ins, '\0', sizeof (the_ins));
6569       m68k_ip (&the_ins, buf);
6570       if (the_ins.error)
6571         {
6572           printf ("Error %s in %s\n", the_ins.error, buf);
6573         }
6574       else
6575         {
6576           printf ("Opcode(%d.%s): ", the_ins.numo, the_ins.args);
6577           for (n = 0; n < the_ins.numo; n++)
6578             printf (" 0x%x", the_ins.opcode[n] & 0xffff);
6579           printf ("    ");
6580           print_the_insn (&the_ins.opcode[0], stdout);
6581           (void) putchar ('\n');
6582         }
6583       for (n = 0; n < strlen (the_ins.args) / 2; n++)
6584         {
6585           if (the_ins.operands[n].error)
6586             {
6587               printf ("op%d Error %s in %s\n", n, the_ins.operands[n].error, buf);
6588               continue;
6589             }
6590           printf ("mode %d, reg %d, ", the_ins.operands[n].mode, the_ins.operands[n].reg);
6591           if (the_ins.operands[n].b_const)
6592             printf ("Constant: '%.*s', ", 1 + the_ins.operands[n].e_const - the_ins.operands[n].b_const, the_ins.operands[n].b_const);
6593           printf ("ireg %d, isiz %d, imul %d, ", the_ins.operands[n].ireg, the_ins.operands[n].isiz, the_ins.operands[n].imul);
6594           if (the_ins.operands[n].b_iadd)
6595             printf ("Iadd: '%.*s',", 1 + the_ins.operands[n].e_iadd - the_ins.operands[n].b_iadd, the_ins.operands[n].b_iadd);
6596           (void) putchar ('\n');
6597         }
6598     }
6599   m68k_ip_end ();
6600   return 0;
6601 }
6602
6603 is_label (str)
6604      char *str;
6605 {
6606   while (*str == ' ')
6607     str++;
6608   while (*str && *str != ' ')
6609     str++;
6610   if (str[-1] == ':' || str[1] == '=')
6611     return 1;
6612   return 0;
6613 }
6614
6615 #endif
6616
6617 /* Possible states for relaxation:
6618
6619    0 0  branch offset   byte    (bra, etc)
6620    0 1                  word
6621    0 2                  long
6622
6623    1 0  indexed offsets byte    a0@(32,d4:w:1) etc
6624    1 1                  word
6625    1 2                  long
6626
6627    2 0  two-offset index word-word a0@(32,d4)@(45) etc
6628    2 1                  word-long
6629    2 2                  long-word
6630    2 3                  long-long
6631
6632    */
6633
6634 /* We have no need to default values of symbols.  */
6635
6636 /* ARGSUSED */
6637 symbolS *
6638 md_undefined_symbol (name)
6639      char *name;
6640 {
6641   return 0;
6642 }
6643
6644 /* Round up a section size to the appropriate boundary.  */
6645 valueT
6646 md_section_align (segment, size)
6647      segT segment;
6648      valueT size;
6649 {
6650   return size;                  /* Byte alignment is fine */
6651 }
6652
6653 /* Exactly what point is a PC-relative offset relative TO?
6654    On the 68k, it is relative to the address of the first extension
6655    word.  The difference between the addresses of the offset and the
6656    first extension word is stored in fx_pcrel_adjust. */
6657 long
6658 md_pcrel_from (fixP)
6659      fixS *fixP;
6660 {
6661   int adjust;
6662
6663   /* Because fx_pcrel_adjust is a char, and may be unsigned, we store
6664      -1 as 64.  */
6665   adjust = fixP->fx_pcrel_adjust;
6666   if (adjust == 64)
6667     adjust = -1;
6668   return fixP->fx_where + fixP->fx_frag->fr_address - adjust;
6669 }
6670
6671 #ifndef BFD_ASSEMBLER
6672 /*ARGSUSED*/
6673 void
6674 tc_coff_symbol_emit_hook (ignore)
6675      symbolS *ignore;
6676 {
6677 }
6678
6679 int
6680 tc_coff_sizemachdep (frag)
6681      fragS *frag;
6682 {
6683   switch (frag->fr_subtype & 0x3)
6684     {
6685     case BYTE:
6686       return 1;
6687     case SHORT:
6688       return 2;
6689     case LONG:
6690       return 4;
6691     default:
6692       abort ();
6693       return 0;
6694     }
6695 }
6696 #endif
6697
6698 /* end of tc-m68k.c */