3832755e398c22798943b93ef1a8daadc7abbc8a
[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, 1995 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to the Free
18    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
21 #include <ctype.h>
22 #define  NO_RELOC 0
23 #include "as.h"
24 #include "obstack.h"
25
26 #include "opcode/m68k.h"
27 #include "m68k-parse.h"
28
29 /* This array holds the chars that always start a comment.  If the
30    pre-processor is disabled, these aren't very useful */
31 #ifdef OBJ_ELF
32 CONST char comment_chars[] = "|#";
33 #else
34 CONST char comment_chars[] = "|";
35 #endif
36
37 /* This array holds the chars that only start a comment at the beginning of
38    a line.  If the line seems to have the form '# 123 filename'
39    .line and .file directives will appear in the pre-processed output */
40 /* Note that input_file.c hand checks for '#' at the beginning of the
41    first line of the input file.  This is because the compiler outputs
42    #NO_APP at the beginning of its output. */
43 /* Also note that comments like this one will always work. */
44 CONST char line_comment_chars[] = "#";
45
46 CONST char line_separator_chars[] = "";
47
48 /* Chars that can be used to separate mant from exp in floating point nums */
49 CONST char EXP_CHARS[] = "eE";
50
51 /* Chars that mean this number is a floating point constant, as
52    in "0f12.456" or "0d1.2345e12".  */
53
54 CONST char FLT_CHARS[] = "rRsSfFdDxXeEpP";
55
56 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
57    changed in read.c .  Ideally it shouldn't have to know about it at all,
58    but nothing is ideal around here.  */
59
60 const int md_reloc_size = 8;    /* Size of relocation record */
61
62 /* Are we trying to generate PIC code?  If so, absolute references
63    ought to be made into linkage table references or pc-relative
64    references.  */
65 int flag_want_pic;
66
67 static int flag_short_refs;     /* -l option */
68 static int flag_long_jumps;     /* -S option */
69
70 #ifdef REGISTER_PREFIX_OPTIONAL
71 int flag_reg_prefix_optional = REGISTER_PREFIX_OPTIONAL;
72 #else
73 int flag_reg_prefix_optional;
74 #endif
75
76 /* The floating point coprocessor to use by default.  */
77 static enum m68k_register m68k_float_copnum = COP1;
78
79 /* If this is non-zero, then references to number(%pc) will be taken
80    to refer to number, rather than to %pc + number.  */
81 static int m68k_abspcadd;
82
83 /* If this is non-zero, then the quick forms of the move, add, and sub
84    instructions are used when possible.  */
85 static int m68k_quick = 1;
86
87 /* If this is non-zero, then if the size is not specified for a base
88    or outer displacement, the assembler assumes that the size should
89    be 32 bits.  */
90 static int m68k_rel32 = 1;
91
92 /* Its an arbitrary name:  This means I don't approve of it */
93 /* See flames below */
94 static struct obstack robyn;
95
96 #define TAB(x,y)        (((x)<<2)+(y))
97 #define TABTYPE(xy)     ((xy) >> 2)
98 #define BYTE            0
99 #define SHORT           1
100 #define LONG            2
101 #define SZ_UNDEF        3
102 #undef BRANCH
103 /* Case `g' except when BCC68000 is applicable.  */
104 #define ABRANCH         1
105 /* Coprocessor branches.  */
106 #define FBRANCH         2
107 /* Mode 7.2 -- program counter indirect with (16-bit) displacement,
108    supported on all cpus.  Widens to 32-bit absolute.  */
109 #define PCREL           3
110 /* For inserting an extra jmp instruction with long offset on 68000,
111    for expanding conditional branches.  (Not bsr or bra.)  Since the
112    68000 doesn't support 32-bit displacements for conditional
113    branches, we fake it by reversing the condition and branching
114    around a jmp with an absolute long operand.  */
115 #define BCC68000        4
116 /* For the DBcc "instructions".  If the displacement requires 32 bits,
117    the branch-around-a-jump game is played here too.  */
118 #define DBCC            5
119 /* Not currently used?  */
120 #define PCLEA           6
121 /* Mode AINDX (apc-relative) using PC, with variable target, might fit
122    in 16 or 8 bits.  */
123 #define PCINDEX         7
124
125 struct m68k_incant
126   {
127     const char *m_operands;
128     unsigned long m_opcode;
129     short m_opnum;
130     short m_codenum;
131     int m_arch;
132     struct m68k_incant *m_next;
133   };
134
135 #define getone(x)       ((((x)->m_opcode)>>16)&0xffff)
136 #define gettwo(x)       (((x)->m_opcode)&0xffff)
137
138 static const enum m68k_register m68000_control_regs[] = { 0 };
139 static const enum m68k_register m68010_control_regs[] = {
140   SFC, DFC, USP, VBR,
141   0
142 };
143 static const enum m68k_register m68020_control_regs[] = {
144   SFC, DFC, USP, VBR, CACR, CAAR, MSP, ISP,
145   0
146 };
147 static const enum m68k_register m68040_control_regs[] = {
148   SFC, DFC, CACR, TC, ITT0, ITT1, DTT0, DTT1,
149   USP, VBR, MSP, ISP, MMUSR, URP, SRP,
150   0
151 };
152 static const enum m68k_register m68060_control_regs[] = {
153   SFC, DFC, CACR, TC, ITT0, ITT1, DTT0, DTT1, BUSCR,
154   USP, VBR, URP, SRP, PCR,
155   0
156 };
157 #define cpu32_control_regs m68010_control_regs
158
159 static const enum m68k_register *control_regs;
160
161 /* internal form of a 68020 instruction */
162 struct m68k_it
163 {
164   const char *error;
165   const char *args;             /* list of opcode info */
166   int numargs;
167
168   int numo;                     /* Number of shorts in opcode */
169   short opcode[11];
170
171   struct m68k_op operands[6];
172
173   int nexp;                     /* number of exprs in use */
174   struct m68k_exp exprs[4];
175
176   int nfrag;                    /* Number of frags we have to produce */
177   struct
178     {
179       int fragoff;              /* Where in the current opcode the frag ends */
180       symbolS *fadd;
181       long foff;
182       int fragty;
183     }
184   fragb[4];
185
186   int nrel;                     /* Num of reloc strucs in use */
187   struct
188     {
189       int n;
190       expressionS exp;
191       char wid;
192       char pcrel;
193       /* In a pc relative address the difference between the address
194          of the offset and the address that the offset is relative
195          to.  This depends on the addressing mode.  Basically this
196          is the value to put in the offset field to address the
197          first byte of the offset, without regarding the special
198          significance of some values (in the branch instruction, for
199          example).  */
200       int pcrel_fix;
201     }
202   reloc[5];                     /* Five is enough??? */
203 };
204
205 #define cpu_of_arch(x)          ((x) & m68000up)
206 #define float_of_arch(x)        ((x) & mfloat)
207 #define mmu_of_arch(x)          ((x) & mmmu)
208
209 static struct m68k_it the_ins;  /* the instruction being assembled */
210
211 #define op(ex)          ((ex)->exp.X_op)
212 #define adds(ex)        ((ex)->exp.X_add_symbol)
213 #define subs(ex)        ((ex)->exp.X_op_symbol)
214 #define offs(ex)        ((ex)->exp.X_add_number)
215
216 /* Macros for adding things to the m68k_it struct */
217
218 #define addword(w)      the_ins.opcode[the_ins.numo++]=(w)
219
220 /* Like addword, but goes BEFORE general operands */
221 static void
222 insop (w, opcode)
223      int w;
224      struct m68k_incant *opcode;
225 {
226   int z;
227   for(z=the_ins.numo;z>opcode->m_codenum;--z)
228     the_ins.opcode[z]=the_ins.opcode[z-1];
229   for(z=0;z<the_ins.nrel;z++)
230     the_ins.reloc[z].n+=2;
231   for (z = 0; z < the_ins.nfrag; z++)
232     the_ins.fragb[z].fragoff++;
233   the_ins.opcode[opcode->m_codenum]=w;
234   the_ins.numo++;
235 }
236
237 /* The numo+1 kludge is so we can hit the low order byte of the prev word.
238    Blecch.  */
239 static void
240 add_fix (width, exp, pc_rel, pc_fix)
241      char width;
242      struct m68k_exp *exp;
243      int pc_rel;
244      int pc_fix;
245 {
246   the_ins.reloc[the_ins.nrel].n = (((width)=='B')
247                                    ? (the_ins.numo*2-1)
248                                    : (((width)=='b')
249                                       ? (the_ins.numo*2+1)
250                                       : (the_ins.numo*2)));
251   the_ins.reloc[the_ins.nrel].exp = exp->exp;
252   the_ins.reloc[the_ins.nrel].wid = width;
253   the_ins.reloc[the_ins.nrel].pcrel_fix = pc_fix;
254   the_ins.reloc[the_ins.nrel++].pcrel = pc_rel;
255 }
256
257 /* Cause an extra frag to be generated here, inserting up to 10 bytes
258    (that value is chosen in the frag_var call in md_assemble).  TYPE
259    is the subtype of the frag to be generated; its primary type is
260    rs_machine_dependent.
261
262    The TYPE parameter is also used by md_convert_frag_1 and
263    md_estimate_size_before_relax.  The appropriate type of fixup will
264    be emitted by md_convert_frag_1.
265
266    ADD becomes the FR_SYMBOL field of the frag, and OFF the FR_OFFSET.  */
267 static void
268 add_frag(add,off,type)
269      symbolS *add;
270      long off;
271      int type;
272 {
273   the_ins.fragb[the_ins.nfrag].fragoff=the_ins.numo;
274   the_ins.fragb[the_ins.nfrag].fadd=add;
275   the_ins.fragb[the_ins.nfrag].foff=off;
276   the_ins.fragb[the_ins.nfrag++].fragty=type;
277 }
278
279 #define isvar(ex) \
280   (op (ex) != O_constant && op (ex) != O_big)
281
282 static char *crack_operand PARAMS ((char *str, struct m68k_op *opP));
283 static int get_num PARAMS ((struct m68k_exp *exp, int ok));
284 static int reverse_16_bits PARAMS ((int in));
285 static int reverse_8_bits PARAMS ((int in));
286 static void install_gen_operand PARAMS ((int mode, int val));
287 static void install_operand PARAMS ((int mode, int val));
288 static void s_bss PARAMS ((int));
289 static void s_data1 PARAMS ((int));
290 static void s_data2 PARAMS ((int));
291 static void s_even PARAMS ((int));
292 static void s_proc PARAMS ((int));
293 static void mri_chip PARAMS ((void));
294 static void s_chip PARAMS ((int));
295 static void s_fopt PARAMS ((int));
296 static void s_opt PARAMS ((int));
297 static void s_reg PARAMS ((int));
298
299 static int current_architecture;
300
301 struct m68k_cpu {
302   unsigned long arch;
303   const char *name;
304 };
305
306 static const struct m68k_cpu archs[] = {
307   { m68000, "68000" },
308   { m68010, "68010" },
309   { m68020, "68020" },
310   { m68030, "68030" },
311   { m68040, "68040" },
312   { m68060, "68060" },
313   { cpu32,  "cpu32" },
314   { m68881, "68881" },
315   { m68851, "68851" },
316   /* Aliases (effectively, so far as gas is concerned) for the above
317      cpus.  */
318   { m68020, "68k" },
319   { m68000, "68302" },
320   { m68000, "68008" },
321   { m68000, "68ec000" },
322   { m68000, "68hc000" },
323   { m68000, "68hc001" },
324   { m68020, "68ec020" },
325   { m68030, "68ec030" },
326   { m68040, "68ec040" },
327   { cpu32,  "68330" },
328   { cpu32,  "68331" },
329   { cpu32,  "68332" },
330   { cpu32,  "68333" },
331   { cpu32,  "68340" },
332   { cpu32,  "68360" },
333   { m68881, "68882" },
334 };
335
336 static const int n_archs = sizeof (archs) / sizeof (archs[0]);
337
338 /* BCC68000 is for patching in an extra jmp instruction for long offsets
339    on the 68000.  The 68000 doesn't support long branches with branchs */
340
341 /* This table desribes how you change sizes for the various types of variable
342    size expressions.  This version only supports two kinds. */
343
344 /* Note that calls to frag_var need to specify the maximum expansion
345    needed; this is currently 10 bytes for DBCC.  */
346
347 /* The fields are:
348    How far Forward this mode will reach:
349    How far Backward this mode will reach:
350    How many bytes this mode will add to the size of the frag
351    Which mode to go to if the offset won't fit in this one
352    */
353 relax_typeS md_relax_table[] =
354 {
355   {1, 1, 0, 0},                 /* First entries aren't used */
356   {1, 1, 0, 0},                 /* For no good reason except */
357   {1, 1, 0, 0},                 /* that the VAX doesn't either */
358   {1, 1, 0, 0},
359
360   {(127), (-128), 0, TAB (ABRANCH, SHORT)},
361   {(32767), (-32768), 2, TAB (ABRANCH, LONG)},
362   {0, 0, 4, 0},
363   {1, 1, 0, 0},
364
365   {1, 1, 0, 0},                 /* FBRANCH doesn't come BYTE */
366   {(32767), (-32768), 2, TAB (FBRANCH, LONG)},
367   {0, 0, 4, 0},
368   {1, 1, 0, 0},
369
370   {1, 1, 0, 0},                 /* PCREL doesn't come BYTE */
371   {(32767), (-32768), 2, TAB (PCREL, LONG)},
372   {0, 0, 4, 0},
373   {1, 1, 0, 0},
374
375   {(127), (-128), 0, TAB (BCC68000, SHORT)},
376   {(32767), (-32768), 2, TAB (BCC68000, LONG)},
377   {0, 0, 6, 0},                 /* jmp long space */
378   {1, 1, 0, 0},
379
380   {1, 1, 0, 0},                 /* DBCC doesn't come BYTE */
381   {(32767), (-32768), 2, TAB (DBCC, LONG)},
382   {0, 0, 10, 0},                /* bra/jmp long space */
383   {1, 1, 0, 0},
384
385   {1, 1, 0, 0},                 /* PCLEA doesn't come BYTE */
386   {32767, -32768, 2, TAB (PCLEA, LONG)},
387   {0, 0, 6, 0},
388   {1, 1, 0, 0},
389
390   /* For, e.g., jmp pcrel indexed.  */
391   {125, -130, 0, TAB (PCINDEX, SHORT)},
392   {32765, -32770, 2, TAB (PCINDEX, LONG)},
393   {0, 0, 4, 0},
394   {1, 1, 0, 0},
395 };
396
397 /* These are the machine dependent pseudo-ops.  These are included so
398    the assembler can work on the output from the SUN C compiler, which
399    generates these.
400    */
401
402 /* This table describes all the machine specific pseudo-ops the assembler
403    has to support.  The fields are:
404    pseudo-op name without dot
405    function to call to execute this pseudo-op
406    Integer arg to pass to the function
407    */
408 CONST pseudo_typeS md_pseudo_table[] =
409 {
410   {"data1", s_data1, 0},
411   {"data2", s_data2, 0},
412   {"bss", s_bss, 0},
413   {"even", s_even, 0},
414   {"skip", s_space, 0},
415   {"proc", s_proc, 0},
416 #ifdef TE_SUN3
417   {"align", s_align_bytes, 0},
418 #endif
419 #ifdef OBJ_ELF
420   {"swbeg", s_ignore, 0},
421 #endif
422
423   /* The following pseudo-ops are supported for MRI compatibility.  */
424   {"chip", s_chip, 0},
425   {"comline", s_space, 1},
426   {"fopt", s_fopt, 0},
427   {"mask2", s_ignore, 0},
428   {"opt", s_opt, 0},
429   {"reg", s_reg, 0},
430
431   {0, 0, 0}
432 };
433
434
435 /* The mote pseudo ops are put into the opcode table, since they
436    don't start with a . they look like opcodes to gas.
437    */
438 extern void obj_coff_section ();
439
440 CONST pseudo_typeS mote_pseudo_table[] =
441 {
442
443   {"dcl", cons, 4},
444   {"dc", cons, 2},
445   {"dcw", cons, 2},
446   {"dcb", cons, 1},
447
448   {"dsl", s_space, 4},
449   {"ds", s_space, 2},
450   {"dsw", s_space, 2},
451   {"dsb", s_space, 1},
452
453   {"xdef", s_globl, 0},
454   {"align", s_align_ptwo, 0},
455 #ifdef M68KCOFF
456   {"sect", obj_coff_section, 0},
457   {"section", obj_coff_section, 0},
458 #endif
459   {0, 0, 0}
460 };
461
462 #define issbyte(x)      ((x)>=-128 && (x)<=127)
463 #define isubyte(x)      ((x)>=0 && (x)<=255)
464 #define issword(x)      ((x)>=-32768 && (x)<=32767)
465 #define isuword(x)      ((x)>=0 && (x)<=65535)
466
467 #define isbyte(x)       ((x)>= -255 && (x)<=255)
468 #define isword(x)       ((x)>=-32768 && (x)<=65535)
469 #define islong(x)       (1)
470
471 extern char *input_line_pointer;
472
473 static char mklower_table[256];
474 #define mklower(c) (mklower_table[(unsigned char)(c)])
475 static char notend_table[256];
476 static char alt_notend_table[256];
477 #define notend(s)                                               \
478   (! (notend_table[(unsigned char) *s]                          \
479       || (*s == ':'                                             \
480           && alt_notend_table[(unsigned char) s[1]])))
481
482 #if defined (M68KCOFF) && !defined (BFD_ASSEMBLER)
483
484 #ifdef NO_PCREL_RELOCS
485
486 int
487 make_pcrel_absolute(fixP, add_number)
488     fixS *fixP;
489     long *add_number;
490 {
491   register unsigned char *opcode = fixP->fx_frag->fr_opcode;
492
493   /* rewrite the PC relative instructions to absolute address ones.
494    * these are rumoured to be faster, and the apollo linker refuses
495    * to deal with the PC relative relocations.
496    */
497   if (opcode[0] == 0x60 && opcode[1] == 0xff) /* BRA -> JMP */
498     {
499       opcode[0] = 0x4e;
500       opcode[1] = 0xf9;
501     }
502   else if (opcode[0] == 0x61 && opcode[1] == 0xff) /* BSR -> JSR */
503     {
504       opcode[0] = 0x4e;
505       opcode[1] = 0xb9;
506     }
507   else
508     as_fatal ("Unknown PC relative instruction");
509   *add_number -= 4;
510   return 0;
511 }
512
513 #endif /* NO_PCREL_RELOCS */
514
515 short
516 tc_coff_fix2rtype (fixP)
517      fixS *fixP;
518 {
519 #ifdef NO_PCREL_RELOCS
520   know (fixP->fx_pcrel == 0);
521   return (fixP->fx_size == 1 ? R_RELBYTE
522           : fixP->fx_size == 2 ? R_DIR16
523           : R_DIR32);
524 #else
525   return (fixP->fx_pcrel ?
526           (fixP->fx_size == 1 ? R_PCRBYTE :
527            fixP->fx_size == 2 ? R_PCRWORD :
528            R_PCRLONG) :
529           (fixP->fx_size == 1 ? R_RELBYTE :
530            fixP->fx_size == 2 ? R_RELWORD :
531            R_RELLONG));
532 #endif
533 }
534
535 #endif
536
537 #ifdef BFD_ASSEMBLER
538
539 arelent *
540 tc_gen_reloc (section, fixp)
541      asection *section;
542      fixS *fixp;
543 {
544   arelent *reloc;
545   bfd_reloc_code_real_type code;
546
547 #define F(SZ,PCREL)             (((SZ) << 1) + (PCREL))
548   switch (F (fixp->fx_size, fixp->fx_pcrel))
549     {
550 #define MAP(SZ,PCREL,TYPE)      case F(SZ,PCREL): code = (TYPE); break
551       MAP (1, 0, BFD_RELOC_8);
552       MAP (2, 0, BFD_RELOC_16);
553       MAP (4, 0, BFD_RELOC_32);
554       MAP (1, 1, BFD_RELOC_8_PCREL);
555       MAP (2, 1, BFD_RELOC_16_PCREL);
556       MAP (4, 1, BFD_RELOC_32_PCREL);
557     default:
558       abort ();
559     }
560
561   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
562   assert (reloc != 0);
563   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
564   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
565   if (fixp->fx_pcrel)
566     reloc->addend = fixp->fx_addnumber;
567   else
568     reloc->addend = 0;
569
570   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
571   assert (reloc->howto != 0);
572
573   return reloc;
574 }
575
576 #endif /* BFD_ASSEMBLER */
577
578 /* Handle of the OPCODE hash table.  NULL means any use before
579    m68k_ip_begin() will crash.  */
580 static struct hash_control *op_hash;
581 \f
582 /* Assemble an m68k instruction.  */
583
584 void
585 m68k_ip (instring)
586      char *instring;
587 {
588   register char *p;
589   register struct m68k_op *opP;
590   register struct m68k_incant *opcode;
591   register const char *s;
592   register int tmpreg = 0, baseo = 0, outro = 0, nextword;
593   char *pdot, *pdotmove;
594   enum m68k_size siz1, siz2;
595   char c;
596   int losing;
597   int opsfound;
598   char *crack_operand ();
599   LITTLENUM_TYPE words[6];
600   LITTLENUM_TYPE *wordp;
601   unsigned long ok_arch = 0;
602
603   if (*instring == ' ')
604     instring++;                 /* skip leading whitespace */
605
606   /* Scan up to end of operation-code, which MUST end in end-of-string
607      or exactly 1 space. */
608   pdot = 0;
609   for (p = instring; *p != '\0'; p++)
610     {
611       if (*p == ' ')
612         break;
613       if (*p == '.')
614         pdot = p;
615     }
616
617   if (p == instring)
618     {
619       the_ins.error = "No operator";
620       return;
621     }
622
623   /* p now points to the end of the opcode name, probably whitespace.
624      Make sure the name is null terminated by clobbering the
625      whitespace, look it up in the hash table, then fix it back.
626      Remove a dot, first, since the opcode tables have none.  */
627   if (pdot != NULL)
628     {
629       for (pdotmove = pdot; pdotmove < p; pdotmove++)
630         *pdotmove = pdotmove[1];
631       p--;
632     }
633
634   c = *p;
635   *p = '\0';
636   opcode = (struct m68k_incant *) hash_find (op_hash, instring);
637   *p = c;
638
639   if (pdot != NULL)
640     {
641       for (pdotmove = p; pdotmove > pdot; pdotmove--)
642         *pdotmove = pdotmove[-1];
643       *pdot = '.';
644       ++p;
645     }
646
647   if (opcode == NULL)
648     {
649       the_ins.error = "Unknown operator";
650       return;
651     }
652
653   /* found a legitimate opcode, start matching operands */
654   while (*p == ' ')
655     ++p;
656
657   if (opcode->m_operands == 0)
658     {
659       char *old = input_line_pointer;
660       *old = '\n';
661       input_line_pointer = p;
662       /* Ahh - it's a motorola style psuedo op */
663       mote_pseudo_table[opcode->m_opnum].poc_handler
664         (mote_pseudo_table[opcode->m_opnum].poc_val);
665       input_line_pointer = old;
666       *old = 0;
667
668       return;
669     }
670
671   for (opP = &the_ins.operands[0]; *p; opP++)
672     {
673       p = crack_operand (p, opP);
674
675       if (opP->error)
676         {
677           the_ins.error = opP->error;
678           return;
679         }
680     }
681
682   opsfound = opP - &the_ins.operands[0];
683
684   /* This ugly hack is to support the floating pt opcodes in their
685      standard form.  Essentially, we fake a first enty of type COP#1 */
686   if (opcode->m_operands[0] == 'I')
687     {
688       int n;
689
690       for (n = opsfound; n > 0; --n)
691         the_ins.operands[n] = the_ins.operands[n - 1];
692
693       memset ((char *) (&the_ins.operands[0]), '\0',
694               sizeof (the_ins.operands[0]));
695       the_ins.operands[0].mode = CONTROL;
696       the_ins.operands[0].reg = m68k_float_copnum;
697       opsfound++;
698     }
699
700   /* We've got the operands.  Find an opcode that'll accept them */
701   for (losing = 0;;)
702     {
703       /* If we didn't get the right number of ops, or we have no
704          common model with this pattern then reject this pattern. */
705
706       if (opsfound != opcode->m_opnum
707           || ((opcode->m_arch & current_architecture) == 0))
708         {
709           ++losing;
710           ok_arch |= opcode->m_arch;
711         }
712       else
713         {
714           for (s = opcode->m_operands, opP = &the_ins.operands[0];
715                *s && !losing;
716                s += 2, opP++)
717             {
718               /* Warning: this switch is huge! */
719               /* I've tried to organize the cases into this order:
720                  non-alpha first, then alpha by letter.  Lower-case
721                  goes directly before uppercase counterpart.  */
722               /* Code with multiple case ...: gets sorted by the lowest
723                  case ... it belongs to.  I hope this makes sense.  */
724               switch (*s)
725                 {
726                 case '!':
727                   switch (opP->mode)
728                     {
729                     case IMMED:
730                     case DREG:
731                     case AREG:
732                     case FPREG:
733                     case CONTROL:
734                     case AINC:
735                     case ADEC:
736                     case REGLST:
737                       losing++;
738                       break;
739                     default:
740                       break;
741                     }
742                   break;
743
744                 case '`':
745                   switch (opP->mode)
746                     {
747                     case IMMED:
748                     case DREG:
749                     case AREG:
750                     case FPREG:
751                     case CONTROL:
752                     case AINC:
753                     case REGLST:
754                     case AINDR:
755                       losing++;
756                       break;
757                     default:
758                       break;
759                     }
760                   break;
761
762                 case '#':
763                   if (opP->mode != IMMED)
764                     losing++;
765                   else if (s[1] == 'b'
766                            && ! isvar (&opP->disp)
767                            && ! expr8 (&opP->disp))
768                     losing++;
769                   else if (s[1] == 'w'
770                            && ! isvar (&opP->disp)
771                            && ! expr16 (&opP->disp))
772                     losing++;
773                   break;
774
775                 case '^':
776                 case 'T':
777                   if (opP->mode != IMMED)
778                     losing++;
779                   break;
780
781                 case '$':
782                   if (opP->mode == AREG
783                       || opP->mode == CONTROL
784                       || opP->mode == FPREG
785                       || opP->mode == IMMED
786                       || opP->mode == REGLST
787                       || (opP->mode != ABSL
788                           && (opP->reg == PC
789                               || opP->reg == ZPC)))
790                     losing++;
791                   break;
792
793                 case '%':
794                   if (opP->mode == CONTROL
795                       || opP->mode == FPREG
796                       || opP->mode == REGLST
797                       || (opP->mode != ABSL
798                           && opP->mode != IMMED
799                           && (opP->reg == PC
800                               || opP->reg == ZPC)))
801                     losing++;
802                   break;
803
804                 case '&':
805                   switch (opP->mode)
806                     {
807                     case DREG:
808                     case AREG:
809                     case FPREG:
810                     case CONTROL:
811                     case IMMED:
812                     case AINC:
813                     case ADEC:
814                     case REGLST:
815                       losing++;
816                       break;
817                     case ABSL:
818                       break;
819                     default:
820                       if (opP->reg == PC
821                           || opP->reg == ZPC)
822                         losing++;
823                       break;
824                     }
825                   break;
826
827                 case '*':
828                   if (opP->mode == CONTROL
829                       || opP->mode == FPREG
830                       || opP->mode == REGLST)
831                     losing++;
832                   break;
833
834                 case '+':
835                   if (opP->mode != AINC)
836                     losing++;
837                   break;
838
839                 case '-':
840                   if (opP->mode != ADEC)
841                     losing++;
842                   break;
843
844                 case '/':
845                   switch (opP->mode)
846                     {
847                     case AREG:
848                     case CONTROL:
849                     case FPREG:
850                     case AINC:
851                     case ADEC:
852                     case IMMED:
853                     case REGLST:
854                       losing++;
855                       break;
856                     default:
857                       break;
858                     }
859                   break;
860
861                 case ';':
862                   switch (opP->mode)
863                     {
864                     case AREG:
865                     case CONTROL:
866                     case FPREG:
867                     case REGLST:
868                       losing++;
869                       break;
870                     default:
871                       break;
872                     }
873                   break;
874
875                 case '?':
876                   switch (opP->mode)
877                     {
878                     case AREG:
879                     case CONTROL:
880                     case FPREG:
881                     case AINC:
882                     case ADEC:
883                     case IMMED:
884                     case REGLST:
885                       losing++;
886                       break;
887                     case ABSL:
888                       break;
889                     default:
890                       if (opP->reg == PC || opP->reg == ZPC)
891                         losing++;
892                       break;
893                     }
894                   break;
895
896                 case '@':
897                   switch (opP->mode)
898                     {
899                     case AREG:
900                     case CONTROL:
901                     case FPREG:
902                     case IMMED:
903                     case REGLST:
904                       losing++;
905                       break;
906                     default:
907                       break;
908                     }
909                   break;
910
911                 case '~':       /* For now! (JF FOO is this right?) */
912                   switch (opP->mode)
913                     {
914                     case DREG:
915                     case AREG:
916                     case CONTROL:
917                     case FPREG:
918                     case IMMED:
919                     case REGLST:
920                       losing++;
921                       break;
922                     case ABSL:
923                       break;
924                     default:
925                       if (opP->reg == PC
926                           || opP->reg == ZPC)
927                         losing++;
928                       break;
929                     }
930                   break;
931
932                 case '3':
933                   if (opP->mode != CONTROL
934                       || (opP->reg != TT0 && opP->reg != TT1))
935                     losing++;
936                   break;
937
938                 case 'A':
939                   if (opP->mode != AREG)
940                     losing++;
941                   break;
942
943                 case 'a':
944                   if (opP->mode != AINDR)
945                     ++losing;
946                   break;
947
948                 case 'B':       /* FOO */
949                   if (opP->mode != ABSL
950                       || (flag_long_jumps
951                           && strncmp (instring, "jbsr", 4) == 0))
952                     losing++;
953                   break;
954
955                 case 'C':
956                   if (opP->mode != CONTROL || opP->reg != CCR)
957                     losing++;
958                   break;
959
960                 case 'd':
961                   if (opP->mode != DISP
962                       || opP->reg < ADDR0
963                       || opP->reg > ADDR7)
964                     losing++;
965                   break;
966
967                 case 'D':
968                   if (opP->mode != DREG)
969                     losing++;
970                   break;
971
972                 case 'F':
973                   if (opP->mode != FPREG)
974                     losing++;
975                   break;
976
977                 case 'I':
978                   if (opP->mode != CONTROL
979                       || opP->reg < COP0
980                       || opP->reg > COP7)
981                     losing++;
982                   break;
983
984                 case 'J':
985                   if (opP->mode != CONTROL
986                       || opP->reg < USP
987                       || opP->reg > last_movec_reg)
988                     losing++;
989                   else
990                     {
991                       const enum m68k_register *rp;
992                       for (rp = control_regs; *rp; rp++)
993                         if (*rp == opP->reg)
994                           break;
995                       if (*rp == 0)
996                         losing++;
997                     }
998                   break;
999
1000                 case 'k':
1001                   if (opP->mode != IMMED)
1002                     losing++;
1003                   break;
1004
1005                 case 'l':
1006                 case 'L':
1007                   if (opP->mode == DREG
1008                       || opP->mode == AREG
1009                       || opP->mode == FPREG)
1010                     {
1011                       if (s[1] == '8')
1012                         losing++;
1013                       else
1014                         {
1015                           switch (opP->mode)
1016                             {
1017                             case DREG:
1018                               opP->mask = 1 << (opP->reg - DATA0);
1019                               break;
1020                             case AREG:
1021                               opP->mask = 1 << (opP->reg - ADDR0 + 8);
1022                               break;
1023                             case FPREG:
1024                               opP->mask = 1 << (opP->reg - FP0 + 16);
1025                               break;
1026                             default:
1027                               abort ();
1028                             }
1029                           opP->mode = REGLST;
1030                         }
1031                     }
1032                   else if (opP->mode == CONTROL)
1033                     {
1034                       if (s[1] != '8')
1035                         losing++;
1036                       else
1037                         {
1038                           switch (opP->reg)
1039                             {
1040                             case FPI:
1041                               opP->mask = 1 << 24;
1042                               break;
1043                             case FPS:
1044                               opP->mask = 1 << 25;
1045                               break;
1046                             case FPC:
1047                               opP->mask = 1 << 26;
1048                               break;
1049                             default:
1050                               losing++;
1051                               break;
1052                             }
1053                           opP->mode = REGLST;
1054                         }
1055                     }
1056                   else if (opP->mode == ABSL
1057                            && opP->disp.size == SIZE_UNSPEC
1058                            && opP->disp.exp.X_op == O_constant)
1059                     {
1060                       /* This is what the MRI REG pseudo-op generates.  */
1061                       opP->mode = REGLST;
1062                       opP->mask = opP->disp.exp.X_add_number;
1063                     }
1064                   else if (opP->mode != REGLST)
1065                     losing++;
1066                   else if (s[1] == '8' && (opP->mask & 0x0ffffff) != 0)
1067                     losing++;
1068                   else if (s[1] == '3' && (opP->mask & 0x7000000) != 0)
1069                     losing++;
1070                   break;
1071
1072                 case 'M':
1073                   if (opP->mode != IMMED)
1074                     losing++;
1075                   else if (! expr8 (&opP->disp))
1076                     losing++;
1077                   else if (! m68k_quick
1078                            && instring[3] != 'q'
1079                            && instring[4] != 'q')
1080                     losing++;
1081                   break;
1082
1083                 case 'O':
1084                   if (opP->mode != DREG && opP->mode != IMMED)
1085                     losing++;
1086                   break;
1087
1088                 case 'Q':
1089                   if (opP->mode != IMMED)
1090                     losing++;
1091                   else if (! expr8 (&opP->disp)
1092                            || opP->disp.exp.X_add_number < 1
1093                            || opP->disp.exp.X_add_number > 8)
1094                     losing++;
1095                   else if (! m68k_quick
1096                            && (strncmp (instring, "add", 3) == 0
1097                                || strncmp (instring, "sub", 3) == 0)
1098                            && instring[3] != 'q')
1099                     losing++;
1100                   break;
1101
1102                 case 'R':
1103                   if (opP->mode != DREG && opP->mode != AREG)
1104                     losing++;
1105                   break;
1106
1107                 case 'r':
1108                   if (opP->mode != AINDR
1109                       && (opP->mode != BASE
1110                           || (opP->reg != 0
1111                               && opP->reg != ZADDR0)
1112                           || opP->disp.exp.X_op != O_absent
1113                           || ((opP->index.reg < DATA0
1114                                || opP->index.reg > DATA7)
1115                               && (opP->index.reg < ADDR0
1116                                   || opP->index.reg > ADDR7))
1117                           || opP->index.size != SIZE_UNSPEC
1118                           || opP->index.scale != 1))
1119                     losing++;
1120                   break;
1121
1122                 case 's':
1123                   if (opP->mode != CONTROL
1124                       || ! (opP->reg == FPI
1125                             || opP->reg == FPS
1126                             || opP->reg == FPC))
1127                     losing++;
1128                   break;
1129
1130                 case 'S':
1131                   if (opP->mode != CONTROL || opP->reg != SR)
1132                     losing++;
1133                   break;
1134
1135                 case 't':
1136                   if (opP->mode != IMMED)
1137                     losing++;
1138                   else if (! expr8 (&opP->disp)
1139                            || opP->disp.exp.X_add_number < 0
1140                            || opP->disp.exp.X_add_number > 7)
1141                     losing++;
1142                   break;
1143
1144                 case 'U':
1145                   if (opP->mode != CONTROL || opP->reg != USP)
1146                     losing++;
1147                   break;
1148
1149                   /* JF these are out of order.  We could put them
1150                      in order if we were willing to put up with
1151                      bunches of #ifdef m68851s in the code.
1152
1153                      Don't forget that you need these operands
1154                      to use 68030 MMU instructions.  */
1155 #ifndef NO_68851
1156                   /* Memory addressing mode used by pflushr */
1157                 case '|':
1158                   if (opP->mode == CONTROL
1159                       || opP->mode == FPREG
1160                       || opP->mode == DREG
1161                       || opP->mode == AREG
1162                       || opP->mode == REGLST)
1163                     losing++;
1164                   break;
1165
1166                 case 'f':
1167                   if (opP->mode != CONTROL
1168                       || (opP->reg != SFC && opP->reg != DFC))
1169                     losing++;
1170                   break;
1171
1172                 case 'P':
1173                   if (opP->mode != CONTROL
1174                       || (opP->reg != TC
1175                           && opP->reg != CAL
1176                           && opP->reg != VAL
1177                           && opP->reg != SCC
1178                           && opP->reg != AC))
1179                     losing++;
1180                   break;
1181
1182                 case 'V':
1183                   if (opP->mode != CONTROL
1184                       || opP->reg != VAL)
1185                     losing++;
1186                   break;
1187
1188                 case 'W':
1189                   if (opP->mode != CONTROL
1190                       || (opP->reg != DRP
1191                           && opP->reg != SRP
1192                           && opP->reg != CRP))
1193                     losing++;
1194                   break;
1195
1196                 case 'X':
1197                   if (opP->mode != CONTROL
1198                       || (!(opP->reg >= BAD && opP->reg <= BAD + 7)
1199                           && !(opP->reg >= BAC && opP->reg <= BAC + 7)))
1200                     losing++;
1201                   break;
1202
1203                 case 'Y':
1204                   if (opP->mode != CONTROL || opP->reg != PSR)
1205                     losing++;
1206                   break;
1207
1208                 case 'Z':
1209                   if (opP->mode != CONTROL || opP->reg != PCSR)
1210                     losing++;
1211                   break;
1212 #endif
1213                 case 'c':
1214                   if (opP->mode != CONTROL
1215                       || (opP->reg != NC
1216                           && opP->reg != IC
1217                           && opP->reg != DC
1218                           && opP->reg != BC))
1219                     {
1220                       losing++;
1221                     }           /* not a cache specifier. */
1222                   break;
1223
1224                 case '_':
1225                   if (opP->mode != ABSL)
1226                     ++losing;
1227                   break;
1228
1229                 default:
1230                   abort ();
1231                 }               /* switch on type of operand */
1232
1233               if (losing)
1234                 break;
1235             }                   /* for each operand */
1236         }                       /* if immediately wrong */
1237
1238       if (!losing)
1239         {
1240           break;
1241         }                       /* got it. */
1242
1243       opcode = opcode->m_next;
1244
1245       if (!opcode)
1246         {
1247           if (ok_arch
1248               && !(ok_arch & current_architecture))
1249             {
1250               char buf[200], *cp;
1251               int len;
1252               strcpy (buf,
1253                       "invalid instruction for this architecture; needs ");
1254               cp = buf + strlen (buf);
1255               switch (ok_arch)
1256                 {
1257                 case mfloat:
1258                   strcpy (cp, "fpu (68040, 68060 or 68881/68882)");
1259                   break;
1260                 case mmmu:
1261                   strcpy (cp, "mmu (68030 or 68851)");
1262                   break;
1263                 case m68020up:
1264                   strcpy (cp, "68020 or higher");
1265                   break;
1266                 case m68000up:
1267                   strcpy (cp, "68000 or higher");
1268                   break;
1269                 case m68010up:
1270                   strcpy (cp, "68010 or higher");
1271                   break;
1272                 default:
1273                   {
1274                     int got_one = 0, idx;
1275                     for (idx = 0; idx < sizeof (archs) / sizeof (archs[0]);
1276                          idx++)
1277                       {
1278                         if (archs[idx].arch & ok_arch)
1279                           {
1280                             if (got_one)
1281                               {
1282                                 strcpy (cp, " or ");
1283                                 cp += strlen (cp);
1284                               }
1285                             got_one = 1;
1286                             strcpy (cp, archs[idx].name);
1287                             cp += strlen (cp);
1288                           }
1289                       }
1290                   }
1291                 }
1292               len = cp - buf + 1;
1293               cp = malloc (len);
1294               strcpy (cp, buf);
1295               the_ins.error = cp;
1296             }
1297           else
1298             the_ins.error = "operands mismatch";
1299           return;
1300         }                       /* Fell off the end */
1301
1302       losing = 0;
1303     }
1304
1305   /* now assemble it */
1306
1307   the_ins.args = opcode->m_operands;
1308   the_ins.numargs = opcode->m_opnum;
1309   the_ins.numo = opcode->m_codenum;
1310   the_ins.opcode[0] = getone (opcode);
1311   the_ins.opcode[1] = gettwo (opcode);
1312
1313   for (s = the_ins.args, opP = &the_ins.operands[0]; *s; s += 2, opP++)
1314     {
1315       /* This switch is a doozy.
1316        Watch the first step; its a big one! */
1317       switch (s[0])
1318         {
1319
1320         case '*':
1321         case '~':
1322         case '%':
1323         case ';':
1324         case '@':
1325         case '!':
1326         case '&':
1327         case '$':
1328         case '?':
1329         case '/':
1330         case '`':
1331 #ifndef NO_68851
1332         case '|':
1333 #endif
1334           switch (opP->mode)
1335             {
1336             case IMMED:
1337               tmpreg = 0x3c;    /* 7.4 */
1338               if (strchr ("bwl", s[1]))
1339                 nextword = get_num (&opP->disp, 80);
1340               else
1341                 nextword = get_num (&opP->disp, 0);
1342               if (isvar (&opP->disp))
1343                 add_fix (s[1], &opP->disp, 0, 0);
1344               switch (s[1])
1345                 {
1346                 case 'b':
1347                   if (!isbyte (nextword))
1348                     opP->error = "operand out of range";
1349                   addword (nextword);
1350                   baseo = 0;
1351                   break;
1352                 case 'w':
1353                   if (!isword (nextword))
1354                     opP->error = "operand out of range";
1355                   addword (nextword);
1356                   baseo = 0;
1357                   break;
1358                 case 'l':
1359                   addword (nextword >> 16);
1360                   addword (nextword);
1361                   baseo = 0;
1362                   break;
1363
1364                 case 'f':
1365                   baseo = 2;
1366                   outro = 8;
1367                   break;
1368                 case 'F':
1369                   baseo = 4;
1370                   outro = 11;
1371                   break;
1372                 case 'x':
1373                   baseo = 6;
1374                   outro = 15;
1375                   break;
1376                 case 'p':
1377                   baseo = 6;
1378                   outro = -1;
1379                   break;
1380                 default:
1381                   abort ();
1382                 }
1383               if (!baseo)
1384                 break;
1385
1386               /* We gotta put out some float */
1387               if (op (&opP->disp) != O_big)
1388                 {
1389                   valueT val;
1390                   int gencnt;
1391
1392                   /* Can other cases happen here?  */
1393                   if (op (&opP->disp) != O_constant)
1394                     abort ();
1395
1396                   val = (valueT) offs (&opP->disp);
1397                   gencnt = 0;
1398                   do
1399                     {
1400                       generic_bignum[gencnt] = (LITTLENUM_TYPE) val;
1401                       val >>= LITTLENUM_NUMBER_OF_BITS;
1402                       ++gencnt;
1403                     }
1404                   while (val != 0);
1405                   offs (&opP->disp) = gencnt;
1406                 }
1407               if (offs (&opP->disp) > 0)
1408                 {
1409                   if (offs (&opP->disp) > baseo)
1410                     {
1411                       as_warn ("Bignum too big for %c format; truncated",
1412                                s[1]);
1413                       offs (&opP->disp) = baseo;
1414                     }
1415                   baseo -= offs (&opP->disp);
1416                   while (baseo--)
1417                     addword (0);
1418                   for (wordp = generic_bignum + offs (&opP->disp) - 1;
1419                        offs (&opP->disp)--;
1420                        --wordp)
1421                     addword (*wordp);
1422                   break;
1423                 }
1424               gen_to_words (words, baseo, (long) outro);
1425               for (wordp = words; baseo--; wordp++)
1426                 addword (*wordp);
1427               break;
1428             case DREG:
1429               tmpreg = opP->reg - DATA; /* 0.dreg */
1430               break;
1431             case AREG:
1432               tmpreg = 0x08 + opP->reg - ADDR;  /* 1.areg */
1433               break;
1434             case AINDR:
1435               tmpreg = 0x10 + opP->reg - ADDR;  /* 2.areg */
1436               break;
1437             case ADEC:
1438               tmpreg = 0x20 + opP->reg - ADDR;  /* 4.areg */
1439               break;
1440             case AINC:
1441               tmpreg = 0x18 + opP->reg - ADDR;  /* 3.areg */
1442               break;
1443             case DISP:
1444
1445               nextword = get_num (&opP->disp, 80);
1446
1447               if (opP->reg == PC
1448                   && ! isvar (&opP->disp)
1449                   && m68k_abspcadd)
1450                 {
1451                   opP->disp.exp.X_op = O_symbol;
1452 #ifndef BFD_ASSEMBLER
1453                   opP->disp.exp.X_add_symbol = &abs_symbol;
1454 #else
1455                   opP->disp.exp.X_add_symbol =
1456                     section_symbol (absolute_section);
1457 #endif
1458                 }
1459
1460               /* Force into index mode.  Hope this works */
1461
1462               /* We do the first bit for 32-bit displacements, and the
1463                  second bit for 16 bit ones.  It is possible that we
1464                  should make the default be WORD instead of LONG, but
1465                  I think that'd break GCC, so we put up with a little
1466                  inefficiency for the sake of working output.  */
1467
1468               if (!issword (nextword)
1469                   || (isvar (&opP->disp)
1470                       && ((opP->disp.size == SIZE_UNSPEC
1471                            && flag_short_refs == 0
1472                            && cpu_of_arch (current_architecture) >= m68020)
1473                           || opP->disp.size == SIZE_LONG)))
1474                 {
1475                   if (opP->reg == PC)
1476                     tmpreg = 0x3B;      /* 7.3 */
1477                   else
1478                     tmpreg = 0x30 + opP->reg - ADDR;    /* 6.areg */
1479                   if (isvar (&opP->disp))
1480                     {
1481                       if (opP->reg == PC)
1482                         {
1483 #if 0
1484                           addword (0x0170);
1485                           add_fix ('l', &opP->disp, 1, 2);
1486                           addword (0), addword (0);
1487 #else
1488                           add_frag (adds (&opP->disp),
1489                                     offs (&opP->disp),
1490                                     TAB (PCLEA, SZ_UNDEF));
1491 #endif
1492                           break;
1493                         }
1494                       else
1495                         {
1496                           addword (0x0170);
1497                           add_fix ('l', &opP->disp, 0, 0);
1498                         }
1499                     }
1500                   else
1501                     addword (0x0170);
1502                   addword (nextword >> 16);
1503                 }
1504               else
1505                 {
1506                   if (opP->reg == PC)
1507                     tmpreg = 0x3A;      /* 7.2 */
1508                   else
1509                     tmpreg = 0x28 + opP->reg - ADDR;    /* 5.areg */
1510
1511                   if (isvar (&opP->disp))
1512                     {
1513                       if (opP->reg == PC)
1514                         {
1515                           add_fix ('w', &opP->disp, 1, 0);
1516                         }
1517                       else
1518                         add_fix ('w', &opP->disp, 0, 0);
1519                     }
1520                 }
1521               addword (nextword);
1522               break;
1523
1524             case POST:
1525             case PRE:
1526             case BASE:
1527               nextword = 0;
1528               baseo = get_num (&opP->disp, 80);
1529               if (opP->mode == POST || opP->mode == PRE)
1530                 outro = get_num (&opP->odisp, 80);
1531               /* Figure out the `addressing mode'.
1532                  Also turn on the BASE_DISABLE bit, if needed.  */
1533               if (opP->reg == PC || opP->reg == ZPC)
1534                 {
1535                   tmpreg = 0x3b;        /* 7.3 */
1536                   if (opP->reg == ZPC)
1537                     nextword |= 0x80;
1538                 }
1539               else if (opP->reg == 0)
1540                 {
1541                   nextword |= 0x80;
1542                   tmpreg = 0x30;        /* 6.garbage */
1543                 }
1544               else if (opP->reg >= ZADDR0 && opP->reg <= ZADDR7)
1545                 {
1546                   nextword |= 0x80;
1547                   tmpreg = 0x30 + opP->reg - ZADDR0;
1548                 }
1549               else
1550                 tmpreg = 0x30 + opP->reg - ADDR;        /* 6.areg */
1551
1552               siz1 = opP->disp.size;
1553               if (opP->mode == POST || opP->mode == PRE)
1554                 siz2 = opP->odisp.size;
1555               else
1556                 siz2 = SIZE_UNSPEC;
1557
1558               /* Index register stuff */
1559               if (opP->index.reg != 0
1560                   && opP->index.reg >= DATA
1561                   && opP->index.reg <= ADDR7)
1562                 {
1563                   nextword |= (opP->index.reg - DATA) << 12;
1564
1565                   if (opP->index.size == SIZE_UNSPEC
1566                       || opP->index.size == SIZE_LONG)
1567                     nextword |= 0x800;
1568
1569                   if (cpu_of_arch (current_architecture) < m68020)
1570                     {
1571                       if (opP->index.scale != 1)
1572                         {
1573                           opP->error =
1574                             "scale factor invalid on this architecture; needs 68020 or higher";
1575                         }
1576                     }
1577
1578                   switch (opP->index.scale)
1579                     {
1580                     case 1:
1581                       break;
1582                     case 2:
1583                       nextword |= 0x200;
1584                       break;
1585                     case 4:
1586                       nextword |= 0x400;
1587                       break;
1588                     case 8:
1589                       nextword |= 0x600;
1590                       break;
1591                     default:
1592                       abort ();
1593                     }
1594                   /* IF its simple,
1595                      GET US OUT OF HERE! */
1596
1597                   /* Must be INDEX, with an index register.  Address
1598                      register cannot be ZERO-PC, and either :b was
1599                      forced, or we know it will fit.  For a 68000 or
1600                      68010, force this mode anyways, because the
1601                      larger modes aren't supported.  */
1602                   if (opP->mode == BASE
1603                       && ((opP->reg >= ADDR0
1604                            && opP->reg <= ADDR7)
1605                           || opP->reg == PC))
1606                     {
1607                       if (siz1 == SIZE_BYTE
1608                           || cpu_of_arch (current_architecture) < m68020
1609                           || (siz1 == SIZE_UNSPEC
1610                               && ! isvar (&opP->disp)
1611                               && issbyte (baseo)))
1612                         {
1613                           nextword += baseo & 0xff;
1614                           addword (nextword);
1615                           if (isvar (&opP->disp))
1616                             {
1617                               /* Do a byte relocation.  If it doesn't
1618                                  fit (possible on m68000) let the
1619                                  fixup processing complain later.  */
1620                               if (opP->reg == PC)
1621                                 add_fix ('B', &opP->disp, 1, 1);
1622                               else
1623                                 add_fix ('B', &opP->disp, 0, 0);
1624                             }
1625                           else if (siz1 != SIZE_BYTE)
1626                             {
1627                               if (siz1 != SIZE_UNSPEC)
1628                                 as_warn ("Forcing byte displacement");
1629                               if (! issbyte (baseo))
1630                                 opP->error = "byte displacement out of range";
1631                             }
1632
1633                           break;
1634                         }
1635                       else if (siz1 == SIZE_UNSPEC
1636                                && opP->reg == PC
1637                                && isvar (&opP->disp)
1638                                && subs (&opP->disp) == NULL)
1639                         {
1640                           nextword += baseo & 0xff;
1641                           addword (nextword);
1642                           add_frag (adds (&opP->disp), offs (&opP->disp),
1643                                     TAB (PCINDEX, SZ_UNDEF));
1644
1645                           break;
1646                         }
1647                     }
1648                 }
1649               else
1650                 {
1651                   nextword |= 0x40;     /* No index reg */
1652                   if (opP->index.reg >= ZDATA0
1653                       && opP->index.reg <= ZDATA7)
1654                     nextword |= (opP->index.reg - ZDATA0) << 12;
1655                   else if (opP->index.reg >= ZADDR0
1656                            || opP->index.reg <= ZADDR7)
1657                     nextword |= (opP->index.reg - ZADDR0 + 8) << 12;
1658                 }
1659
1660               /* It isn't simple.  */
1661
1662               if (cpu_of_arch (current_architecture) < m68020)
1663                 opP->error =
1664                   "invalid operand mode for this architecture; needs 68020 or higher";
1665
1666               nextword |= 0x100;
1667               /* If the guy specified a width, we assume that it is
1668                  wide enough.  Maybe it isn't.  If so, we lose.  */
1669               switch (siz1)
1670                 {
1671                 case SIZE_UNSPEC:
1672                   if (isvar (&opP->disp)
1673                       ? m68k_rel32
1674                       : ! issword (baseo))
1675                     {
1676                       siz1 = SIZE_LONG;
1677                       nextword |= 0x30;
1678                     }
1679                   else if (! isvar (&opP->disp) && baseo == 0)
1680                     nextword |= 0x10;
1681                   else
1682                     {
1683                       nextword |= 0x20;
1684                       siz1 = SIZE_WORD;
1685                     }
1686                   break;
1687                 case SIZE_BYTE:
1688                   as_warn (":b not permitted; defaulting to :w");
1689                   /* Fall through.  */
1690                 case SIZE_WORD:
1691                   nextword |= 0x20;
1692                   break;
1693                 case SIZE_LONG:
1694                   nextword |= 0x30;
1695                   break;
1696                 }
1697
1698               /* Figure out innner displacement stuff */
1699               if (opP->mode == POST || opP->mode == PRE)
1700                 {
1701                   switch (siz2)
1702                     {
1703                     case SIZE_UNSPEC:
1704                       if (isvar (&opP->odisp)
1705                           ? m68k_rel32
1706                           : ! issword (outro))
1707                         {
1708                           siz2 = SIZE_LONG;
1709                           nextword |= 0x3;
1710                         }
1711                       else if (! isvar (&opP->disp) && outro == 0)
1712                         nextword |= 0x1;
1713                       else
1714                         {
1715                           nextword |= 0x2;
1716                           siz2 = SIZE_WORD;
1717                         }
1718                       break;
1719                     case 1:
1720                       as_warn (":b not permitted; defaulting to :w");
1721                       /* Fall through.  */
1722                     case 2:
1723                       nextword |= 0x2;
1724                       break;
1725                     case 3:
1726                       nextword |= 0x3;
1727                       break;
1728                     }
1729                   if (opP->mode == POST)
1730                     nextword |= 0x04;
1731                 }
1732               addword (nextword);
1733
1734               if (siz1 != SIZE_UNSPEC && isvar (&opP->disp))
1735                 {
1736                   if (opP->reg == PC || opP->reg == ZPC)
1737                     add_fix (siz1 == SIZE_LONG ? 'l' : 'w', &opP->disp, 1, 2);
1738                   else
1739                     add_fix (siz1 == SIZE_LONG ? 'l' : 'w', &opP->disp, 0, 0);
1740                 }
1741               if (siz1 == SIZE_LONG)
1742                 addword (baseo >> 16);
1743               if (siz1 != SIZE_UNSPEC)
1744                 addword (baseo);
1745
1746               if (siz2 != SIZE_UNSPEC && isvar (&opP->odisp))
1747                 add_fix (siz2 == SIZE_LONG ? 'l' : 'w', &opP->odisp, 0, 0);
1748               if (siz2 == SIZE_LONG)
1749                 addword (outro >> 16);
1750               if (siz2 != SIZE_UNSPEC)
1751                 addword (outro);
1752
1753               break;
1754
1755             case ABSL:
1756               nextword = get_num (&opP->disp, 80);
1757               switch (opP->disp.size)
1758                 {
1759                 default:
1760                   abort ();
1761                 case SIZE_UNSPEC:
1762                   if (!isvar (&opP->disp) && issword (offs (&opP->disp)))
1763                     {
1764                       tmpreg = 0x38;    /* 7.0 */
1765                       addword (nextword);
1766                       break;
1767                     }
1768                   /* Don't generate pc relative code on 68010 and
1769                      68000.  */
1770                   if (isvar (&opP->disp)
1771                       && !subs (&opP->disp)
1772                       && adds (&opP->disp)
1773                       && (S_GET_SEGMENT (adds (&opP->disp)) == text_section)
1774                       && now_seg == text_section
1775                       && cpu_of_arch (current_architecture) >= m68020
1776                       && !flag_long_jumps
1777                       && !strchr ("~%&$?", s[0]))
1778                     {
1779                       tmpreg = 0x3A;    /* 7.2 */
1780                       add_frag (adds (&opP->disp),
1781                                 offs (&opP->disp),
1782                                 TAB (PCREL, SZ_UNDEF));
1783                       break;
1784                     }
1785                   /* Fall through into long */
1786                 case SIZE_LONG:
1787                   if (isvar (&opP->disp))
1788                     add_fix ('l', &opP->disp, 0, 0);
1789
1790                   tmpreg = 0x39;/* 7.1 mode */
1791                   addword (nextword >> 16);
1792                   addword (nextword);
1793                   break;
1794
1795                 case SIZE_WORD: /* Word */
1796                   if (isvar (&opP->disp))
1797                     add_fix ('w', &opP->disp, 0, 0);
1798
1799                   tmpreg = 0x38;/* 7.0 mode */
1800                   addword (nextword);
1801                   break;
1802                 }
1803               break;
1804             case CONTROL:
1805             case FPREG:
1806             default:
1807               as_bad ("unknown/incorrect operand");
1808               /* abort(); */
1809             }
1810           install_gen_operand (s[1], tmpreg);
1811           break;
1812
1813         case '#':
1814         case '^':
1815           switch (s[1])
1816             {                   /* JF: I hate floating point! */
1817             case 'j':
1818               tmpreg = 70;
1819               break;
1820             case '8':
1821               tmpreg = 20;
1822               break;
1823             case 'C':
1824               tmpreg = 50;
1825               break;
1826             case '3':
1827             default:
1828               tmpreg = 80;
1829               break;
1830             }
1831           tmpreg = get_num (&opP->disp, tmpreg);
1832           if (isvar (&opP->disp))
1833             add_fix (s[1], &opP->disp, 0, 0);
1834           switch (s[1])
1835             {
1836             case 'b':           /* Danger:  These do no check for
1837                                    certain types of overflow.
1838                                    user beware! */
1839               if (!isbyte (tmpreg))
1840                 opP->error = "out of range";
1841               insop (tmpreg, opcode);
1842               if (isvar (&opP->disp))
1843                 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
1844               break;
1845             case 'w':
1846               if (!isword (tmpreg))
1847                 opP->error = "out of range";
1848               insop (tmpreg, opcode);
1849               if (isvar (&opP->disp))
1850                 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
1851               break;
1852             case 'l':
1853               /* Because of the way insop works, we put these two out
1854                  backwards.  */
1855               insop (tmpreg, opcode);
1856               insop (tmpreg >> 16, opcode);
1857               if (isvar (&opP->disp))
1858                 the_ins.reloc[the_ins.nrel - 1].n = (opcode->m_codenum) * 2;
1859               break;
1860             case '3':
1861               tmpreg &= 0xFF;
1862             case '8':
1863             case 'C':
1864               install_operand (s[1], tmpreg);
1865               break;
1866             default:
1867               abort ();
1868             }
1869           break;
1870
1871         case '+':
1872         case '-':
1873         case 'A':
1874         case 'a':
1875           install_operand (s[1], opP->reg - ADDR);
1876           break;
1877
1878         case 'B':
1879           tmpreg = get_num (&opP->disp, 80);
1880           switch (s[1])
1881             {
1882             case 'B':
1883               add_fix ('B', &opP->disp, 1, -1);
1884               break;
1885             case 'W':
1886               add_fix ('w', &opP->disp, 1, 0);
1887               addword (0);
1888               break;
1889             case 'L':
1890             long_branch:
1891               if (cpu_of_arch (current_architecture) < m68020)
1892                 as_warn ("Can't use long branches on 68000/68010");
1893               the_ins.opcode[the_ins.numo - 1] |= 0xff;
1894               add_fix ('l', &opP->disp, 1, 0);
1895               addword (0);
1896               addword (0);
1897               break;
1898             case 'g':
1899               if (subs (&opP->disp))    /* We can't relax it */
1900                 goto long_branch;
1901
1902               /* This could either be a symbol, or an absolute
1903                  address.  No matter, the frag hacking will finger it
1904                  out.  Not quite: it can't switch from BRANCH to
1905                  BCC68000 for the case where opnd is absolute (it
1906                  needs to use the 68000 hack since no conditional abs
1907                  jumps).  */
1908               if (((cpu_of_arch (current_architecture) < m68020)
1909                    || (0 == adds (&opP->disp)))
1910                   && (the_ins.opcode[0] >= 0x6200)
1911                   && (the_ins.opcode[0] <= 0x6f00))
1912                 add_frag (adds (&opP->disp), offs (&opP->disp),
1913                           TAB (BCC68000, SZ_UNDEF));
1914               else
1915                 add_frag (adds (&opP->disp), offs (&opP->disp),
1916                           TAB (ABRANCH, SZ_UNDEF));
1917               break;
1918             case 'w':
1919               if (isvar (&opP->disp))
1920                 {
1921 #if 1
1922                   /* check for DBcc instruction */
1923                   if ((the_ins.opcode[0] & 0xf0f8) == 0x50c8)
1924                     {
1925                       /* size varies if patch */
1926                       /* needed for long form */
1927                       add_frag (adds (&opP->disp), offs (&opP->disp),
1928                                 TAB (DBCC, SZ_UNDEF));
1929                       break;
1930                     }
1931 #endif
1932                   add_fix ('w', &opP->disp, 1, 0);
1933                 }
1934               addword (0);
1935               break;
1936             case 'C':           /* Fixed size LONG coproc branches */
1937               add_fix ('l', &opP->disp, 1, 0);
1938               addword (0);
1939               addword (0);
1940               break;
1941             case 'c':           /* Var size Coprocesssor branches */
1942               if (subs (&opP->disp))
1943                 {
1944                   add_fix ('l', &opP->disp, 1, 0);
1945                   add_frag ((symbolS *) 0, (long) 0, TAB (FBRANCH, LONG));
1946                 }
1947               else if (adds (&opP->disp))
1948                 add_frag (adds (&opP->disp), offs (&opP->disp),
1949                           TAB (FBRANCH, SZ_UNDEF));
1950               else
1951                 {
1952                   /* add_frag((symbolS *) 0, offs(&opP->disp),
1953                      TAB(FBRANCH,SHORT)); */
1954                   the_ins.opcode[the_ins.numo - 1] |= 0x40;
1955                   add_fix ('l', &opP->disp, 1, 0);
1956                   addword (0);
1957                   addword (0);
1958                 }
1959               break;
1960             default:
1961               abort ();
1962             }
1963           break;
1964
1965         case 'C':               /* Ignore it */
1966           break;
1967
1968         case 'd':               /* JF this is a kludge */
1969           install_operand ('s', opP->reg - ADDR);
1970           tmpreg = get_num (&opP->disp, 80);
1971           if (!issword (tmpreg))
1972             {
1973               as_warn ("Expression out of range, using 0");
1974               tmpreg = 0;
1975             }
1976           addword (tmpreg);
1977           break;
1978
1979         case 'D':
1980           install_operand (s[1], opP->reg - DATA);
1981           break;
1982
1983         case 'F':
1984           install_operand (s[1], opP->reg - FP0);
1985           break;
1986
1987         case 'I':
1988           tmpreg = opP->reg - COP0;
1989           install_operand (s[1], tmpreg);
1990           break;
1991
1992         case 'J':               /* JF foo */
1993           switch (opP->reg)
1994             {
1995             case SFC:
1996               tmpreg = 0x000;
1997               break;
1998             case DFC:
1999               tmpreg = 0x001;
2000               break;
2001             case CACR:
2002               tmpreg = 0x002;
2003               break;
2004             case TC:
2005               tmpreg = 0x003;
2006               break;
2007             case ITT0:
2008               tmpreg = 0x004;
2009               break;
2010             case ITT1:
2011               tmpreg = 0x005;
2012               break;
2013             case DTT0:
2014               tmpreg = 0x006;
2015               break;
2016             case DTT1:
2017               tmpreg = 0x007;
2018               break;
2019             case BUSCR:
2020               tmpreg = 0x008;
2021               break;
2022
2023             case USP:
2024               tmpreg = 0x800;
2025               break;
2026             case VBR:
2027               tmpreg = 0x801;
2028               break;
2029             case CAAR:
2030               tmpreg = 0x802;
2031               break;
2032             case MSP:
2033               tmpreg = 0x803;
2034               break;
2035             case ISP:
2036               tmpreg = 0x804;
2037               break;
2038             case MMUSR:
2039               tmpreg = 0x805;
2040               break;
2041             case URP:
2042               tmpreg = 0x806;
2043               break;
2044             case SRP:
2045               tmpreg = 0x807;
2046               break;
2047             case PCR:
2048               tmpreg = 0x808;
2049               break;
2050             default:
2051               abort ();
2052             }
2053           install_operand (s[1], tmpreg);
2054           break;
2055
2056         case 'k':
2057           tmpreg = get_num (&opP->disp, 55);
2058           install_operand (s[1], tmpreg & 0x7f);
2059           break;
2060
2061         case 'l':
2062           tmpreg = opP->mask;
2063           if (s[1] == 'w')
2064             {
2065               if (tmpreg & 0x7FF0000)
2066                 as_bad ("Floating point register in register list");
2067               insop (reverse_16_bits (tmpreg), opcode);
2068             }
2069           else
2070             {
2071               if (tmpreg & 0x700FFFF)
2072                 as_bad ("Wrong register in floating-point reglist");
2073               install_operand (s[1], reverse_8_bits (tmpreg >> 16));
2074             }
2075           break;
2076
2077         case 'L':
2078           tmpreg = opP->mask;
2079           if (s[1] == 'w')
2080             {
2081               if (tmpreg & 0x7FF0000)
2082                 as_bad ("Floating point register in register list");
2083               insop (tmpreg, opcode);
2084             }
2085           else if (s[1] == '8')
2086             {
2087               if (tmpreg & 0x0FFFFFF)
2088                 as_bad ("incorrect register in reglist");
2089               install_operand (s[1], tmpreg >> 24);
2090             }
2091           else
2092             {
2093               if (tmpreg & 0x700FFFF)
2094                 as_bad ("wrong register in floating-point reglist");
2095               else
2096                 install_operand (s[1], tmpreg >> 16);
2097             }
2098           break;
2099
2100         case 'M':
2101           install_operand (s[1], get_num (&opP->disp, 60));
2102           break;
2103
2104         case 'O':
2105           tmpreg = ((opP->mode == DREG)
2106                     ? 0x20 + opP->reg - DATA
2107                     : (get_num (&opP->disp, 40) & 0x1F));
2108           install_operand (s[1], tmpreg);
2109           break;
2110
2111         case 'Q':
2112           tmpreg = get_num (&opP->disp, 10);
2113           if (tmpreg == 8)
2114             tmpreg = 0;
2115           install_operand (s[1], tmpreg);
2116           break;
2117
2118         case 'R':
2119           /* This depends on the fact that ADDR registers are eight
2120              more than their corresponding DATA regs, so the result
2121              will have the ADDR_REG bit set */
2122           install_operand (s[1], opP->reg - DATA);
2123           break;
2124
2125         case 'r':
2126           if (opP->mode == AINDR)
2127             install_operand (s[1], opP->reg - DATA);
2128           else
2129             install_operand (s[1], opP->index.reg - DATA);
2130           break;
2131
2132         case 's':
2133           if (opP->reg == FPI)
2134             tmpreg = 0x1;
2135           else if (opP->reg == FPS)
2136             tmpreg = 0x2;
2137           else if (opP->reg == FPC)
2138             tmpreg = 0x4;
2139           else
2140             abort ();
2141           install_operand (s[1], tmpreg);
2142           break;
2143
2144         case 'S':               /* Ignore it */
2145           break;
2146
2147         case 'T':
2148           install_operand (s[1], get_num (&opP->disp, 30));
2149           break;
2150
2151         case 'U':               /* Ignore it */
2152           break;
2153
2154         case 'c':
2155           switch (opP->reg)
2156             {
2157             case NC:
2158               tmpreg = 0;
2159               break;
2160             case DC:
2161               tmpreg = 1;
2162               break;
2163             case IC:
2164               tmpreg = 2;
2165               break;
2166             case BC:
2167               tmpreg = 3;
2168               break;
2169             default:
2170               as_fatal ("failed sanity check");
2171             }                   /* switch on cache token */
2172           install_operand (s[1], tmpreg);
2173           break;
2174 #ifndef NO_68851
2175           /* JF: These are out of order, I fear. */
2176         case 'f':
2177           switch (opP->reg)
2178             {
2179             case SFC:
2180               tmpreg = 0;
2181               break;
2182             case DFC:
2183               tmpreg = 1;
2184               break;
2185             default:
2186               abort ();
2187             }
2188           install_operand (s[1], tmpreg);
2189           break;
2190
2191         case 'P':
2192           switch (opP->reg)
2193             {
2194             case TC:
2195               tmpreg = 0;
2196               break;
2197             case CAL:
2198               tmpreg = 4;
2199               break;
2200             case VAL:
2201               tmpreg = 5;
2202               break;
2203             case SCC:
2204               tmpreg = 6;
2205               break;
2206             case AC:
2207               tmpreg = 7;
2208               break;
2209             default:
2210               abort ();
2211             }
2212           install_operand (s[1], tmpreg);
2213           break;
2214
2215         case 'V':
2216           if (opP->reg == VAL)
2217             break;
2218           abort ();
2219
2220         case 'W':
2221           switch (opP->reg)
2222             {
2223             case DRP:
2224               tmpreg = 1;
2225               break;
2226             case SRP:
2227               tmpreg = 2;
2228               break;
2229             case CRP:
2230               tmpreg = 3;
2231               break;
2232             default:
2233               abort ();
2234             }
2235           install_operand (s[1], tmpreg);
2236           break;
2237
2238         case 'X':
2239           switch (opP->reg)
2240             {
2241             case BAD:
2242             case BAD + 1:
2243             case BAD + 2:
2244             case BAD + 3:
2245             case BAD + 4:
2246             case BAD + 5:
2247             case BAD + 6:
2248             case BAD + 7:
2249               tmpreg = (4 << 10) | ((opP->reg - BAD) << 2);
2250               break;
2251
2252             case BAC:
2253             case BAC + 1:
2254             case BAC + 2:
2255             case BAC + 3:
2256             case BAC + 4:
2257             case BAC + 5:
2258             case BAC + 6:
2259             case BAC + 7:
2260               tmpreg = (5 << 10) | ((opP->reg - BAC) << 2);
2261               break;
2262
2263             default:
2264               abort ();
2265             }
2266           install_operand (s[1], tmpreg);
2267           break;
2268         case 'Y':
2269           know (opP->reg == PSR);
2270           break;
2271         case 'Z':
2272           know (opP->reg == PCSR);
2273           break;
2274 #endif /* m68851 */
2275         case '3':
2276           switch (opP->reg)
2277             {
2278             case TT0:
2279               tmpreg = 2;
2280               break;
2281             case TT1:
2282               tmpreg = 3;
2283               break;
2284             default:
2285               abort ();
2286             }
2287           install_operand (s[1], tmpreg);
2288           break;
2289         case 't':
2290           tmpreg = get_num (&opP->disp, 20);
2291           install_operand (s[1], tmpreg);
2292           break;
2293         case '_':       /* used only for move16 absolute 32-bit address */
2294           tmpreg = get_num (&opP->disp, 80);
2295           addword (tmpreg >> 16);
2296           addword (tmpreg & 0xFFFF);
2297           break;
2298         default:
2299           abort ();
2300         }
2301     }
2302
2303   /* By the time whe get here (FINALLY) the_ins contains the complete
2304      instruction, ready to be emitted. . . */
2305 }
2306
2307 static int
2308 reverse_16_bits (in)
2309      int in;
2310 {
2311   int out = 0;
2312   int n;
2313
2314   static int mask[16] =
2315   {
2316     0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
2317     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000
2318   };
2319   for (n = 0; n < 16; n++)
2320     {
2321       if (in & mask[n])
2322         out |= mask[15 - n];
2323     }
2324   return out;
2325 }                               /* reverse_16_bits() */
2326
2327 static int
2328 reverse_8_bits (in)
2329      int in;
2330 {
2331   int out = 0;
2332   int n;
2333
2334   static int mask[8] =
2335   {
2336     0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
2337   };
2338
2339   for (n = 0; n < 8; n++)
2340     {
2341       if (in & mask[n])
2342         out |= mask[7 - n];
2343     }
2344   return out;
2345 }                               /* reverse_8_bits() */
2346
2347 /* Cause an extra frag to be generated here, inserting up to 10 bytes
2348    (that value is chosen in the frag_var call in md_assemble).  TYPE
2349    is the subtype of the frag to be generated; its primary type is
2350    rs_machine_dependent.
2351
2352    The TYPE parameter is also used by md_convert_frag_1 and
2353    md_estimate_size_before_relax.  The appropriate type of fixup will
2354    be emitted by md_convert_frag_1.
2355
2356    ADD becomes the FR_SYMBOL field of the frag, and OFF the FR_OFFSET.  */
2357 static void
2358 install_operand (mode, val)
2359      int mode;
2360      int val;
2361 {
2362   switch (mode)
2363     {
2364     case 's':
2365       the_ins.opcode[0] |= val & 0xFF;  /* JF FF is for M kludge */
2366       break;
2367     case 'd':
2368       the_ins.opcode[0] |= val << 9;
2369       break;
2370     case '1':
2371       the_ins.opcode[1] |= val << 12;
2372       break;
2373     case '2':
2374       the_ins.opcode[1] |= val << 6;
2375       break;
2376     case '3':
2377       the_ins.opcode[1] |= val;
2378       break;
2379     case '4':
2380       the_ins.opcode[2] |= val << 12;
2381       break;
2382     case '5':
2383       the_ins.opcode[2] |= val << 6;
2384       break;
2385     case '6':
2386       /* DANGER!  This is a hack to force cas2l and cas2w cmds to be
2387          three words long! */
2388       the_ins.numo++;
2389       the_ins.opcode[2] |= val;
2390       break;
2391     case '7':
2392       the_ins.opcode[1] |= val << 7;
2393       break;
2394     case '8':
2395       the_ins.opcode[1] |= val << 10;
2396       break;
2397 #ifndef NO_68851
2398     case '9':
2399       the_ins.opcode[1] |= val << 5;
2400       break;
2401 #endif
2402
2403     case 't':
2404       the_ins.opcode[1] |= (val << 10) | (val << 7);
2405       break;
2406     case 'D':
2407       the_ins.opcode[1] |= (val << 12) | val;
2408       break;
2409     case 'g':
2410       the_ins.opcode[0] |= val = 0xff;
2411       break;
2412     case 'i':
2413       the_ins.opcode[0] |= val << 9;
2414       break;
2415     case 'C':
2416       the_ins.opcode[1] |= val;
2417       break;
2418     case 'j':
2419       the_ins.opcode[1] |= val;
2420       the_ins.numo++;           /* What a hack */
2421       break;
2422     case 'k':
2423       the_ins.opcode[1] |= val << 4;
2424       break;
2425     case 'b':
2426     case 'w':
2427     case 'l':
2428       break;
2429     case 'e':
2430       the_ins.opcode[0] |= (val << 6);
2431       break;
2432     case 'L':
2433       the_ins.opcode[1] = (val >> 16);
2434       the_ins.opcode[2] = val & 0xffff;
2435       break;
2436     case 'c':
2437     default:
2438       as_fatal ("failed sanity check.");
2439     }
2440 }                               /* install_operand() */
2441
2442 static void
2443 install_gen_operand (mode, val)
2444      int mode;
2445      int val;
2446 {
2447   switch (mode)
2448     {
2449     case 's':
2450       the_ins.opcode[0] |= val;
2451       break;
2452     case 'd':
2453       /* This is a kludge!!! */
2454       the_ins.opcode[0] |= (val & 0x07) << 9 | (val & 0x38) << 3;
2455       break;
2456     case 'b':
2457     case 'w':
2458     case 'l':
2459     case 'f':
2460     case 'F':
2461     case 'x':
2462     case 'p':
2463       the_ins.opcode[0] |= val;
2464       break;
2465       /* more stuff goes here */
2466     default:
2467       as_fatal ("failed sanity check.");
2468     }
2469 }                               /* install_gen_operand() */
2470
2471 /*
2472  * verify that we have some number of paren pairs, do m68k_ip_op(), and
2473  * then deal with the bitfield hack.
2474  */
2475
2476 static char *
2477 crack_operand (str, opP)
2478      register char *str;
2479      register struct m68k_op *opP;
2480 {
2481   register int parens;
2482   register int c;
2483   register char *beg_str;
2484
2485   if (!str)
2486     {
2487       return str;
2488     }
2489   beg_str = str;
2490   for (parens = 0; *str && (parens > 0 || notend (str)); str++)
2491     {
2492       if (*str == '(')
2493         parens++;
2494       else if (*str == ')')
2495         {
2496           if (!parens)
2497             {                   /* ERROR */
2498               opP->error = "Extra )";
2499               return str;
2500             }
2501           --parens;
2502         }
2503     }
2504   if (!*str && parens)
2505     {                           /* ERROR */
2506       opP->error = "Missing )";
2507       return str;
2508     }
2509   c = *str;
2510   *str = '\0';
2511   if (m68k_ip_op (beg_str, opP) != 0)
2512     {
2513       *str = c;
2514       return str;
2515     }
2516   *str = c;
2517   if (c == '}')
2518     c = *++str;                 /* JF bitfield hack */
2519   if (c)
2520     {
2521       c = *++str;
2522       if (!c)
2523         as_bad ("Missing operand");
2524     }
2525   return str;
2526 }
2527
2528 /* This is the guts of the machine-dependent assembler.  STR points to a
2529    machine dependent instruction.  This function is supposed to emit
2530    the frags/bytes it assembles to.
2531    */
2532
2533 void
2534 insert_reg (regname, regnum)
2535      char *regname;
2536      int regnum;
2537 {
2538   char buf[100];
2539   int i;
2540
2541 #ifdef REGISTER_PREFIX
2542   if (!flag_reg_prefix_optional)
2543     {
2544       buf[0] = REGISTER_PREFIX;
2545       strcpy (buf + 1, regname);
2546       regname = buf;
2547     }
2548 #endif
2549
2550   symbol_table_insert (symbol_new (regname, reg_section, regnum,
2551                                    &zero_address_frag));
2552
2553   for (i = 0; regname[i]; i++)
2554     buf[i] = islower (regname[i]) ? toupper (regname[i]) : regname[i];
2555   buf[i] = '\0';
2556
2557   symbol_table_insert (symbol_new (buf, reg_section, regnum,
2558                                    &zero_address_frag));
2559 }
2560
2561 struct init_entry
2562   {
2563     const char *name;
2564     int number;
2565   };
2566
2567 static const struct init_entry init_table[] =
2568 {
2569   { "d0", DATA0 },
2570   { "d1", DATA1 },
2571   { "d2", DATA2 },
2572   { "d3", DATA3 },
2573   { "d4", DATA4 },
2574   { "d5", DATA5 },
2575   { "d6", DATA6 },
2576   { "d7", DATA7 },
2577   { "a0", ADDR0 },
2578   { "a1", ADDR1 },
2579   { "a2", ADDR2 },
2580   { "a3", ADDR3 },
2581   { "a4", ADDR4 },
2582   { "a5", ADDR5 },
2583   { "a6", ADDR6 },
2584   { "fp", ADDR6 },
2585   { "a7", ADDR7 },
2586   { "sp", ADDR7 },
2587   { "ssp", ADDR7 },
2588   { "fp0", FP0 },
2589   { "fp1", FP1 },
2590   { "fp2", FP2 },
2591   { "fp3", FP3 },
2592   { "fp4", FP4 },
2593   { "fp5", FP5 },
2594   { "fp6", FP6 },
2595   { "fp7", FP7 },
2596   { "fpi", FPI },
2597   { "fpiar", FPI },
2598   { "fpc", FPI },
2599   { "fps", FPS },
2600   { "fpsr", FPS },
2601   { "fpc", FPC },
2602   { "fpcr", FPC },
2603   { "control", FPC },
2604   { "status", FPS },
2605   { "iaddr", FPI },
2606
2607   { "cop0", COP0 },
2608   { "cop1", COP1 },
2609   { "cop2", COP2 },
2610   { "cop3", COP3 },
2611   { "cop4", COP4 },
2612   { "cop5", COP5 },
2613   { "cop6", COP6 },
2614   { "cop7", COP7 },
2615   { "pc", PC },
2616   { "zpc", ZPC },
2617   { "sr", SR },
2618
2619   { "ccr", CCR },
2620   { "cc", CCR },
2621
2622   { "usp", USP },
2623   { "isp", ISP },
2624   { "sfc", SFC },
2625   { "sfcr", SFC },
2626   { "dfc", DFC },
2627   { "dfcr", DFC },
2628   { "cacr", CACR },
2629   { "caar", CAAR },
2630
2631   { "vbr", VBR },
2632
2633   { "msp", MSP },
2634   { "itt0", ITT0 },
2635   { "itt1", ITT1 },
2636   { "dtt0", DTT0 },
2637   { "dtt1", DTT1 },
2638   { "mmusr", MMUSR },
2639   { "tc", TC },
2640   { "srp", SRP },
2641   { "urp", URP },
2642   { "buscr", BUSCR },
2643   { "pcr", PCR },
2644
2645   { "ac", AC },
2646   { "bc", BC },
2647   { "cal", CAL },
2648   { "crp", CRP },
2649   { "drp", DRP },
2650   { "pcsr", PCSR },
2651   { "psr", PSR },
2652   { "scc", SCC },
2653   { "val", VAL },
2654   { "bad0", BAD0 },
2655   { "bad1", BAD1 },
2656   { "bad2", BAD2 },
2657   { "bad3", BAD3 },
2658   { "bad4", BAD4 },
2659   { "bad5", BAD5 },
2660   { "bad6", BAD6 },
2661   { "bad7", BAD7 },
2662   { "bac0", BAC0 },
2663   { "bac1", BAC1 },
2664   { "bac2", BAC2 },
2665   { "bac3", BAC3 },
2666   { "bac4", BAC4 },
2667   { "bac5", BAC5 },
2668   { "bac6", BAC6 },
2669   { "bac7", BAC7 },
2670
2671   { "ic", IC },
2672   { "dc", DC },
2673   { "nc", NC },
2674
2675   { "tt0", TT0 },
2676   { "tt1", TT1 },
2677   /* 68ec030 versions of same */
2678   { "ac0", TT0 },
2679   { "ac1", TT1 },
2680   /* 68ec030 access control unit, identical to 030 MMU status reg */
2681   { "acusr", PSR },
2682
2683   /* Suppressed data and address registers.  */
2684   { "zd0", ZDATA0 },
2685   { "zd1", ZDATA1 },
2686   { "zd2", ZDATA2 },
2687   { "zd3", ZDATA3 },
2688   { "zd4", ZDATA4 },
2689   { "zd5", ZDATA5 },
2690   { "zd6", ZDATA6 },
2691   { "zd7", ZDATA7 },
2692   { "za0", ZADDR0 },
2693   { "za1", ZADDR1 },
2694   { "za2", ZADDR2 },
2695   { "za3", ZADDR3 },
2696   { "za4", ZADDR4 },
2697   { "za5", ZADDR5 },
2698   { "za6", ZADDR6 },
2699   { "za7", ZADDR7 },
2700
2701   { 0, 0 }
2702 };
2703
2704 void
2705 init_regtable ()
2706 {
2707   int i;
2708   for (i = 0; init_table[i].name; i++)
2709     insert_reg (init_table[i].name, init_table[i].number);
2710 }
2711
2712 static int no_68851, no_68881;
2713
2714 #ifdef OBJ_AOUT
2715 /* a.out machine type.  Default to 68020.  */
2716 int m68k_aout_machtype = 2;
2717 #endif
2718
2719 void
2720 md_assemble (str)
2721      char *str;
2722 {
2723   const char *er;
2724   short *fromP;
2725   char *toP = NULL;
2726   int m, n = 0;
2727   char *to_beg_P;
2728   int shorts_this_frag;
2729   fixS *fixP;
2730
2731   memset ((char *) (&the_ins), '\0', sizeof (the_ins));
2732   m68k_ip (str);
2733   er = the_ins.error;
2734   if (!er)
2735     {
2736       for (n = 0; n < the_ins.numargs; n++)
2737         if (the_ins.operands[n].error)
2738           {
2739             er = the_ins.operands[n].error;
2740             break;
2741           }
2742     }
2743   if (er)
2744     {
2745       as_bad ("%s -- statement `%s' ignored", er, str);
2746       return;
2747     }
2748
2749   if (the_ins.nfrag == 0)
2750     {
2751       /* No frag hacking involved; just put it out */
2752       toP = frag_more (2 * the_ins.numo);
2753       fromP = &the_ins.opcode[0];
2754       for (m = the_ins.numo; m; --m)
2755         {
2756           md_number_to_chars (toP, (long) (*fromP), 2);
2757           toP += 2;
2758           fromP++;
2759         }
2760       /* put out symbol-dependent info */
2761       for (m = 0; m < the_ins.nrel; m++)
2762         {
2763           switch (the_ins.reloc[m].wid)
2764             {
2765             case 'B':
2766               n = 1;
2767               break;
2768             case 'b':
2769               n = 1;
2770               break;
2771             case '3':
2772               n = 2;
2773               break;
2774             case 'w':
2775               n = 2;
2776               break;
2777             case 'l':
2778               n = 4;
2779               break;
2780             default:
2781               as_fatal ("Don't know how to figure width of %c in md_assemble()",
2782                         the_ins.reloc[m].wid);
2783             }
2784
2785           fixP = fix_new_exp (frag_now,
2786                               ((toP - frag_now->fr_literal)
2787                                - the_ins.numo * 2 + the_ins.reloc[m].n),
2788                               n,
2789                               &the_ins.reloc[m].exp,
2790                               the_ins.reloc[m].pcrel,
2791                               NO_RELOC);
2792           fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
2793         }
2794       return;
2795     }
2796
2797   /* There's some frag hacking */
2798   for (n = 0, fromP = &the_ins.opcode[0]; n < the_ins.nfrag; n++)
2799     {
2800       int wid;
2801
2802       if (n == 0)
2803         wid = 2 * the_ins.fragb[n].fragoff;
2804       else
2805         wid = 2 * (the_ins.numo - the_ins.fragb[n - 1].fragoff);
2806       toP = frag_more (wid);
2807       to_beg_P = toP;
2808       shorts_this_frag = 0;
2809       for (m = wid / 2; m; --m)
2810         {
2811           md_number_to_chars (toP, (long) (*fromP), 2);
2812           toP += 2;
2813           fromP++;
2814           shorts_this_frag++;
2815         }
2816       for (m = 0; m < the_ins.nrel; m++)
2817         {
2818           if ((the_ins.reloc[m].n) >= 2 * shorts_this_frag)
2819             {
2820               the_ins.reloc[m].n -= 2 * shorts_this_frag;
2821               break;
2822             }
2823           wid = the_ins.reloc[m].wid;
2824           if (wid == 0)
2825             continue;
2826           the_ins.reloc[m].wid = 0;
2827           wid = (wid == 'b') ? 1 : (wid == 'w') ? 2 : (wid == 'l') ? 4 : 4000;
2828
2829           fixP = fix_new_exp (frag_now,
2830                               ((toP - frag_now->fr_literal)
2831                                - the_ins.numo * 2 + the_ins.reloc[m].n),
2832                               wid,
2833                               &the_ins.reloc[m].exp,
2834                               the_ins.reloc[m].pcrel,
2835                               NO_RELOC);
2836           fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
2837         }
2838       (void) frag_var (rs_machine_dependent, 10, 0,
2839                        (relax_substateT) (the_ins.fragb[n].fragty),
2840                        the_ins.fragb[n].fadd, the_ins.fragb[n].foff, to_beg_P);
2841     }
2842   n = (the_ins.numo - the_ins.fragb[n - 1].fragoff);
2843   shorts_this_frag = 0;
2844   if (n)
2845     {
2846       toP = frag_more (n * sizeof (short));
2847       while (n--)
2848         {
2849           md_number_to_chars (toP, (long) (*fromP), 2);
2850           toP += 2;
2851           fromP++;
2852           shorts_this_frag++;
2853         }
2854     }
2855   for (m = 0; m < the_ins.nrel; m++)
2856     {
2857       int wid;
2858
2859       wid = the_ins.reloc[m].wid;
2860       if (wid == 0)
2861         continue;
2862       the_ins.reloc[m].wid = 0;
2863       wid = (wid == 'b') ? 1 : (wid == 'w') ? 2 : (wid == 'l') ? 4 : 4000;
2864
2865       fixP = fix_new_exp (frag_now,
2866                           ((the_ins.reloc[m].n + toP - frag_now->fr_literal)
2867                            - shorts_this_frag * 2),
2868                           wid,
2869                           &the_ins.reloc[m].exp,
2870                           the_ins.reloc[m].pcrel,
2871                           NO_RELOC);
2872       fixP->fx_pcrel_adjust = the_ins.reloc[m].pcrel_fix;
2873     }
2874 }
2875
2876 void
2877 md_begin ()
2878 {
2879   /*
2880    * md_begin -- set up hash tables with 68000 instructions.
2881    * similar to what the vax assembler does.  ---phr
2882    */
2883   /* RMS claims the thing to do is take the m68k-opcode.h table, and make
2884      a copy of it at runtime, adding in the information we want but isn't
2885      there.  I think it'd be better to have an awk script hack the table
2886      at compile time.  Or even just xstr the table and use it as-is.  But
2887      my lord ghod hath spoken, so we do it this way.  Excuse the ugly var
2888      names.  */
2889
2890   register const struct m68k_opcode *ins;
2891   register struct m68k_incant *hack, *slak;
2892   register const char *retval = 0;      /* empty string, or error msg text */
2893   register unsigned int i;
2894   register char c;
2895
2896   if (flag_mri)
2897     {
2898       flag_reg_prefix_optional = 1;
2899       m68k_abspcadd = 1;
2900       m68k_rel32 = 0;
2901     }
2902
2903   op_hash = hash_new ();
2904
2905   obstack_begin (&robyn, 4000);
2906   for (i = 0; i < m68k_numopcodes; i++)
2907     {
2908       hack = slak = (struct m68k_incant *) obstack_alloc (&robyn, sizeof (struct m68k_incant));
2909       do
2910         {
2911           ins = &m68k_opcodes[i];
2912           /* We *could* ignore insns that don't match our arch here
2913              but just leaving them out of the hash. */
2914           slak->m_operands = ins->args;
2915           slak->m_opnum = strlen (slak->m_operands) / 2;
2916           slak->m_arch = ins->arch;
2917           slak->m_opcode = ins->opcode;
2918           /* This is kludgey */
2919           slak->m_codenum = ((ins->match) & 0xffffL) ? 2 : 1;
2920           if (i + 1 != m68k_numopcodes
2921               && !strcmp (ins->name, m68k_opcodes[i + 1].name))
2922             {
2923               slak->m_next = (struct m68k_incant *) obstack_alloc (&robyn, sizeof (struct m68k_incant));
2924               i++;
2925             }
2926           else
2927             slak->m_next = 0;
2928           slak = slak->m_next;
2929         }
2930       while (slak);
2931
2932       retval = hash_insert (op_hash, ins->name, (char *) hack);
2933       if (retval)
2934         as_fatal ("Internal Error:  Can't hash %s: %s", ins->name, retval);
2935     }
2936
2937   for (i = 0; i < m68k_numaliases; i++)
2938     {
2939       const char *name = m68k_opcode_aliases[i].primary;
2940       const char *alias = m68k_opcode_aliases[i].alias;
2941       PTR val = hash_find (op_hash, name);
2942       if (!val)
2943         as_fatal ("Internal Error: Can't find %s in hash table", name);
2944       retval = hash_insert (op_hash, alias, val);
2945       if (retval)
2946         as_fatal ("Internal Error: Can't hash %s: %s", alias, retval);
2947     }
2948
2949   /* In MRI mode, all unsized branches are variable sized.  Normally,
2950      they are word sized.  */
2951   if (flag_mri)
2952     {
2953       static struct m68k_opcode_alias mri_aliases[] =
2954         {
2955           { "bhi",      "jhi", },
2956           { "bls",      "jls", },
2957           { "bcc",      "jcc", },
2958           { "bcs",      "jcs", },
2959           { "bne",      "jne", },
2960           { "beq",      "jeq", },
2961           { "bvc",      "jvc", },
2962           { "bvs",      "jvs", },
2963           { "bpl",      "jpl", },
2964           { "bmi",      "jmi", },
2965           { "bge",      "jge", },
2966           { "blt",      "jlt", },
2967           { "bgt",      "jgt", },
2968           { "ble",      "jle", },
2969           { "bra",      "jra", },
2970           { "bsr",      "jbsr", },
2971         };
2972
2973       for (i = 0; i < sizeof mri_aliases / sizeof mri_aliases[0]; i++)
2974         {
2975           const char *name = mri_aliases[i].primary;
2976           const char *alias = mri_aliases[i].alias;
2977           PTR val = hash_find (op_hash, name);
2978           if (!val)
2979             as_fatal ("Internal Error: Can't find %s in hash table", name);
2980           retval = hash_jam (op_hash, alias, val);
2981           if (retval)
2982             as_fatal ("Internal Error: Can't hash %s: %s", alias, retval);
2983         }
2984     }
2985
2986   for (i = 0; i < sizeof (mklower_table); i++)
2987     mklower_table[i] = (isupper (c = (char) i)) ? tolower (c) : c;
2988
2989   for (i = 0; i < sizeof (notend_table); i++)
2990     {
2991       notend_table[i] = 0;
2992       alt_notend_table[i] = 0;
2993     }
2994   notend_table[','] = 1;
2995   notend_table['{'] = 1;
2996   notend_table['}'] = 1;
2997   alt_notend_table['a'] = 1;
2998   alt_notend_table['A'] = 1;
2999   alt_notend_table['d'] = 1;
3000   alt_notend_table['D'] = 1;
3001   alt_notend_table['#'] = 1;
3002   alt_notend_table['&'] = 1;
3003   alt_notend_table['f'] = 1;
3004   alt_notend_table['F'] = 1;
3005 #ifdef REGISTER_PREFIX
3006   alt_notend_table[REGISTER_PREFIX] = 1;
3007 #endif
3008
3009   /* We need to put '(' in alt_notend_table to handle
3010        cas2 %d0:%d2,%d3:%d4,(%a0):(%a1)
3011      */
3012   alt_notend_table['('] = 1;
3013
3014   /* We need to put '@' in alt_notend_table to handle
3015        cas2 %d0:%d2,%d3:%d4,@(%d0):@(%d1)
3016      */
3017   alt_notend_table['@'] = 1;
3018
3019 #ifndef MIT_SYNTAX_ONLY
3020   /* Insert pseudo ops, these have to go into the opcode table since
3021      gas expects pseudo ops to start with a dot */
3022   {
3023     int n = 0;
3024     while (mote_pseudo_table[n].poc_name)
3025       {
3026         hack = (struct m68k_incant *)
3027           obstack_alloc (&robyn, sizeof (struct m68k_incant));
3028         hash_insert (op_hash,
3029                      mote_pseudo_table[n].poc_name, (char *) hack);
3030         hack->m_operands = 0;
3031         hack->m_opnum = n;
3032         n++;
3033       }
3034   }
3035 #endif
3036
3037   init_regtable ();
3038 }
3039
3040 void
3041 m68k_init_after_args ()
3042 {
3043   if (cpu_of_arch (current_architecture) == 0)
3044     {
3045       int i;
3046       const char *default_cpu = TARGET_CPU;
3047
3048       if (*default_cpu == 'm')
3049         default_cpu++;
3050       for (i = 0; i < n_archs; i++)
3051         if (strcasecmp (default_cpu, archs[i].name) == 0)
3052           break;
3053       if (i == n_archs)
3054         {
3055           as_bad ("unrecognized default cpu `%s' ???", TARGET_CPU);
3056           current_architecture |= m68020;
3057         }
3058       else
3059         current_architecture |= archs[i].arch;
3060     }
3061   /* Permit m68881 specification with all cpus; those that can't work
3062      with a coprocessor could be doing emulation.  */
3063   if (current_architecture & m68851)
3064     {
3065       if (current_architecture & m68040)
3066         {
3067           as_warn ("68040 and 68851 specified; mmu instructions may assemble incorrectly");
3068         }
3069     }
3070   /* What other incompatibilities could we check for?  */
3071
3072   /* Toss in some default assumptions about coprocessors.  */
3073   if (!no_68881
3074       && (cpu_of_arch (current_architecture)
3075           /* Can CPU32 have a 68881 coprocessor??  */
3076           & (m68020 | m68030 | cpu32)))
3077     {
3078       current_architecture |= m68881;
3079     }
3080   if (!no_68851
3081       && (cpu_of_arch (current_architecture) & m68020up) != 0
3082       && (cpu_of_arch (current_architecture) & m68040up) == 0)
3083     {
3084       current_architecture |= m68851;
3085     }
3086   if (no_68881 && (current_architecture & m68881))
3087     as_bad ("options for 68881 and no-68881 both given");
3088   if (no_68851 && (current_architecture & m68851))
3089     as_bad ("options for 68851 and no-68851 both given");
3090
3091 #ifdef OBJ_AOUT
3092   /* Work out the magic number.  This isn't very general.  */
3093   if (current_architecture & m68000)
3094     m68k_aout_machtype = 0;
3095   else if (current_architecture & m68010)
3096     m68k_aout_machtype = 1;
3097   else if (current_architecture & m68020)
3098     m68k_aout_machtype = 2;
3099   else
3100     m68k_aout_machtype = 2;
3101 #endif
3102
3103   /* Note which set of "movec" control registers is available.  */
3104   switch (cpu_of_arch (current_architecture))
3105     {
3106     case m68000:
3107       control_regs = m68000_control_regs;
3108       break;
3109     case m68010:
3110       control_regs = m68010_control_regs;
3111       break;
3112     case m68020:
3113     case m68030:
3114       control_regs = m68020_control_regs;
3115       break;
3116     case m68040:
3117       control_regs = m68040_control_regs;
3118       break;
3119     case m68060:
3120       control_regs = m68060_control_regs;
3121       break;
3122     case cpu32:
3123       control_regs = cpu32_control_regs;
3124       break;
3125     default:
3126       abort ();
3127     }
3128
3129   if (cpu_of_arch (current_architecture) < m68020)
3130     md_relax_table[TAB (PCINDEX, BYTE)].rlx_more = 0;
3131 }
3132
3133 /* Equal to MAX_PRECISION in atof-ieee.c */
3134 #define MAX_LITTLENUMS 6
3135
3136 /* Turn a string in input_line_pointer into a floating point constant
3137    of type type, and store the appropriate bytes in *litP.  The number
3138    of LITTLENUMS emitted is stored in *sizeP .  An error message is
3139    returned, or NULL on OK.  */
3140
3141 char *
3142 md_atof (type, litP, sizeP)
3143      char type;
3144      char *litP;
3145      int *sizeP;
3146 {
3147   int prec;
3148   LITTLENUM_TYPE words[MAX_LITTLENUMS];
3149   LITTLENUM_TYPE *wordP;
3150   char *t;
3151   char *atof_ieee ();
3152
3153   switch (type)
3154     {
3155     case 'f':
3156     case 'F':
3157     case 's':
3158     case 'S':
3159       prec = 2;
3160       break;
3161
3162     case 'd':
3163     case 'D':
3164     case 'r':
3165     case 'R':
3166       prec = 4;
3167       break;
3168
3169     case 'x':
3170     case 'X':
3171       prec = 6;
3172       break;
3173
3174     case 'p':
3175     case 'P':
3176       prec = 6;
3177       break;
3178
3179     default:
3180       *sizeP = 0;
3181       return "Bad call to MD_ATOF()";
3182     }
3183   t = atof_ieee (input_line_pointer, type, words);
3184   if (t)
3185     input_line_pointer = t;
3186
3187   *sizeP = prec * sizeof (LITTLENUM_TYPE);
3188   for (wordP = words; prec--;)
3189     {
3190       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
3191       litP += sizeof (LITTLENUM_TYPE);
3192     }
3193   return 0;
3194 }
3195
3196 void
3197 md_number_to_chars (buf, val, n)
3198      char *buf;
3199      valueT val;
3200      int n;
3201 {
3202   number_to_chars_bigendian (buf, val, n);
3203 }
3204
3205 static void
3206 md_apply_fix_2 (fixP, val)
3207      fixS *fixP;
3208      offsetT val;
3209 {
3210   addressT upper_limit;
3211   offsetT lower_limit;
3212
3213   /* This is unnecessary but it convinces the native rs6000 compiler
3214      to generate the code we want.  */
3215   char *buf = fixP->fx_frag->fr_literal;
3216   buf += fixP->fx_where;
3217   /* end ibm compiler workaround */
3218
3219   if (val & 0x80000000)
3220     val |= ~(addressT)0x7fffffff;
3221   else
3222     val &= 0x7fffffff;
3223
3224   switch (fixP->fx_size)
3225     {
3226       /* The cast to offsetT below are necessary to make code correct for
3227          machines where ints are smaller than offsetT */
3228     case 1:
3229       *buf++ = val;
3230       upper_limit = 0x7f;
3231       lower_limit = - (offsetT) 0x80;
3232       break;
3233     case 2:
3234       *buf++ = (val >> 8);
3235       *buf++ = val;
3236       upper_limit = 0x7fff;
3237       lower_limit = - (offsetT) 0x8000;
3238       break;
3239     case 4:
3240       *buf++ = (val >> 24);
3241       *buf++ = (val >> 16);
3242       *buf++ = (val >> 8);
3243       *buf++ = val;
3244       upper_limit = 0x7fffffff;
3245       lower_limit = - (offsetT) 0x7fffffff - 1; /* avoid constant overflow */
3246       break;
3247     default:
3248       BAD_CASE (fixP->fx_size);
3249     }
3250
3251   /* For non-pc-relative values, it's conceivable we might get something
3252      like "0xff" for a byte field.  So extend the upper part of the range
3253      to accept such numbers.  We arbitrarily disallow "-0xff" or "0xff+0xff",
3254      so that we can do any range checking at all.  */
3255   if (!fixP->fx_pcrel)
3256     upper_limit = upper_limit * 2 + 1;
3257
3258   if ((addressT) val > upper_limit
3259       && (val > 0 || val < lower_limit))
3260     as_bad_where (fixP->fx_file, fixP->fx_line, "value out of range");
3261
3262   /* A one byte PC-relative reloc means a short branch.  We can't use
3263      a short branch with a value of 0 or -1, because those indicate
3264      different opcodes (branches with longer offsets).  */
3265   if (fixP->fx_pcrel
3266       && fixP->fx_size == 1
3267       && (fixP->fx_addsy == NULL
3268           || S_IS_DEFINED (fixP->fx_addsy))
3269       && (val == 0 || val == -1))
3270     as_bad_where (fixP->fx_file, fixP->fx_line, "invalid byte branch offset");
3271 }
3272
3273 #ifdef BFD_ASSEMBLER
3274 int
3275 md_apply_fix (fixP, valp)
3276      fixS *fixP;
3277      valueT *valp;
3278 {
3279   md_apply_fix_2 (fixP, (addressT) *valp);
3280   return 1;
3281 }
3282 #else
3283 void md_apply_fix (fixP, val)
3284      fixS *fixP;
3285      long val;
3286 {
3287   md_apply_fix_2 (fixP, (addressT) val);
3288 }
3289 #endif
3290
3291 /* *fragP has been relaxed to its final size, and now needs to have
3292    the bytes inside it modified to conform to the new size  There is UGLY
3293    MAGIC here. ..
3294    */
3295 void
3296 md_convert_frag_1 (fragP)
3297      register fragS *fragP;
3298 {
3299   long disp;
3300   long ext = 0;
3301   fixS *fixP;
3302
3303   /* Address in object code of the displacement.  */
3304   register int object_address = fragP->fr_fix + fragP->fr_address;
3305
3306   /* Address in gas core of the place to store the displacement.  */
3307   /* This convinces the native rs6000 compiler to generate the code we
3308      want. */
3309   register char *buffer_address = fragP->fr_literal;
3310   buffer_address += fragP->fr_fix;
3311   /* end ibm compiler workaround */
3312
3313   /* The displacement of the address, from current location.  */
3314   disp = fragP->fr_symbol ? S_GET_VALUE (fragP->fr_symbol) : 0;
3315   disp = (disp + fragP->fr_offset) - object_address;
3316
3317 #ifdef BFD_ASSEMBLER
3318   disp += fragP->fr_symbol->sy_frag->fr_address;
3319 #endif
3320
3321   switch (fragP->fr_subtype)
3322     {
3323     case TAB (BCC68000, BYTE):
3324     case TAB (ABRANCH, BYTE):
3325       know (issbyte (disp));
3326       if (disp == 0)
3327         as_bad ("short branch with zero offset: use :w");
3328       fragP->fr_opcode[1] = disp;
3329       ext = 0;
3330       break;
3331     case TAB (DBCC, SHORT):
3332       know (issword (disp));
3333       ext = 2;
3334       break;
3335     case TAB (BCC68000, SHORT):
3336     case TAB (ABRANCH, SHORT):
3337       know (issword (disp));
3338       fragP->fr_opcode[1] = 0x00;
3339       ext = 2;
3340       break;
3341     case TAB (ABRANCH, LONG):
3342       if (cpu_of_arch (current_architecture) < m68020)
3343         {
3344           if (fragP->fr_opcode[0] == 0x61)
3345             /* BSR */
3346             {
3347               fragP->fr_opcode[0] = 0x4E;
3348               fragP->fr_opcode[1] = (char) 0xB9; /* JBSR with ABSL LONG offset */
3349               subseg_change (text_section, 0); /* @@ */
3350
3351               fix_new (fragP,
3352                        fragP->fr_fix,
3353                        4,
3354                        fragP->fr_symbol,
3355                        fragP->fr_offset,
3356                        0,
3357                        NO_RELOC);
3358
3359               fragP->fr_fix += 4;
3360               ext = 0;
3361             }
3362           /* BRA */
3363           else if (fragP->fr_opcode[0] == 0x60)
3364             {
3365               fragP->fr_opcode[0] = 0x4E;
3366               fragP->fr_opcode[1] = (char) 0xF9; /* JMP  with ABSL LONG offset */
3367               subseg_change (text_section, 0); /* @@ */
3368               fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3369                        fragP->fr_offset, 0, NO_RELOC);
3370               fragP->fr_fix += 4;
3371               ext = 0;
3372             }
3373           else
3374             {
3375               as_bad ("Long branch offset not supported.");
3376             }
3377         }
3378       else
3379         {
3380           fragP->fr_opcode[1] = (char) 0xff;
3381           ext = 4;
3382         }
3383       break;
3384     case TAB (BCC68000, LONG):
3385       /* only Bcc 68000 instructions can come here */
3386       /* change bcc into b!cc/jmp absl long */
3387       fragP->fr_opcode[0] ^= 0x01;      /* invert bcc */
3388       fragP->fr_opcode[1] = 0x6;/* branch offset = 6 */
3389
3390       /* JF: these used to be fr_opcode[2,3], but they may be in a
3391            different frag, in which case refering to them is a no-no.
3392            Only fr_opcode[0,1] are guaranteed to work. */
3393       *buffer_address++ = 0x4e; /* put in jmp long (0x4ef9) */
3394       *buffer_address++ = (char) 0xf9;
3395       fragP->fr_fix += 2;       /* account for jmp instruction */
3396       subseg_change (text_section, 0);
3397       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3398                fragP->fr_offset, 0, NO_RELOC);
3399       fragP->fr_fix += 4;
3400       ext = 0;
3401       break;
3402     case TAB (DBCC, LONG):
3403       /* only DBcc 68000 instructions can come here */
3404       /* change dbcc into dbcc/jmp absl long */
3405       /* JF: these used to be fr_opcode[2-7], but that's wrong */
3406       *buffer_address++ = 0x00; /* branch offset = 4 */
3407       *buffer_address++ = 0x04;
3408       *buffer_address++ = 0x60; /* put in bra pc+6 */
3409       *buffer_address++ = 0x06;
3410       *buffer_address++ = 0x4e; /* put in jmp long (0x4ef9) */
3411       *buffer_address++ = (char) 0xf9;
3412
3413       fragP->fr_fix += 6;       /* account for bra/jmp instructions */
3414       subseg_change (text_section, 0);
3415       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3416                fragP->fr_offset, 0, NO_RELOC);
3417       fragP->fr_fix += 4;
3418       ext = 0;
3419       break;
3420     case TAB (FBRANCH, SHORT):
3421       know ((fragP->fr_opcode[1] & 0x40) == 0);
3422       ext = 2;
3423       break;
3424     case TAB (FBRANCH, LONG):
3425       fragP->fr_opcode[1] |= 0x40;      /* Turn on LONG bit */
3426       ext = 4;
3427       break;
3428     case TAB (PCREL, SHORT):
3429       ext = 2;
3430       break;
3431     case TAB (PCREL, LONG):
3432       /* The thing to do here is force it to ABSOLUTE LONG, since
3433         PCREL is really trying to shorten an ABSOLUTE address anyway */
3434       /* JF FOO This code has not been tested */
3435       subseg_change (text_section, 0);
3436       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol, fragP->fr_offset,
3437                0, NO_RELOC);
3438       if ((fragP->fr_opcode[1] & 0x3F) != 0x3A)
3439         as_bad ("Internal error (long PC-relative operand) for insn 0x%04x at 0x%lx",
3440                 (unsigned) fragP->fr_opcode[0],
3441                 (unsigned long) fragP->fr_address);
3442       fragP->fr_opcode[1] &= ~0x3F;
3443       fragP->fr_opcode[1] |= 0x39;      /* Mode 7.1 */
3444       fragP->fr_fix += 4;
3445       ext = 0;
3446       break;
3447     case TAB (PCLEA, SHORT):
3448       subseg_change (text_section, 0);
3449       fix_new (fragP, (int) (fragP->fr_fix), 2, fragP->fr_symbol,
3450                fragP->fr_offset, 1, NO_RELOC);
3451       fragP->fr_opcode[1] &= ~0x3F;
3452       fragP->fr_opcode[1] |= 0x3A; /* 072 - mode 7.2 */
3453       ext = 2;
3454       break;
3455     case TAB (PCLEA, LONG):
3456       subseg_change (text_section, 0);
3457       fixP = fix_new (fragP, (int) (fragP->fr_fix) + 2, 4, fragP->fr_symbol,
3458                       fragP->fr_offset, 1, NO_RELOC);
3459       fixP->fx_pcrel_adjust = 2;
3460       /* Already set to mode 7.3; this indicates: PC indirect with
3461          suppressed index, 32-bit displacement.  */
3462       *buffer_address++ = 0x01;
3463       *buffer_address++ = 0x70;
3464       fragP->fr_fix += 2;
3465       ext = 4;
3466       break;
3467
3468     case TAB (PCINDEX, BYTE):
3469       disp += 2;
3470       if (!issbyte (disp))
3471         {
3472           as_bad ("displacement doesn't fit in one byte");
3473           disp = 0;
3474         }
3475       assert (fragP->fr_fix >= 2);
3476       buffer_address[-2] &= ~1;
3477       buffer_address[-1] = disp;
3478       ext = 0;
3479       break;
3480     case TAB (PCINDEX, SHORT):
3481       subseg_change (text_section, 0);
3482       disp += 2;
3483       assert (issword (disp));
3484       assert (fragP->fr_fix >= 2);
3485       buffer_address[-2] |= 0x1;
3486       buffer_address[-1] = 0x20;
3487       fixP = fix_new (fragP, (int) (fragP->fr_fix), 2, fragP->fr_symbol,
3488                       fragP->fr_offset, (fragP->fr_opcode[1] & 077) == 073,
3489                       NO_RELOC);
3490       fixP->fx_pcrel_adjust = 2;
3491       ext = 2;
3492       break;
3493     case TAB (PCINDEX, LONG):
3494       subseg_change (text_section, 0);
3495       disp += 2;
3496       fixP = fix_new (fragP, (int) (fragP->fr_fix), 4, fragP->fr_symbol,
3497                       fragP->fr_offset, (fragP->fr_opcode[1] & 077) == 073,
3498                       NO_RELOC);
3499       fixP->fx_pcrel_adjust = 2;
3500       assert (fragP->fr_fix >= 2);
3501       buffer_address[-2] |= 0x1;
3502       buffer_address[-1] = 0x30;
3503       ext = 4;
3504       break;
3505     }
3506
3507   if (ext)
3508     {
3509       md_number_to_chars (buffer_address, (long) disp, (int) ext);
3510       fragP->fr_fix += ext;
3511     }
3512 }
3513
3514 #ifndef BFD_ASSEMBLER
3515
3516 void
3517 md_convert_frag (headers, sec, fragP)
3518      object_headers *headers;
3519      segT sec;
3520      fragS *fragP;
3521 {
3522   md_convert_frag_1 (fragP);
3523 }
3524
3525 #else
3526
3527 void
3528 md_convert_frag (abfd, sec, fragP)
3529      bfd *abfd;
3530      segT sec;
3531      fragS *fragP;
3532 {
3533   md_convert_frag_1 (fragP);
3534 }
3535 #endif
3536
3537 /* Force truly undefined symbols to their maximum size, and generally set up
3538    the frag list to be relaxed
3539    */
3540 int
3541 md_estimate_size_before_relax (fragP, segment)
3542      register fragS *fragP;
3543      segT segment;
3544 {
3545   int old_fix;
3546   register char *buffer_address = fragP->fr_fix + fragP->fr_literal;
3547
3548   old_fix = fragP->fr_fix;
3549
3550   /* handle SZ_UNDEF first, it can be changed to BYTE or SHORT */
3551   switch (fragP->fr_subtype)
3552     {
3553
3554     case TAB (ABRANCH, SZ_UNDEF):
3555       {
3556         if ((fragP->fr_symbol != NULL)  /* Not absolute */
3557             && S_GET_SEGMENT (fragP->fr_symbol) == segment)
3558           {
3559             fragP->fr_subtype = TAB (TABTYPE (fragP->fr_subtype), BYTE);
3560             break;
3561           }
3562         else if ((fragP->fr_symbol == 0) || (cpu_of_arch (current_architecture) < m68020))
3563           {
3564             /* On 68000, or for absolute value, switch to abs long */
3565             /* FIXME, we should check abs val, pick short or long */
3566             if (fragP->fr_opcode[0] == 0x61)
3567               {
3568                 fragP->fr_opcode[0] = 0x4E;
3569                 fragP->fr_opcode[1] = (char) 0xB9; /* JBSR with ABSL LONG offset */
3570                 subseg_change (text_section, 0);
3571                 fix_new (fragP, fragP->fr_fix, 4,
3572                          fragP->fr_symbol, fragP->fr_offset, 0, NO_RELOC);
3573                 fragP->fr_fix += 4;
3574                 frag_wane (fragP);
3575               }
3576             else if (fragP->fr_opcode[0] == 0x60)
3577               {
3578                 fragP->fr_opcode[0] = 0x4E;
3579                 fragP->fr_opcode[1] = (char) 0xF9; /* JMP  with ABSL LONG offset */
3580                 subseg_change (text_section, 0);
3581                 fix_new (fragP, fragP->fr_fix, 4,
3582                          fragP->fr_symbol, fragP->fr_offset, 0, NO_RELOC);
3583                 fragP->fr_fix += 4;
3584                 frag_wane (fragP);
3585               }
3586             else
3587               {
3588                 as_warn ("Long branch offset to extern symbol not supported.");
3589               }
3590           }
3591         else
3592           {                     /* Symbol is still undefined.  Make it simple */
3593             fix_new (fragP, (int) (fragP->fr_fix), 4, fragP->fr_symbol,
3594                      fragP->fr_offset, 1, NO_RELOC);
3595             fragP->fr_fix += 4;
3596             fragP->fr_opcode[1] = (char) 0xff;
3597             frag_wane (fragP);
3598             break;
3599           }
3600
3601         break;
3602       }                         /* case TAB(ABRANCH,SZ_UNDEF) */
3603
3604     case TAB (FBRANCH, SZ_UNDEF):
3605       {
3606         if (S_GET_SEGMENT (fragP->fr_symbol) == segment || flag_short_refs)
3607           {
3608             fragP->fr_subtype = TAB (FBRANCH, SHORT);
3609             fragP->fr_var += 2;
3610           }
3611         else
3612           {
3613             fix_new (fragP, (int) fragP->fr_fix, 4, fragP->fr_symbol,
3614                      fragP->fr_offset, 1, NO_RELOC);
3615             fragP->fr_fix += 4;
3616             fragP->fr_opcode[1] |= 0x40; /* Turn on LONG bit */
3617             frag_wane (fragP);
3618           }
3619         break;
3620       }                         /* TAB(FBRANCH,SZ_UNDEF) */
3621
3622     case TAB (PCREL, SZ_UNDEF):
3623       {
3624         if (S_GET_SEGMENT (fragP->fr_symbol) == segment
3625             || flag_short_refs
3626             || cpu_of_arch (current_architecture) < m68020)
3627           {
3628             fragP->fr_subtype = TAB (PCREL, SHORT);
3629             fragP->fr_var += 2;
3630           }
3631         else
3632           {
3633             fragP->fr_subtype = TAB (PCREL, LONG);
3634             fragP->fr_var += 4;
3635           }
3636         break;
3637       }                         /* TAB(PCREL,SZ_UNDEF) */
3638
3639     case TAB (BCC68000, SZ_UNDEF):
3640       {
3641         if ((fragP->fr_symbol != NULL)
3642             && S_GET_SEGMENT (fragP->fr_symbol) == segment)
3643           {
3644             fragP->fr_subtype = TAB (BCC68000, BYTE);
3645             break;
3646           }
3647         /* only Bcc 68000 instructions can come here */
3648         /* change bcc into b!cc/jmp absl long */
3649         fragP->fr_opcode[0] ^= 0x01;    /* invert bcc */
3650         if (flag_short_refs)
3651           {
3652             fragP->fr_opcode[1] = 0x04; /* branch offset = 6 */
3653             /* JF: these were fr_opcode[2,3] */
3654             buffer_address[0] = 0x4e;   /* put in jmp long (0x4ef9) */
3655             buffer_address[1] = (char) 0xf8;
3656             fragP->fr_fix += 2; /* account for jmp instruction */
3657             subseg_change (text_section, 0);
3658             fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3659                      fragP->fr_offset, 0, NO_RELOC);
3660             fragP->fr_fix += 2;
3661           }
3662         else
3663           {
3664             fragP->fr_opcode[1] = 0x06; /* branch offset = 6 */
3665             /* JF: these were fr_opcode[2,3] */
3666             buffer_address[0] = 0x4e;   /* put in jmp long (0x4ef9) */
3667             buffer_address[1] = (char) 0xf9;
3668             fragP->fr_fix += 2; /* account for jmp instruction */
3669             subseg_change (text_section, 0);
3670             fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3671                      fragP->fr_offset, 0, NO_RELOC);
3672             fragP->fr_fix += 4;
3673           }
3674         frag_wane (fragP);
3675         break;
3676       }                         /* case TAB(BCC68000,SZ_UNDEF) */
3677
3678     case TAB (DBCC, SZ_UNDEF):
3679       {
3680         if (fragP->fr_symbol != NULL && S_GET_SEGMENT (fragP->fr_symbol) == segment)
3681           {
3682             fragP->fr_subtype = TAB (DBCC, SHORT);
3683             fragP->fr_var += 2;
3684             break;
3685           }
3686         /* only DBcc 68000 instructions can come here */
3687         /* change dbcc into dbcc/jmp absl long */
3688         /* JF: these used to be fr_opcode[2-4], which is wrong. */
3689         buffer_address[0] = 0x00;       /* branch offset = 4 */
3690         buffer_address[1] = 0x04;
3691         buffer_address[2] = 0x60;       /* put in bra pc + ... */
3692
3693         if (flag_short_refs)
3694           {
3695             /* JF: these were fr_opcode[5-7] */
3696             buffer_address[3] = 0x04;   /* plus 4 */
3697             buffer_address[4] = 0x4e;   /* Put in Jump Word */
3698             buffer_address[5] = (char) 0xf8;
3699             fragP->fr_fix += 6; /* account for bra/jmp instruction */
3700             subseg_change (text_section, 0);
3701             fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3702                      fragP->fr_offset, 0, NO_RELOC);
3703             fragP->fr_fix += 2;
3704           }
3705         else
3706           {
3707             /* JF: these were fr_opcode[5-7] */
3708             buffer_address[3] = 0x06;   /* Plus 6 */
3709             buffer_address[4] = 0x4e;   /* put in jmp long (0x4ef9) */
3710             buffer_address[5] = (char) 0xf9;
3711             fragP->fr_fix += 6; /* account for bra/jmp instruction */
3712             subseg_change (text_section, 0);
3713             fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
3714                      fragP->fr_offset, 0, NO_RELOC);
3715             fragP->fr_fix += 4;
3716           }
3717
3718         frag_wane (fragP);
3719         break;
3720       }                         /* case TAB(DBCC,SZ_UNDEF) */
3721
3722     case TAB (PCLEA, SZ_UNDEF):
3723       {
3724         if ((S_GET_SEGMENT (fragP->fr_symbol)) == segment
3725             || flag_short_refs
3726             || cpu_of_arch (current_architecture) < m68020)
3727           {
3728             fragP->fr_subtype = TAB (PCLEA, SHORT);
3729             fragP->fr_var += 2;
3730           }
3731         else
3732           {
3733             fragP->fr_subtype = TAB (PCLEA, LONG);
3734             fragP->fr_var += 6;
3735           }
3736         break;
3737       }                         /* TAB(PCLEA,SZ_UNDEF) */
3738
3739     case TAB (PCINDEX, SZ_UNDEF):
3740       if (S_GET_SEGMENT (fragP->fr_symbol) == segment
3741           || cpu_of_arch (current_architecture) < m68020)
3742         {
3743           fragP->fr_subtype = TAB (PCINDEX, BYTE);
3744         }
3745       else
3746         {
3747           fragP->fr_subtype = TAB (PCINDEX, LONG);
3748           fragP->fr_var += 4;
3749         }
3750       break;
3751
3752     default:
3753       break;
3754     }
3755
3756   /* now that SZ_UNDEF are taken care of, check others */
3757   switch (fragP->fr_subtype)
3758     {
3759     case TAB (BCC68000, BYTE):
3760     case TAB (ABRANCH, BYTE):
3761       /* We can't do a short jump to the next instruction,
3762            so we force word mode.  */
3763       if (fragP->fr_symbol && S_GET_VALUE (fragP->fr_symbol) == 0 &&
3764           fragP->fr_symbol->sy_frag == fragP->fr_next)
3765         {
3766           fragP->fr_subtype = TAB (TABTYPE (fragP->fr_subtype), SHORT);
3767           fragP->fr_var += 2;
3768         }
3769       break;
3770     default:
3771       break;
3772     }
3773   return fragP->fr_var + fragP->fr_fix - old_fix;
3774 }
3775
3776 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
3777 /* the bit-field entries in the relocation_info struct plays hell
3778    with the byte-order problems of cross-assembly.  So as a hack,
3779    I added this mach. dependent ri twiddler.  Ugly, but it gets
3780    you there. -KWK */
3781 /* on m68k: first 4 bytes are normal unsigned long, next three bytes
3782    are symbolnum, most sig. byte first.  Last byte is broken up with
3783    bit 7 as pcrel, bits 6 & 5 as length, bit 4 as pcrel, and the lower
3784    nibble as nuthin. (on Sun 3 at least) */
3785 /* Translate the internal relocation information into target-specific
3786    format. */
3787 #ifdef comment
3788 void
3789 md_ri_to_chars (the_bytes, ri)
3790      char *the_bytes;
3791      struct reloc_info_generic *ri;
3792 {
3793   /* this is easy */
3794   md_number_to_chars (the_bytes, ri->r_address, 4);
3795   /* now the fun stuff */
3796   the_bytes[4] = (ri->r_symbolnum >> 16) & 0x0ff;
3797   the_bytes[5] = (ri->r_symbolnum >> 8) & 0x0ff;
3798   the_bytes[6] = ri->r_symbolnum & 0x0ff;
3799   the_bytes[7] = (((ri->r_pcrel << 7) & 0x80) | ((ri->r_length << 5) & 0x60) |
3800                   ((ri->r_extern << 4) & 0x10));
3801 }
3802
3803 #endif /* comment */
3804
3805 #ifndef BFD_ASSEMBLER
3806 void
3807 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
3808      char *where;
3809      fixS *fixP;
3810      relax_addressT segment_address_in_file;
3811 {
3812   /*
3813    * In: length of relocation (or of address) in chars: 1, 2 or 4.
3814    * Out: GNU LD relocation length code: 0, 1, or 2.
3815    */
3816
3817   static CONST unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
3818   long r_symbolnum;
3819
3820   know (fixP->fx_addsy != NULL);
3821
3822   md_number_to_chars (where,
3823        fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
3824                       4);
3825
3826   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
3827                  ? S_GET_TYPE (fixP->fx_addsy)
3828                  : fixP->fx_addsy->sy_number);
3829
3830   where[4] = (r_symbolnum >> 16) & 0x0ff;
3831   where[5] = (r_symbolnum >> 8) & 0x0ff;
3832   where[6] = r_symbolnum & 0x0ff;
3833   where[7] = (((fixP->fx_pcrel << 7) & 0x80) | ((nbytes_r_length[fixP->fx_size] << 5) & 0x60) |
3834               (((!S_IS_DEFINED (fixP->fx_addsy)) << 4) & 0x10));
3835 }
3836 #endif
3837
3838 #endif /* OBJ_AOUT or OBJ_BOUT */
3839
3840 #ifndef WORKING_DOT_WORD
3841 CONST int md_short_jump_size = 4;
3842 CONST int md_long_jump_size = 6;
3843
3844 void
3845 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
3846      char *ptr;
3847      addressT from_addr, to_addr;
3848      fragS *frag;
3849      symbolS *to_symbol;
3850 {
3851   valueT offset;
3852
3853   offset = to_addr - (from_addr + 2);
3854
3855   md_number_to_chars (ptr, (valueT) 0x6000, 2);
3856   md_number_to_chars (ptr + 2, (valueT) offset, 2);
3857 }
3858
3859 void
3860 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
3861      char *ptr;
3862      addressT from_addr, to_addr;
3863      fragS *frag;
3864      symbolS *to_symbol;
3865 {
3866   valueT offset;
3867
3868   if (cpu_of_arch (current_architecture) < m68020)
3869     {
3870       offset = to_addr - S_GET_VALUE (to_symbol);
3871       md_number_to_chars (ptr, (valueT) 0x4EF9, 2);
3872       md_number_to_chars (ptr + 2, (valueT) offset, 4);
3873       fix_new (frag, (ptr + 2) - frag->fr_literal, 4, to_symbol, (offsetT) 0,
3874                0, NO_RELOC);
3875     }
3876   else
3877     {
3878       offset = to_addr - (from_addr + 2);
3879       md_number_to_chars (ptr, (valueT) 0x60ff, 2);
3880       md_number_to_chars (ptr + 2, (valueT) offset, 4);
3881     }
3882 }
3883
3884 #endif
3885
3886 /* Different values of OK tell what its OK to return.  Things that
3887    aren't OK are an error (what a shock, no?)
3888
3889    0:  Everything is OK
3890    10:  Absolute 1:8    only
3891    20:  Absolute 0:7    only
3892    30:  absolute 0:15   only
3893    40:  Absolute 0:31   only
3894    50:  absolute 0:127  only
3895    55:  absolute -64:63    only
3896    60:  absolute -128:127       only
3897    70:  absolute 0:4095 only
3898    80:  No bignums
3899
3900    */
3901
3902 static int
3903 get_num (exp, ok)
3904      struct m68k_exp *exp;
3905      int ok;
3906 {
3907   if (exp->exp.X_op == O_absent)
3908     {
3909       /* Do the same thing the VAX asm does */
3910       op (exp) = O_constant;
3911       adds (exp) = 0;
3912       subs (exp) = 0;
3913       offs (exp) = 0;
3914       if (ok == 10)
3915         {
3916           as_warn ("expression out of range: defaulting to 1");
3917           offs (exp) = 1;
3918         }
3919     }
3920   else if (exp->exp.X_op == O_constant)
3921     {
3922       switch (ok)
3923         {
3924         case 10:
3925           if (offs (exp) < 1 || offs (exp) > 8)
3926             {
3927               as_warn ("expression out of range: defaulting to 1");
3928               offs (exp) = 1;
3929             }
3930           break;
3931         case 20:
3932           if (offs (exp) < 0 || offs (exp) > 7)
3933             goto outrange;
3934           break;
3935         case 30:
3936           if (offs (exp) < 0 || offs (exp) > 15)
3937             goto outrange;
3938           break;
3939         case 40:
3940           if (offs (exp) < 0 || offs (exp) > 32)
3941             goto outrange;
3942           break;
3943         case 50:
3944           if (offs (exp) < 0 || offs (exp) > 127)
3945             goto outrange;
3946           break;
3947         case 55:
3948           if (offs (exp) < -64 || offs (exp) > 63)
3949             goto outrange;
3950           break;
3951         case 60:
3952           if (offs (exp) < -128 || offs (exp) > 127)
3953             goto outrange;
3954           break;
3955         case 70:
3956           if (offs (exp) < 0 || offs (exp) > 4095)
3957             {
3958             outrange:
3959               as_warn ("expression out of range: defaulting to 0");
3960               offs (exp) = 0;
3961             }
3962           break;
3963         default:
3964           break;
3965         }
3966     }
3967   else if (exp->exp.X_op == O_big)
3968     {
3969       if (offs (exp) <= 0       /* flonum */
3970           && (ok == 80          /* no bignums */
3971               || (ok > 10       /* small-int ranges including 0 ok */
3972                   /* If we have a flonum zero, a zero integer should
3973                      do as well (e.g., in moveq).  */
3974                   && generic_floating_point_number.exponent == 0
3975                   && generic_floating_point_number.low[0] == 0)))
3976         {
3977           /* HACK! Turn it into a long */
3978           LITTLENUM_TYPE words[6];
3979
3980           gen_to_words (words, 2, 8L);  /* These numbers are magic! */
3981           op (exp) = O_constant;
3982           adds (exp) = 0;
3983           subs (exp) = 0;
3984           offs (exp) = words[1] | (words[0] << 16);
3985         }
3986       else if (ok != 0)
3987         {
3988           op (exp) = O_constant;
3989           adds (exp) = 0;
3990           subs (exp) = 0;
3991           offs (exp) = (ok == 10) ? 1 : 0;
3992           as_warn ("Can't deal with expression; defaulting to %ld",
3993                    offs (exp));
3994         }
3995     }
3996   else
3997     {
3998       if (ok >= 10 && ok <= 70)
3999         {
4000           op (exp) = O_constant;
4001           adds (exp) = 0;
4002           subs (exp) = 0;
4003           offs (exp) = (ok == 10) ? 1 : 0;
4004           as_warn ("Can't deal with expression; defaulting to %ld",
4005                    offs (exp));
4006         }
4007     }
4008
4009   if (exp->size != SIZE_UNSPEC)
4010     {
4011       switch (exp->size)
4012         {
4013         case SIZE_UNSPEC:
4014         case SIZE_LONG:
4015           break;
4016         case SIZE_BYTE:
4017           if (!isbyte (offs (exp)))
4018             as_warn ("expression doesn't fit in BYTE");
4019           break;
4020         case SIZE_WORD:
4021           if (!isword (offs (exp)))
4022             as_warn ("expression doesn't fit in WORD");
4023           break;
4024         }
4025     }
4026
4027   return offs (exp);
4028 }
4029
4030 /* These are the back-ends for the various machine dependent pseudo-ops.  */
4031 void demand_empty_rest_of_line ();      /* Hate those extra verbose names */
4032
4033 static void
4034 s_data1 (ignore)
4035      int ignore;
4036 {
4037   subseg_set (data_section, 1);
4038   demand_empty_rest_of_line ();
4039 }
4040
4041 static void
4042 s_data2 (ignore)
4043      int ignore;
4044 {
4045   subseg_set (data_section, 2);
4046   demand_empty_rest_of_line ();
4047 }
4048
4049 static void
4050 s_bss (ignore)
4051      int ignore;
4052 {
4053   /* We don't support putting frags in the BSS segment, we fake it
4054      by marking in_bss, then looking at s_skip for clues.  */
4055
4056   subseg_set (bss_section, 0);
4057   demand_empty_rest_of_line ();
4058 }
4059
4060 static void
4061 s_even (ignore)
4062      int ignore;
4063 {
4064   register int temp;
4065   register long temp_fill;
4066
4067   temp = 1;                     /* JF should be 2? */
4068   temp_fill = get_absolute_expression ();
4069   if (!need_pass_2)             /* Never make frag if expect extra pass. */
4070     frag_align (temp, (int) temp_fill);
4071   demand_empty_rest_of_line ();
4072 }
4073
4074 static void
4075 s_proc (ignore)
4076      int ignore;
4077 {
4078   demand_empty_rest_of_line ();
4079 }
4080 \f
4081 /* Pseudo-ops handled for MRI compatibility.  */
4082
4083 /* Handle an MRI style chip specification.  */
4084
4085 static void
4086 mri_chip ()
4087 {
4088   char *s;
4089   char c;
4090   int i;
4091
4092   s = input_line_pointer;
4093   c = get_symbol_end ();
4094   for (i = 0; i < n_archs; i++)
4095     if (strcasecmp (s, archs[i].name) == 0)
4096       break;
4097   if (i >= n_archs)
4098     {
4099       as_bad ("%s: unrecognized processor name", s);
4100       *input_line_pointer = c;
4101       ignore_rest_of_line ();
4102       return;
4103     }
4104   *input_line_pointer = c;
4105
4106   if (*input_line_pointer == '/')
4107     current_architecture = 0;
4108   else
4109     current_architecture &= m68881 | m68851;
4110   current_architecture |= archs[i].arch;
4111
4112   while (*input_line_pointer == '/')
4113     {
4114       ++input_line_pointer;
4115       s = input_line_pointer;
4116       c = get_symbol_end ();
4117       if (strcmp (s, "68881") == 0)
4118         current_architecture |= m68881;
4119       else if (strcmp (s, "68851") == 0)
4120         current_architecture |= m68851;
4121       *input_line_pointer = c;
4122     }
4123 }
4124
4125 /* The MRI CHIP pseudo-op.  */
4126
4127 static void
4128 s_chip (ignore)
4129      int ignore;
4130 {
4131   mri_chip ();
4132   demand_empty_rest_of_line ();
4133 }
4134
4135 /* The MRI FOPT pseudo-op.  */
4136
4137 static void
4138 s_fopt (ignore)
4139      int ignore;
4140 {
4141   SKIP_WHITESPACE ();
4142
4143   if (strncasecmp (input_line_pointer, "ID=", 3) == 0)
4144     {
4145       int temp;
4146
4147       input_line_pointer += 3;
4148       temp = get_absolute_expression ();
4149       if (temp < 0 || temp > 7)
4150         as_bad ("bad coprocessor id");
4151       else
4152         m68k_float_copnum = COP0 + temp;
4153     }
4154   else
4155     {
4156       as_bad ("unrecognized fopt option");
4157       ignore_rest_of_line ();
4158       return;
4159     }
4160
4161   demand_empty_rest_of_line ();
4162 }
4163
4164 /* The structure used to handle the MRI OPT pseudo-op.  */
4165
4166 struct opt_action
4167 {
4168   /* The name of the option.  */
4169   const char *name;
4170
4171   /* If this is not NULL, just call this function.  The first argument
4172      is the ARG field of this structure, the second argument is
4173      whether the option was negated.  */
4174   void (*pfn) PARAMS ((int arg, int on));
4175
4176   /* If this is not NULL, and the PFN field is NULL, set the variable
4177      this points to.  Set it to the ARG field if the option was not
4178      negated, and the NOTARG field otherwise.  */
4179   int *pvar;
4180
4181   /* The value to pass to PFN or to assign to *PVAR.  */
4182   int arg;
4183
4184   /* The value to assign to *PVAR if the option is negated.  If PFN is
4185      NULL, and PVAR is not NULL, and ARG and NOTARG are the same, then
4186      the option may not be negated.  */
4187   int notarg;
4188 };
4189
4190 /* The table used to handle the MRI OPT pseudo-op.  */
4191
4192 static void skip_to_comma PARAMS ((int, int));
4193 static void opt_chip PARAMS ((int, int));
4194 static void opt_list PARAMS ((int, int));
4195 static void opt_list_symbols PARAMS ((int, int));
4196
4197 static const struct opt_action opt_table[] =
4198 {
4199   { "abspcadd", 0, &m68k_abspcadd, 1, 0 },
4200
4201   /* We do relaxing, so there is little use for these options.  */
4202   { "b", 0, 0, 0, 0 },
4203   { "brs", 0, 0, 0, 0 },
4204   { "brb", 0, 0, 0, 0 },
4205   { "brl", 0, 0, 0, 0 },
4206   { "brw", 0, 0, 0, 0 },
4207
4208   { "c", 0, 0, 0, 0 },
4209   { "cex", 0, 0, 0, 0 },
4210   { "case", 0, &symbols_case_sensitive, 1, 0 },
4211   { "cl", 0, 0, 0, 0 },
4212   { "cre", 0, 0, 0, 0 },
4213   { "d", 0, &flag_keep_locals, 1, 0 },
4214   { "e", 0, 0, 0, 0 },
4215   { "f", 0, &flag_short_refs, 1, 0 },
4216   { "frs", 0, &flag_short_refs, 1, 0 },
4217   { "frl", 0, &flag_short_refs, 0, 1 },
4218   { "g", 0, 0, 0, 0 },
4219   { "i", 0, 0, 0, 0 },
4220   { "m", 0, 0, 0, 0 },
4221   { "mex", 0, 0, 0, 0 },
4222   { "mc", 0, 0, 0, 0 },
4223   { "md", 0, 0, 0, 0 },
4224   { "next", skip_to_comma, 0, 0, 0 },
4225   { "o", 0, 0, 0, 0 },
4226   { "old", 0, 0, 0, 0 },
4227   { "op", skip_to_comma, 0, 0, 0 },
4228   { "pco", 0, 0, 0, 0 },
4229   { "p", opt_chip, 0, 0, 0 },
4230   { "pcr", 0, 0, 0, 0 },
4231   { "pcs", 0, 0, 0, 0 },
4232   { "r", 0, 0, 0, 0 },
4233   { "quick", 0, &m68k_quick, 1, 0 },
4234   { "rel32", 0, &m68k_rel32, 1, 0 },
4235   { "s", opt_list, 0, 0, 0 },
4236   { "t", opt_list_symbols, 0, 0, 0 },
4237   { "w", 0, &flag_no_warnings, 0, 1 },
4238   { "x", 0, 0, 0, 0 }
4239 };
4240
4241 #define OPTCOUNT (sizeof opt_table / sizeof opt_table[0])
4242
4243 /* The MRI OPT pseudo-op.  */
4244
4245 static void
4246 s_opt (ignore)
4247      int ignore;
4248 {
4249   do
4250     {
4251       int t;
4252       char *s;
4253       char c;
4254       int i;
4255       const struct opt_action *o;
4256
4257       SKIP_WHITESPACE ();
4258
4259       t = 1;
4260       if (*input_line_pointer == '-')
4261         {
4262           ++input_line_pointer;
4263           t = 0;
4264         }
4265       else if (strncasecmp (input_line_pointer, "NO", 2) == 0)
4266         {
4267           input_line_pointer += 2;
4268           t = 0;
4269         }
4270
4271       s = input_line_pointer;
4272       c = get_symbol_end ();
4273
4274       for (i = 0, o = opt_table; i < OPTCOUNT; i++, o++)
4275         {
4276           if (strcasecmp (s, o->name) == 0)
4277             {
4278               if (o->pfn)
4279                 {
4280                   /* Restore input_line_pointer now in case the option
4281                      takes arguments.  */
4282                   *input_line_pointer = c;
4283                   (*o->pfn) (o->arg, t);
4284                 }
4285               else if (o->pvar != NULL)
4286                 {
4287                   if (! t && o->arg == o->notarg)
4288                     as_bad ("option `%s' may not be negated", s);
4289                   *input_line_pointer = c;
4290                   *o->pvar = t ? o->arg : o->notarg;
4291                 }
4292               break;
4293             }
4294         }
4295       if (i >= OPTCOUNT)
4296         {
4297           as_bad ("option `%s' not recognized", s);
4298           *input_line_pointer = c;
4299         }
4300     }
4301   while (*input_line_pointer++ == ',');
4302
4303   /* Move back to terminating character.  */
4304   --input_line_pointer;
4305   demand_empty_rest_of_line ();
4306 }
4307
4308 /* Skip ahead to a comma.  This is used for OPT options which we do
4309    not suppor tand which take arguments.  */
4310
4311 static void
4312 skip_to_comma (arg, on)
4313      int arg;
4314      int on;
4315 {
4316   while (*input_line_pointer != ','
4317          && ! is_end_of_line[(unsigned char) *input_line_pointer])
4318     ++input_line_pointer;
4319 }
4320
4321 /* Handle the OPT P=chip option.  */
4322
4323 static void
4324 opt_chip (arg, on)
4325      int arg;
4326      int on;
4327 {
4328   if (*input_line_pointer != '=')
4329     {
4330       /* This is just OPT P, which we do not support.  */
4331       return;
4332     }
4333
4334   ++input_line_pointer;
4335   mri_chip ();
4336 }
4337
4338 /* Handle the OPT S option.  */
4339
4340 static void
4341 opt_list (arg, on)
4342      int arg;
4343      int on;
4344 {
4345   listing_list (on);
4346 }
4347
4348 /* Handle the OPT T option.  */
4349
4350 static void
4351 opt_list_symbols (arg, on)
4352      int arg;
4353      int on;
4354 {
4355   if (on)
4356     listing |= LISTING_SYMBOLS;
4357   else
4358     listing &=~ LISTING_SYMBOLS;
4359 }
4360
4361 /* Handle the MRI REG pseudo-op.  */
4362
4363 static void
4364 s_reg (ignore)
4365      int ignore;
4366 {
4367   char *s;
4368   int c;
4369   struct m68k_op op;
4370   unsigned long mask;
4371
4372   if (mri_line_label == NULL)
4373     {
4374       as_bad ("missing label");
4375       ignore_rest_of_line ();
4376       return;
4377     }
4378
4379   SKIP_WHITESPACE ();
4380
4381   s = input_line_pointer;
4382   while (isalnum ((unsigned char) *input_line_pointer)
4383 #ifdef REGISTER_PREFIX
4384          || *input_line_pointer == REGISTER_PREFIX
4385 #endif
4386          || *input_line_pointer == '/'
4387          || *input_line_pointer == '-')
4388     ++input_line_pointer;
4389   c = *input_line_pointer;
4390   *input_line_pointer = '\0';
4391
4392   if (m68k_ip_op (s, &op) != 0)
4393     {
4394       if (op.error == NULL)
4395         as_bad ("bad register list");
4396       else
4397         as_bad ("bad register list: %s", op.error);
4398       *input_line_pointer = c;
4399       ignore_rest_of_line ();
4400       return;
4401     }
4402
4403   *input_line_pointer = c;
4404
4405   if (op.mode == REGLST)
4406     mask = op.mask;
4407   else if (op.mode == DREG)
4408     mask = 1 << (op.reg - DATA0);
4409   else if (op.mode == AREG)
4410     mask = 1 << (op.reg - ADDR0 + 8);
4411   else if (op.mode == FPREG)
4412     mask = 1 << (op.reg - FP0 + 16);
4413   else if (op.mode == CONTROL
4414            && op.reg == FPI)
4415     mask = 1 << 24;
4416   else if (op.mode == CONTROL
4417            && op.reg == FPS)
4418     mask = 1 << 25;
4419   else if (op.mode == CONTROL
4420            && op.reg == FPC)
4421     mask = 1 << 26;
4422   else
4423     {
4424       as_bad ("bad register list");
4425       ignore_rest_of_line ();
4426       return;
4427     }
4428
4429   S_SET_SEGMENT (mri_line_label, absolute_section);
4430   S_SET_VALUE (mri_line_label, mask);
4431   mri_line_label->sy_frag = &zero_address_frag;
4432
4433   demand_empty_rest_of_line ();
4434 }
4435 \f
4436 /*
4437  * md_parse_option
4438  *      Invocation line includes a switch not recognized by the base assembler.
4439  *      See if it's a processor-specific option.  These are:
4440  *
4441  *      -[A]m[c]68000, -[A]m[c]68008, -[A]m[c]68010, -[A]m[c]68020, -[A]m[c]68030, -[A]m[c]68040
4442  *      -[A]m[c]68881, -[A]m[c]68882, -[A]m[c]68851
4443  *              Select the architecture.  Instructions or features not
4444  *              supported by the selected architecture cause fatal
4445  *              errors.  More than one may be specified.  The default is
4446  *              -m68020 -m68851 -m68881.  Note that -m68008 is a synonym
4447  *              for -m68000, and -m68882 is a synonym for -m68881.
4448  *      -[A]m[c]no-68851, -[A]m[c]no-68881
4449  *              Don't accept 688?1 instructions.  (The "c" is kind of silly,
4450  *              so don't use or document it, but that's the way the parsing
4451  *              works).
4452  *
4453  *      -pic    Indicates PIC.
4454  *      -k      Indicates PIC.  (Sun 3 only.)
4455  *
4456  */
4457
4458 #ifdef OBJ_ELF
4459 CONST char *md_shortopts = "lSA:m:kQ:V";
4460 #else
4461 CONST char *md_shortopts = "lSA:m:k";
4462 #endif
4463
4464 struct option md_longopts[] = {
4465 #define OPTION_PIC (OPTION_MD_BASE)
4466   {"pic", no_argument, NULL, OPTION_PIC},
4467 #define OPTION_REGISTER_PREFIX_OPTIONAL (OPTION_MD_BASE + 1)
4468   {"register-prefix-optional", no_argument, NULL,
4469      OPTION_REGISTER_PREFIX_OPTIONAL},
4470   {NULL, no_argument, NULL, 0}
4471 };
4472 size_t md_longopts_size = sizeof(md_longopts);
4473
4474 int
4475 md_parse_option (c, arg)
4476      int c;
4477      char *arg;
4478 {
4479   switch (c)
4480     {
4481     case 'l':                   /* -l means keep external to 2 bit offset
4482                                    rather than 16 bit one */
4483       flag_short_refs = 1;
4484       break;
4485
4486     case 'S':                   /* -S means that jbsr's always turn into
4487                                    jsr's.  */
4488       flag_long_jumps = 1;
4489       break;
4490
4491     case 'A':
4492       if (*arg == 'm')
4493         arg++;
4494       /* intentional fall-through */
4495     case 'm':
4496
4497       if (arg[0] == 'n' && arg[1] == 'o' && arg[2] == '-')
4498         {
4499           int i;
4500           unsigned long arch;
4501           const char *oarg = arg;
4502
4503           arg += 3;
4504           if (*arg == 'm')
4505             {
4506               arg++;
4507               if (arg[0] == 'c' && arg[1] == '6')
4508                 arg++;
4509             }
4510           for (i = 0; i < n_archs; i++)
4511             if (!strcmp (arg, archs[i].name))
4512               break;
4513           if (i == n_archs)
4514             {
4515             unknown:
4516               as_bad ("unrecognized option `%s'", oarg);
4517               return 0;
4518             }
4519           arch = archs[i].arch;
4520           if (arch == m68881)
4521             no_68881 = 1;
4522           else if (arch == m68851)
4523             no_68851 = 1;
4524           else
4525             goto unknown;
4526         }
4527       else
4528         {
4529           int i;
4530
4531           if (arg[0] == 'c' && arg[1] == '6')
4532             arg++;
4533
4534           for (i = 0; i < n_archs; i++)
4535             if (!strcmp (arg, archs[i].name))
4536               {
4537                 unsigned long arch = archs[i].arch;
4538                 if (cpu_of_arch (arch))
4539                   /* It's a cpu spec.  */
4540                   {
4541                     current_architecture &= ~m68000up;
4542                     current_architecture |= arch;
4543                   }
4544                 else if (arch == m68881)
4545                   {
4546                     current_architecture |= m68881;
4547                     no_68881 = 0;
4548                   }
4549                 else if (arch == m68851)
4550                   {
4551                     current_architecture |= m68851;
4552                     no_68851 = 0;
4553                   }
4554                 else
4555                   /* ??? */
4556                   abort ();
4557                 break;
4558               }
4559           if (i == n_archs)
4560             {
4561               as_bad ("unrecognized architecture specification `%s'", arg);
4562               return 0;
4563             }
4564         }
4565       break;
4566
4567     case OPTION_PIC:
4568     case 'k':
4569       flag_want_pic = 1;
4570       break;                    /* -pic, Position Independent Code */
4571
4572     case OPTION_REGISTER_PREFIX_OPTIONAL:
4573       flag_reg_prefix_optional = 1;
4574       break;
4575
4576     case 'Q':
4577     case 'V':
4578       break;
4579
4580     default:
4581       return 0;
4582     }
4583
4584   return 1;
4585 }
4586
4587 void
4588 md_show_usage (stream)
4589      FILE *stream;
4590 {
4591   fprintf(stream, "\
4592 680X0 options:\n\
4593 -l                      use 1 word for refs to undefined symbols [default 2]\n\
4594 -m68000 | -m68008 | -m68010 | -m68020 | -m68030 | -m68040 | -m68060\n\
4595  | -m68302 | -m68331 | -m68332 | -m68333 | -m68340 | -m68360\n\
4596  | -mcpu32\n\
4597                         specify variant of 680X0 architecture [default 68020]\n\
4598 -m68881 | -m68882 | -mno-68881 | -mno-68882\n\
4599                         target has/lacks floating-point coprocessor\n\
4600                         [default yes for 68020, 68030, and cpu32]\n\
4601 -m68851 | -mno-68851\n\
4602                         target has/lacks memory-management unit coprocessor\n\
4603                         [default yes for 68020 and up]\n\
4604 -pic, -k                generate position independent code\n\
4605 -S                      turn jbsr into jsr\n\
4606 --register-prefix-optional\n\
4607                         recognize register names without prefix character\n");
4608 }
4609 \f
4610 #ifdef TEST2
4611
4612 /* TEST2:  Test md_assemble() */
4613 /* Warning, this routine probably doesn't work anymore */
4614
4615 main ()
4616 {
4617   struct m68k_it the_ins;
4618   char buf[120];
4619   char *cp;
4620   int n;
4621
4622   m68k_ip_begin ();
4623   for (;;)
4624     {
4625       if (!gets (buf) || !*buf)
4626         break;
4627       if (buf[0] == '|' || buf[1] == '.')
4628         continue;
4629       for (cp = buf; *cp; cp++)
4630         if (*cp == '\t')
4631           *cp = ' ';
4632       if (is_label (buf))
4633         continue;
4634       memset (&the_ins, '\0', sizeof (the_ins));
4635       m68k_ip (&the_ins, buf);
4636       if (the_ins.error)
4637         {
4638           printf ("Error %s in %s\n", the_ins.error, buf);
4639         }
4640       else
4641         {
4642           printf ("Opcode(%d.%s): ", the_ins.numo, the_ins.args);
4643           for (n = 0; n < the_ins.numo; n++)
4644             printf (" 0x%x", the_ins.opcode[n] & 0xffff);
4645           printf ("    ");
4646           print_the_insn (&the_ins.opcode[0], stdout);
4647           (void) putchar ('\n');
4648         }
4649       for (n = 0; n < strlen (the_ins.args) / 2; n++)
4650         {
4651           if (the_ins.operands[n].error)
4652             {
4653               printf ("op%d Error %s in %s\n", n, the_ins.operands[n].error, buf);
4654               continue;
4655             }
4656           printf ("mode %d, reg %d, ", the_ins.operands[n].mode, the_ins.operands[n].reg);
4657           if (the_ins.operands[n].b_const)
4658             printf ("Constant: '%.*s', ", 1 + the_ins.operands[n].e_const - the_ins.operands[n].b_const, the_ins.operands[n].b_const);
4659           printf ("ireg %d, isiz %d, imul %d, ", the_ins.operands[n].ireg, the_ins.operands[n].isiz, the_ins.operands[n].imul);
4660           if (the_ins.operands[n].b_iadd)
4661             printf ("Iadd: '%.*s',", 1 + the_ins.operands[n].e_iadd - the_ins.operands[n].b_iadd, the_ins.operands[n].b_iadd);
4662           (void) putchar ('\n');
4663         }
4664     }
4665   m68k_ip_end ();
4666   return 0;
4667 }
4668
4669 is_label (str)
4670      char *str;
4671 {
4672   while (*str == ' ')
4673     str++;
4674   while (*str && *str != ' ')
4675     str++;
4676   if (str[-1] == ':' || str[1] == '=')
4677     return 1;
4678   return 0;
4679 }
4680
4681 #endif
4682
4683 /* Possible states for relaxation:
4684
4685    0 0  branch offset   byte    (bra, etc)
4686    0 1                  word
4687    0 2                  long
4688
4689    1 0  indexed offsets byte    a0@(32,d4:w:1) etc
4690    1 1                  word
4691    1 2                  long
4692
4693    2 0  two-offset index word-word a0@(32,d4)@(45) etc
4694    2 1                  word-long
4695    2 2                  long-word
4696    2 3                  long-long
4697
4698    */
4699
4700 /* We have no need to default values of symbols.  */
4701
4702 /* ARGSUSED */
4703 symbolS *
4704 md_undefined_symbol (name)
4705      char *name;
4706 {
4707   return 0;
4708 }
4709
4710 /* Round up a section size to the appropriate boundary.  */
4711 valueT
4712 md_section_align (segment, size)
4713      segT segment;
4714      valueT size;
4715 {
4716   return size;                  /* Byte alignment is fine */
4717 }
4718
4719 /* Exactly what point is a PC-relative offset relative TO?
4720    On the 68k, it is relative to the address of the first extension
4721    word.  The difference between the addresses of the offset and the
4722    first extension word is stored in fx_pcrel_adjust. */
4723 long
4724 md_pcrel_from (fixP)
4725      fixS *fixP;
4726 {
4727   return (fixP->fx_where + fixP->fx_frag->fr_address - fixP->fx_pcrel_adjust);
4728 }
4729
4730 #ifndef BFD_ASSEMBLER
4731 /*ARGSUSED*/
4732 void
4733 tc_coff_symbol_emit_hook (ignore)
4734      symbolS *ignore;
4735 {
4736 }
4737
4738 int
4739 tc_coff_sizemachdep (frag)
4740      fragS *frag;
4741 {
4742   switch (frag->fr_subtype & 0x3)
4743     {
4744     case BYTE:
4745       return 1;
4746     case SHORT:
4747       return 2;
4748     case LONG:
4749       return 4;
4750     default:
4751       abort ();
4752       return 0;
4753     }
4754 }
4755 #endif
4756
4757 /* end of tc-m68k.c */