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