OSDN Git Service

Convert unmaintained files over to ISO-C90 and fix formatting.
[pf3gnuchains/pf3gnuchains3x.git] / gas / config / tc-or32.c
1 /* Assembly backend for the OpenRISC 1000.
2    Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
3    Contributed by Damjan Lampret <lampret@opencores.org>.
4    Modified bu Johan Rydberg, <johan.rydberg@netinsight.se>.
5    Based upon a29k port.
6
7    This file is part of GAS, the GNU Assembler.
8
9    GAS is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2, or (at your option)
12    any later version.
13
14    GAS is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with GAS; see the file COPYING.  If not, write to
21    the Free Software Foundation, 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 /* tc-a29k.c used as a template.  */
25
26 #include "safe-ctype.h"
27 #include "as.h"
28 #include "opcode/or32.h"
29
30 #ifdef BFD_ASSEMBLER
31 #include "elf/or32.h"
32 #endif
33
34 #define DEBUG 0
35
36 #ifndef REGISTER_PREFIX
37 #define REGISTER_PREFIX   '%'
38 #endif
39
40 /* Make it easier to clone this machine desc into another one.  */
41 #define machine_opcode  or32_opcode
42 #define machine_opcodes or32_opcodes
43 #define machine_ip      or32_ip
44 #define machine_it      or32_it
45
46 /* Handle of the OPCODE hash table.  */
47 static struct hash_control *op_hash = NULL;
48
49 struct machine_it
50 {
51   char *          error;
52   unsigned long   opcode;
53   struct nlist *  nlistp;
54   expressionS     exp;
55   int             pcrel;
56   int             reloc_offset;   /* Offset of reloc within insn.  */
57   int             reloc;
58 }
59 the_insn;
60
61 const pseudo_typeS md_pseudo_table[] =
62 {
63   {"align",   s_align_bytes,  4 },
64   {"space",   s_space,        0 },
65   {"cputype", s_ignore,       0 },
66   {"reg",     s_lsym,         0 },  /* Register equate, same as equ.  */
67   {"sect",    s_ignore,       0 },  /* Creation of coff sections.  */
68   {"proc",    s_ignore,       0 },  /* Start of a function.  */
69   {"endproc", s_ignore,       0 },  /* Function end.  */
70   {"word",    cons,           4 },
71   {NULL,      0,              0 },
72 };
73
74 int md_short_jump_size  = 4;
75 int md_long_jump_size   = 4;
76
77 #if defined(BFD_HEADERS)
78 #ifdef RELSZ
79 const int md_reloc_size = RELSZ;  /* Coff headers.  */
80 #else
81 const int md_reloc_size = 12;   /* Something else headers.  */
82 #endif
83 #else
84 const int md_reloc_size = 12;   /* Not bfdized.  */
85 #endif
86
87 /* This array holds the chars that always start a comment.
88    If the pre-processor is disabled, these aren't very useful.  */
89 const char comment_chars[] = "#";
90
91 /* This array holds the chars that only start a comment at the beginning of
92    a line.  If the line seems to have the form '# 123 filename'
93    .line and .file directives will appear in the pre-processed output.  */
94 /* Note that input_file.c hand checks for '#' at the beginning of the
95    first line of the input file.  This is because the compiler outputs
96    #NO_APP at the beginning of its output.  */
97 /* Also note that comments like this one will always work.  */
98 const char line_comment_chars[] = "#";
99
100 /* We needed an unused char for line separation to work around the
101    lack of macros, using sed and such.  */
102 const char line_separator_chars[] = ";";
103
104 /* Chars that can be used to separate mant from exp in floating point nums.  */
105 const char EXP_CHARS[] = "eE";
106
107 /* Chars that mean this number is a floating point constant.
108    As in 0f12.456
109    or    0d1.2345e12.  */
110 const char FLT_CHARS[] = "rRsSfFdDxXpP";
111
112 /* "l.jalr r9" precalculated opcode.  */
113 static unsigned long jalr_r9_opcode;
114
115 static void machine_ip (char *);
116
117
118 /* Set bits in machine opcode according to insn->encoding
119    description and passed operand.  */
120
121 static void
122 encode (const struct machine_opcode *insn,
123         unsigned long *opcode,
124         signed long param_val,
125         char param_ch)
126 {
127   int opc_pos = 0;
128   int param_pos = 0;
129   char *enc;
130
131 #if DEBUG
132   printf ("    encode:  opcode=%.8lx  param_val=%.8lx abs=%.8lx param_ch=%c\n",
133           *opcode, param_val, abs (param_val), param_ch);
134 #endif
135   for (enc = insn->encoding; *enc != '\0'; enc++)
136     if (*enc == param_ch)
137       {
138         if (enc - 2 >= insn->encoding && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
139           continue;
140         else
141           param_pos ++;
142       }
143
144   opc_pos = 32;
145
146   for (enc = insn->encoding; *enc != '\0';)
147     {
148       if ((*enc == '0') && (*(enc + 1) == 'x'))
149         {
150           int tmp = strtol (enc, NULL, 16);
151
152           opc_pos -= 4;
153           *opcode |= tmp << opc_pos;
154           enc += 3;
155         }
156       else if ((*enc == '0') || (*enc == '-'))
157         {
158           opc_pos--;
159           enc++;
160         }
161       else if (*enc == '1')
162         {
163           opc_pos--;
164           *opcode |= 1 << opc_pos;
165           enc++;
166         }
167       else if (*enc == param_ch)
168         {
169           opc_pos--;
170           param_pos--;
171           *opcode |= ((param_val >> param_pos) & 0x1) << opc_pos;
172           enc++;
173         }
174       else if (ISALPHA (*enc))
175         {
176           opc_pos--;
177           enc++;
178         }
179       else
180         enc++;
181     }
182
183 #if DEBUG
184   printf ("    opcode=%.8lx\n", *opcode);
185 #endif
186 }
187
188 /* This function is called once, at assembler startup time.  It should
189    set up all the tables, etc., that the MD part of the assembler will
190    need.  */
191
192 void
193 md_begin (void)
194 {
195   const char *retval = NULL;
196   int lose = 0;
197   int skipnext = 0;
198   unsigned int i;
199
200   /* Hash up all the opcodes for fast use later.  */
201   op_hash = hash_new ();
202
203   for (i = 0; i < or32_num_opcodes; i++)
204     {
205       const char *name = machine_opcodes[i].name;
206
207       if (skipnext)
208         {
209           skipnext = 0;
210           continue;
211         }
212
213       retval = hash_insert (op_hash, name, (void *) &machine_opcodes[i]);
214       if (retval != NULL)
215         {
216           fprintf (stderr, "internal error: can't hash `%s': %s\n",
217                    machine_opcodes[i].name, retval);
218           lose = 1;
219         }
220     }
221
222   if (lose)
223     as_fatal (_("Broken assembler.  No assembly attempted."));
224
225   encode (&machine_opcodes[insn_index ("l.jalr")], &jalr_r9_opcode, 9, 'B');
226 }
227
228 /* Returns non zero if instruction is to be used.  */
229
230 static int
231 check_invalid_opcode (unsigned long opcode)
232 {
233   return opcode == jalr_r9_opcode;
234 }
235
236 /* Assemble a single instruction.  Its label has already been handled
237    by the generic front end.  We just parse opcode and operands, and
238    produce the bytes of data and relocation.  */
239
240 void
241 md_assemble (char *str)
242 {
243   char *toP;
244
245 #if DEBUG
246   printf ("NEW INSTRUCTION\n");
247 #endif
248
249   know (str);
250   machine_ip (str);
251   toP = frag_more (4);
252
253   /* Put out the opcode.  */
254   md_number_to_chars (toP, the_insn.opcode, 4);
255
256   /* Put out the symbol-dependent stuff.  */
257 #ifdef BFD_ASSEMBLER
258   if (the_insn.reloc != BFD_RELOC_NONE)
259 #else
260   if (the_insn.reloc != NO_RELOC)
261 #endif
262     {
263       fix_new_exp (frag_now,
264                    (toP - frag_now->fr_literal + the_insn.reloc_offset),
265                    4,   /* size */
266                    &the_insn.exp,
267                    the_insn.pcrel,
268                    the_insn.reloc);
269     }
270 }
271
272 /* This is true of the we have issued a "lo(" or "hi"(.  */
273 static int waiting_for_shift = 0;
274
275 static int mask_or_shift = 0;
276
277 #ifdef BFD_ASSEMBLER
278 static char *
279 parse_operand (char *s, expressionS *operandp, int opt)
280 {
281   char *save = input_line_pointer;
282   char *new;
283
284 #if DEBUG
285   printf ("  PROCESS NEW OPERAND(%s) == %c (%d)\n", s, opt ? opt : '!', opt);
286 #endif
287
288   input_line_pointer = s;
289
290   if (strncasecmp (s, "HI(", 3) == 0)
291     {
292       waiting_for_shift = 1;
293       mask_or_shift = BFD_RELOC_HI16;
294
295       input_line_pointer += 3;
296     }
297   else if (strncasecmp (s, "LO(", 3) == 0)
298     {
299       mask_or_shift = BFD_RELOC_LO16;
300
301       input_line_pointer += 3;
302     }
303   else
304     mask_or_shift = 0;
305
306   if ((*s == '(') && (*(s+1) == 'r'))
307     s++;
308
309   if ((*s == 'r') && ISDIGIT (*(s + 1)))
310     {
311       operandp->X_add_number = strtol (s + 1, NULL, 10);
312       operandp->X_op = O_register;
313       for (; (*s != ',') && (*s != '\0');)
314         s++;
315       input_line_pointer = save;
316       return s;
317     }
318
319   expression (operandp);
320
321   if (operandp->X_op == O_absent)
322     {
323       if (! opt)
324         as_bad (_("missing operand"));
325       else
326         {
327           operandp->X_add_number = 0;
328           operandp->X_op = O_constant;
329         }
330     }
331
332   new = input_line_pointer;
333   input_line_pointer = save;
334
335 #if DEBUG
336   printf ("  %s=parse_operand(%s): operandp->X_op = %u\n", new, s, operandp->X_op);
337 #endif
338
339   return new;
340 }
341 #else
342
343 static char *
344 parse_operand (char *s, expressionS *operandp, int opt)
345 {
346   char *save = input_line_pointer;
347   char *new;
348
349 #if DEBUG
350   printf ("  PROCESS NEW OPERAND(%s) == %c (%d)\n", s, opt ? opt : '!', opt);
351 #endif
352
353   input_line_pointer = s;
354
355   if (strncasecmp (s, "HI(", 3) == 0)
356     {
357       waiting_for_shift = 1;
358       mask_or_shift = RELOC_CONSTH;
359
360       input_line_pointer += 3;
361     }
362   else if (strncasecmp (s, "LO(", 3) == 0)
363     {
364       mask_or_shift = RELOC_CONST;
365
366       input_line_pointer += 3;
367     }
368   else
369     mask_or_shift = 0;
370
371   expression (operandp);
372
373   if (operandp->X_op == O_absent)
374     {
375       if (! opt)
376         as_bad (_("missing operand"));
377       else
378         {
379           operandp->X_add_number = 0;
380           operandp->X_op = O_constant;
381         }
382     }
383
384   new = input_line_pointer;
385   input_line_pointer = save;
386
387   if ((operandp->X_op == O_symbol) && (*s != '_'))
388     {
389 #if DEBUG
390       printf ("symbol: '%s'\n", save);
391 #endif
392
393       for (save = s; s < new; s++)
394         if ((*s == REGISTER_PREFIX) && (*(s + 1) == 'r')) /* Register prefix.  */
395           s++;
396
397         if ((*s == 'r') && ISDIGIT (*(s + 1)))
398           {
399             operandp->X_add_number = strtol (s + 1, NULL, 10);
400             operandp->X_op = O_register;
401           }
402       s = save;
403     }
404
405 #if DEBUG
406   printf ("  %s=parse_operand(%s): operandp->X_op = %u\n", new, s, operandp->X_op);
407 #endif
408
409   return new;
410 }
411 #endif
412
413 /* Instruction parsing.  Takes a string containing the opcode.
414    Operands are at input_line_pointer.  Output is in the_insn.
415    Warnings or errors are generated.  */
416
417 #ifdef BFD_ASSEMBLER
418 static void
419 machine_ip (char *str)
420 {
421   char *s;
422   const char *args;
423   const struct machine_opcode *insn;
424   char *argsStart;
425   unsigned long opcode;
426   expressionS the_operand;
427   expressionS *operand = &the_operand;
428   unsigned int regno;
429   int reloc = BFD_RELOC_NONE;
430
431 #if DEBUG
432   printf ("machine_ip(%s)\n", str);
433 #endif
434
435   s = str;
436   for (; ISALNUM (*s) || *s == '.'; ++s)
437     if (ISUPPER (*s))
438       *s = TOLOWER (*s);
439
440   switch (*s)
441     {
442     case '\0':
443       break;
444
445     case ' ':     /* FIXME-SOMEDAY more whitespace.  */
446       *s++ = '\0';
447       break;
448
449     default:
450       as_bad (_("unknown opcode1: `%s'"), str);
451       return;
452     }
453
454   if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
455     {
456       as_bad (_("unknown opcode2 `%s'."), str);
457       return;
458     }
459
460   argsStart = s;
461   opcode = 0;
462   memset (&the_insn, '\0', sizeof (the_insn));
463   the_insn.reloc = BFD_RELOC_NONE;
464
465   reloc = BFD_RELOC_NONE;
466
467   /* Build the opcode, checking as we go to make sure that the
468      operands match.
469
470      If an operand matches, we modify the_insn or opcode appropriately,
471      and do a "continue".  If an operand fails to match, we "break".  */
472   if (insn->args[0] != '\0')
473     /* Prime the pump.  */
474     s = parse_operand (s, operand, insn->args[0] == 'I');
475
476   for (args = insn->args;; ++args)
477     {
478 #if DEBUG
479       printf ("  args = %s\n", args);
480 #endif
481       switch (*args)
482         {
483         case '\0':    /* End of args.  */
484           /* We have have 0 args, do the bazoooka!  */
485           if (args == insn->args)
486             encode (insn, &opcode, 0, 0);
487
488           if (*s == '\0')
489             {
490               /* We are truly done.  */
491               the_insn.opcode = opcode;
492               if (check_invalid_opcode (opcode))
493                 as_bad (_("instruction not allowed: %s"), str);
494               return;
495             }
496           as_bad (_("too many operands: %s"), s);
497           break;
498
499         case ',':   /* Must match a comma.  */
500           if (*s++ == ',')
501             {
502               reloc = BFD_RELOC_NONE;
503
504               /* Parse next operand.  */
505               s = parse_operand (s, operand, args[1] == 'I');
506 #if DEBUG
507               printf ("    ',' case: operand->X_add_number = %d, *args = %s, *s = %s\n",
508                       operand->X_add_number, args, s);
509 #endif
510               continue;
511             }
512           break;
513
514         case '(':   /* Must match a (.  */
515           s = parse_operand (s, operand, args[1] == 'I');
516           continue;
517
518         case ')':   /* Must match a ).  */
519           continue;
520
521         case 'r':   /* A general register.  */
522           args++;
523
524           if (operand->X_op != O_register)
525             break;    /* Only registers.  */
526
527           know (operand->X_add_symbol == 0);
528           know (operand->X_op_symbol == 0);
529           regno = operand->X_add_number;
530           encode (insn, &opcode, regno, *args);
531 #if DEBUG
532           printf ("    r: operand->X_op = %d\n", operand->X_op);
533 #endif
534           continue;
535
536         default:
537           /* if (! ISALPHA (*args))
538                break;  */   /* Only immediate values.  */
539
540           if (mask_or_shift)
541             {
542 #if DEBUG
543               printf ("mask_or_shift = %d\n", mask_or_shift);
544 #endif
545               reloc = mask_or_shift;
546             }
547           mask_or_shift = 0;
548
549           if (strncasecmp (args, "LO(", 3) == 0)
550             {
551 #if DEBUG
552               printf ("reloc_const\n");
553 #endif
554               reloc = BFD_RELOC_LO16;
555             }
556           else if (strncasecmp (args, "HI(", 3) == 0)
557             {
558 #if DEBUG
559               printf ("reloc_consth\n");
560 #endif
561               reloc = BFD_RELOC_HI16;
562             }
563
564           if (*s == '(')
565             operand->X_op = O_constant;
566           else if (*s == ')')
567             s += 1;
568 #if DEBUG
569           printf ("    default case: operand->X_add_number = %d, *args = %s, *s = %s\n", operand->X_add_number, args, s);
570 #endif
571           if (operand->X_op == O_constant)
572             {
573               if (reloc == BFD_RELOC_NONE)
574                 {
575                   bfd_vma v, mask;
576
577                   mask = 0x3ffffff;
578                   v = abs (operand->X_add_number) & ~ mask;
579                   if (v)
580                     as_bad (_("call/jmp target out of range (1)"));
581                 }
582
583               if (reloc == BFD_RELOC_HI16)
584                 operand->X_add_number = ((operand->X_add_number >> 16) & 0xffff);
585
586               the_insn.pcrel = 0;
587               encode (insn, &opcode, operand->X_add_number, *args);
588  /*             the_insn.reloc = BFD_RELOC_NONE; */
589               continue;
590             }
591
592           if (reloc == BFD_RELOC_NONE)
593             the_insn.reloc = BFD_RELOC_32_GOT_PCREL;
594           else
595             the_insn.reloc = reloc;
596
597           /* the_insn.reloc = insn->reloc;  */
598 #if DEBUG
599           printf ("    reloc sym=%d\n", the_insn.reloc);
600           printf ("    BFD_RELOC_NONE=%d\n", BFD_RELOC_NONE);
601 #endif
602           the_insn.exp = *operand;
603
604           /*  the_insn.reloc_offset = 1;  */
605           the_insn.pcrel = 1; /* Assume PC-relative jump.  */
606
607           /* FIXME-SOON, Do we figure out whether abs later, after
608              know sym val?  */
609           if (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_HI16)
610             the_insn.pcrel = 0;
611
612           encode (insn, &opcode, operand->X_add_number, *args);
613           continue;
614         }
615
616       /* Types or values of args don't match.  */
617       as_bad (_("invalid operands"));
618       return;
619     }
620 }
621
622 #else
623
624 static void
625 machine_ip (char *str)
626 {
627   char *s;
628   const char *args;
629   const struct machine_opcode *insn;
630   char *argsStart;
631   unsigned long opcode;
632   expressionS the_operand;
633   expressionS *operand = &the_operand;
634   unsigned int regno;
635   int reloc = NO_RELOC;
636
637 #if DEBUG
638   printf ("machine_ip(%s)\n", str);
639 #endif
640
641   s = str;
642   for (; ISALNUM (*s) || *s == '.'; ++s)
643     if (ISUPPER (*s))
644       *s = TOLOWER (*s);
645
646   switch (*s)
647     {
648     case '\0':
649       break;
650
651     case ' ':     /* FIXME-SOMEDAY more whitespace.  */
652       *s++ = '\0';
653       break;
654
655     default:
656       as_bad (_("unknown opcode1: `%s'"), str);
657       return;
658     }
659
660   if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
661     {
662       as_bad (_("unknown opcode2 `%s'."), str);
663       return;
664     }
665
666   argsStart = s;
667   opcode = 0;
668   memset (&the_insn, '\0', sizeof (the_insn));
669   the_insn.reloc = NO_RELOC;
670
671   reloc = NO_RELOC;
672
673   /* Build the opcode, checking as we go to make sure that the
674      operands match.
675
676      If an operand matches, we modify the_insn or opcode appropriately,
677      and do a "continue".  If an operand fails to match, we "break".  */
678   if (insn->args[0] != '\0')
679     /* Prime the pump.  */
680     s = parse_operand (s, operand,
681                        insn->args[0] == 'I'
682                        || strcmp (insn->name, "l.nop") == 0);
683
684   for (args = insn->args;; ++args)
685     {
686 #if DEBUG
687       printf ("  args = %s\n", args);
688 #endif
689       switch (*args)
690         {
691         case '\0':    /* End of args.  */
692           /* We have have 0 args, do the bazoooka!  */
693           if (args == insn->args)
694             encode (insn, &opcode, 0, 0);
695
696           if (*s == '\0')
697             {
698               /* We are truly done.  */
699               the_insn.opcode = opcode;
700               if (check_invalid_opcode (opcode))
701                 as_bad (_("instruction not allowed: %s"), str);
702               return;
703             }
704           as_bad (_("too many operands: %s"), s);
705           break;
706
707         case ',':   /* Must match a comma.  */
708           if (*s++ == ',')
709             {
710               reloc = NO_RELOC;
711
712               /* Parse next operand.  */
713               s = parse_operand (s, operand, args[1] == 'I');
714 #if DEBUG
715               printf ("    ',' case: operand->X_add_number = %d, *args = %s, *s = %s\n",
716                       operand->X_add_number, args, s);
717 #endif
718               continue;
719             }
720           break;
721
722         case '(':   /* Must match a (.  */
723           s = parse_operand (s, operand, args[1] == 'I');
724           continue;
725
726         case ')':   /* Must match a ).  */
727           continue;
728
729         case 'r':   /* A general register.  */
730           args++;
731
732           if (operand->X_op != O_register)
733             break;    /* Only registers.  */
734
735           know (operand->X_add_symbol == 0);
736           know (operand->X_op_symbol == 0);
737           regno = operand->X_add_number;
738           encode (insn, &opcode, regno, *args);
739 #if DEBUG
740           printf ("    r: operand->X_op = %d\n", operand->X_op);
741 #endif
742           continue;
743
744         default:
745           /* if (! ISALPHA (*args))
746                break;  */   /* Only immediate values.  */
747
748           if (mask_or_shift)
749             {
750 #if DEBUG
751               printf ("mask_or_shift = %d\n", mask_or_shift);
752 #endif
753               reloc = mask_or_shift;
754             }
755           mask_or_shift = 0;
756
757           if (strncasecmp (args, "LO(", 3) == 0)
758             {
759 #if DEBUG
760               printf ("reloc_const\n");
761 #endif
762               reloc = RELOC_CONST;
763             }
764           else if (strncasecmp (args, "HI(", 3) == 0)
765             {
766 #if DEBUG
767               printf ("reloc_consth\n");
768 #endif
769               reloc = RELOC_CONSTH;
770             }
771
772           if (*s == '(')
773             operand->X_op = O_constant;
774           else if (*s == ')')
775             s += 1;
776 #if DEBUG
777           printf ("    default case: operand->X_add_number = %d, *args = %s, *s = %s\n",
778                   operand->X_add_number, args, s);
779 #endif
780           if (operand->X_op == O_constant)
781             {
782               if (reloc == NO_RELOC)
783                 {
784                   unsigned long v, mask;
785
786                   mask = 0x3ffffff;
787                   v = abs (operand->X_add_number) & ~ mask;
788                   if (v)
789                     as_bad (_("call/jmp target out of range (1)"));
790                 }
791
792               if (reloc == RELOC_CONSTH)
793                 operand->X_add_number = ((operand->X_add_number>>16) & 0xffff);
794
795               the_insn.pcrel = 0;
796               encode (insn, &opcode, operand->X_add_number, *args);
797               /* the_insn.reloc = NO_RELOC; */
798               continue;
799             }
800
801           if (reloc == NO_RELOC)
802             the_insn.reloc = RELOC_JUMPTARG;
803           else
804             the_insn.reloc = reloc;
805 #if DEBUG
806           printf ("    reloc sym=%d\n", the_insn.reloc);
807           printf ("    NO_RELOC=%d\n", NO_RELOC);
808 #endif
809           the_insn.exp = *operand;
810
811           /*  the_insn.reloc_offset = 1;  */
812           the_insn.pcrel = 1; /* Assume PC-relative jump.  */
813
814           /* FIXME-SOON, Do we figure out whether abs later, after
815              know sym val?  */
816           if (reloc == RELOC_CONST || reloc == RELOC_CONSTH)
817             the_insn.pcrel = 0;
818
819           encode (insn, &opcode, operand->X_add_number, *args);
820           continue;
821         }
822
823       /* Types or values of args don't match.  */
824       as_bad (_("invalid operands"));
825       return;
826     }
827 }
828 #endif
829
830 /* This is identical to the md_atof in m68k.c.  I think this is right,
831    but I'm not sure.
832
833    Turn a string in input_line_pointer into a floating point constant
834    of type type, and store the appropriate bytes in *litP.  The number
835    of LITTLENUMS emitted is stored in *sizeP .  An error message is
836    returned, or NULL on OK.  */
837
838 /* Equal to MAX_PRECISION in atof-ieee.c.  */
839 #define MAX_LITTLENUMS 6
840
841 char *
842 md_atof (int type, char * litP, int *  sizeP)
843 {
844   int prec;
845   LITTLENUM_TYPE words[MAX_LITTLENUMS];
846   LITTLENUM_TYPE *wordP;
847   char *t;
848
849   switch (type)
850     {
851     case 'f':
852     case 'F':
853     case 's':
854     case 'S':
855       prec = 2;
856       break;
857
858     case 'd':
859     case 'D':
860     case 'r':
861     case 'R':
862       prec = 4;
863       break;
864
865     case 'x':
866     case 'X':
867       prec = 6;
868       break;
869
870     case 'p':
871     case 'P':
872       prec = 6;
873       break;
874
875     default:
876       *sizeP = 0;
877       return _("Bad call to MD_ATOF()");
878     }
879
880   t = atof_ieee (input_line_pointer, type, words);
881   if (t)
882     input_line_pointer = t;
883
884   *sizeP = prec * sizeof (LITTLENUM_TYPE);
885
886   for (wordP = words; prec--;)
887     {
888       md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
889       litP += sizeof (LITTLENUM_TYPE);
890     }
891
892   return NULL;
893 }
894
895 /* Write out big-endian.  */
896
897 void
898 md_number_to_chars (char *buf, valueT val, int n)
899 {
900   number_to_chars_bigendian (buf, val, n);
901 }
902
903 #ifdef BFD_ASSEMBLER
904 void
905 md_apply_fix3 (fixS * fixP, valueT * val, segT seg ATTRIBUTE_UNUSED)
906 {
907   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
908   long t_val;
909
910   t_val = (long) *val;
911
912 #if DEBUG
913   printf ("md_apply_fix val:%x\n", t_val);
914 #endif
915
916   fixP->fx_addnumber = t_val; /* Remember value for emit_reloc.  */
917
918   know (fixP->fx_size == 4);
919   know (fixP->fx_r_type < BFD_RELOC_NONE);
920
921   switch (fixP->fx_r_type)
922     {
923     case BFD_RELOC_32:      /* XXXXXXXX pattern in a word.  */
924 #if DEBUG
925       printf ("reloc_const: val=%x\n", t_val);
926 #endif
927       buf[0] = t_val >> 24;
928       buf[1] = t_val >> 16;
929       buf[2] = t_val >> 8;
930       buf[3] = t_val;
931       break;
932
933     case BFD_RELOC_16:      /* XXXX0000 pattern in a word.  */
934 #if DEBUG
935       printf ("reloc_const: val=%x\n", t_val);
936 #endif
937       buf[0] = t_val >> 8;
938       buf[1] = t_val;
939       break;
940
941     case BFD_RELOC_8:      /* XX000000 pattern in a word.  */
942 #if DEBUG
943       printf ("reloc_const: val=%x\n", t_val);
944 #endif
945       buf[0] = t_val;
946       break;
947
948     case BFD_RELOC_LO16:      /* 0000XXXX pattern in a word.  */
949 #if DEBUG
950       printf ("reloc_const: val=%x\n", t_val);
951 #endif
952       buf[2] = t_val >> 8;  /* Holds bits 0000XXXX.  */
953       buf[3] = t_val;
954       break;
955
956     case BFD_RELOC_HI16:    /* 0000XXXX pattern in a word.  */
957 #if DEBUG
958       printf ("reloc_consth: val=%x\n", t_val);
959 #endif
960       buf[2] = t_val >> 24; /* Holds bits XXXX0000.  */
961       buf[3] = t_val >> 16;
962       break;
963
964     case BFD_RELOC_32_GOT_PCREL:  /* 0000XXXX pattern in a word.  */
965       if (!fixP->fx_done)
966         ;
967       else if (fixP->fx_pcrel)
968         {
969           long v = t_val >> 28;
970
971           if (v != 0 && v != -1)
972             as_bad_where (fixP->fx_file, fixP->fx_line,
973                           _("call/jmp target out of range (2)"));
974         }
975       else
976         /* This case was supposed to be handled in machine_ip.  */
977         abort ();
978
979       buf[0] |= (t_val >> 26) & 0x03; /* Holds bits 0FFFFFFC of address.  */
980       buf[1] = t_val >> 18;
981       buf[2] = t_val >> 10;
982       buf[3] = t_val >> 2;
983       break;
984
985     case BFD_RELOC_VTABLE_INHERIT:
986     case BFD_RELOC_VTABLE_ENTRY:
987       fixP->fx_done = 0;
988       break;
989
990     case BFD_RELOC_NONE:
991     default:
992       as_bad (_("bad relocation type: 0x%02x"), fixP->fx_r_type);
993       break;
994     }
995
996   if (fixP->fx_addsy == (symbolS *) NULL)
997     fixP->fx_done = 1;
998 }
999 #else
1000 void
1001 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1002 {
1003   long val = *valP;
1004   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1005
1006 #if DEBUG
1007   printf ("md_apply_fix val:%x\n", val);
1008 #endif
1009
1010   fixP->fx_addnumber = val; /* Remember value for emit_reloc.  */
1011
1012   know (fixP->fx_size == 4);
1013   know (fixP->fx_r_type < NO_RELOC);
1014
1015   /* This is a hack.  There should be a better way to handle this.  */
1016   if (fixP->fx_r_type == RELOC_WDISP30 && fixP->fx_addsy)
1017     val += fixP->fx_where + fixP->fx_frag->fr_address;
1018
1019   switch (fixP->fx_r_type)
1020     {
1021     case RELOC_32:
1022       buf[0] = val >> 24;
1023       buf[1] = val >> 16;
1024       buf[2] = val >> 8;
1025       buf[3] = val;
1026       break;
1027
1028     case RELOC_8:
1029       buf[0] = val;
1030       break;
1031
1032     case RELOC_WDISP30:
1033       val = (val >> 2) + 1;
1034       buf[0] |= (val >> 24) & 0x3f;
1035       buf[1] = (val >> 16);
1036       buf[2] = val >> 8;
1037       buf[3] = val;
1038       break;
1039
1040     case RELOC_HI22:
1041       buf[1] |= (val >> 26) & 0x3f;
1042       buf[2] = val >> 18;
1043       buf[3] = val >> 10;
1044       break;
1045
1046     case RELOC_LO10:
1047       buf[2] |= (val >> 8) & 0x03;
1048       buf[3] = val;
1049       break;
1050
1051     case RELOC_BASE13:
1052       buf[2] |= (val >> 8) & 0x1f;
1053       buf[3] = val;
1054       break;
1055
1056     case RELOC_WDISP22:
1057       val = (val >> 2) + 1;
1058       /* FALLTHROUGH */
1059     case RELOC_BASE22:
1060       buf[1] |= (val >> 16) & 0x3f;
1061       buf[2] = val >> 8;
1062       buf[3] = val;
1063       break;
1064
1065     case RELOC_JUMPTARG:  /* 0000XXXX pattern in a word.  */
1066       if (!fixP->fx_done)
1067         {
1068           /* The linker tries to support both AMD and old GNU style
1069              R_IREL relocs.  That means that if the addend is exactly
1070              the negative of the address within the section, the
1071              linker will not handle it correctly.  */
1072         }
1073       else if (fixP->fx_pcrel)
1074         {
1075           long v = val >> 28;
1076           if (v != 0 && v != -1)
1077             as_bad_where (fixP->fx_file, fixP->fx_line,
1078                           _("call/jmp target out of range (2)"));
1079         }
1080       else
1081         /* This case was supposed to be handled in machine_ip.  */
1082         abort ();
1083
1084       buf[0] |= (val >> 26) & 0x03; /* Holds bits 0FFFFFFC of address.  */
1085       buf[1] = val >> 18;
1086       buf[2] = val >> 10;
1087       buf[3] = val >> 2;
1088       break;
1089
1090     case RELOC_CONST:     /* 0000XXXX pattern in a word.  */
1091 #if DEBUG
1092       printf ("reloc_const: val=%x\n", val);
1093 #endif
1094       buf[2] = val >> 8;  /* Holds bits 0000XXXX.  */
1095       buf[3] = val;
1096       break;
1097
1098     case RELOC_CONSTH:    /* 0000XXXX pattern in a word.  */
1099 #if DEBUG
1100       printf ("reloc_consth: val=%x\n", val);
1101 #endif
1102       buf[2] = val >> 24; /* Holds bits XXXX0000.  */
1103       buf[3] = val >> 16;
1104       break;
1105
1106     case BFD_RELOC_VTABLE_INHERIT:
1107     case BFD_RELOC_VTABLE_ENTRY:
1108       fixP->fx_done = 0;
1109       break;
1110
1111     case NO_RELOC:
1112     default:
1113       as_bad (_("bad relocation type: 0x%02x"), fixP->fx_r_type);
1114       break;
1115     }
1116
1117   if (fixP->fx_addsy == (symbolS *) NULL)
1118     fixP->fx_done = 1;
1119 }
1120 #endif
1121
1122 #ifdef OBJ_COFF
1123 short
1124 tc_coff_fix2rtype (fixS *fixP)
1125 {
1126 #if DEBUG
1127   printf ("tc_coff_fix2rtype\n");
1128 #endif
1129
1130   switch (fixP->fx_r_type)
1131     {
1132     case RELOC_32:        return R_WORD;
1133     case RELOC_8:         return R_BYTE;
1134     case RELOC_CONST:     return R_ILOHALF;
1135     case RELOC_CONSTH:    return R_IHIHALF;
1136     case RELOC_JUMPTARG:  return R_IREL;
1137     default:
1138       printf ("need %d\n", fixP->fx_r_type);
1139       abort ();
1140     }
1141
1142   return 0;
1143 }
1144
1145 #endif /* OBJ_COFF */
1146
1147 /* Should never be called for or32.  */
1148
1149 void
1150 md_create_short_jump (char *    ptr       ATTRIBUTE_UNUSED,
1151                       addressT  from_addr ATTRIBUTE_UNUSED,
1152                       addressT  to_addr   ATTRIBUTE_UNUSED,
1153                       fragS *   frag      ATTRIBUTE_UNUSED,
1154                       symbolS * to_symbol ATTRIBUTE_UNUSED)
1155 {
1156   as_fatal ("or32_create_short_jmp\n");
1157 }
1158
1159 /* Should never be called for or32.  */
1160
1161 #ifndef BFD_ASSEMBLER
1162 void
1163 md_convert_frag (object_headers * headers ATTRIBUTE_UNUSED,
1164                  segT             seg     ATTRIBUTE_UNUSED,
1165                  register fragS * fragP   ATTRIBUTE_UNUSED)
1166 {
1167   as_fatal ("or32_convert_frag\n");
1168 }
1169
1170 #else
1171 void
1172 md_convert_frag (bfd *   headers ATTRIBUTE_UNUSED,
1173                  segT    seg     ATTRIBUTE_UNUSED,
1174                  fragS * fragP   ATTRIBUTE_UNUSED)
1175 {
1176   as_fatal ("or32_convert_frag\n");
1177 }
1178 #endif
1179
1180 /* Should never be called for or32.  */
1181
1182 void
1183 md_create_long_jump (char *    ptr       ATTRIBUTE_UNUSED,
1184                      addressT  from_addr ATTRIBUTE_UNUSED,
1185                      addressT  to_addr   ATTRIBUTE_UNUSED,
1186                      fragS *   frag      ATTRIBUTE_UNUSED,
1187                      symbolS * to_symbol ATTRIBUTE_UNUSED)
1188 {
1189   as_fatal ("or32_create_long_jump\n");
1190 }
1191
1192 /* Should never be called for or32.  */
1193
1194 int
1195 md_estimate_size_before_relax (fragS * fragP   ATTRIBUTE_UNUSED,
1196                                segT    segtype ATTRIBUTE_UNUSED)
1197 {
1198   as_fatal ("or32_estimate_size_before_relax\n");
1199   return 0;
1200 }
1201
1202 /* Translate internal representation of relocation info to target format.
1203
1204    On sparc/29k: first 4 bytes are normal unsigned long address, next three
1205    bytes are index, most sig. byte first.  Byte 7 is broken up with
1206    bit 7 as external, bits 6 & 5 unused, and the lower
1207    five bits as relocation type.  Next 4 bytes are long addend.  */
1208 /* Thanx and a tip of the hat to Michael Bloom, mb@ttidca.tti.com.  */
1209
1210 #ifdef OBJ_AOUT
1211 void
1212 tc_aout_fix_to_chars (char *where,
1213                       fixS *fixP,
1214                       relax_addressT segment_address_in_file)
1215 {
1216   long r_symbolnum;
1217
1218 #if DEBUG
1219   printf ("tc_aout_fix_to_chars\n");
1220 #endif
1221
1222   know (fixP->fx_r_type < BFD_RELOC_NONE);
1223   know (fixP->fx_addsy != NULL);
1224
1225   md_number_to_chars
1226     (where,
1227      fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
1228      4);
1229
1230   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
1231      ? S_GET_TYPE (fixP->fx_addsy)
1232      : fixP->fx_addsy->sy_number);
1233
1234   where[4] = (r_symbolnum >> 16) & 0x0ff;
1235   where[5] = (r_symbolnum >> 8) & 0x0ff;
1236   where[6] = r_symbolnum & 0x0ff;
1237   where[7] = (((!S_IS_DEFINED (fixP->fx_addsy)) << 7) & 0x80) | (0 & 0x60) | (fixP->fx_r_type & 0x1F);
1238
1239   /* Also easy.  */
1240   md_number_to_chars (&where[8], fixP->fx_addnumber, 4);
1241 }
1242
1243 #endif /* OBJ_AOUT */
1244 \f
1245 const char *md_shortopts = "";
1246
1247 struct option md_longopts[] =
1248 {
1249   { NULL, no_argument, NULL, 0 }
1250 };
1251 size_t md_longopts_size = sizeof (md_longopts);
1252
1253 int
1254 md_parse_option (int c ATTRIBUTE_UNUSED, char * arg ATTRIBUTE_UNUSED)
1255 {
1256   return 0;
1257 }
1258
1259 void
1260 md_show_usage (FILE * stream ATTRIBUTE_UNUSED)
1261 {
1262 }
1263 \f
1264 /* This is called when a line is unrecognized.  This is used to handle
1265    definitions of or32 style local labels.  */
1266
1267 int
1268 or32_unrecognized_line (int c)
1269 {
1270   int lab;
1271   char *s;
1272
1273   if (c != '$'
1274       || ! ISDIGIT ((unsigned char) input_line_pointer[0]))
1275     return 0;
1276
1277   s = input_line_pointer;
1278
1279   lab = 0;
1280   while (ISDIGIT ((unsigned char) *s))
1281     {
1282       lab = lab * 10 + *s - '0';
1283       ++s;
1284     }
1285
1286   if (*s != ':')
1287     /* Not a label definition.  */
1288     return 0;
1289
1290   if (dollar_label_defined (lab))
1291     {
1292       as_bad (_("label \"$%d\" redefined"), lab);
1293       return 0;
1294     }
1295
1296   define_dollar_label (lab);
1297   colon (dollar_label_name (lab, 0));
1298   input_line_pointer = s + 1;
1299
1300   return 1;
1301 }
1302
1303 /* Default the values of symbols known that should be "predefined".  We
1304    don't bother to predefine them unless you actually use one, since there
1305    are a lot of them.  */
1306
1307 symbolS *
1308 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1309 {
1310 #ifndef BFD_ASSEMBLER
1311   long regnum;
1312   char testbuf[5 + /*SLOP*/ 5];
1313
1314 #if DEBUG
1315   printf ("md_undefined_symbol(%s)\n", name);
1316 #endif
1317
1318   /* Register name.  */
1319   if (name[0] == 'r' || name[0] == 'R' || name[0] == 'a' || name[0] == 'b')
1320     {
1321       /* Parse the number, make sure it has no extra zeroes or
1322          trailing chars.  */
1323       regnum = atol (& name[1]);
1324
1325       if (regnum > 31)
1326         as_fatal (_("register out of range"));
1327
1328       sprintf (testbuf, "%ld", regnum);
1329
1330       if (strcmp (testbuf, &name[1]) != 0)
1331         return NULL;  /* gr007 or lr7foo or whatever.  */
1332
1333       /* We have a wiener!  Define and return a new symbol for it.  */
1334       return (symbol_new (name, SEG_REGISTER, (valueT) regnum,
1335                           &zero_address_frag));
1336     }
1337 #endif
1338   return NULL;
1339 }
1340
1341 /* Parse an operand that is machine-specific.  */
1342
1343 void
1344 md_operand (expressionS *expressionP)
1345 {
1346 #if DEBUG
1347   printf ("  md_operand(input_line_pointer = %s)\n", input_line_pointer);
1348 #endif
1349
1350   if (input_line_pointer[0] == REGISTER_PREFIX && input_line_pointer[1] == 'r')
1351     {
1352       /* We have a numeric register expression.  No biggy.  */
1353       input_line_pointer += 2;  /* Skip %r */
1354       (void) expression (expressionP);
1355
1356       if (expressionP->X_op != O_constant
1357           || expressionP->X_add_number > 255)
1358         as_bad (_("Invalid expression after %%%%\n"));
1359       expressionP->X_op = O_register;
1360     }
1361   else if (input_line_pointer[0] == '&')
1362     {
1363       /* We are taking the 'address' of a register...this one is not
1364          in the manual, but it *is* in traps/fpsymbol.h!  What they
1365          seem to want is the register number, as an absolute number.  */
1366       input_line_pointer++; /* Skip & */
1367       (void) expression (expressionP);
1368
1369       if (expressionP->X_op != O_register)
1370         as_bad (_("invalid register in & expression"));
1371       else
1372         expressionP->X_op = O_constant;
1373     }
1374   else if (input_line_pointer[0] == '$'
1375            && ISDIGIT ((unsigned char) input_line_pointer[1]))
1376     {
1377       long lab;
1378       char *name;
1379       symbolS *sym;
1380
1381       /* This is a local label.  */
1382       ++input_line_pointer;
1383       lab = (long) get_absolute_expression ();
1384
1385       if (dollar_label_defined (lab))
1386         {
1387           name = dollar_label_name (lab, 0);
1388           sym = symbol_find (name);
1389         }
1390       else
1391         {
1392           name = dollar_label_name (lab, 1);
1393           sym = symbol_find_or_make (name);
1394         }
1395
1396       expressionP->X_op = O_symbol;
1397       expressionP->X_add_symbol = sym;
1398       expressionP->X_add_number = 0;
1399     }
1400   else if (input_line_pointer[0] == '$')
1401     {
1402       char *s;
1403       char type;
1404       int fieldnum, fieldlimit;
1405       LITTLENUM_TYPE floatbuf[8];
1406
1407       /* $float(), $doubleN(), or $extendN() convert floating values
1408          to integers.  */
1409       s = input_line_pointer;
1410
1411       ++s;
1412
1413       fieldnum = 0;
1414       if (strncmp (s, "double", sizeof "double" - 1) == 0)
1415         {
1416           s += sizeof "double" - 1;
1417           type = 'd';
1418           fieldlimit = 2;
1419         }
1420       else if (strncmp (s, "float", sizeof "float" - 1) == 0)
1421         {
1422           s += sizeof "float" - 1;
1423           type = 'f';
1424           fieldlimit = 1;
1425         }
1426       else if (strncmp (s, "extend", sizeof "extend" - 1) == 0)
1427         {
1428           s += sizeof "extend" - 1;
1429           type = 'x';
1430           fieldlimit = 4;
1431         }
1432       else
1433         return;
1434
1435       if (ISDIGIT (*s))
1436         {
1437           fieldnum = *s - '0';
1438           ++s;
1439         }
1440       if (fieldnum >= fieldlimit)
1441         return;
1442
1443       SKIP_WHITESPACE ();
1444       if (*s != '(')
1445         return;
1446       ++s;
1447       SKIP_WHITESPACE ();
1448
1449       s = atof_ieee (s, type, floatbuf);
1450       if (s == NULL)
1451         return;
1452       s = s;
1453
1454       SKIP_WHITESPACE ();
1455       if (*s != ')')
1456         return;
1457       ++s;
1458       SKIP_WHITESPACE ();
1459
1460       input_line_pointer = s;
1461       expressionP->X_op = O_constant;
1462       expressionP->X_unsigned = 1;
1463       expressionP->X_add_number = ((floatbuf[fieldnum * 2]
1464                                     << LITTLENUM_NUMBER_OF_BITS)
1465                                    + floatbuf[fieldnum * 2 + 1]);
1466     }
1467 }
1468
1469 /* Round up a section size to the appropriate boundary.  */
1470
1471 valueT
1472 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size ATTRIBUTE_UNUSED)
1473 {
1474   return size;      /* Byte alignment is fine.  */
1475 }
1476
1477 /* Exactly what point is a PC-relative offset relative TO?
1478    On the 29000, they're relative to the address of the instruction,
1479    which we have set up as the address of the fixup too.  */
1480
1481 long
1482 md_pcrel_from (fixS *fixP)
1483 {
1484   return fixP->fx_where + fixP->fx_frag->fr_address;
1485 }
1486
1487 /* Generate a reloc for a fixup.  */
1488
1489 #ifdef BFD_ASSEMBLER
1490 arelent *
1491 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
1492 {
1493   arelent *reloc;
1494
1495   reloc = xmalloc (sizeof (arelent));
1496   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1497   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1498   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1499   /*  reloc->address = fixp->fx_frag->fr_address + fixp->fx_where + fixp->fx_addnumber;*/
1500   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1501
1502   if (reloc->howto == (reloc_howto_type *) NULL)
1503     {
1504       as_bad_where (fixp->fx_file, fixp->fx_line,
1505                     _("reloc %d not supported by object file format"),
1506                     (int) fixp->fx_r_type);
1507       return NULL;
1508     }
1509
1510   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1511     reloc->address = fixp->fx_offset;
1512
1513   reloc->addend = fixp->fx_addnumber;
1514   return reloc;
1515 }
1516 #endif