* elf32-ppc.c (LWZU_0_X_11): Delete.
[binutils.git] / gas / config / tc-or32.c
blob5794047cf72eff3ae342691a3eece0f33f8228ce
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.
7 This file is part of GAS, the GNU Assembler.
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.
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.
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, 51 Franklin Street - Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* tc-a29k.c used as a template. */
26 #include "safe-ctype.h"
27 #include "as.h"
28 #include "opcode/or32.h"
30 #ifdef BFD_ASSEMBLER
31 #include "elf/or32.h"
32 #endif
34 #define DEBUG 0
36 #ifndef REGISTER_PREFIX
37 #define REGISTER_PREFIX '%'
38 #endif
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
46 /* Handle of the OPCODE hash table. */
47 static struct hash_control *op_hash = NULL;
49 struct machine_it
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;
59 the_insn;
61 const pseudo_typeS md_pseudo_table[] =
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 },
74 int md_short_jump_size = 4;
75 int md_long_jump_size = 4;
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
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[] = "#";
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[] = "#";
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[] = ";";
104 /* Chars that can be used to separate mant from exp in floating point nums. */
105 const char EXP_CHARS[] = "eE";
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";
112 /* "l.jalr r9" precalculated opcode. */
113 static unsigned long jalr_r9_opcode;
115 static void machine_ip (char *);
118 /* Set bits in machine opcode according to insn->encoding
119 description and passed operand. */
121 static void
122 encode (const struct machine_opcode *insn,
123 unsigned long *opcode,
124 signed long param_val,
125 char param_ch)
127 int opc_pos = 0;
128 int param_pos = 0;
129 char *enc;
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)
138 if (enc - 2 >= insn->encoding && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
139 continue;
140 else
141 param_pos ++;
144 opc_pos = 32;
146 for (enc = insn->encoding; *enc != '\0';)
148 if ((*enc == '0') && (*(enc + 1) == 'x'))
150 int tmp = strtol (enc, NULL, 16);
152 opc_pos -= 4;
153 *opcode |= tmp << opc_pos;
154 enc += 3;
156 else if ((*enc == '0') || (*enc == '-'))
158 opc_pos--;
159 enc++;
161 else if (*enc == '1')
163 opc_pos--;
164 *opcode |= 1 << opc_pos;
165 enc++;
167 else if (*enc == param_ch)
169 opc_pos--;
170 param_pos--;
171 *opcode |= ((param_val >> param_pos) & 0x1) << opc_pos;
172 enc++;
174 else if (ISALPHA (*enc))
176 opc_pos--;
177 enc++;
179 else
180 enc++;
183 #if DEBUG
184 printf (" opcode=%.8lx\n", *opcode);
185 #endif
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. */
192 void
193 md_begin (void)
195 const char *retval = NULL;
196 int lose = 0;
197 int skipnext = 0;
198 unsigned int i;
200 /* Hash up all the opcodes for fast use later. */
201 op_hash = hash_new ();
203 for (i = 0; i < or32_num_opcodes; i++)
205 const char *name = machine_opcodes[i].name;
207 if (skipnext)
209 skipnext = 0;
210 continue;
213 retval = hash_insert (op_hash, name, (void *) &machine_opcodes[i]);
214 if (retval != NULL)
216 fprintf (stderr, "internal error: can't hash `%s': %s\n",
217 machine_opcodes[i].name, retval);
218 lose = 1;
222 if (lose)
223 as_fatal (_("Broken assembler. No assembly attempted."));
225 encode (&machine_opcodes[insn_index ("l.jalr")], &jalr_r9_opcode, 9, 'B');
228 /* Returns non zero if instruction is to be used. */
230 static int
231 check_invalid_opcode (unsigned long opcode)
233 return opcode == jalr_r9_opcode;
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. */
240 void
241 md_assemble (char *str)
243 char *toP;
245 #if DEBUG
246 printf ("NEW INSTRUCTION\n");
247 #endif
249 know (str);
250 machine_ip (str);
251 toP = frag_more (4);
253 /* Put out the opcode. */
254 md_number_to_chars (toP, the_insn.opcode, 4);
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
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);
272 /* This is true of the we have issued a "lo(" or "hi"(. */
273 static int waiting_for_shift = 0;
275 static int mask_or_shift = 0;
277 #ifdef BFD_ASSEMBLER
278 static char *
279 parse_operand (char *s, expressionS *operandp, int opt)
281 char *save = input_line_pointer;
282 char *new;
284 #if DEBUG
285 printf (" PROCESS NEW OPERAND(%s) == %c (%d)\n", s, opt ? opt : '!', opt);
286 #endif
288 input_line_pointer = s;
290 if (strncasecmp (s, "HI(", 3) == 0)
292 waiting_for_shift = 1;
293 mask_or_shift = BFD_RELOC_HI16;
295 input_line_pointer += 3;
297 else if (strncasecmp (s, "LO(", 3) == 0)
299 mask_or_shift = BFD_RELOC_LO16;
301 input_line_pointer += 3;
303 else
304 mask_or_shift = 0;
306 if ((*s == '(') && (*(s+1) == 'r'))
307 s++;
309 if ((*s == 'r') && ISDIGIT (*(s + 1)))
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;
319 expression (operandp);
321 if (operandp->X_op == O_absent)
323 if (! opt)
324 as_bad (_("missing operand"));
325 else
327 operandp->X_add_number = 0;
328 operandp->X_op = O_constant;
332 new = input_line_pointer;
333 input_line_pointer = save;
335 #if DEBUG
336 printf (" %s=parse_operand(%s): operandp->X_op = %u\n", new, s, operandp->X_op);
337 #endif
339 return new;
341 #else
343 static char *
344 parse_operand (char *s, expressionS *operandp, int opt)
346 char *save = input_line_pointer;
347 char *new;
349 #if DEBUG
350 printf (" PROCESS NEW OPERAND(%s) == %c (%d)\n", s, opt ? opt : '!', opt);
351 #endif
353 input_line_pointer = s;
355 if (strncasecmp (s, "HI(", 3) == 0)
357 waiting_for_shift = 1;
358 mask_or_shift = RELOC_CONSTH;
360 input_line_pointer += 3;
362 else if (strncasecmp (s, "LO(", 3) == 0)
364 mask_or_shift = RELOC_CONST;
366 input_line_pointer += 3;
368 else
369 mask_or_shift = 0;
371 expression (operandp);
373 if (operandp->X_op == O_absent)
375 if (! opt)
376 as_bad (_("missing operand"));
377 else
379 operandp->X_add_number = 0;
380 operandp->X_op = O_constant;
384 new = input_line_pointer;
385 input_line_pointer = save;
387 if ((operandp->X_op == O_symbol) && (*s != '_'))
389 #if DEBUG
390 printf ("symbol: '%s'\n", save);
391 #endif
393 for (save = s; s < new; s++)
394 if ((*s == REGISTER_PREFIX) && (*(s + 1) == 'r')) /* Register prefix. */
395 s++;
397 if ((*s == 'r') && ISDIGIT (*(s + 1)))
399 operandp->X_add_number = strtol (s + 1, NULL, 10);
400 operandp->X_op = O_register;
402 s = save;
405 #if DEBUG
406 printf (" %s=parse_operand(%s): operandp->X_op = %u\n", new, s, operandp->X_op);
407 #endif
409 return new;
411 #endif
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. */
417 #ifdef BFD_ASSEMBLER
418 static void
419 machine_ip (char *str)
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;
431 #if DEBUG
432 printf ("machine_ip(%s)\n", str);
433 #endif
435 s = str;
436 for (; ISALNUM (*s) || *s == '.'; ++s)
437 if (ISUPPER (*s))
438 *s = TOLOWER (*s);
440 switch (*s)
442 case '\0':
443 break;
445 case ' ': /* FIXME-SOMEDAY more whitespace. */
446 *s++ = '\0';
447 break;
449 default:
450 as_bad (_("unknown opcode1: `%s'"), str);
451 return;
454 if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
456 as_bad (_("unknown opcode2 `%s'."), str);
457 return;
460 argsStart = s;
461 opcode = 0;
462 memset (&the_insn, '\0', sizeof (the_insn));
463 the_insn.reloc = BFD_RELOC_NONE;
465 reloc = BFD_RELOC_NONE;
467 /* Build the opcode, checking as we go to make sure that the
468 operands match.
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');
476 for (args = insn->args;; ++args)
478 #if DEBUG
479 printf (" args = %s\n", args);
480 #endif
481 switch (*args)
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);
488 if (*s == '\0')
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;
496 as_bad (_("too many operands: %s"), s);
497 break;
499 case ',': /* Must match a comma. */
500 if (*s++ == ',')
502 reloc = BFD_RELOC_NONE;
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;
512 break;
514 case '(': /* Must match a (. */
515 s = parse_operand (s, operand, args[1] == 'I');
516 continue;
518 case ')': /* Must match a ). */
519 continue;
521 case 'r': /* A general register. */
522 args++;
524 if (operand->X_op != O_register)
525 break; /* Only registers. */
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;
536 default:
537 /* if (! ISALPHA (*args))
538 break; */ /* Only immediate values. */
540 if (mask_or_shift)
542 #if DEBUG
543 printf ("mask_or_shift = %d\n", mask_or_shift);
544 #endif
545 reloc = mask_or_shift;
547 mask_or_shift = 0;
549 if (strncasecmp (args, "LO(", 3) == 0)
551 #if DEBUG
552 printf ("reloc_const\n");
553 #endif
554 reloc = BFD_RELOC_LO16;
556 else if (strncasecmp (args, "HI(", 3) == 0)
558 #if DEBUG
559 printf ("reloc_consth\n");
560 #endif
561 reloc = BFD_RELOC_HI16;
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)
573 if (reloc == BFD_RELOC_NONE)
575 bfd_vma v, mask;
577 mask = 0x3ffffff;
578 v = abs (operand->X_add_number) & ~ mask;
579 if (v)
580 as_bad (_("call/jmp target out of range (1)"));
583 if (reloc == BFD_RELOC_HI16)
584 operand->X_add_number = ((operand->X_add_number >> 16) & 0xffff);
586 the_insn.pcrel = 0;
587 encode (insn, &opcode, operand->X_add_number, *args);
588 /* the_insn.reloc = BFD_RELOC_NONE; */
589 continue;
592 if (reloc == BFD_RELOC_NONE)
593 the_insn.reloc = BFD_RELOC_32_GOT_PCREL;
594 else
595 the_insn.reloc = reloc;
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;
604 /* the_insn.reloc_offset = 1; */
605 the_insn.pcrel = 1; /* Assume PC-relative jump. */
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;
612 encode (insn, &opcode, operand->X_add_number, *args);
613 continue;
616 /* Types or values of args don't match. */
617 as_bad (_("invalid operands"));
618 return;
622 #else
624 static void
625 machine_ip (char *str)
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;
637 #if DEBUG
638 printf ("machine_ip(%s)\n", str);
639 #endif
641 s = str;
642 for (; ISALNUM (*s) || *s == '.'; ++s)
643 if (ISUPPER (*s))
644 *s = TOLOWER (*s);
646 switch (*s)
648 case '\0':
649 break;
651 case ' ': /* FIXME-SOMEDAY more whitespace. */
652 *s++ = '\0';
653 break;
655 default:
656 as_bad (_("unknown opcode1: `%s'"), str);
657 return;
660 if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
662 as_bad (_("unknown opcode2 `%s'."), str);
663 return;
666 argsStart = s;
667 opcode = 0;
668 memset (&the_insn, '\0', sizeof (the_insn));
669 the_insn.reloc = NO_RELOC;
671 reloc = NO_RELOC;
673 /* Build the opcode, checking as we go to make sure that the
674 operands match.
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);
684 for (args = insn->args;; ++args)
686 #if DEBUG
687 printf (" args = %s\n", args);
688 #endif
689 switch (*args)
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);
696 if (*s == '\0')
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;
704 as_bad (_("too many operands: %s"), s);
705 break;
707 case ',': /* Must match a comma. */
708 if (*s++ == ',')
710 reloc = NO_RELOC;
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;
720 break;
722 case '(': /* Must match a (. */
723 s = parse_operand (s, operand, args[1] == 'I');
724 continue;
726 case ')': /* Must match a ). */
727 continue;
729 case 'r': /* A general register. */
730 args++;
732 if (operand->X_op != O_register)
733 break; /* Only registers. */
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;
744 default:
745 /* if (! ISALPHA (*args))
746 break; */ /* Only immediate values. */
748 if (mask_or_shift)
750 #if DEBUG
751 printf ("mask_or_shift = %d\n", mask_or_shift);
752 #endif
753 reloc = mask_or_shift;
755 mask_or_shift = 0;
757 if (strncasecmp (args, "LO(", 3) == 0)
759 #if DEBUG
760 printf ("reloc_const\n");
761 #endif
762 reloc = RELOC_CONST;
764 else if (strncasecmp (args, "HI(", 3) == 0)
766 #if DEBUG
767 printf ("reloc_consth\n");
768 #endif
769 reloc = RELOC_CONSTH;
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)
782 if (reloc == NO_RELOC)
784 unsigned long v, mask;
786 mask = 0x3ffffff;
787 v = abs (operand->X_add_number) & ~ mask;
788 if (v)
789 as_bad (_("call/jmp target out of range (1)"));
792 if (reloc == RELOC_CONSTH)
793 operand->X_add_number = ((operand->X_add_number>>16) & 0xffff);
795 the_insn.pcrel = 0;
796 encode (insn, &opcode, operand->X_add_number, *args);
797 /* the_insn.reloc = NO_RELOC; */
798 continue;
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;
811 /* the_insn.reloc_offset = 1; */
812 the_insn.pcrel = 1; /* Assume PC-relative jump. */
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;
819 encode (insn, &opcode, operand->X_add_number, *args);
820 continue;
823 /* Types or values of args don't match. */
824 as_bad (_("invalid operands"));
825 return;
828 #endif
830 /* This is identical to the md_atof in m68k.c. I think this is right,
831 but I'm not sure.
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. */
838 /* Equal to MAX_PRECISION in atof-ieee.c. */
839 #define MAX_LITTLENUMS 6
841 char *
842 md_atof (int type, char * litP, int * sizeP)
844 int prec;
845 LITTLENUM_TYPE words[MAX_LITTLENUMS];
846 LITTLENUM_TYPE *wordP;
847 char *t;
849 switch (type)
851 case 'f':
852 case 'F':
853 case 's':
854 case 'S':
855 prec = 2;
856 break;
858 case 'd':
859 case 'D':
860 case 'r':
861 case 'R':
862 prec = 4;
863 break;
865 case 'x':
866 case 'X':
867 prec = 6;
868 break;
870 case 'p':
871 case 'P':
872 prec = 6;
873 break;
875 default:
876 *sizeP = 0;
877 return _("Bad call to MD_ATOF()");
880 t = atof_ieee (input_line_pointer, type, words);
881 if (t)
882 input_line_pointer = t;
884 *sizeP = prec * sizeof (LITTLENUM_TYPE);
886 for (wordP = words; prec--;)
888 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
889 litP += sizeof (LITTLENUM_TYPE);
892 return NULL;
895 /* Write out big-endian. */
897 void
898 md_number_to_chars (char *buf, valueT val, int n)
900 number_to_chars_bigendian (buf, val, n);
903 #ifdef BFD_ASSEMBLER
904 void
905 md_apply_fix3 (fixS * fixP, valueT * val, segT seg ATTRIBUTE_UNUSED)
907 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
908 long t_val;
910 t_val = (long) *val;
912 #if DEBUG
913 printf ("md_apply_fix val:%x\n", t_val);
914 #endif
916 fixP->fx_addnumber = t_val; /* Remember value for emit_reloc. */
918 know (fixP->fx_size == 4);
919 know (fixP->fx_r_type < BFD_RELOC_NONE);
921 switch (fixP->fx_r_type)
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;
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;
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;
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;
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;
964 case BFD_RELOC_32_GOT_PCREL: /* 0000XXXX pattern in a word. */
965 if (!fixP->fx_done)
967 else if (fixP->fx_pcrel)
969 long v = t_val >> 28;
971 if (v != 0 && v != -1)
972 as_bad_where (fixP->fx_file, fixP->fx_line,
973 _("call/jmp target out of range (2)"));
975 else
976 /* This case was supposed to be handled in machine_ip. */
977 abort ();
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;
985 case BFD_RELOC_VTABLE_INHERIT:
986 case BFD_RELOC_VTABLE_ENTRY:
987 fixP->fx_done = 0;
988 break;
990 case BFD_RELOC_NONE:
991 default:
992 as_bad (_("bad relocation type: 0x%02x"), fixP->fx_r_type);
993 break;
996 if (fixP->fx_addsy == (symbolS *) NULL)
997 fixP->fx_done = 1;
999 #else
1000 void
1001 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1003 long val = *valP;
1004 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1006 #if DEBUG
1007 printf ("md_apply_fix val:%x\n", val);
1008 #endif
1010 fixP->fx_addnumber = val; /* Remember value for emit_reloc. */
1012 know (fixP->fx_size == 4);
1013 know (fixP->fx_r_type < NO_RELOC);
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;
1019 switch (fixP->fx_r_type)
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;
1028 case RELOC_8:
1029 buf[0] = val;
1030 break;
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;
1040 case RELOC_HI22:
1041 buf[1] |= (val >> 26) & 0x3f;
1042 buf[2] = val >> 18;
1043 buf[3] = val >> 10;
1044 break;
1046 case RELOC_LO10:
1047 buf[2] |= (val >> 8) & 0x03;
1048 buf[3] = val;
1049 break;
1051 case RELOC_BASE13:
1052 buf[2] |= (val >> 8) & 0x1f;
1053 buf[3] = val;
1054 break;
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;
1065 case RELOC_JUMPTARG: /* 0000XXXX pattern in a word. */
1066 if (!fixP->fx_done)
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. */
1073 else if (fixP->fx_pcrel)
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)"));
1080 else
1081 /* This case was supposed to be handled in machine_ip. */
1082 abort ();
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;
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;
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;
1106 case BFD_RELOC_VTABLE_INHERIT:
1107 case BFD_RELOC_VTABLE_ENTRY:
1108 fixP->fx_done = 0;
1109 break;
1111 case NO_RELOC:
1112 default:
1113 as_bad (_("bad relocation type: 0x%02x"), fixP->fx_r_type);
1114 break;
1117 if (fixP->fx_addsy == (symbolS *) NULL)
1118 fixP->fx_done = 1;
1120 #endif
1122 #ifdef OBJ_COFF
1123 short
1124 tc_coff_fix2rtype (fixS *fixP)
1126 #if DEBUG
1127 printf ("tc_coff_fix2rtype\n");
1128 #endif
1130 switch (fixP->fx_r_type)
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 ();
1142 return 0;
1145 #endif /* OBJ_COFF */
1147 /* Should never be called for or32. */
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)
1156 as_fatal ("or32_create_short_jmp\n");
1159 /* Should never be called for or32. */
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)
1167 as_fatal ("or32_convert_frag\n");
1170 #else
1171 void
1172 md_convert_frag (bfd * headers ATTRIBUTE_UNUSED,
1173 segT seg ATTRIBUTE_UNUSED,
1174 fragS * fragP ATTRIBUTE_UNUSED)
1176 as_fatal ("or32_convert_frag\n");
1178 #endif
1180 /* Should never be called for or32. */
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)
1189 as_fatal ("or32_create_long_jump\n");
1192 /* Should never be called for or32. */
1195 md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED,
1196 segT segtype ATTRIBUTE_UNUSED)
1198 as_fatal ("or32_estimate_size_before_relax\n");
1199 return 0;
1202 /* Translate internal representation of relocation info to target format.
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. */
1210 #ifdef OBJ_AOUT
1211 void
1212 tc_aout_fix_to_chars (char *where,
1213 fixS *fixP,
1214 relax_addressT segment_address_in_file)
1216 long r_symbolnum;
1218 #if DEBUG
1219 printf ("tc_aout_fix_to_chars\n");
1220 #endif
1222 know (fixP->fx_r_type < BFD_RELOC_NONE);
1223 know (fixP->fx_addsy != NULL);
1225 md_number_to_chars
1226 (where,
1227 fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
1230 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
1231 ? S_GET_TYPE (fixP->fx_addsy)
1232 : fixP->fx_addsy->sy_number);
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);
1239 /* Also easy. */
1240 md_number_to_chars (&where[8], fixP->fx_addnumber, 4);
1243 #endif /* OBJ_AOUT */
1245 const char *md_shortopts = "";
1247 struct option md_longopts[] =
1249 { NULL, no_argument, NULL, 0 }
1251 size_t md_longopts_size = sizeof (md_longopts);
1254 md_parse_option (int c ATTRIBUTE_UNUSED, char * arg ATTRIBUTE_UNUSED)
1256 return 0;
1259 void
1260 md_show_usage (FILE * stream ATTRIBUTE_UNUSED)
1264 /* This is called when a line is unrecognized. This is used to handle
1265 definitions of or32 style local labels. */
1268 or32_unrecognized_line (int c)
1270 int lab;
1271 char *s;
1273 if (c != '$'
1274 || ! ISDIGIT ((unsigned char) input_line_pointer[0]))
1275 return 0;
1277 s = input_line_pointer;
1279 lab = 0;
1280 while (ISDIGIT ((unsigned char) *s))
1282 lab = lab * 10 + *s - '0';
1283 ++s;
1286 if (*s != ':')
1287 /* Not a label definition. */
1288 return 0;
1290 if (dollar_label_defined (lab))
1292 as_bad (_("label \"$%d\" redefined"), lab);
1293 return 0;
1296 define_dollar_label (lab);
1297 colon (dollar_label_name (lab, 0));
1298 input_line_pointer = s + 1;
1300 return 1;
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. */
1307 symbolS *
1308 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1310 #ifndef BFD_ASSEMBLER
1311 long regnum;
1312 char testbuf[5 + /*SLOP*/ 5];
1314 #if DEBUG
1315 printf ("md_undefined_symbol(%s)\n", name);
1316 #endif
1318 /* Register name. */
1319 if (name[0] == 'r' || name[0] == 'R' || name[0] == 'a' || name[0] == 'b')
1321 /* Parse the number, make sure it has no extra zeroes or
1322 trailing chars. */
1323 regnum = atol (& name[1]);
1325 if (regnum > 31)
1326 as_fatal (_("register out of range"));
1328 sprintf (testbuf, "%ld", regnum);
1330 if (strcmp (testbuf, &name[1]) != 0)
1331 return NULL; /* gr007 or lr7foo or whatever. */
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));
1337 #endif
1338 return NULL;
1341 /* Parse an operand that is machine-specific. */
1343 void
1344 md_operand (expressionS *expressionP)
1346 #if DEBUG
1347 printf (" md_operand(input_line_pointer = %s)\n", input_line_pointer);
1348 #endif
1350 if (input_line_pointer[0] == REGISTER_PREFIX && input_line_pointer[1] == 'r')
1352 /* We have a numeric register expression. No biggy. */
1353 input_line_pointer += 2; /* Skip %r */
1354 (void) expression (expressionP);
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;
1361 else if (input_line_pointer[0] == '&')
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);
1369 if (expressionP->X_op != O_register)
1370 as_bad (_("invalid register in & expression"));
1371 else
1372 expressionP->X_op = O_constant;
1374 else if (input_line_pointer[0] == '$'
1375 && ISDIGIT ((unsigned char) input_line_pointer[1]))
1377 long lab;
1378 char *name;
1379 symbolS *sym;
1381 /* This is a local label. */
1382 ++input_line_pointer;
1383 lab = (long) get_absolute_expression ();
1385 if (dollar_label_defined (lab))
1387 name = dollar_label_name (lab, 0);
1388 sym = symbol_find (name);
1390 else
1392 name = dollar_label_name (lab, 1);
1393 sym = symbol_find_or_make (name);
1396 expressionP->X_op = O_symbol;
1397 expressionP->X_add_symbol = sym;
1398 expressionP->X_add_number = 0;
1400 else if (input_line_pointer[0] == '$')
1402 char *s;
1403 char type;
1404 int fieldnum, fieldlimit;
1405 LITTLENUM_TYPE floatbuf[8];
1407 /* $float(), $doubleN(), or $extendN() convert floating values
1408 to integers. */
1409 s = input_line_pointer;
1411 ++s;
1413 fieldnum = 0;
1414 if (strncmp (s, "double", sizeof "double" - 1) == 0)
1416 s += sizeof "double" - 1;
1417 type = 'd';
1418 fieldlimit = 2;
1420 else if (strncmp (s, "float", sizeof "float" - 1) == 0)
1422 s += sizeof "float" - 1;
1423 type = 'f';
1424 fieldlimit = 1;
1426 else if (strncmp (s, "extend", sizeof "extend" - 1) == 0)
1428 s += sizeof "extend" - 1;
1429 type = 'x';
1430 fieldlimit = 4;
1432 else
1433 return;
1435 if (ISDIGIT (*s))
1437 fieldnum = *s - '0';
1438 ++s;
1440 if (fieldnum >= fieldlimit)
1441 return;
1443 SKIP_WHITESPACE ();
1444 if (*s != '(')
1445 return;
1446 ++s;
1447 SKIP_WHITESPACE ();
1449 s = atof_ieee (s, type, floatbuf);
1450 if (s == NULL)
1451 return;
1452 s = s;
1454 SKIP_WHITESPACE ();
1455 if (*s != ')')
1456 return;
1457 ++s;
1458 SKIP_WHITESPACE ();
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]);
1469 /* Round up a section size to the appropriate boundary. */
1471 valueT
1472 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size ATTRIBUTE_UNUSED)
1474 return size; /* Byte alignment is fine. */
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. */
1481 long
1482 md_pcrel_from (fixS *fixP)
1484 return fixP->fx_where + fixP->fx_frag->fr_address;
1487 /* Generate a reloc for a fixup. */
1489 #ifdef BFD_ASSEMBLER
1490 arelent *
1491 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
1493 arelent *reloc;
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);
1502 if (reloc->howto == (reloc_howto_type *) NULL)
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;
1510 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1511 reloc->address = fixp->fx_offset;
1513 reloc->addend = fixp->fx_addnumber;
1514 return reloc;
1516 #endif