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