Convert unmaintained files over to ISO-C90 and fix formatting.
[platform/upstream/binutils.git] / gas / config / tc-w65.c
1 /* tc-w65.c -- Assemble code for the W65816
2    Copyright 1995, 1998, 2000, 2001, 2002, 2005
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* Written By Steve Chamberlain <sac@cygnus.com>.  */
23
24 #include <stdio.h>
25 #include "as.h"
26 #include "bfd.h"
27 #include "subsegs.h"
28 #define DEFINE_TABLE
29 #include "../opcodes/w65-opc.h"
30
31 const char comment_chars[]        = "!";
32 const char line_separator_chars[] = ";";
33 const char line_comment_chars[]   = "!#";
34
35 /* This table describes all the machine specific pseudo-ops the assembler
36    has to support.  The fields are:
37
38    pseudo-op name without dot
39    function to call to execute this pseudo-op
40    Integer arg to pass to the function.  */
41
42 #define OP_BCC  0x90
43 #define OP_BCS  0xB0
44 #define OP_BEQ  0xF0
45 #define OP_BMI  0x30
46 #define OP_BNE  0xD0
47 #define OP_BPL  0x10
48 #define OP_BRA  0x80
49 #define OP_BRL  0x82
50 #define OP_BVC  0x50
51 #define OP_BVS  0x70
52
53 static int M;                           /* M flag.  */
54 static int X;                           /* X flag.  */
55
56 /* This function is called once, at assembler startup time.  This
57    should set up all the tables, etc that the MD part of the assembler
58    needs.  */
59
60 static void
61 s_longa (int xmode)
62 {
63   int *p = xmode ? &X : &M;
64
65   while (*input_line_pointer == ' ')
66     input_line_pointer++;
67   if (strncmp (input_line_pointer, "on", 2) == 0)
68     {
69       input_line_pointer += 2;
70       *p = 0;
71     }
72   else if (strncmp (input_line_pointer, "off", 3) == 0)
73     {
74       *p = 1;
75       input_line_pointer += 3;
76     }
77   else
78     as_bad (_("need on or off."));
79   demand_empty_rest_of_line ();
80 }
81
82 const pseudo_typeS md_pseudo_table[] =
83 {
84   {"int", cons, 2},
85   {"word", cons, 2},
86   {"longa", s_longa, 0},
87   {"longi", s_longa, 1},
88   {0, 0, 0}
89 };
90
91 const char EXP_CHARS[] = "eE";
92
93 /* Chars that mean this number is a floating point constant.  */
94 /* As in 0f12.456 */
95 /* or    0d1.2345e12 */
96 const char FLT_CHARS[] = "rRsSfFdDxXpP";
97
98 /* Opcode mnemonics */
99 static struct hash_control *opcode_hash_control;
100
101 #define C(a, b)                    ENCODE_RELAX(a,b)
102 #define ENCODE_RELAX(what, length) (((what) << 2) + (length))
103
104 #define GET_WHAT(x) (((x) >> 2))
105
106 #define BYTE_DISP       1
107 #define WORD_DISP       2
108 #define UNDEF_BYTE_DISP 0
109 #define UNDEF_WORD_DISP 3
110 #define COND_BRANCH     1
111 #define UNCOND_BRANCH   2
112 #define END             3
113
114 #define BYTE_F 127      /* How far we can branch forwards.  */
115 #define BYTE_B -126     /* How far we can branch backwards.  */
116 #define WORD_F 32767
117 #define WORD_B 32768
118
119 relax_typeS md_relax_table[C (END, 0)] =
120 {
121   { 0, 0, 0, 0 },
122   { 0, 0, 0, 0 },
123   { 0, 0, 0, 0 },
124   { 0, 0, 0, 0 },
125
126   /* COND_BRANCH */
127   { 0,       0,       0,  0 },                          /* UNDEF_BYTE_DISP */
128   { BYTE_F,  BYTE_B,  2,  C (COND_BRANCH, WORD_DISP) }, /* BYTE_DISP */
129   { WORD_F,  WORD_B,  5,  0 },                          /* WORD_DISP */
130   { 0,       0,       5,  0 },                          /* UNDEF_WORD_DISP */
131
132   /* UNCOND_BRANCH */
133   { 0,       0,       0,  0 },                            /* UNDEF_BYTE_DISP */
134   { BYTE_F,  BYTE_B,  2,  C (UNCOND_BRANCH, WORD_DISP) }, /* BYTE_DISP */
135   { WORD_F,  WORD_B,  3,  0 },                            /* WORD_DISP */
136   { 0,       0,       3,  0 }                             /* UNDEF_WORD_DISP */
137 };
138
139 void
140 md_begin (void)
141 {
142   const struct opinfo *opcode;
143   char *prev_name = "";
144
145   opcode_hash_control = hash_new ();
146
147   /* Insert unique names into hash table.  */
148   for (opcode = optable; opcode->name; opcode++)
149     {
150       if (strcmp (prev_name, opcode->name))
151         {
152           prev_name = opcode->name;
153           hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
154         }
155     }
156
157   flag_signed_overflow_ok = 1;
158 }
159
160 static expressionS immediate;   /* Absolute expression.  */
161 static expressionS immediate1;  /* Absolute expression.  */
162 int expr_size;
163 int expr_shift;
164 int tc_cons_reloc;
165
166 void
167 w65_expression (expressionS *dest)
168 {
169   expr_size = 0;
170   expr_shift = 0;
171   tc_cons_reloc = 0;
172   while (*input_line_pointer == ' ')
173     input_line_pointer++;
174
175   if (*input_line_pointer == '<')
176     {
177       expr_size = 1;
178       input_line_pointer++;
179     }
180   else if (*input_line_pointer == '>')
181     {
182       expr_shift = 1;
183       input_line_pointer++;
184     }
185   else if (*input_line_pointer == '^')
186     {
187       expr_shift = 2;
188       input_line_pointer++;
189     }
190
191   expr (0, dest);
192 }
193
194 int amode;
195
196 static char *
197 parse_exp (char *s)
198 {
199   char *save;
200   char *new;
201
202   save = input_line_pointer;
203   input_line_pointer = s;
204   w65_expression (&immediate);
205   if (immediate.X_op == O_absent)
206     as_bad (_("missing operand"));
207   new = input_line_pointer;
208   input_line_pointer = save;
209   return new;
210 }
211
212 static char *
213 get_operands (const struct opinfo *info, char *ptr)
214 {
215   int override_len = 0;
216   int bytes = 0;
217
218   while (*ptr == ' ')
219     ptr++;
220
221   if (ptr[0] == '#')
222     {
223       ptr++;
224       switch (info->amode)
225         {
226         case ADDR_IMMTOI:
227           bytes = X ? 1 : 2;
228           amode = ADDR_IMMTOI;
229           break;
230         case ADDR_IMMTOA:
231           bytes = M ? 1 : 2;
232           amode = ADDR_IMMTOA;
233           break;
234         case ADDR_IMMCOP:
235           bytes = 1;
236           amode = ADDR_IMMCOP;
237           break;
238         case ADDR_DIR:
239           bytes = 2;
240           amode = ADDR_ABS;
241           break;
242         default:
243           abort ();
244           break;
245         }
246       ptr = parse_exp (ptr);
247     }
248   else if (ptr[0] == '!')
249     {
250       ptr = parse_exp (ptr + 1);
251       if (ptr[0] == ',')
252         {
253           if (ptr[1] == 'y')
254             {
255               amode = ADDR_ABS_IDX_Y;
256               bytes = 2;
257               ptr += 2;
258             }
259           else if (ptr[1] == 'x')
260             {
261               amode = ADDR_ABS_IDX_X;
262               bytes = 2;
263               ptr += 2;
264             }
265           else
266             as_bad (_("syntax error after <exp"));
267         }
268       else
269         {
270           amode = ADDR_ABS;
271           bytes = 2;
272         }
273     }
274   else if (ptr[0] == '>')
275     {
276       ptr = parse_exp (ptr + 1);
277       if (ptr[0] == ',' && ptr[1] == 'x')
278         {
279           amode = ADDR_ABS_LONG_IDX_X;
280           bytes = 3;
281           ptr += 2;
282         }
283       else
284         {
285           amode = ADDR_ABS_LONG;
286           bytes = 3;
287         }
288     }
289   else if (ptr[0] == '<')
290     {
291       ptr = parse_exp (ptr + 1);
292       if (ptr[0] == ',')
293         {
294           if (ptr[1] == 'y')
295             {
296               amode = ADDR_DIR_IDX_Y;
297               ptr += 2;
298               bytes = 2;
299             }
300           else if (ptr[1] == 'x')
301             {
302               amode = ADDR_DIR_IDX_X;
303               ptr += 2;
304               bytes = 2;
305             }
306           else
307             as_bad (_("syntax error after <exp"));
308         }
309       else
310         {
311           amode = ADDR_DIR;
312           bytes = 1;
313         }
314     }
315   else if (ptr[0] == 'a')
316     amode = ADDR_ACC;
317   else if (ptr[0] == '(')
318     {
319       /* Look for (exp),y
320          (<exp),y
321          (exp,x)
322          (<exp,x)
323          (exp)
324          (!exp)
325          (exp)
326          (<exp)
327          (exp,x)
328          (!exp,x)
329          (exp,s)
330          (exp,s),y */
331
332       ptr++;
333       if (ptr[0] == '<')
334         {
335           override_len = 1;
336           ptr++;
337         }
338       else if (ptr[0] == '!')
339         {
340           override_len = 2;
341           ptr++;
342         }
343       else if (ptr[0] == '>')
344         {
345           override_len = 3;
346           ptr++;
347         }
348       else
349         override_len = 0;
350
351       ptr = parse_exp (ptr);
352
353       if (ptr[0] == ',')
354         {
355           ptr++;
356           if (ptr[0] == 'x' && ptr[1] == ')')
357             {
358               ptr += 2;
359
360               if (override_len == 1)
361                 {
362                   amode = ADDR_DIR_IDX_IND_X;
363                   bytes = 2;
364                 }
365               else
366                 {
367                   amode = ADDR_ABS_IND_IDX;
368                   bytes = 2;
369                 }
370             }
371           else if (ptr[0] == 's' && ptr[1] == ')'
372                    && ptr[2] == ',' && ptr[3] == 'y')
373             {
374               amode = ADDR_STACK_REL_INDX_IDX;
375               bytes = 1;
376               ptr += 4;
377             }
378         }
379       else if (ptr[0] == ')')
380         {
381           if (ptr[1] == ',' && ptr[2] == 'y')
382             {
383               amode = ADDR_DIR_IND_IDX_Y;
384               ptr += 3;
385               bytes = 2;
386             }
387           else
388             {
389               if (override_len == 1)
390                 {
391                   amode = ADDR_DIR_IND;
392                   bytes = 1;
393                 }
394               else
395                 {
396                   amode = ADDR_ABS_IND;
397                   bytes = 2;
398                 }
399               ptr++;
400             }
401         }
402     }
403   else if (ptr[0] == '[')
404     {
405       ptr = parse_exp (ptr + 1);
406
407       if (ptr[0] == ']')
408         {
409           ptr++;
410           if (ptr[0] == ',' && ptr[1] == 'y')
411             {
412               bytes = 1;
413               amode = ADDR_DIR_IND_IDX_Y_LONG;
414               ptr += 2;
415             }
416           else
417             {
418               if (info->code == O_jmp)
419                 {
420                   bytes = 2;
421                   amode = ADDR_ABS_IND_LONG;
422                 }
423               else
424                 {
425                   bytes = 1;
426                   amode = ADDR_DIR_IND_LONG;
427                 }
428             }
429         }
430     }
431   else
432     {
433       ptr = parse_exp (ptr);
434       if (ptr[0] == ',')
435         {
436           if (ptr[1] == 'y')
437             {
438               if (override_len == 1)
439                 {
440                   bytes = 1;
441                   amode = ADDR_DIR_IDX_Y;
442                 }
443               else
444                 {
445                   amode = ADDR_ABS_IDX_Y;
446                   bytes = 2;
447                 }
448               ptr += 2;
449             }
450           else if (ptr[1] == 'x')
451             {
452               if (override_len == 1)
453                 {
454                   amode = ADDR_DIR_IDX_X;
455                   bytes = 1;
456                 }
457               else
458                 {
459                   amode = ADDR_ABS_IDX_X;
460                   bytes = 2;
461                 }
462               ptr += 2;
463             }
464           else if (ptr[1] == 's')
465             {
466               bytes = 1;
467               amode = ADDR_STACK_REL;
468               ptr += 2;
469             }
470           else
471             {
472               bytes = 1;
473               immediate1 = immediate;
474               ptr = parse_exp (ptr + 1);
475               amode = ADDR_BLOCK_MOVE;
476             }
477         }
478       else
479         {
480           switch (info->amode)
481             {
482             case ADDR_PC_REL:
483               amode = ADDR_PC_REL;
484               bytes = 1;
485               break;
486             case ADDR_PC_REL_LONG:
487               amode = ADDR_PC_REL_LONG;
488               bytes = 2;
489               break;
490             default:
491               if (override_len == 1)
492                 {
493                   amode = ADDR_DIR;
494                   bytes = 1;
495                 }
496               else if (override_len == 3)
497                 {
498                   bytes = 3;
499                   amode = ADDR_ABS_LONG;
500                 }
501               else
502                 {
503                   amode = ADDR_ABS;
504                   bytes = 2;
505                 }
506             }
507         }
508     }
509
510   switch (bytes)
511     {
512     case 1:
513       switch (expr_shift)
514         {
515         case 0:
516           if (amode == ADDR_DIR)
517             tc_cons_reloc = R_W65_DP;
518           else
519             tc_cons_reloc = R_W65_ABS8;
520           break;
521         case 1:
522           tc_cons_reloc = R_W65_ABS8S8;
523           break;
524         case 2:
525           tc_cons_reloc = R_W65_ABS8S16;
526           break;
527         }
528       break;
529     case 2:
530       switch (expr_shift)
531         {
532         case 0:
533           tc_cons_reloc = R_W65_ABS16;
534           break;
535         case 1:
536           tc_cons_reloc = R_W65_ABS16S8;
537           break;
538         case 2:
539           tc_cons_reloc = R_W65_ABS16S16;
540           break;
541         }
542     }
543   return ptr;
544 }
545
546 /* Passed a pointer to a list of opcodes which use different
547    addressing modes, return the opcode which matches the opcodes
548    provided.  */
549
550 static const struct opinfo *
551 get_specific (const struct opinfo *opcode)
552 {
553   int ocode = opcode->code;
554
555   for (; opcode->code == ocode; opcode++)
556     if (opcode->amode == amode)
557       return opcode;
558
559   return 0;
560 }
561
562 /* Now we know what sort of opcodes it is, let's build the bytes.  */
563
564 static void
565 build_Mytes (const struct opinfo *opcode)
566 {
567   int size;
568   int type;
569   int pcrel;
570   char *output;
571
572   if (opcode->amode == ADDR_IMPLIED)
573     output = frag_more (1);
574
575   else if (opcode->amode == ADDR_PC_REL)
576     {
577       int type;
578
579       /* This is a relaxable insn, so we do some special handling.  */
580       type = opcode->val == OP_BRA ? UNCOND_BRANCH : COND_BRANCH;
581       output = frag_var (rs_machine_dependent,
582                          md_relax_table[C (type, WORD_DISP)].rlx_length,
583                          md_relax_table[C (type, BYTE_DISP)].rlx_length,
584                          C (type, UNDEF_BYTE_DISP),
585                          immediate.X_add_symbol,
586                          immediate.X_add_number,
587                          0);
588     }
589   else
590     {
591       switch (opcode->amode)
592         {
593           GETINFO (size, type, pcrel);
594         default:
595           abort ();
596         }
597
598       /* If something special was done in the expression modify the
599          reloc type.  */
600       if (tc_cons_reloc)
601         type = tc_cons_reloc;
602
603       /* 1 byte for the opcode + the bytes for the addrmode.  */
604       output = frag_more (size + 1);
605
606       if (opcode->amode == ADDR_BLOCK_MOVE)
607         {
608           /* Two relocs for this one.  */
609           fix_new_exp (frag_now,
610                        output + 1 - frag_now->fr_literal,
611                        1,
612                        &immediate,
613                        0,
614                        R_W65_ABS8S16);
615
616           fix_new_exp (frag_now,
617                        output + 2 - frag_now->fr_literal,
618                        1,
619                        &immediate1,
620                        0,
621                        R_W65_ABS8S16);
622         }
623       else if (type >= 0
624                && opcode->amode != ADDR_IMPLIED
625                && opcode->amode != ADDR_ACC
626                && opcode->amode != ADDR_STACK)
627         {
628           fix_new_exp (frag_now,
629                        output + 1 - frag_now->fr_literal,
630                        size,
631                        &immediate,
632                        pcrel,
633                        type);
634         }
635     }
636   output[0] = opcode->val;
637 }
638
639 /* This is the guts of the machine-dependent assembler.  STR points to
640    a machine dependent instruction.  This function is supposed to emit
641    the frags/bytes it assembles to.  */
642
643 void
644 md_assemble (char *str)
645 {
646   const struct opinfo *opcode;
647   char name[20];
648
649   /* Drop leading whitespace */
650   while (*str == ' ')
651     str++;
652
653   /* All opcodes are three letters.  */
654   name[0] = str[0];
655   name[1] = str[1];
656   name[2] = str[2];
657   name[3] = 0;
658
659   tc_cons_reloc = 0;
660   str += 3;
661   opcode = (struct opinfo *) hash_find (opcode_hash_control, name);
662
663   if (opcode == NULL)
664     {
665       as_bad (_("unknown opcode"));
666       return;
667     }
668
669   if (opcode->amode != ADDR_IMPLIED
670       && opcode->amode != ADDR_STACK)
671     {
672       get_operands (opcode, str);
673       opcode = get_specific (opcode);
674     }
675
676   if (opcode == 0)
677     {
678       /* Couldn't find an opcode which matched the operands.  */
679
680       char *where = frag_more (1);
681
682       where[0] = 0x0;
683       where[1] = 0x0;
684       as_bad (_("invalid operands for opcode"));
685       return;
686     }
687
688   build_Mytes (opcode);
689 }
690
691 symbolS *
692 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
693 {
694   return 0;
695 }
696
697 /* Various routines to kill one day.  */
698 /* Equal to MAX_PRECISION in atof-ieee.c.  */
699 #define MAX_LITTLENUMS 6
700
701 /* Turn a string in input_line_pointer into a floating point constant
702    of type TYPE, and store the appropriate bytes in *LITP.  The number
703    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
704    returned, or NULL on OK.  */
705
706 char *
707 md_atof (int type, char *litP, int *sizeP)
708 {
709   int prec;
710   LITTLENUM_TYPE words[MAX_LITTLENUMS];
711   LITTLENUM_TYPE *wordP;
712   char *t;
713
714   switch (type)
715     {
716     case 'f':
717     case 'F':
718     case 's':
719     case 'S':
720       prec = 2;
721       break;
722
723     case 'd':
724     case 'D':
725     case 'r':
726     case 'R':
727       prec = 4;
728       break;
729
730     case 'x':
731     case 'X':
732       prec = 6;
733       break;
734
735     case 'p':
736     case 'P':
737       prec = 6;
738       break;
739
740     default:
741       *sizeP = 0;
742       return _("Bad call to MD_NTOF()");
743     }
744   t = atof_ieee (input_line_pointer, type, words);
745   if (t)
746     input_line_pointer = t;
747
748   *sizeP = prec * sizeof (LITTLENUM_TYPE);
749   for (wordP = words + prec - 1; prec--;)
750     {
751       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
752       litP += sizeof (LITTLENUM_TYPE);
753     }
754   return 0;
755 }
756
757 int
758 md_parse_option (int c ATTRIBUTE_UNUSED, char *a ATTRIBUTE_UNUSED)
759 {
760   return 0;
761 }
762
763 /* Called after relaxing, change the frags so they know how big they
764    are.  */
765
766 void
767 md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED,
768                  segT seg ATTRIBUTE_UNUSED,
769                  fragS *fragP)
770 {
771   int disp_size = 0;
772   int inst_size = 0;
773   unsigned char *buffer =
774     (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
775
776   switch (fragP->fr_subtype)
777     {
778     case C (COND_BRANCH, BYTE_DISP):
779     case C (UNCOND_BRANCH, BYTE_DISP):
780       disp_size = 1;
781       inst_size = 1;
782       break;
783
784       /* Conditional branches to a known 16 bit displacement.  */
785     case C (COND_BRANCH, WORD_DISP):
786       switch (buffer[0])
787         {
788         case OP_BCC:
789         case OP_BCS:
790         case OP_BEQ:
791         case OP_BMI:
792         case OP_BNE:
793         case OP_BPL:
794         case OP_BVS:
795         case OP_BVC:
796           /* Invert the sense of the test.  */
797           buffer[0] ^= 0x20;
798           buffer[1] = 3;        /* Jump over following brl.  */
799           buffer[2] = OP_BRL;
800           buffer[3] = 0;
801           buffer[4] = 0;
802           disp_size = 2;
803           inst_size = 3;
804           break;
805         default:
806           abort ();
807         }
808       break;
809     case C (UNCOND_BRANCH, WORD_DISP):
810       /* Unconditional branches to a known 16 bit displacement.  */
811
812       switch (buffer[0])
813         {
814         case OP_BRA:
815           buffer[0] = OP_BRL;
816           disp_size = 2;
817           inst_size = 1;
818           break;
819         default:
820           abort ();
821         }
822       break;
823       /* Got to create a branch over a reloc here.  */
824     case C (COND_BRANCH, UNDEF_WORD_DISP):
825       buffer[0] ^= 0x20;        /* Invert test.  */
826       buffer[1] = 3;
827       buffer[2] = OP_BRL;
828       buffer[3] = 0;
829       buffer[4] = 0;
830       fix_new (fragP,
831                fragP->fr_fix + 3,
832                4,
833                fragP->fr_symbol,
834                fragP->fr_offset,
835                0,
836                R_W65_PCR16);
837
838       fragP->fr_fix += disp_size + inst_size;
839       fragP->fr_var = 0;
840       break;
841     case C (UNCOND_BRANCH, UNDEF_WORD_DISP):
842       buffer[0] = OP_BRL;
843       buffer[1] = 0;
844       buffer[2] = 0;
845       fix_new (fragP,
846                fragP->fr_fix + 1,
847                4,
848                fragP->fr_symbol,
849                fragP->fr_offset,
850                0,
851                R_W65_PCR16);
852
853       fragP->fr_fix += disp_size + inst_size;
854       fragP->fr_var = 0;
855       break;
856     default:
857       abort ();
858     }
859   if (inst_size)
860     {
861       /* Get the address of the end of the instruction.  */
862       int next_inst = (fragP->fr_fix + fragP->fr_address
863                        + disp_size + inst_size);
864       int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
865                        fragP->fr_offset);
866       int disp = targ_addr - next_inst;
867
868       md_number_to_chars ((char *) buffer + inst_size, disp, disp_size);
869       fragP->fr_fix += disp_size + inst_size;
870       fragP->fr_var = 0;
871     }
872 }
873
874 valueT
875 md_section_align (segT seg, valueT size)
876 {
877   return ((size + (1 << section_alignment[(int) seg]) - 1)
878           & (-1 << section_alignment[(int) seg]));
879 }
880
881 void
882 md_apply_fix3 (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
883 {
884   long val = * (long *) valP;
885   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
886   int addr = fixP->fx_frag->fr_address + fixP->fx_where;
887
888   if (fixP->fx_r_type == 0)
889     {
890       if (fixP->fx_size == 1)
891         fixP->fx_r_type = R_W65_ABS8;
892       else
893         fixP->fx_r_type = R_W65_ABS16;
894     }
895
896   switch (fixP->fx_r_type)
897     {
898     case R_W65_ABS8S16:
899       val >>= 8;
900     case R_W65_ABS8S8:
901       val >>= 8;
902     case R_W65_ABS8:
903       *buf++ = val;
904       break;
905     case R_W65_ABS16S16:
906       val >>= 8;
907     case R_W65_ABS16S8:
908       val >>= 8;
909     case R_W65_ABS16:
910       *buf++ = val >> 0;
911       *buf++ = val >> 8;
912       break;
913     case R_W65_ABS24:
914       *buf++ = val >> 0;
915       *buf++ = val >> 8;
916       *buf++ = val >> 16;
917       break;
918     case R_W65_PCR8:
919       *buf++ = val - addr - 1;
920       break;
921     case R_W65_PCR16:
922       val = val - addr - 1;
923       *buf++ = val;
924       *buf++ = val >> 8;
925       break;
926     case R_W65_DP:
927       *buf++ = val;
928       break;
929
930     default:
931       abort ();
932     }
933
934   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
935     fixP->fx_done = 1;
936 }
937
938 /* Put number into target byte order.  */
939
940 void
941 md_number_to_chars (char *ptr, valueT use, int nbytes)
942 {
943   number_to_chars_littleendian (ptr, use, nbytes);
944 }
945
946 long
947 md_pcrel_from (fixS *fixP)
948 {
949   int gap = fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address - 1;
950   return gap;
951 }
952
953 void
954 tc_coff_symbol_emit_hook (symbolS *x ATTRIBUTE_UNUSED)
955 {
956 }
957
958 short
959 tc_coff_fix2rtype (fixS *fix_ptr)
960 {
961   return fix_ptr->fx_r_type;
962 }
963
964 void
965 tc_reloc_mangle (fixS *fix_ptr,
966                  struct internal_reloc *intr,
967                  bfd_vma base)
968
969 {
970   symbolS *symbol_ptr;
971
972   symbol_ptr = fix_ptr->fx_addsy;
973
974   /* If this relocation is attached to a symbol then it's ok
975      to output it */
976   if (fix_ptr->fx_r_type == RELOC_32)
977     {
978       /* Cons likes to create reloc32's whatever the size of the reloc.  */
979       switch (fix_ptr->fx_size)
980         {
981         case 2:
982           intr->r_type = R_IMM16;
983           break;
984         case 1:
985           intr->r_type = R_IMM8;
986           break;
987         default:
988           abort ();
989         }
990     }
991   else
992     {
993       if (fix_ptr->fx_size == 4)
994         intr->r_type = R_W65_ABS24;
995       else
996         intr->r_type = fix_ptr->fx_r_type;
997     }
998
999   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1000   intr->r_offset = fix_ptr->fx_offset;
1001
1002   /* Turn the segment of the symbol into an offset.  */
1003   if (symbol_ptr)
1004     {
1005       symbolS *dot;
1006
1007       dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1008       if (dot)
1009         {
1010           intr->r_offset += S_GET_VALUE (symbol_ptr);
1011           intr->r_symndx = dot->sy_number;
1012         }
1013       else
1014         intr->r_symndx = symbol_ptr->sy_number;
1015     }
1016   else
1017     intr->r_symndx = -1;
1018 }
1019
1020 int
1021 tc_coff_sizemachdep (fragS *frag)
1022 {
1023   return md_relax_table[frag->fr_subtype].rlx_length;
1024 }
1025
1026 /* Called just before address relaxation, return the length by which a
1027    fragment must grow to reach it's destination.  */
1028
1029 int
1030 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
1031 {
1032   int what;
1033
1034   switch (fragP->fr_subtype)
1035     {
1036     default:
1037       abort ();
1038
1039     case C (COND_BRANCH, UNDEF_BYTE_DISP):
1040     case C (UNCOND_BRANCH, UNDEF_BYTE_DISP):
1041       what = GET_WHAT (fragP->fr_subtype);
1042       /* Used to be a branch to somewhere which was unknown.  */
1043       if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1044         /* Got a symbol and it's defined in this segment, become byte
1045            sized - maybe it will fix up.  */
1046         fragP->fr_subtype = C (what, BYTE_DISP);
1047       else
1048         /* Its got a segment, but its not ours, so it will always be
1049            long.  */
1050         fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1051       break;
1052
1053     case C (COND_BRANCH, BYTE_DISP):
1054     case C (COND_BRANCH, WORD_DISP):
1055     case C (COND_BRANCH, UNDEF_WORD_DISP):
1056     case C (UNCOND_BRANCH, BYTE_DISP):
1057     case C (UNCOND_BRANCH, WORD_DISP):
1058     case C (UNCOND_BRANCH, UNDEF_WORD_DISP):
1059       /* When relaxing a section for the second time, we don't need to
1060          do anything besides return the current size.  */
1061       break;
1062     }
1063
1064   fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
1065   return fragP->fr_var;
1066 }
1067
1068 const char *md_shortopts = "";
1069 struct option md_longopts[] =
1070 {
1071 #define OPTION_RELAX (OPTION_MD_BASE)
1072   {NULL, no_argument, NULL, 0}
1073 };
1074
1075 void
1076 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
1077 {
1078 }
1079
1080 size_t md_longopts_size = sizeof (md_longopts);