* config/tc-sh.c (parse_reg): Handle new FP registers.
[external/binutils.git] / gas / config / tc-sh.c
1 /* tc-sh.c -- Assemble code for the Hitachi Super-H
2
3    Copyright (C) 1993, 1994, 1995 Free Software Foundation.
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
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /*
23    Written By Steve Chamberlain
24    sac@cygnus.com
25  */
26
27 #include <stdio.h>
28 #include "as.h"
29 #include "bfd.h"
30 #include "subsegs.h"
31 #define DEFINE_TABLE
32 #include "opcodes/sh-opc.h"
33 #include <ctype.h>
34 const char comment_chars[] = "!";
35 const char line_separator_chars[] = ";";
36 const char line_comment_chars[] = "!#";
37
38 static void s_uses PARAMS ((int));
39
40 /* This table describes all the machine specific pseudo-ops the assembler
41    has to support.  The fields are:
42    pseudo-op name without dot
43    function to call to execute this pseudo-op
44    Integer arg to pass to the function
45  */
46
47 void cons ();
48 void s_align_bytes ();
49
50 int shl = 0;
51
52 static void
53 little (ignore)
54      int ignore;
55 {
56   shl = 1;
57 }
58
59 const pseudo_typeS md_pseudo_table[] =
60 {
61   {"int", cons, 4},
62   {"word", cons, 2},
63   {"form", listing_psize, 0},
64   {"little", little, 0},
65   {"heading", listing_title, 0},
66   {"import", s_ignore, 0},
67   {"page", listing_eject, 0},
68   {"program", s_ignore, 0},
69   {"uses", s_uses, 0},
70   {0, 0, 0}
71 };
72
73 /*int md_reloc_size; */
74
75 int sh_relax;           /* set if -relax seen */
76
77 const char EXP_CHARS[] = "eE";
78
79 /* Chars that mean this number is a floating point constant */
80 /* As in 0f12.456 */
81 /* or    0d1.2345e12 */
82 const char FLT_CHARS[] = "rRsSfFdDxXpP";
83
84 #define C(a,b) ENCODE_RELAX(a,b)
85
86 #define JREG 14                 /* Register used as a temp when relaxing */
87 #define ENCODE_RELAX(what,length) (((what) << 4) + (length))
88 #define GET_WHAT(x) ((x>>4))
89
90 /* These are the two types of relaxable instrction */
91 #define COND_JUMP 1
92 #define UNCOND_JUMP  2
93
94 #define UNDEF_DISP 0
95 #define COND8  1
96 #define COND12 2
97 #define COND32 3
98 #define UNCOND12 1
99 #define UNCOND32 2
100 #define UNDEF_WORD_DISP 4
101 #define END 5
102
103 #define UNCOND12 1
104 #define UNCOND32 2
105
106 /* Branch displacements are from the address of the branch plus
107    four, thus all minimum and maximum values have 4 added to them.  */
108 #define COND8_F 258
109 #define COND8_M -252
110 #define COND8_LENGTH 2
111
112 /* There is one extra instruction before the branch, so we must add
113    two more bytes to account for it.  */
114 #define COND12_F 4100
115 #define COND12_M -4090
116 #define COND12_LENGTH 6
117
118 /* ??? The minimum and maximum values are wrong, but this does not matter
119    since this relocation type is not supported yet.  */
120 #define COND32_F (1<<30)
121 #define COND32_M -(1<<30)
122 #define COND32_LENGTH 14
123
124 #define UNCOND12_F 4098
125 #define UNCOND12_M -4092
126 #define UNCOND12_LENGTH 2
127
128 /* ??? The minimum and maximum values are wrong, but this does not matter
129    since this relocation type is not supported yet.  */
130 #define UNCOND32_F (1<<30)
131 #define UNCOND32_M -(1<<30)
132 #define UNCOND32_LENGTH 14
133
134 const relax_typeS md_relax_table[C (END, 0)] = {
135   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
136   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
137
138   { 0 },
139   /* C (COND_JUMP, COND8) */
140   { COND8_F, COND8_M, COND8_LENGTH, C (COND_JUMP, COND12) },
141   /* C (COND_JUMP, COND12) */
142   { COND12_F, COND12_M, COND12_LENGTH, C (COND_JUMP, COND32), },
143   /* C (COND_JUMP, COND32) */
144   { COND32_F, COND32_M, COND32_LENGTH, 0, },
145   { 0 }, { 0 }, { 0 }, { 0 },
146   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
147
148   { 0 },
149   /* C (UNCOND_JUMP, UNCOND12) */
150   { UNCOND12_F, UNCOND12_M, UNCOND12_LENGTH, C (UNCOND_JUMP, UNCOND32), },
151   /* C (UNCOND_JUMP, UNCOND32) */
152   { UNCOND32_F, UNCOND32_M, UNCOND32_LENGTH, 0, },
153   { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
154   { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 },
155 };
156
157 static struct hash_control *opcode_hash_control;        /* Opcode mnemonics */
158
159 /*
160    This function is called once, at assembler startup time.  This should
161    set up all the tables, etc that the MD part of the assembler needs
162  */
163
164 void
165 md_begin ()
166 {
167   sh_opcode_info *opcode;
168   char *prev_name = "";
169
170   opcode_hash_control = hash_new ();
171
172   /* Insert unique names into hash table */
173   for (opcode = sh_table; opcode->name; opcode++)
174     {
175       if (strcmp (prev_name, opcode->name))
176         {
177           prev_name = opcode->name;
178           hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
179         }
180       else
181         {
182           /* Make all the opcodes with the same name point to the same
183              string */
184           opcode->name = prev_name;
185         }
186     }
187 }
188
189 static int reg_m;
190 static int reg_n;
191 static expressionS immediate;   /* absolute expression */
192
193 typedef struct
194   {
195     sh_arg_type type;
196     int reg;
197   }
198
199 sh_operand_info;
200
201 /* try and parse a reg name, returns number of chars consumed */
202 static int
203 parse_reg (src, mode, reg)
204      char *src;
205      int *mode;
206      int *reg;
207 {
208   if (src[0] == 'r')
209     {
210       if (src[1] == '1')
211         {
212           if (src[2] >= '0' && src[2] <= '5')
213             {
214               *mode = A_REG_N;
215               *reg = 10 + src[2] - '0';
216               return 3;
217             }
218         }
219       if (src[1] >= '0' && src[1] <= '9')
220         {
221           *mode = A_REG_N;
222           *reg = (src[1] - '0');
223           return 2;
224         }
225     }
226
227   if (src[0] == 's' && src[1] == 'r')
228     {
229       *mode = A_SR;
230       return 2;
231     }
232
233   if (src[0] == 's' && src[1] == 'p')
234     {
235       *mode = A_REG_N;
236       *reg = 15;
237       return 2;
238     }
239
240   if (src[0] == 'p' && src[1] == 'r')
241     {
242       *mode = A_PR;
243       return 2;
244     }
245   if (src[0] == 'p' && src[1] == 'c')
246     {
247       *mode = A_DISP_PC;
248       return 2;
249     }
250   if (src[0] == 'g' && src[1] == 'b' && src[2] == 'r')
251     {
252       *mode = A_GBR;
253       return 3;
254     }
255   if (src[0] == 'v' && src[1] == 'b' && src[2] == 'r')
256     {
257       *mode = A_VBR;
258       return 3;
259     }
260
261   if (src[0] == 'm' && src[1] == 'a' && src[2] == 'c')
262     {
263       if (src[3] == 'l')
264         {
265           *mode = A_MACL;
266           return 4;
267         }
268       if (src[3] == 'h')
269         {
270           *mode = A_MACH;
271           return 4;
272         }
273     }
274 /* start-sanitize-sh3e */
275   if (src[0] == 'f' && src[1] == 'r')
276     {
277       if (src[2] == '1')
278         {
279           if (src[3] >= '0' && src[3] <= '5')
280             {
281               *mode = F_REG_N;
282               *reg = 10 + src[3] - '0';
283               return 4;
284             }
285         }
286       if (src[2] >= '0' && src[2] <= '9')
287         {
288           *mode = F_REG_N;
289           *reg = (src[2] - '0');
290           return 3;
291         }
292     }
293   if (src[0] == 'f' && src[1] == 'p' && src[2] == 'u' && src[3] == 'l')
294     {
295       *mode = FPUL_N;
296       return 4;
297     }
298
299   if (src[0] == 'f' && src[1] == 'p'
300       && src[2] == 's' && src[3] == 'c' && src[4] == 'r')
301     {
302       *mode = FPSCR_N;
303       return 5;
304     }
305 /* end-sanitize-sh3e */
306
307   return 0;
308 }
309
310 static symbolS *dot()
311 {
312   const char *fake;
313
314   /* JF: '.' is pseudo symbol with value of current location
315      in current segment.  */
316   fake = FAKE_LABEL_NAME;
317   return  symbol_new (fake,
318                       now_seg,
319                       (valueT) frag_now_fix (),
320                       frag_now);
321
322 }
323
324
325 static
326 char *
327 parse_exp (s)
328      char *s;
329 {
330   char *save;
331   char *new;
332
333   save = input_line_pointer;
334   input_line_pointer = s;
335   expression (&immediate);
336   if (immediate.X_op == O_absent)
337     as_bad ("missing operand");
338   new = input_line_pointer;
339   input_line_pointer = save;
340   return new;
341 }
342
343
344 /* The many forms of operand:
345
346    Rn                   Register direct
347    @Rn                  Register indirect
348    @Rn+                 Autoincrement
349    @-Rn                 Autodecrement
350    @(disp:4,Rn)
351    @(disp:8,GBR)
352    @(disp:8,PC)
353
354    @(R0,Rn)
355    @(R0,GBR)
356
357    disp:8
358    disp:12
359    #imm8
360    pr, gbr, vbr, macl, mach
361
362  */
363
364 static
365 char *
366 parse_at (src, op)
367      char *src;
368      sh_operand_info *op;
369 {
370   int len;
371   int mode;
372   src++;
373   if (src[0] == '-')
374     {
375       /* Must be predecrement */
376       src++;
377
378       len = parse_reg (src, &mode, &(op->reg));
379       if (mode != A_REG_N)
380         as_bad ("illegal register after @-");
381
382       op->type = A_DEC_N;
383       src += len;
384     }
385   else if (src[0] == '(')
386     {
387       /* Could be @(disp, rn), @(disp, gbr), @(disp, pc),  @(r0, gbr) or
388          @(r0, rn) */
389       src++;
390       len = parse_reg (src, &mode, &(op->reg));
391       if (len && mode == A_REG_N)
392         {
393           src += len;
394           if (op->reg != 0)
395             {
396               as_bad ("must be @(r0,...)");
397             }
398           if (src[0] == ',')
399             src++;
400           /* Now can be rn or gbr */
401           len = parse_reg (src, &mode, &(op->reg));
402           if (mode == A_GBR)
403             {
404               op->type = A_R0_GBR;
405             }
406           else if (mode == A_REG_N)
407             {
408               op->type = A_IND_R0_REG_N;
409             }
410           else
411             {
412               as_bad ("syntax error in @(r0,...)");
413             }
414         }
415       else
416         {
417           /* Must be an @(disp,.. thing) */
418           src = parse_exp (src);
419           if (src[0] == ',')
420             src++;
421           /* Now can be rn, gbr or pc */
422           len = parse_reg (src, &mode, &op->reg);
423           if (len)
424             {
425               if (mode == A_REG_N)
426                 {
427                   op->type = A_DISP_REG_N;
428                 }
429               else if (mode == A_GBR)
430                 {
431                   op->type = A_DISP_GBR;
432                 }
433               else if (mode == A_DISP_PC)
434                 {
435                   /* Turn a plain @(4,pc) into @(.+4,pc) */
436                   if (immediate.X_op == O_constant) { 
437                     immediate.X_add_symbol = dot();
438                     immediate.X_op = O_symbol;
439                   }
440                   op->type = A_DISP_PC;
441                 }
442               else
443                 {
444                   as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
445                 }
446             }
447           else
448             {
449               as_bad ("syntax error in @(disp,[Rn, gbr, pc])");
450             }
451         }
452       src += len;
453       if (src[0] != ')')
454         as_bad ("expecting )");
455       else
456         src++;
457     }
458   else
459     {
460       src += parse_reg (src, &mode, &(op->reg));
461       if (mode != A_REG_N)
462         {
463           as_bad ("illegal register after @");
464         }
465       if (src[0] == '+')
466         {
467           op->type = A_INC_N;
468           src++;
469         }
470       else
471         {
472           op->type = A_IND_N;
473         }
474     }
475   return src;
476 }
477
478 static void
479 get_operand (ptr, op)
480      char **ptr;
481      sh_operand_info *op;
482 {
483   char *src = *ptr;
484   int mode = -1;
485   unsigned int len;
486
487   if (src[0] == '#')
488     {
489       src++;
490       *ptr = parse_exp (src);
491       op->type = A_IMM;
492       return;
493     }
494
495   else if (src[0] == '@')
496     {
497       *ptr = parse_at (src, op);
498       return;
499     }
500   len = parse_reg (src, &mode, &(op->reg));
501   if (len)
502     {
503       *ptr = src + len;
504       op->type = mode;
505       return;
506     }
507   else
508     {
509       /* Not a reg, the only thing left is a displacement */
510       *ptr = parse_exp (src);
511       op->type = A_DISP_PC;
512       return;
513     }
514 }
515
516 static
517 char *
518 get_operands (info, args, operand)
519      sh_opcode_info *info;
520      char *args;
521      sh_operand_info *operand;
522
523 {
524   char *ptr = args;
525   if (info->arg[0])
526     {
527       ptr++;
528
529       get_operand (&ptr, operand + 0);
530       if (info->arg[1])
531         {
532           if (*ptr == ',')
533             {
534               ptr++;
535             }
536           get_operand (&ptr, operand + 1);
537         }
538       else
539         {
540           operand[1].type = 0;
541         }
542     }
543   else
544     {
545       operand[0].type = 0;
546       operand[1].type = 0;
547     }
548   return ptr;
549 }
550
551 /* Passed a pointer to a list of opcodes which use different
552    addressing modes, return the opcode which matches the opcodes
553    provided
554  */
555
556 static
557 sh_opcode_info *
558 get_specific (opcode, operands)
559      sh_opcode_info *opcode;
560      sh_operand_info *operands;
561 {
562   sh_opcode_info *this_try = opcode;
563   char *name = opcode->name;
564   int arg_to_test = 0;
565   int n = 0;
566   while (opcode->name)
567     {
568       this_try = opcode++;
569       if (this_try->name != name)
570         {
571           /* We've looked so far down the table that we've run out of
572              opcodes with the same name */
573           return 0;
574         }
575       /* look at both operands needed by the opcodes and provided by
576          the user - since an arg test will often fail on the same arg
577          again and again, we'll try and test the last failing arg the
578          first on each opcode try */
579
580       for (n = 0; this_try->arg[n]; n++)
581         {
582           sh_operand_info *user = operands + arg_to_test;
583           sh_arg_type arg = this_try->arg[arg_to_test];
584           switch (arg)
585             {
586             case A_IMM:
587             case A_BDISP12:
588             case A_BDISP8:
589             case A_DISP_GBR:
590             case A_DISP_PC:
591             case A_MACH:
592             case A_PR:
593             case A_MACL:
594               if (user->type != arg)
595                 goto fail;
596               break;
597             case A_R0:
598               /* opcode needs r0 */
599               if (user->type != A_REG_N || user->reg != 0)
600                 goto fail;
601               break;
602             case A_R0_GBR:
603               if (user->type != A_R0_GBR || user->reg != 0)
604                 goto fail;
605               break;
606
607             case A_REG_N:
608             case A_INC_N:
609             case A_DEC_N:
610             case A_IND_N:
611             case A_IND_R0_REG_N:
612             case A_DISP_REG_N:
613 /* start-sanitize-sh3e */
614             case F_REG_N:
615             case FPUL_N:
616             case FPSCR_N:
617 /* end-sanitize-sh3e */
618               /* Opcode needs rn */
619               if (user->type != arg)
620                 goto fail;
621               reg_n = user->reg;
622               break;
623             case A_GBR:
624             case A_SR:
625             case A_VBR:
626               if (user->type != arg)
627                 goto fail;
628               break;
629
630             case A_REG_M:
631             case A_INC_M:
632             case A_DEC_M:
633             case A_IND_M:
634             case A_IND_R0_REG_M:
635             case A_DISP_REG_M:
636               /* Opcode needs rn */
637               if (user->type != arg - A_REG_M + A_REG_N)
638                 goto fail;
639               reg_m = user->reg;
640               break;
641
642 /* start-sanitize-sh3e */
643             case F_REG_M:
644             case FPUL_M:
645             case FPSCR_M:
646               /* Opcode needs rn */
647               if (user->type != arg - F_REG_M + F_REG_N)
648                 goto fail;
649               reg_m = user->reg;
650               break;
651 /* end-sanitize-sh3e */
652         
653             default:
654               printf ("unhandled %d\n", arg);
655               goto fail;
656             }
657           /* If we did 0, test 1 next, else 0 */
658           arg_to_test = 1 - arg_to_test;
659         }
660       return this_try;
661     fail:;
662     }
663
664   return 0;
665 }
666
667 int
668 check (operand, low, high)
669      expressionS *operand;
670      int low;
671      int high;
672 {
673   if (operand->X_op != O_constant
674       || operand->X_add_number < low
675       || operand->X_add_number > high)
676     {
677       as_bad ("operand must be absolute in range %d..%d", low, high);
678     }
679   return operand->X_add_number;
680 }
681
682
683 static void
684 insert (where, how, pcrel)
685      char *where;
686      int how;
687      int pcrel;
688 {
689   fix_new_exp (frag_now,
690                where - frag_now->fr_literal,
691                2,
692                &immediate,
693                pcrel,
694                how);
695 }
696
697 static void
698 build_relax (opcode)
699      sh_opcode_info *opcode;
700 {
701   int high_byte = shl ? 1 : 0 ;
702   char *p;
703
704   if (opcode->arg[0] == A_BDISP8)
705     {
706       p = frag_var (rs_machine_dependent,
707                     md_relax_table[C (COND_JUMP, COND32)].rlx_length,
708                     md_relax_table[C (COND_JUMP, COND8)].rlx_length,
709                     C (COND_JUMP, 0),
710                     immediate.X_add_symbol,
711                     immediate.X_add_number,
712                     0);
713       p[high_byte] = (opcode->nibbles[0] << 4) | (opcode->nibbles[1]);
714     }
715   else if (opcode->arg[0] == A_BDISP12)
716     {
717       p = frag_var (rs_machine_dependent,
718                     md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length,
719                     md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length,
720                     C (UNCOND_JUMP, 0),
721                     immediate.X_add_symbol,
722                     immediate.X_add_number,
723                     0);
724       p[high_byte] = (opcode->nibbles[0] << 4);
725     }
726
727 }
728
729 /* Now we know what sort of opcodes it is, lets build the bytes -
730  */
731 static void
732 build_Mytes (opcode, operand)
733      sh_opcode_info *opcode;
734      sh_operand_info *operand;
735
736 {
737   int index;
738   char nbuf[4];
739   char *output = frag_more (2);
740   int low_byte = shl ? 0 : 1;
741   nbuf[0] = 0;
742   nbuf[1] = 0;
743   nbuf[2] = 0;
744   nbuf[3] = 0;
745
746   for (index = 0; index < 4; index++)
747     {
748       sh_nibble_type i = opcode->nibbles[index];
749       if (i < 16)
750         {
751           nbuf[index] = i;
752         }
753       else
754         {
755           switch (i)
756             {
757             case REG_N:
758               nbuf[index] = reg_n;
759               break;
760             case REG_M:
761               nbuf[index] = reg_m;
762               break;
763             case DISP_4:
764               insert (output + low_byte, R_SH_IMM4, 0);
765               break;
766             case IMM_4BY4:
767               insert (output + low_byte, R_SH_IMM4BY4, 0);
768               break;
769             case IMM_4BY2:
770               insert (output + low_byte, R_SH_IMM4BY2, 0);
771               break;
772             case IMM_4:
773               insert (output + low_byte, R_SH_IMM4, 0);
774               break;
775             case IMM_8BY4:
776               insert (output + low_byte, R_SH_IMM8BY4, 0);
777               break;
778             case IMM_8BY2:
779               insert (output + low_byte, R_SH_IMM8BY2, 0);
780               break;
781             case IMM_8:
782               insert (output + low_byte, R_SH_IMM8, 0);
783               break;
784             case PCRELIMM_8BY4:
785               insert (output, R_SH_PCRELIMM8BY4, 1);
786               break;
787             case PCRELIMM_8BY2:
788               insert (output, R_SH_PCRELIMM8BY2, 1);
789               break;
790             default:
791               printf ("failed for %d\n", i);
792             }
793         }
794     }
795   if (shl) {
796     output[1] = (nbuf[0] << 4) | (nbuf[1]);
797     output[0] = (nbuf[2] << 4) | (nbuf[3]);
798   }
799   else {
800     output[0] = (nbuf[0] << 4) | (nbuf[1]);
801     output[1] = (nbuf[2] << 4) | (nbuf[3]);
802   }
803 }
804
805 /* This is the guts of the machine-dependent assembler.  STR points to a
806    machine dependent instruction.  This function is supposed to emit
807    the frags/bytes it assembles to.
808  */
809
810 void
811 md_assemble (str)
812      char *str;
813 {
814   unsigned char *op_start;
815   unsigned char *op_end;
816   sh_operand_info operand[2];
817   sh_opcode_info *opcode;
818   char name[20];
819   int nlen = 0;
820   /* Drop leading whitespace */
821   while (*str == ' ')
822     str++;
823
824   /* find the op code end */
825   for (op_start = op_end = (unsigned char *) (str);
826        *op_end
827        && nlen < 20
828        && !is_end_of_line[*op_end] && *op_end != ' ';
829        op_end++)
830     {
831       name[nlen] = op_start[nlen];
832       nlen++;
833     }
834   name[nlen] = 0;
835
836   if (nlen == 0)
837     {
838       as_bad ("can't find opcode ");
839     }
840
841   opcode = (sh_opcode_info *) hash_find (opcode_hash_control, name);
842
843   if (opcode == NULL)
844     {
845       as_bad ("unknown opcode");
846       return;
847     }
848
849   if (opcode->arg[0] == A_BDISP12
850       || opcode->arg[0] == A_BDISP8)
851     {
852       parse_exp (op_end + 1);
853       build_relax (opcode);
854     }
855   else
856     {
857       if (opcode->arg[0] != A_END)
858         {
859           get_operands (opcode, op_end, operand);
860         }
861       opcode = get_specific (opcode, operand);
862
863       if (opcode == 0)
864         {
865           /* Couldn't find an opcode which matched the operands */
866           char *where = frag_more (2);
867
868           where[0] = 0x0;
869           where[1] = 0x0;
870           as_bad ("invalid operands for opcode");
871           return;
872         }
873
874       build_Mytes (opcode, operand);
875     }
876
877 }
878
879 void
880 DEFUN (tc_crawl_symbol_chain, (headers),
881        object_headers * headers)
882 {
883   printf ("call to tc_crawl_symbol_chain \n");
884 }
885
886 symbolS *
887 DEFUN (md_undefined_symbol, (name),
888        char *name)
889 {
890   return 0;
891 }
892
893 void
894 DEFUN (tc_headers_hook, (headers),
895        object_headers * headers)
896 {
897   printf ("call to tc_headers_hook \n");
898 }
899
900 /* Various routines to kill one day */
901 /* Equal to MAX_PRECISION in atof-ieee.c */
902 #define MAX_LITTLENUMS 6
903
904 /* Turn a string in input_line_pointer into a floating point constant of type
905    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
906    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
907  */
908 char *
909 md_atof (type, litP, sizeP)
910      int type;
911      char *litP;
912      int *sizeP;
913 {
914   int prec;
915   LITTLENUM_TYPE words[4];
916   char *t;
917   int i;
918
919   switch (type)
920     {
921     case 'f':
922       prec = 2;
923       break;
924
925     case 'd':
926       prec = 4;
927       break;
928
929     default:
930       *sizeP = 0;
931       return "bad call to md_atof";
932     }
933
934   t = atof_ieee (input_line_pointer, type, words);
935   if (t)
936     input_line_pointer = t;
937
938   *sizeP = prec * 2;
939
940   if (shl)
941     {
942       for (i = prec - 1; i >= 0; i--)
943         {
944           md_number_to_chars (litP, (valueT) words[i], 2);
945           litP += 2;
946         }
947     }
948   else
949     {
950       for (i = 0; i < prec; i++)
951         {
952           md_number_to_chars (litP, (valueT) words[i], 2);
953           litP += 2;
954         }
955     }
956      
957   return NULL;
958 }
959
960 /* Handle the .uses pseudo-op.  This pseudo-op is used just before a
961    call instruction.  It refers to a label of the instruction which
962    loads the register which the call uses.  We use it to generate a
963    special reloc for the linker.  */
964
965 static void
966 s_uses (ignore)
967      int ignore;
968 {
969   expressionS ex;
970
971   if (! sh_relax)
972     as_warn (".uses pseudo-op seen when not relaxing");
973
974   expression (&ex);
975
976   if (ex.X_op != O_symbol || ex.X_add_number != 0)
977     {
978       as_bad ("bad .uses format");
979       ignore_rest_of_line ();
980       return;
981     }
982
983   fix_new_exp (frag_now, frag_now_fix (), 2, &ex, 1, R_SH_USES);
984
985   demand_empty_rest_of_line ();
986 }
987 \f
988 CONST char *md_shortopts = "";
989 struct option md_longopts[] = {
990
991 #define OPTION_RELAX  (OPTION_MD_BASE)
992 #define OPTION_LITTLE (OPTION_MD_BASE+1)
993
994   {"relax", no_argument, NULL, OPTION_RELAX},
995   {"little", no_argument, NULL, OPTION_LITTLE},
996   {NULL, no_argument, NULL, 0}
997 };
998 size_t md_longopts_size = sizeof(md_longopts);
999
1000 int
1001 md_parse_option (c, arg)
1002      int c;
1003      char *arg;
1004 {
1005   switch (c)
1006     {
1007     case OPTION_RELAX:
1008       sh_relax = 1;
1009       break;
1010     case OPTION_LITTLE:
1011       shl = 1;
1012       break;
1013
1014     default:
1015       return 0;
1016     }
1017
1018   return 1;
1019 }
1020
1021 void
1022 md_show_usage (stream)
1023      FILE *stream;
1024 {
1025   fprintf(stream, "\
1026 SH options:\n\
1027 -little                 generate little endian code\n\
1028 -relax                  alter jump instructions for long displacements\n");
1029 }
1030 \f
1031 int md_short_jump_size;
1032
1033 void
1034 tc_Nout_fix_to_chars ()
1035 {
1036   printf ("call to tc_Nout_fix_to_chars \n");
1037   abort ();
1038 }
1039
1040 void
1041 md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1042      char *ptr;
1043      addressT from_Nddr;
1044      addressT to_Nddr;
1045      fragS *frag;
1046      symbolS *to_symbol;
1047 {
1048   as_fatal ("failed sanity check.");
1049 }
1050
1051 void
1052 md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol)
1053      char *ptr;
1054      addressT from_Nddr, to_Nddr;
1055      fragS *frag;
1056      symbolS *to_symbol;
1057 {
1058   as_fatal ("failed sanity check.");
1059 }
1060
1061 /* This is function is called after the symbol table has been
1062    completed, but before md_convert_frag has been called.  If we have
1063    seen any .uses pseudo-ops, they point to an instruction which loads
1064    a register with the address of a function.  We look through the
1065    fixups to find where the function address is being loaded from.  We
1066    then generate a COUNT reloc giving the number of times that
1067    function address is referred to.  The linker uses this information
1068    when doing relaxing, to decide when it can eliminate the stored
1069    function address entirely.  */
1070
1071 void
1072 sh_coff_frob_file ()
1073 {
1074   int iseg;
1075
1076   if (! sh_relax)
1077     return;
1078
1079   for (iseg = SEG_E0; iseg < SEG_UNKNOWN; iseg++)
1080     {
1081       fixS *fix;
1082
1083       for (fix = segment_info[iseg].fix_root; fix != NULL; fix = fix->fx_next)
1084         {
1085           symbolS *sym;
1086           bfd_vma val;
1087           bfd_vma paddr;
1088           fixS *fscan;
1089           int iscan;
1090           int count;
1091
1092           if (fix->fx_r_type != R_SH_USES)
1093             continue;
1094
1095           /* The R_SH_USES reloc should refer to a defined local
1096              symbol in the same section.  */
1097           sym = fix->fx_addsy;
1098           if (sym == NULL
1099               || fix->fx_subsy != NULL
1100               || fix->fx_addnumber != 0
1101               || S_GET_SEGMENT (sym) != iseg
1102               || S_GET_STORAGE_CLASS (sym) == C_EXT)
1103             {
1104               as_warn_where (fix->fx_file, fix->fx_line,
1105                              ".uses does not refer to a local symbol in the same section");
1106               continue;
1107             }
1108
1109           /* Look through the fixups again, this time looking for one
1110              at the same location as sym.  */
1111           val = S_GET_VALUE (sym);
1112           paddr = segment_info[iseg].scnhdr.s_paddr;
1113           for (fscan = segment_info[iseg].fix_root;
1114                fscan != NULL;
1115                fscan = fscan->fx_next)
1116             if (val == paddr + fscan->fx_frag->fr_address + fscan->fx_where)
1117               break;
1118           if (fscan == NULL)
1119             {
1120               as_warn_where (fix->fx_file, fix->fx_line,
1121                              "can't find fixup pointed to by .uses");
1122               continue;
1123             }
1124
1125           if (fscan->fx_tcbit)
1126             {
1127               /* We've already done this one.  */
1128               continue;
1129             }
1130
1131           /* fscan should also be a fixup to a local symbol in the same
1132              section.  */
1133           sym = fscan->fx_addsy;
1134           if (sym == NULL
1135               || fscan->fx_subsy != NULL
1136               || fscan->fx_addnumber != 0
1137               || S_GET_SEGMENT (sym) != iseg
1138               || S_GET_STORAGE_CLASS (sym) == C_EXT)
1139             {
1140               as_warn_where (fix->fx_file, fix->fx_line,
1141                              ".uses target does not refer to a local symbol in the same section");
1142               continue;
1143             }
1144
1145           /* Now we look through all the fixups of all the sections,
1146              counting the number of times we find a reference to sym.  */
1147           count = 0;
1148           for (iscan = SEG_E0; iscan < SEG_UNKNOWN; iscan++)
1149             {
1150               paddr = segment_info[iscan].scnhdr.s_paddr;
1151               for (fscan = segment_info[iscan].fix_root;
1152                    fscan != NULL;
1153                    fscan = fscan->fx_next)
1154                 {
1155                   if (fscan->fx_addsy == sym)
1156                     {
1157                       ++count;
1158                       fscan->fx_tcbit = 1;
1159                     }
1160                 }
1161             }
1162
1163           if (count < 1)
1164             abort ();
1165
1166           /* Generate a R_SH_COUNT fixup at the location of sym.  We
1167              have already adjusted the value of sym to include the
1168              fragment address, so we undo that adjustment here.  */
1169           subseg_change (iseg, 0);
1170           fix_new (sym->sy_frag, S_GET_VALUE (sym) - sym->sy_frag->fr_address,
1171                    4, &abs_symbol, count, 0, R_SH_COUNT);
1172         }
1173     }
1174 }
1175
1176 /* Called after relaxing.  Set the correct sizes of the fragments, and
1177    create relocs so that md_apply_fix will fill in the correct values.  */
1178
1179 void
1180 md_convert_frag (headers, seg, fragP)
1181      object_headers *headers;
1182      segT seg;
1183      fragS *fragP;
1184 {
1185   int donerelax = 0;
1186
1187   switch (fragP->fr_subtype)
1188     {
1189     case C (COND_JUMP, COND8):
1190       subseg_change (seg, 0);
1191       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1192                1, R_SH_PCDISP8BY2);
1193       fragP->fr_fix += 2;
1194       fragP->fr_var = 0;
1195       break;
1196
1197     case C (UNCOND_JUMP, UNCOND12):
1198       subseg_change (seg, 0);
1199       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol, fragP->fr_offset,
1200                1, R_SH_PCDISP);
1201       fragP->fr_fix += 2;
1202       fragP->fr_var = 0;
1203       break;
1204
1205     case C (UNCOND_JUMP, UNCOND32):
1206     case C (UNCOND_JUMP, UNDEF_WORD_DISP):
1207       if (fragP->fr_symbol == NULL)
1208         as_bad ("at 0x%lx, displacement overflows 12-bit field",
1209                 (unsigned long) fragP->fr_address);
1210       else
1211         as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 12-bit field",
1212                 (unsigned long) fragP->fr_address,              
1213                 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1214                 S_GET_NAME (fragP->fr_symbol));
1215
1216 #if 0                           /* This code works, but generates poor code and the compiler
1217                                    should never produce a sequence that requires it to be used.  */
1218
1219       /* A jump wont fit in 12 bits, make code which looks like
1220          bra foo
1221          mov.w @(0, PC), r14
1222          .long disp
1223          foo: bra @r14
1224          */
1225       int t = buffer[0] & 0x10;
1226
1227       buffer[highbyte] = 0xa0;  /* branch over move and disp */
1228       buffer[lowbyte] = 3;
1229       buffer[highbyte+2] = 0xd0 | JREG; /* Build mov insn */
1230       buffer[lowbyte+2] = 0x00;
1231
1232       buffer[highbyte+4] = 0;   /* space for 32 bit jump disp */
1233       buffer[lowbyte+4] = 0;
1234       buffer[highbyte+6] = 0;
1235       buffer[lowbyte+6] = 0;
1236
1237       buffer[highbyte+8] = 0x40 | JREG; /* Build jmp @JREG */
1238       buffer[lowbyte+8] = t ? 0xb : 0x2b;
1239
1240       buffer[highbyte+10] = 0x20; /* build nop */
1241       buffer[lowbyte+10] = 0x0b;
1242
1243       /* Make reloc for the long disp */
1244       fix_new (fragP,
1245                fragP->fr_fix + 4,
1246                4,
1247                fragP->fr_symbol,
1248                fragP->fr_offset,
1249                0,
1250                R_SH_IMM32);
1251       fragP->fr_fix += UNCOND32_LENGTH;
1252       fragP->fr_var = 0;
1253       donerelax = 1;
1254 #endif
1255
1256       break;
1257
1258     case C (COND_JUMP, COND12):
1259       /* A bcond won't fit, so turn it into a b!cond; bra disp; nop */
1260       {
1261         unsigned char *buffer =
1262           (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1263         int highbyte = shl ? 1 : 0;
1264         int lowbyte = shl ? 0 : 1;
1265
1266         /* Toggle the true/false bit of the bcond.  */
1267         buffer[highbyte] ^= 0x2;
1268
1269         /* Build a relocation to six bytes farther on.  */
1270         subseg_change (seg, 0);
1271         fix_new (fragP, fragP->fr_fix, 2,
1272                  segment_info[seg].dot,
1273                  fragP->fr_address + fragP->fr_fix + 6,
1274                  1, R_SH_PCDISP8BY2);
1275
1276         /* Set up a jump instruction.  */
1277         buffer[highbyte + 2] = 0xa0;
1278         buffer[lowbyte + 2] = 0;
1279         fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
1280                  fragP->fr_offset, 1, R_SH_PCDISP);
1281
1282         /* Fill in a NOP instruction.  */
1283         buffer[highbyte + 4] = 0x0;
1284         buffer[lowbyte + 4] = 0x9;
1285
1286         fragP->fr_fix += 6;
1287         fragP->fr_var = 0;
1288         donerelax = 1;
1289       }
1290       break;
1291
1292     case C (COND_JUMP, COND32):
1293     case C (COND_JUMP, UNDEF_WORD_DISP):
1294       if (fragP->fr_symbol == NULL)
1295         as_bad ("at %0xlx, displacement overflows 8-bit field", 
1296                 (unsigned long) fragP->fr_address);
1297       else  
1298         as_bad ("at 0x%lx, displacement to %sdefined symbol %s overflows 8-bit field ",
1299                 (unsigned long) fragP->fr_address,              
1300                 S_IS_DEFINED (fragP->fr_symbol) ? "" : "un",
1301                 S_GET_NAME (fragP->fr_symbol));
1302
1303 #if 0                           /* This code works, but generates poor code, and the compiler
1304                                    should never produce a sequence that requires it to be used.  */
1305
1306       /* A bcond won't fit and it won't go into a 12 bit
1307          displacement either, the code sequence looks like:
1308          b!cond foop
1309          mov.w @(n, PC), r14
1310          jmp  @r14
1311          nop
1312          .long where
1313          foop:
1314          */
1315
1316       buffer[0] ^= 0x2;         /* Toggle T/F bit */
1317 #define JREG 14
1318       buffer[1] = 5;            /* branch over mov, jump, nop and ptr */
1319       buffer[2] = 0xd0 | JREG;  /* Build mov insn */
1320       buffer[3] = 0x2;
1321       buffer[4] = 0x40 | JREG;  /* Build jmp @JREG */
1322       buffer[5] = 0x0b;
1323       buffer[6] = 0x20;         /* build nop */
1324       buffer[7] = 0x0b;
1325       buffer[8] = 0;            /* space for 32 bit jump disp */
1326       buffer[9] = 0;
1327       buffer[10] = 0;
1328       buffer[11] = 0;
1329       buffer[12] = 0;
1330       buffer[13] = 0;
1331       /* Make reloc for the long disp */
1332       fix_new (fragP,
1333                fragP->fr_fix + 8,
1334                4,
1335                fragP->fr_symbol,
1336                fragP->fr_offset,
1337                0,
1338                R_SH_IMM32);
1339       fragP->fr_fix += COND32_LENGTH;
1340       fragP->fr_var = 0;
1341       donerelax = 1;
1342 #endif
1343
1344       break;
1345
1346     default:
1347       abort ();
1348     }
1349
1350   if (donerelax && !sh_relax)
1351     as_warn ("Offset doesn't fit at 0x%lx, trying to get to %s+0x%lx",
1352              (unsigned long) fragP->fr_address,
1353              fragP->fr_symbol ? S_GET_NAME(fragP->fr_symbol): "",
1354              (unsigned long) fragP->fr_offset);
1355 }
1356
1357 valueT
1358 DEFUN (md_section_align, (seg, size),
1359        segT seg AND
1360        valueT size)
1361 {
1362   return ((size + (1 << section_alignment[(int) seg]) - 1)
1363           & (-1 << section_alignment[(int) seg]));
1364
1365 }
1366
1367 /* When relaxing, we need to output a reloc for any .align directive
1368    that requests alignment to a four byte boundary or larger.  */
1369
1370 void
1371 sh_handle_align (frag)
1372      fragS *frag;
1373 {
1374   if (sh_relax
1375       && frag->fr_type == rs_align
1376       && frag->fr_address + frag->fr_fix > 0
1377       && frag->fr_offset > 1)
1378     fix_new (frag, frag->fr_fix, 2, &abs_symbol, frag->fr_offset, 0,
1379              R_SH_ALIGN);
1380 }
1381
1382 /* This macro decides whether a particular reloc is an entry in a
1383    switch table.  It is used when relaxing, because the linker needs
1384    to know about all such entries so that it can adjust them if
1385    necessary.  */
1386
1387 #define SWITCH_TABLE(fix)                               \
1388   ((fix)->fx_addsy != NULL                              \
1389    && (fix)->fx_subsy != NULL                           \
1390    && S_GET_SEGMENT ((fix)->fx_addsy) == text_section   \
1391    && S_GET_SEGMENT ((fix)->fx_subsy) == text_section   \
1392    && ((fix)->fx_r_type == R_SH_IMM32                   \
1393        || (fix)->fx_r_type == R_SH_IMM16                \
1394        || ((fix)->fx_r_type == 0                        \
1395            && ((fix)->fx_size == 2                      \
1396                || (fix)->fx_size == 4))))
1397
1398 /* See whether we need to force a relocation into the output file.
1399    This is used to force out switch and PC relative relocations when
1400    relaxing.  */
1401
1402 int
1403 sh_force_relocation (fix)
1404      fixS *fix;
1405 {
1406   if (! sh_relax)
1407     return 0;
1408
1409   return (fix->fx_pcrel
1410           || SWITCH_TABLE (fix)
1411           || fix->fx_r_type == R_SH_COUNT
1412           || fix->fx_r_type == R_SH_ALIGN);
1413 }
1414
1415 /* Apply a fixup to the object file.  */
1416
1417 void
1418 md_apply_fix (fixP, val)
1419      fixS *fixP;
1420      long val;
1421 {
1422   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1423   int lowbyte = shl ? 0 : 1;
1424   int highbyte = shl ? 1 : 0;
1425
1426   if (fixP->fx_r_type == 0)
1427     {
1428       if (fixP->fx_size == 2)
1429         fixP->fx_r_type = R_SH_IMM16;
1430       else if (fixP->fx_size == 4)
1431         fixP->fx_r_type = R_SH_IMM32;
1432       else if (fixP->fx_size == 1)
1433         fixP->fx_r_type = R_SH_IMM8;
1434       else
1435         abort ();
1436     }
1437
1438   switch (fixP->fx_r_type)
1439     {
1440     case R_SH_IMM4:
1441       *buf = (*buf & 0xf0) | (val & 0xf);
1442       break;
1443
1444     case R_SH_IMM4BY2:
1445       *buf = (*buf & 0xf0) | ((val >> 1) & 0xf);
1446       break;
1447
1448     case R_SH_IMM4BY4:
1449       *buf = (*buf & 0xf0) | ((val >> 2) & 0xf);
1450       break;
1451
1452     case R_SH_IMM8BY2:
1453       *buf = val >> 1;
1454       break;
1455
1456     case R_SH_IMM8BY4:
1457       *buf = val >> 2;
1458       break;
1459
1460     case R_SH_IMM8:
1461       *buf++ = val;
1462       break;
1463
1464     case R_SH_PCRELIMM8BY4:
1465       /* The lower two bits of the PC are cleared before the
1466          displacement is added in.  We can assume that the destination
1467          is on a 4 byte bounday.  If this instruction is also on a 4
1468          byte boundary, then we want
1469            (target - here) / 4
1470          and target - here is a multiple of 4.
1471          Otherwise, we are on a 2 byte boundary, and we want
1472            (target - (here - 2)) / 4
1473          and target - here is not a multiple of 4.  Computing
1474            (target - (here - 2)) / 4 == (target - here + 2) / 4
1475          works for both cases, since in the first case the addition of
1476          2 will be removed by the division.  target - here is in the
1477          variable val.  */
1478       val = (val + 2) / 4;
1479       if (val & ~0xff)
1480         as_warn_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1481       buf[lowbyte] = val;
1482       break;
1483
1484     case R_SH_PCRELIMM8BY2:
1485       val /= 2;
1486       if (val & ~0xff)
1487         as_warn_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1488       buf[lowbyte] = val;
1489       break;
1490
1491     case R_SH_PCDISP8BY2:
1492       val /= 2;
1493       if (val < -0x80 || val > 0x7f)
1494         as_warn_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1495       buf[lowbyte] = val;
1496       break;
1497
1498     case R_SH_PCDISP:
1499       val /= 2;
1500       if (val < -0x800 || val >= 0x7ff)
1501         as_warn_where (fixP->fx_file, fixP->fx_line, "pcrel too far");
1502       buf[lowbyte] = val & 0xff;
1503       buf[highbyte] |= (val >> 8) & 0xf;
1504       break;
1505
1506     case R_SH_IMM32:
1507       if (shl) 
1508         {
1509           *buf++ = val >> 0;
1510           *buf++ = val >> 8;
1511           *buf++ = val >> 16;
1512           *buf++ = val >> 24;
1513         }
1514       else 
1515         {
1516           *buf++ = val >> 24;
1517           *buf++ = val >> 16;
1518           *buf++ = val >> 8;
1519           *buf++ = val >> 0;
1520         }
1521       break;
1522
1523     case R_SH_IMM16:
1524       if (shl) 
1525         {
1526           *buf++ = val >> 0;
1527           *buf++ = val >> 8;
1528         } 
1529       else 
1530         {
1531           *buf++ = val >> 8;
1532           *buf++ = val >> 0;
1533         }
1534       break;
1535
1536     case R_SH_USES:
1537       /* Pass the value into sh_coff_reloc_mangle.  */
1538       fixP->fx_addnumber = val;
1539       break;
1540
1541     case R_SH_COUNT:
1542     case R_SH_ALIGN:
1543       /* Nothing to do here.  */
1544       break;
1545
1546     default:
1547       abort ();
1548     }
1549 }
1550
1551 int md_long_jump_size;
1552
1553 /* Called just before address relaxation.  Return the length
1554    by which a fragment must grow to reach it's destination.  */
1555
1556 int
1557 md_estimate_size_before_relax (fragP, segment_type)
1558      register fragS *fragP;
1559      register segT segment_type;
1560 {
1561   switch (fragP->fr_subtype)
1562     {
1563     case C (UNCOND_JUMP, UNDEF_DISP):
1564       /* used to be a branch to somewhere which was unknown */
1565       if (!fragP->fr_symbol)
1566         {
1567           fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1568           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1569         }
1570       else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1571         {
1572           fragP->fr_subtype = C (UNCOND_JUMP, UNCOND12);
1573           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND12)].rlx_length;
1574         }
1575       else
1576         {
1577           fragP->fr_subtype = C (UNCOND_JUMP, UNDEF_WORD_DISP);
1578           fragP->fr_var = md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1579           return md_relax_table[C (UNCOND_JUMP, UNCOND32)].rlx_length;
1580         }
1581       break;
1582
1583     default:
1584       abort ();
1585     case C (COND_JUMP, UNDEF_DISP):
1586       /* used to be a branch to somewhere which was unknown */
1587       if (fragP->fr_symbol
1588           && S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1589         {
1590           /* Got a symbol and it's defined in this segment, become byte
1591              sized - maybe it will fix up */
1592           fragP->fr_subtype = C (COND_JUMP, COND8);
1593           fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1594         }
1595       else if (fragP->fr_symbol)
1596         {
1597           /* Its got a segment, but its not ours, so it will always be long */
1598           fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP);
1599           fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1600           return md_relax_table[C (COND_JUMP, COND32)].rlx_length;
1601         }
1602       else
1603         {
1604           /* We know the abs value */
1605           fragP->fr_subtype = C (COND_JUMP, COND8);
1606           fragP->fr_var = md_relax_table[C (COND_JUMP, COND8)].rlx_length;
1607         }
1608
1609       break;
1610     }
1611   return fragP->fr_var;
1612 }
1613
1614 /* Put number into target byte order */
1615
1616 void
1617 md_number_to_chars (ptr, use, nbytes)
1618      char *ptr;
1619      valueT use;
1620      int nbytes;
1621 {
1622   if (shl)
1623     number_to_chars_littleendian (ptr, use, nbytes);
1624   else
1625     number_to_chars_bigendian (ptr, use, nbytes);
1626 }
1627
1628 long
1629 md_pcrel_from (fixP)
1630      fixS *fixP;
1631 {
1632   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address + 2;
1633 }
1634
1635 int
1636 tc_coff_sizemachdep (frag)
1637      fragS *frag;
1638 {
1639   return md_relax_table[frag->fr_subtype].rlx_length;
1640 }
1641
1642 #ifdef OBJ_COFF
1643
1644 /* Adjust a reloc for the SH.  This is similar to the generic code,
1645    but does some minor tweaking.  */
1646
1647 void
1648 sh_coff_reloc_mangle (seg, fix, intr, paddr)
1649      segment_info_type *seg;
1650      fixS *fix;
1651      struct internal_reloc *intr;
1652      unsigned int paddr;
1653 {
1654   symbolS *symbol_ptr = fix->fx_addsy;
1655   symbolS *dot;
1656
1657   intr->r_vaddr = paddr + fix->fx_frag->fr_address + fix->fx_where;
1658
1659   if (! SWITCH_TABLE (fix))
1660     {
1661       intr->r_type = fix->fx_r_type;
1662       intr->r_offset = 0;
1663     }
1664   else
1665     {
1666       know (sh_relax);
1667
1668       if (fix->fx_r_type == R_SH_IMM16)
1669         intr->r_type = R_SH_SWITCH16;
1670       else if (fix->fx_r_type == R_SH_IMM32)
1671         intr->r_type = R_SH_SWITCH32;
1672       else
1673         abort ();
1674
1675       /* For a switch reloc, we set r_offset to the difference between
1676          the reloc address and the subtrahend.  When the linker is
1677          doing relaxing, it can use the determine the starting and
1678          ending points of the switch difference expression.  */
1679       intr->r_offset = intr->r_vaddr - S_GET_VALUE (fix->fx_subsy);
1680     }
1681
1682   /* PC relative relocs are always against the current section.  */
1683   if (symbol_ptr == NULL)
1684     {
1685       switch (fix->fx_r_type)
1686         {
1687         case R_SH_PCRELIMM8BY2:
1688         case R_SH_PCRELIMM8BY4:
1689         case R_SH_PCDISP8BY2:
1690         case R_SH_PCDISP:
1691         case R_SH_USES:
1692           symbol_ptr = seg->dot;
1693           break;
1694         default:
1695           break;
1696         }
1697     }
1698
1699   if (fix->fx_r_type == R_SH_USES)
1700     {
1701       /* We can't store the offset in the object file, since this
1702          reloc does not take up any space, so we store it in r_offset.
1703          The fx_addnumber field was set in md_apply_fix.  */
1704       intr->r_offset = fix->fx_addnumber;
1705     }
1706   else if (fix->fx_r_type == R_SH_COUNT)
1707     {
1708       /* We can't store the count in the object file, since this reloc
1709          does not take up any space, so we store it in r_offset.  The
1710          fx_offset field was set when the fixup was created in
1711          sh_coff_frob_file.  */
1712       intr->r_offset = fix->fx_offset;
1713       /* This reloc is always absolute.  */
1714       symbol_ptr = NULL;
1715     }
1716   else if (fix->fx_r_type == R_SH_ALIGN)
1717     {
1718       /* Store the alignment in the r_offset field.  */
1719       intr->r_offset = fix->fx_offset;
1720       /* This reloc is always absolute.  */
1721       symbol_ptr = NULL;
1722     }
1723
1724   /* Turn the segment of the symbol into an offset.  */
1725   if (symbol_ptr != NULL)
1726     {
1727       dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1728       if (dot != NULL)
1729         intr->r_symndx = dot->sy_number;
1730       else
1731         intr->r_symndx = symbol_ptr->sy_number;
1732     }
1733   else
1734     intr->r_symndx = -1;
1735 }
1736
1737 #endif