* config/tc-mips.c (s_gpdword): New function.
[binutils.git] / gas / config / tc-or32.c
blob2983b7a215df9bf51932188f86f7f1c5081e62fa
1 /* Assembly backend for the OpenRISC 1000.
2 Copyright (C) 2002 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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, 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 static void machine_ip PARAMS ((char *));
63 const pseudo_typeS md_pseudo_table[] =
65 {"align", s_align_bytes, 4 },
66 {"space", s_space, 0 },
67 {"cputype", s_ignore, 0 },
68 {"reg", s_lsym, 0 }, /* Register equate, same as equ. */
69 {"sect", s_ignore, 0 }, /* Creation of coff sections. */
70 {"proc", s_ignore, 0 }, /* Start of a function. */
71 {"endproc", s_ignore, 0 }, /* Function end. */
72 {"word", cons, 4 },
73 {NULL, 0, 0 },
76 int md_short_jump_size = 4;
77 int md_long_jump_size = 4;
79 #if defined(BFD_HEADERS)
80 #ifdef RELSZ
81 const int md_reloc_size = RELSZ; /* Coff headers. */
82 #else
83 const int md_reloc_size = 12; /* Something else headers. */
84 #endif
85 #else
86 const int md_reloc_size = 12; /* Not bfdized. */
87 #endif
89 /* This array holds the chars that always start a comment.
90 If the pre-processor is disabled, these aren't very useful. */
91 const char comment_chars[] = "#";
93 /* This array holds the chars that only start a comment at the beginning of
94 a line. If the line seems to have the form '# 123 filename'
95 .line and .file directives will appear in the pre-processed output. */
96 /* Note that input_file.c hand checks for '#' at the beginning of the
97 first line of the input file. This is because the compiler outputs
98 #NO_APP at the beginning of its output. */
99 /* Also note that comments like this one will always work. */
100 const char line_comment_chars[] = "#";
102 /* We needed an unused char for line separation to work around the
103 lack of macros, using sed and such. */
104 const char line_separator_chars[] = ";";
106 /* Chars that can be used to separate mant from exp in floating point nums. */
107 const char EXP_CHARS[] = "eE";
109 /* Chars that mean this number is a floating point constant.
110 As in 0f12.456
111 or 0d1.2345e12. */
112 const char FLT_CHARS[] = "rRsSfFdDxXpP";
114 /* "l.jalr r9" precalculated opcode. */
115 static unsigned long jalr_r9_opcode;
118 static int check_invalid_opcode PARAMS ((unsigned long));
119 static void encode PARAMS ((const struct machine_opcode *, unsigned long *, signed long, char));
120 #ifdef BFD_ASSEMBLER
121 static char * parse_operand PARAMS ((char *, expressionS *, int));
122 #endif
124 /* Set bits in machine opcode according to insn->encoding
125 description and passed operand. */
127 static void
128 encode (insn, opcode, param_val, param_ch)
129 const struct machine_opcode *insn;
130 unsigned long *opcode;
131 signed long param_val;
132 char param_ch;
134 int opc_pos = 0;
135 int param_pos = 0;
136 char *enc;
138 #if DEBUG
139 printf (" encode: opcode=%.8lx param_val=%.8lx abs=%.8lx param_ch=%c\n",
140 *opcode, param_val, abs (param_val), param_ch);
141 #endif
142 for (enc = insn->encoding; *enc != '\0'; enc++)
143 if (*enc == param_ch)
145 if (enc - 2 >= insn->encoding && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
146 continue;
147 else
148 param_pos ++;
151 opc_pos = 32;
153 for (enc = insn->encoding; *enc != '\0';)
155 if ((*enc == '0') && (*(enc + 1) == 'x'))
157 int tmp = strtol (enc, NULL, 16);
159 opc_pos -= 4;
160 *opcode |= tmp << opc_pos;
161 enc += 3;
163 else if ((*enc == '0') || (*enc == '-'))
165 opc_pos--;
166 enc++;
168 else if (*enc == '1')
170 opc_pos--;
171 *opcode |= 1 << opc_pos;
172 enc++;
174 else if (*enc == param_ch)
176 opc_pos--;
177 param_pos--;
178 *opcode |= ((param_val >> param_pos) & 0x1) << opc_pos;
179 enc++;
181 else if (ISALPHA (*enc))
183 opc_pos--;
184 enc++;
186 else
187 enc++;
190 #if DEBUG
191 printf (" opcode=%.8lx\n", *opcode);
192 #endif
195 /* This function is called once, at assembler startup time. It should
196 set up all the tables, etc., that the MD part of the assembler will
197 need. */
199 void
200 md_begin ()
202 const char *retval = NULL;
203 int lose = 0;
204 int skipnext = 0;
205 unsigned int i;
207 /* Hash up all the opcodes for fast use later. */
208 op_hash = hash_new ();
210 for (i = 0; i < or32_num_opcodes; i++)
212 const char *name = machine_opcodes[i].name;
214 if (skipnext)
216 skipnext = 0;
217 continue;
220 retval = hash_insert (op_hash, name, (PTR) &machine_opcodes[i]);
221 if (retval != NULL)
223 fprintf (stderr, "internal error: can't hash `%s': %s\n",
224 machine_opcodes[i].name, retval);
225 lose = 1;
229 if (lose)
230 as_fatal (_("Broken assembler. No assembly attempted."));
232 encode (&machine_opcodes[insn_index ("l.jalr")], &jalr_r9_opcode, 9, 'B');
235 /* Returs non zero if instruction is to be used. */
237 static int
238 check_invalid_opcode (opcode)
239 unsigned long opcode;
241 return opcode == jalr_r9_opcode;
244 /* Assemble a single instruction. Its label has already been handled
245 by the generic front end. We just parse opcode and operands, and
246 produce the bytes of data and relocation. */
248 void
249 md_assemble (str)
250 char *str;
252 char *toP;
254 #if DEBUG
255 printf ("NEW INSTRUCTION\n");
256 #endif
258 know (str);
259 machine_ip (str);
260 toP = frag_more (4);
262 /* Put out the opcode. */
263 md_number_to_chars (toP, the_insn.opcode, 4);
265 /* Put out the symbol-dependent stuff. */
266 #ifdef BFD_ASSEMBLER
267 if (the_insn.reloc != BFD_RELOC_NONE)
268 #else
269 if (the_insn.reloc != NO_RELOC)
270 #endif
272 fix_new_exp (frag_now,
273 (toP - frag_now->fr_literal + the_insn.reloc_offset),
274 4, /* size */
275 &the_insn.exp,
276 the_insn.pcrel,
277 the_insn.reloc);
281 /* This is true of the we have issued a "lo(" or "hi"(. */
282 static int waiting_for_shift = 0;
284 static int mask_or_shift = 0;
286 #ifdef BFD_ASSEMBLER
287 static char *
288 parse_operand (s, operandp, opt)
289 char *s;
290 expressionS *operandp;
291 int opt;
293 char *save = input_line_pointer;
294 char *new;
296 #if DEBUG
297 printf (" PROCESS NEW OPERAND(%s) == %c (%d)\n", s, opt ? opt : '!', opt);
298 #endif
300 input_line_pointer = s;
302 if (strncasecmp (s, "HI(", 3) == 0)
304 waiting_for_shift = 1;
305 mask_or_shift = BFD_RELOC_HI16;
307 input_line_pointer += 3;
309 else if (strncasecmp (s, "LO(", 3) == 0)
311 mask_or_shift = BFD_RELOC_LO16;
313 input_line_pointer += 3;
315 else
316 mask_or_shift = 0;
318 if ((*s == '(') && (*(s+1) == 'r'))
319 s++;
321 if ((*s == 'r') && ISDIGIT (*(s + 1)))
323 operandp->X_add_number = strtol (s + 1, NULL, 10);
324 operandp->X_op = O_register;
325 for (; (*s != ',') && (*s != '\0');)
326 s++;
327 input_line_pointer = save;
328 return s;
331 expression (operandp);
333 if (operandp->X_op == O_absent)
335 if (! opt)
336 as_bad (_("missing operand"));
337 else
339 operandp->X_add_number = 0;
340 operandp->X_op = O_constant;
344 new = input_line_pointer;
345 input_line_pointer = save;
347 #if DEBUG
348 printf (" %s=parse_operand(%s): operandp->X_op = %u\n", new, s, operandp->X_op);
349 #endif
351 return new;
353 #else
355 char *
356 parse_operand (s, operandp, opt)
357 char *s;
358 expressionS *operandp;
359 int opt;
361 char *save = input_line_pointer;
362 char *new;
364 #if DEBUG
365 printf (" PROCESS NEW OPERAND(%s) == %c (%d)\n", s, opt ? opt : '!', opt);
366 #endif
368 input_line_pointer = s;
370 if (strncasecmp (s, "HI(", 3) == 0)
372 waiting_for_shift = 1;
373 mask_or_shift = RELOC_CONSTH;
375 input_line_pointer += 3;
377 else if (strncasecmp (s, "LO(", 3) == 0)
379 mask_or_shift = RELOC_CONST;
381 input_line_pointer += 3;
383 else
384 mask_or_shift = 0;
387 expression (operandp);
389 if (operandp->X_op == O_absent)
391 if (! opt)
392 as_bad (_("missing operand"));
393 else
395 operandp->X_add_number = 0;
396 operandp->X_op = O_constant;
400 new = input_line_pointer;
401 input_line_pointer = save;
403 if ((operandp->X_op == O_symbol) && (*s != '_'))
405 #if DEBUG
406 printf ("symbol: '%s'\n", save);
407 #endif
409 for (save = s; s < new; s++)
410 if ((*s == REGISTER_PREFIX) && (*(s + 1) == 'r')) /* Register prefix. */
411 s++;
413 if ((*s == 'r') && ISDIGIT (*(s + 1)))
415 operandp->X_add_number = strtol (s + 1, NULL, 10);
416 operandp->X_op = O_register;
418 s = save;
421 #if DEBUG
422 printf (" %s=parse_operand(%s): operandp->X_op = %u\n", new, s, operandp->X_op);
423 #endif
425 return new;
427 #endif
429 /* Instruction parsing. Takes a string containing the opcode.
430 Operands are at input_line_pointer. Output is in the_insn.
431 Warnings or errors are generated. */
433 #ifdef BFD_ASSEMBLER
434 static void
435 machine_ip (str)
436 char *str;
438 char *s;
439 const char *args;
440 const struct machine_opcode *insn;
441 char *argsStart;
442 unsigned long opcode;
443 expressionS the_operand;
444 expressionS *operand = &the_operand;
445 unsigned int regno;
446 int reloc = BFD_RELOC_NONE;
448 #if DEBUG
449 printf ("machine_ip(%s)\n", str);
450 #endif
452 s = str;
453 for (; ISALNUM (*s) || *s == '.'; ++s)
454 if (ISUPPER (*s))
455 *s = TOLOWER (*s);
457 switch (*s)
459 case '\0':
460 break;
462 case ' ': /* FIXME-SOMEDAY more whitespace. */
463 *s++ = '\0';
464 break;
466 default:
467 as_bad (_("unknown opcode1: `%s'"), str);
468 return;
471 if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
473 as_bad (_("unknown opcode2 `%s'."), str);
474 return;
477 argsStart = s;
478 opcode = 0;
479 memset (&the_insn, '\0', sizeof (the_insn));
480 the_insn.reloc = BFD_RELOC_NONE;
482 reloc = BFD_RELOC_NONE;
484 /* Build the opcode, checking as we go to make sure that the
485 operands match.
487 If an operand matches, we modify the_insn or opcode appropriately,
488 and do a "continue". If an operand fails to match, we "break". */
489 if (insn->args[0] != '\0')
491 /* Prime the pump. */
492 s = parse_operand (s, operand, insn->args[0] == 'I');
495 for (args = insn->args;; ++args)
497 #if DEBUG
498 printf (" args = %s\n", args);
499 #endif
500 switch (*args)
502 case '\0': /* End of args. */
503 /* We have have 0 args, do the bazoooka! */
504 if (args == insn->args)
505 encode (insn, &opcode, 0, 0);
507 if (*s == '\0')
509 /* We are truly done. */
510 the_insn.opcode = opcode;
511 if (check_invalid_opcode (opcode))
512 as_bad (_("instruction not allowed: %s"), str);
513 return;
515 as_bad (_("too many operands: %s"), s);
516 break;
518 case ',': /* Must match a comma. */
519 if (*s++ == ',')
521 reloc = BFD_RELOC_NONE;
523 /* Parse next operand. */
524 s = parse_operand (s, operand, args[1] == 'I');
525 #if DEBUG
526 printf (" ',' case: operand->X_add_number = %d, *args = %s, *s = %s\n",
527 operand->X_add_number, args, s);
528 #endif
529 continue;
531 break;
533 case '(': /* Must match a (. */
534 s = parse_operand (s, operand, args[1] == 'I');
535 continue;
537 case ')': /* Must match a ). */
538 continue;
540 case 'r': /* A general register. */
541 args++;
543 if (operand->X_op != O_register)
544 break; /* Only registers. */
546 know (operand->X_add_symbol == 0);
547 know (operand->X_op_symbol == 0);
548 regno = operand->X_add_number;
549 encode (insn, &opcode, regno, *args);
550 #if DEBUG
551 printf (" r: operand->X_op = %d\n", operand->X_op);
552 #endif
553 continue;
555 default:
556 /* if (! ISALPHA (*args))
557 break; */ /* Only immediate values. */
559 if (mask_or_shift)
561 #if DEBUG
562 printf ("mask_or_shift = %d\n", mask_or_shift);
563 #endif
564 reloc = mask_or_shift;
566 mask_or_shift = 0;
568 if (strncasecmp (args, "LO(", 3) == 0)
570 #if DEBUG
571 printf ("reloc_const\n");
572 #endif
573 reloc = BFD_RELOC_LO16;
575 else if (strncasecmp (args, "HI(", 3) == 0)
577 #if DEBUG
578 printf ("reloc_consth\n");
579 #endif
580 reloc = BFD_RELOC_HI16;
583 if (*s == '(')
585 operand->X_op = O_constant;
586 #if 0
587 operand->X_add_number = 0; /* ??? if enabled load/store offsets
588 are zero. */
589 #endif
591 else if (*s == ')')
592 s += 1;
593 #if DEBUG
594 printf (" default case: operand->X_add_number = %d, *args = %s, *s = %s\n", operand->X_add_number, args, s);
595 #endif
596 if (operand->X_op == O_constant)
598 if (reloc == BFD_RELOC_NONE)
600 bfd_vma v, mask;
602 mask = 0x3ffffff;
603 v = abs (operand->X_add_number) & ~ mask;
604 if (v)
605 as_bad (_("call/jmp target out of range (1)"));
608 if (reloc == BFD_RELOC_HI16)
609 operand->X_add_number = ((operand->X_add_number >> 16) & 0xffff);
611 the_insn.pcrel = 0;
612 encode (insn, &opcode, operand->X_add_number, *args);
613 /* the_insn.reloc = BFD_RELOC_NONE; */
614 continue;
617 if (reloc == BFD_RELOC_NONE)
618 the_insn.reloc = BFD_RELOC_32_GOT_PCREL;
619 else
620 the_insn.reloc = reloc;
622 /* the_insn.reloc = insn->reloc; */
623 #if DEBUG
624 printf (" reloc sym=%d\n", the_insn.reloc);
625 printf (" BFD_RELOC_NONE=%d\n", BFD_RELOC_NONE);
626 #endif
627 the_insn.exp = *operand;
629 /* the_insn.reloc_offset = 1; */
630 the_insn.pcrel = 1; /* Assume PC-relative jump. */
632 /* FIXME-SOON, Do we figure out whether abs later, after
633 know sym val? */
634 if (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_HI16)
635 the_insn.pcrel = 0;
637 encode (insn, &opcode, operand->X_add_number, *args);
638 continue;
641 /* Types or values of args don't match. */
642 as_bad (_("invalid operands"));
643 return;
647 #else
649 static void
650 machine_ip (str)
651 char *str;
653 char *s;
654 const char *args;
655 const struct machine_opcode *insn;
656 char *argsStart;
657 unsigned long opcode;
658 expressionS the_operand;
659 expressionS *operand = &the_operand;
660 unsigned int regno;
661 int reloc = NO_RELOC;
663 #if DEBUG
664 printf ("machine_ip(%s)\n", str);
665 #endif
667 s = str;
668 for (; ISALNUM (*s) || *s == '.'; ++s)
669 if (ISUPPER (*s))
670 *s = TOLOWER (*s);
672 switch (*s)
674 case '\0':
675 break;
677 case ' ': /* FIXME-SOMEDAY more whitespace. */
678 *s++ = '\0';
679 break;
681 default:
682 as_bad (_("unknown opcode1: `%s'"), str);
683 return;
686 if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
688 as_bad (_("unknown opcode2 `%s'."), str);
689 return;
692 argsStart = s;
693 opcode = 0;
694 memset (&the_insn, '\0', sizeof (the_insn));
695 the_insn.reloc = NO_RELOC;
697 reloc = NO_RELOC;
699 /* Build the opcode, checking as we go to make sure that the
700 operands match.
702 If an operand matches, we modify the_insn or opcode appropriately,
703 and do a "continue". If an operand fails to match, we "break". */
704 if (insn->args[0] != '\0')
705 /* Prime the pump. */
706 s = parse_operand (s, operand,
707 insn->args[0] == 'I'
708 || strcmp (insn->name, "l.nop") == 0);
710 for (args = insn->args;; ++args)
712 #if DEBUG
713 printf (" args = %s\n", args);
714 #endif
715 switch (*args)
717 case '\0': /* End of args. */
718 /* We have have 0 args, do the bazoooka! */
719 if (args == insn->args)
720 encode (insn, &opcode, 0, 0);
722 if (*s == '\0')
724 /* We are truly done. */
725 the_insn.opcode = opcode;
726 if (check_invalid_opcode (opcode))
727 as_bad (_("instruction not allowed: %s"), str);
728 return;
730 as_bad (_("too many operands: %s"), s);
731 break;
733 case ',': /* Must match a comma. */
734 if (*s++ == ',')
736 reloc = NO_RELOC;
738 /* Parse next operand. */
739 s = parse_operand (s, operand, args[1] == 'I');
740 #if DEBUG
741 printf (" ',' case: operand->X_add_number = %d, *args = %s, *s = %s\n",
742 operand->X_add_number, args, s);
743 #endif
744 continue;
746 break;
748 case '(': /* Must match a (. */
749 s = parse_operand (s, operand, args[1] == 'I');
750 continue;
752 case ')': /* Must match a ). */
753 continue;
755 case 'r': /* A general register. */
756 args++;
758 if (operand->X_op != O_register)
759 break; /* Only registers. */
761 know (operand->X_add_symbol == 0);
762 know (operand->X_op_symbol == 0);
763 regno = operand->X_add_number;
764 encode (insn, &opcode, regno, *args);
765 #if DEBUG
766 printf (" r: operand->X_op = %d\n", operand->X_op);
767 #endif
768 continue;
770 default:
771 /* if (! ISALPHA (*args))
772 break; */ /* Only immediate values. */
774 if (mask_or_shift)
776 #if DEBUG
777 printf ("mask_or_shift = %d\n", mask_or_shift);
778 #endif
779 reloc = mask_or_shift;
781 mask_or_shift = 0;
783 if (strncasecmp (args, "LO(", 3) == 0)
785 #if DEBUG
786 printf ("reloc_const\n");
787 #endif
788 reloc = RELOC_CONST;
790 else if (strncasecmp (args, "HI(", 3) == 0)
792 #if DEBUG
793 printf ("reloc_consth\n");
794 #endif
795 reloc = RELOC_CONSTH;
798 if (*s == '(')
800 operand->X_op = O_constant;
801 #if 0
802 operand->X_add_number = 0; /* ??? if enabled load/store offsets
803 are zero. */
804 #endif
806 else if (*s == ')')
807 s += 1;
808 #if DEBUG
809 printf (" default case: operand->X_add_number = %d, *args = %s, *s = %s\n",
810 operand->X_add_number, args, s);
811 #endif
812 if (operand->X_op == O_constant)
814 if (reloc == NO_RELOC)
816 unsigned long v, mask;
818 mask = 0x3ffffff;
819 v = abs (operand->X_add_number) & ~ mask;
820 if (v)
821 as_bad (_("call/jmp target out of range (1)"));
824 if (reloc == RELOC_CONSTH)
825 operand->X_add_number = ((operand->X_add_number>>16) & 0xffff);
827 the_insn.pcrel = 0;
828 encode (insn, &opcode, operand->X_add_number, *args);
829 /* the_insn.reloc = NO_RELOC; */
830 continue;
833 if (reloc == NO_RELOC)
834 the_insn.reloc = RELOC_JUMPTARG;
835 else
836 the_insn.reloc = reloc;
837 #if DEBUG
838 printf (" reloc sym=%d\n", the_insn.reloc);
839 printf (" NO_RELOC=%d\n", NO_RELOC);
840 #endif
841 the_insn.exp = *operand;
843 /* the_insn.reloc_offset = 1; */
844 the_insn.pcrel = 1; /* Assume PC-relative jump. */
846 /* FIXME-SOON, Do we figure out whether abs later, after
847 know sym val? */
848 if (reloc == RELOC_CONST || reloc == RELOC_CONSTH)
849 the_insn.pcrel = 0;
851 encode (insn, &opcode, operand->X_add_number, *args);
852 continue;
855 /* Types or values of args don't match. */
856 as_bad (_("invalid operands"));
857 return;
860 #endif
862 /* This is identical to the md_atof in m68k.c. I think this is right,
863 but I'm not sure.
865 Turn a string in input_line_pointer into a floating point constant
866 of type type, and store the appropriate bytes in *litP. The number
867 of LITTLENUMS emitted is stored in *sizeP . An error message is
868 returned, or NULL on OK. */
870 /* Equal to MAX_PRECISION in atof-ieee.c. */
871 #define MAX_LITTLENUMS 6
873 char *
874 md_atof (type, litP, sizeP)
875 char type;
876 char * litP;
877 int * sizeP;
879 int prec;
880 LITTLENUM_TYPE words[MAX_LITTLENUMS];
881 LITTLENUM_TYPE *wordP;
882 char *t;
884 switch (type)
886 case 'f':
887 case 'F':
888 case 's':
889 case 'S':
890 prec = 2;
891 break;
893 case 'd':
894 case 'D':
895 case 'r':
896 case 'R':
897 prec = 4;
898 break;
900 case 'x':
901 case 'X':
902 prec = 6;
903 break;
905 case 'p':
906 case 'P':
907 prec = 6;
908 break;
910 default:
911 *sizeP = 0;
912 return _("Bad call to MD_ATOF()");
915 t = atof_ieee (input_line_pointer, type, words);
916 if (t)
917 input_line_pointer = t;
919 *sizeP = prec * sizeof (LITTLENUM_TYPE);
921 for (wordP = words; prec--;)
923 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
924 litP += sizeof (LITTLENUM_TYPE);
927 return NULL;
930 /* Write out big-endian. */
932 void
933 md_number_to_chars (buf, val, n)
934 char *buf;
935 valueT val;
936 int n;
938 number_to_chars_bigendian (buf, val, n);
941 #ifdef BFD_ASSEMBLER
942 void
943 md_apply_fix3 (fixP, val, seg)
944 fixS * fixP;
945 valueT * val;
946 segT seg ATTRIBUTE_UNUSED;
948 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
949 long t_val;
951 t_val = (long) *val;
953 #if DEBUG
954 printf ("md_apply_fix val:%x\n", t_val);
955 #endif
957 fixP->fx_addnumber = t_val; /* Remember value for emit_reloc. */
959 know (fixP->fx_size == 4);
960 know (fixP->fx_r_type < BFD_RELOC_NONE);
962 switch (fixP->fx_r_type)
964 case BFD_RELOC_32: /* XXXXXXXX pattern in a word. */
965 #if DEBUG
966 printf ("reloc_const: val=%x\n", t_val);
967 #endif
968 buf[0] = t_val >> 24;
969 buf[1] = t_val >> 16;
970 buf[2] = t_val >> 8;
971 buf[3] = t_val;
972 break;
974 case BFD_RELOC_16: /* XXXX0000 pattern in a word. */
975 #if DEBUG
976 printf ("reloc_const: val=%x\n", t_val);
977 #endif
978 buf[0] = t_val >> 8;
979 buf[1] = t_val;
980 break;
982 case BFD_RELOC_8: /* XX000000 pattern in a word. */
983 #if DEBUG
984 printf ("reloc_const: val=%x\n", t_val);
985 #endif
986 buf[0] = t_val;
987 break;
989 case BFD_RELOC_LO16: /* 0000XXXX pattern in a word. */
990 #if DEBUG
991 printf ("reloc_const: val=%x\n", t_val);
992 #endif
993 buf[2] = t_val >> 8; /* Holds bits 0000XXXX. */
994 buf[3] = t_val;
995 break;
997 case BFD_RELOC_HI16: /* 0000XXXX pattern in a word. */
998 #if DEBUG
999 printf ("reloc_consth: val=%x\n", t_val);
1000 #endif
1001 buf[2] = t_val >> 24; /* Holds bits XXXX0000. */
1002 buf[3] = t_val >> 16;
1003 break;
1005 case BFD_RELOC_32_GOT_PCREL: /* 0000XXXX pattern in a word. */
1006 if (!fixP->fx_done)
1008 /* The linker tries to support both AMD and old GNU style
1009 R_IREL relocs. That means that if the addend is exactly
1010 the negative of the address within the section, the
1011 linker will not handle it correctly. */
1012 #if 0
1013 if (fixP->fx_pcrel
1014 && t_val != 0
1015 && t_val == - (fixP->fx_frag->fr_address + fixP->fx_where))
1016 as_bad_where
1017 (fixP->fx_file, fixP->fx_line,
1018 _("the linker will not handle this relocation correctly (1)"));
1019 #endif
1021 else if (fixP->fx_pcrel)
1023 long v = t_val >> 28;
1025 if (v != 0 && v != -1)
1026 as_bad_where (fixP->fx_file, fixP->fx_line,
1027 _("call/jmp target out of range (2)"));
1029 else
1030 /* This case was supposed to be handled in machine_ip. */
1031 abort ();
1033 buf[0] |= (t_val >> 26) & 0x03; /* Holds bits 0FFFFFFC of address. */
1034 buf[1] = t_val >> 18;
1035 buf[2] = t_val >> 10;
1036 buf[3] = t_val >> 2;
1037 break;
1039 case BFD_RELOC_VTABLE_INHERIT:
1040 case BFD_RELOC_VTABLE_ENTRY:
1041 fixP->fx_done = 0;
1042 break;
1044 case BFD_RELOC_NONE:
1045 default:
1046 as_bad (_("bad relocation type: 0x%02x"), fixP->fx_r_type);
1047 break;
1050 if (fixP->fx_addsy == (symbolS *) NULL)
1051 fixP->fx_done = 1;
1053 #else
1054 void
1055 md_apply_fix3 (fixP, valP, seg)
1056 fixS *fixP;
1057 valueT *valP;
1058 segT seg ATTRIBUTE_UNUSED;
1060 long val = *valP;
1061 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1063 #if DEBUG
1064 printf ("md_apply_fix val:%x\n", val);
1065 #endif
1067 fixP->fx_addnumber = val; /* Remember value for emit_reloc. */
1069 know (fixP->fx_size == 4);
1070 know (fixP->fx_r_type < NO_RELOC);
1072 /* This is a hack. There should be a better way to handle this. */
1073 if (fixP->fx_r_type == RELOC_WDISP30 && fixP->fx_addsy)
1074 val += fixP->fx_where + fixP->fx_frag->fr_address;
1076 switch (fixP->fx_r_type)
1078 case RELOC_32:
1079 buf[0] = val >> 24;
1080 buf[1] = val >> 16;
1081 buf[2] = val >> 8;
1082 buf[3] = val;
1083 break;
1085 case RELOC_8:
1086 buf[0] = val;
1087 break;
1089 case RELOC_WDISP30:
1090 val = (val >>= 2) + 1;
1091 buf[0] |= (val >> 24) & 0x3f;
1092 buf[1] = (val >> 16);
1093 buf[2] = val >> 8;
1094 buf[3] = val;
1095 break;
1097 case RELOC_HI22:
1098 buf[1] |= (val >> 26) & 0x3f;
1099 buf[2] = val >> 18;
1100 buf[3] = val >> 10;
1101 break;
1103 case RELOC_LO10:
1104 buf[2] |= (val >> 8) & 0x03;
1105 buf[3] = val;
1106 break;
1108 case RELOC_BASE13:
1109 buf[2] |= (val >> 8) & 0x1f;
1110 buf[3] = val;
1111 break;
1113 case RELOC_WDISP22:
1114 val = (val >>= 2) + 1;
1115 /* FALLTHROUGH */
1116 case RELOC_BASE22:
1117 buf[1] |= (val >> 16) & 0x3f;
1118 buf[2] = val >> 8;
1119 buf[3] = val;
1120 break;
1122 case RELOC_JUMPTARG: /* 0000XXXX pattern in a word. */
1123 if (!fixP->fx_done)
1125 /* The linker tries to support both AMD and old GNU style
1126 R_IREL relocs. That means that if the addend is exactly
1127 the negative of the address within the section, the
1128 linker will not handle it correctly. */
1129 #if 0
1130 if (fixP->fx_pcrel
1131 && val != 0
1132 && val == - (fixP->fx_frag->fr_address + fixP->fx_where))
1133 as_bad_where
1134 (fixP->fx_file, fixP->fx_line,
1135 _("the linker will not handle this relocation correctly (1)"));
1136 #endif
1138 else if (fixP->fx_pcrel)
1140 long v = val >> 28;
1141 #if 1
1142 if (v != 0 && v != -1)
1143 as_bad_where (fixP->fx_file, fixP->fx_line,
1144 _("call/jmp target out of range (2)"));
1145 #endif
1147 else
1148 /* This case was supposed to be handled in machine_ip. */
1149 abort ();
1151 buf[0] |= (val >> 26) & 0x03; /* Holds bits 0FFFFFFC of address. */
1152 buf[1] = val >> 18;
1153 buf[2] = val >> 10;
1154 buf[3] = val >> 2;
1155 break;
1157 case RELOC_CONST: /* 0000XXXX pattern in a word. */
1158 #if DEBUG
1159 printf ("reloc_const: val=%x\n", val);
1160 #endif
1161 buf[2] = val >> 8; /* Holds bits 0000XXXX. */
1162 buf[3] = val;
1163 break;
1165 case RELOC_CONSTH: /* 0000XXXX pattern in a word. */
1166 #if DEBUG
1167 printf ("reloc_consth: val=%x\n", val);
1168 #endif
1169 buf[2] = val >> 24; /* Holds bits XXXX0000. */
1170 buf[3] = val >> 16;
1171 break;
1173 case BFD_RELOC_VTABLE_INHERIT:
1174 case BFD_RELOC_VTABLE_ENTRY:
1175 fixP->fx_done = 0;
1176 break;
1178 case NO_RELOC:
1179 default:
1180 as_bad (_("bad relocation type: 0x%02x"), fixP->fx_r_type);
1181 break;
1184 if (fixP->fx_addsy == (symbolS *) NULL)
1185 fixP->fx_done = 1;
1187 #endif
1189 #ifdef OBJ_COFF
1190 short
1191 tc_coff_fix2rtype (fixP)
1192 fixS *fixP;
1194 #if DEBUG
1195 printf ("tc_coff_fix2rtype\n");
1196 #endif
1198 switch (fixP->fx_r_type)
1200 case RELOC_32:
1201 return (R_WORD);
1202 case RELOC_8:
1203 return (R_BYTE);
1204 case RELOC_CONST:
1205 return (R_ILOHALF);
1206 case RELOC_CONSTH:
1207 return (R_IHIHALF);
1208 case RELOC_JUMPTARG:
1209 return (R_IREL);
1210 default:
1211 printf ("need %d\n", fixP->fx_r_type);
1212 abort ();
1215 return 0;
1218 #endif /* OBJ_COFF */
1220 /* Should never be called for or32. */
1222 void
1223 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1224 char * ptr ATTRIBUTE_UNUSED;
1225 addressT from_addr ATTRIBUTE_UNUSED;
1226 addressT to_addr ATTRIBUTE_UNUSED;
1227 fragS * frag ATTRIBUTE_UNUSED;
1228 symbolS * to_symbol ATTRIBUTE_UNUSED;
1230 as_fatal ("or32_create_short_jmp\n");
1233 /* Should never be called for or32. */
1235 #ifndef BFD_ASSEMBLER
1236 void
1237 md_convert_frag (headers, seg, fragP)
1238 object_headers * headers ATTRIBUTE_UNUSED;
1239 segT seg ATTRIBUTE_UNUSED;
1240 register fragS * fragP ATTRIBUTE_UNUSED;
1242 as_fatal ("or32_convert_frag\n");
1245 #else
1246 void
1247 md_convert_frag (headers, seg, fragP)
1248 bfd * headers ATTRIBUTE_UNUSED;
1249 segT seg ATTRIBUTE_UNUSED;
1250 fragS * fragP ATTRIBUTE_UNUSED;
1252 as_fatal ("or32_convert_frag\n");
1254 #endif
1256 /* Should never be called for or32. */
1258 void
1259 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1260 char * ptr ATTRIBUTE_UNUSED;
1261 addressT from_addr ATTRIBUTE_UNUSED;
1262 addressT to_addr ATTRIBUTE_UNUSED;
1263 fragS * frag ATTRIBUTE_UNUSED;
1264 symbolS * to_symbol ATTRIBUTE_UNUSED;
1266 as_fatal ("or32_create_long_jump\n");
1269 /* Should never be called for or32. */
1272 md_estimate_size_before_relax (fragP, segtype)
1273 fragS * fragP ATTRIBUTE_UNUSED;
1274 segT segtype ATTRIBUTE_UNUSED;
1276 as_fatal ("or32_estimate_size_before_relax\n");
1277 return 0;
1280 /* Translate internal representation of relocation info to target format.
1282 On sparc/29k: first 4 bytes are normal unsigned long address, next three
1283 bytes are index, most sig. byte first. Byte 7 is broken up with
1284 bit 7 as external, bits 6 & 5 unused, and the lower
1285 five bits as relocation type. Next 4 bytes are long addend. */
1286 /* Thanx and a tip of the hat to Michael Bloom, mb@ttidca.tti.com. */
1288 #ifdef OBJ_AOUT
1289 void
1290 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
1291 char *where;
1292 fixS *fixP;
1293 relax_addressT segment_address_in_file;
1295 long r_symbolnum;
1297 #if DEBUG
1298 printf ("tc_aout_fix_to_chars\n");
1299 #endif
1301 know (fixP->fx_r_type < BFD_RELOC_NONE);
1302 know (fixP->fx_addsy != NULL);
1304 md_number_to_chars
1305 (where,
1306 fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file,
1309 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
1310 ? S_GET_TYPE (fixP->fx_addsy)
1311 : fixP->fx_addsy->sy_number);
1313 where[4] = (r_symbolnum >> 16) & 0x0ff;
1314 where[5] = (r_symbolnum >> 8) & 0x0ff;
1315 where[6] = r_symbolnum & 0x0ff;
1316 where[7] = (((!S_IS_DEFINED (fixP->fx_addsy)) << 7) & 0x80) | (0 & 0x60) | (fixP->fx_r_type & 0x1F);
1318 /* Also easy. */
1319 md_number_to_chars (&where[8], fixP->fx_addnumber, 4);
1322 #endif /* OBJ_AOUT */
1324 const char *md_shortopts = "";
1326 struct option md_longopts[] =
1328 { NULL, no_argument, NULL, 0 }
1330 size_t md_longopts_size = sizeof (md_longopts);
1333 md_parse_option (c, arg)
1334 int c ATTRIBUTE_UNUSED;
1335 char * arg ATTRIBUTE_UNUSED;
1337 return 0;
1340 void
1341 md_show_usage (stream)
1342 FILE * stream ATTRIBUTE_UNUSED;
1346 /* This is called when a line is unrecognized. This is used to handle
1347 definitions of or32 style local labels. */
1350 or32_unrecognized_line (c)
1351 int c;
1353 int lab;
1354 char *s;
1356 if (c != '$'
1357 || ! ISDIGIT ((unsigned char) input_line_pointer[0]))
1358 return 0;
1360 s = input_line_pointer;
1362 lab = 0;
1363 while (ISDIGIT ((unsigned char) *s))
1365 lab = lab * 10 + *s - '0';
1366 ++s;
1369 if (*s != ':')
1370 /* Not a label definition. */
1371 return 0;
1373 if (dollar_label_defined (lab))
1375 as_bad (_("label \"$%d\" redefined"), lab);
1376 return 0;
1379 define_dollar_label (lab);
1380 colon (dollar_label_name (lab, 0));
1381 input_line_pointer = s + 1;
1383 return 1;
1386 #ifndef BFD_ASSEMBLER
1387 /* Record a fixup for a cons expression. */
1389 void
1390 or32_cons_fix_new (frag, where, nbytes, exp)
1391 fragS *frag;
1392 int where;
1393 int nbytes;
1394 expressionS *exp;
1396 fix_new_exp (frag, where, nbytes, exp, 0,
1397 nbytes == 5 ? RELOC_32
1398 : nbytes == 2 ? RELOC_16
1399 : RELOC_8);
1401 void
1402 tc_aout_pre_write_hook ()
1404 #if DEBUG
1405 printf ("In tc_aout_pre_write_hook()\n");
1406 #endif
1409 #endif
1411 /* Default the values of symbols known that should be "predefined". We
1412 don't bother to predefine them unless you actually use one, since there
1413 are a lot of them. */
1415 symbolS *
1416 md_undefined_symbol (name)
1417 char *name ATTRIBUTE_UNUSED;
1419 #ifndef BFD_ASSEMBLER
1420 long regnum;
1421 char testbuf[5 + /*SLOP*/ 5];
1423 #if DEBUG
1424 printf ("md_undefined_symbol(%s)\n", name);
1425 #endif
1427 /* Register name. */
1428 if (name[0] == 'r' || name[0] == 'R' || name[0] == 'a' || name[0] == 'b')
1430 long maxreg;
1432 /* Parse the number, make sure it has no extra zeroes or
1433 trailing chars. */
1434 regnum = atol (& name[1]);
1436 if (regnum > 31)
1437 as_fatal (_("register out of range"));
1439 sprintf (testbuf, "%ld", regnum);
1441 if (strcmp (testbuf, &name[1]) != 0)
1442 return NULL; /* gr007 or lr7foo or whatever. */
1444 /* We have a wiener! Define and return a new symbol for it. */
1445 return (symbol_new (name, SEG_REGISTER, (valueT) regnum,
1446 &zero_address_frag));
1448 #endif
1449 return NULL;
1452 /* Parse an operand that is machine-specific. */
1454 void
1455 md_operand (expressionP)
1456 expressionS *expressionP;
1458 #if DEBUG
1459 printf (" md_operand(input_line_pointer = %s)\n", input_line_pointer);
1460 #endif
1462 if (input_line_pointer[0] == REGISTER_PREFIX && input_line_pointer[1] == 'r')
1464 /* We have a numeric register expression. No biggy. */
1465 input_line_pointer += 2; /* Skip %r */
1466 (void) expression (expressionP);
1468 if (expressionP->X_op != O_constant
1469 || expressionP->X_add_number > 255)
1470 as_bad (_("Invalid expression after %%%%\n"));
1471 expressionP->X_op = O_register;
1473 else if (input_line_pointer[0] == '&')
1475 /* We are taking the 'address' of a register...this one is not
1476 in the manual, but it *is* in traps/fpsymbol.h! What they
1477 seem to want is the register number, as an absolute number. */
1478 input_line_pointer++; /* Skip & */
1479 (void) expression (expressionP);
1481 if (expressionP->X_op != O_register)
1482 as_bad (_("invalid register in & expression"));
1483 else
1484 expressionP->X_op = O_constant;
1486 else if (input_line_pointer[0] == '$'
1487 && ISDIGIT ((unsigned char) input_line_pointer[1]))
1489 long lab;
1490 char *name;
1491 symbolS *sym;
1493 /* This is a local label. */
1494 ++input_line_pointer;
1495 lab = (long) get_absolute_expression ();
1497 if (dollar_label_defined (lab))
1499 name = dollar_label_name (lab, 0);
1500 sym = symbol_find (name);
1502 else
1504 name = dollar_label_name (lab, 1);
1505 sym = symbol_find_or_make (name);
1508 expressionP->X_op = O_symbol;
1509 expressionP->X_add_symbol = sym;
1510 expressionP->X_add_number = 0;
1512 else if (input_line_pointer[0] == '$')
1514 char *s;
1515 char type;
1516 int fieldnum, fieldlimit;
1517 LITTLENUM_TYPE floatbuf[8];
1519 /* $float(), $doubleN(), or $extendN() convert floating values
1520 to integers. */
1521 s = input_line_pointer;
1523 ++s;
1525 fieldnum = 0;
1526 if (strncmp (s, "double", sizeof "double" - 1) == 0)
1528 s += sizeof "double" - 1;
1529 type = 'd';
1530 fieldlimit = 2;
1532 else if (strncmp (s, "float", sizeof "float" - 1) == 0)
1534 s += sizeof "float" - 1;
1535 type = 'f';
1536 fieldlimit = 1;
1538 else if (strncmp (s, "extend", sizeof "extend" - 1) == 0)
1540 s += sizeof "extend" - 1;
1541 type = 'x';
1542 fieldlimit = 4;
1544 else
1545 return;
1547 if (ISDIGIT (*s))
1549 fieldnum = *s - '0';
1550 ++s;
1552 if (fieldnum >= fieldlimit)
1553 return;
1555 SKIP_WHITESPACE ();
1556 if (*s != '(')
1557 return;
1558 ++s;
1559 SKIP_WHITESPACE ();
1561 s = atof_ieee (s, type, floatbuf);
1562 if (s == NULL)
1563 return;
1564 s = s;
1566 SKIP_WHITESPACE ();
1567 if (*s != ')')
1568 return;
1569 ++s;
1570 SKIP_WHITESPACE ();
1572 input_line_pointer = s;
1573 expressionP->X_op = O_constant;
1574 expressionP->X_unsigned = 1;
1575 expressionP->X_add_number = ((floatbuf[fieldnum * 2]
1576 << LITTLENUM_NUMBER_OF_BITS)
1577 + floatbuf[fieldnum * 2 + 1]);
1581 /* Round up a section size to the appropriate boundary. */
1583 valueT
1584 md_section_align (segment, size)
1585 segT segment ATTRIBUTE_UNUSED;
1586 valueT size ATTRIBUTE_UNUSED;
1588 return size; /* Byte alignment is fine. */
1591 /* Exactly what point is a PC-relative offset relative TO?
1592 On the 29000, they're relative to the address of the instruction,
1593 which we have set up as the address of the fixup too. */
1595 long
1596 md_pcrel_from (fixP)
1597 fixS *fixP;
1599 return fixP->fx_where + fixP->fx_frag->fr_address;
1602 /* Generate a reloc for a fixup. */
1604 #ifdef BFD_ASSEMBLER
1605 arelent *
1606 tc_gen_reloc (seg, fixp)
1607 asection *seg ATTRIBUTE_UNUSED;
1608 fixS *fixp;
1610 arelent *reloc;
1612 reloc = (arelent *) xmalloc (sizeof (arelent));
1613 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1614 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1615 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1616 /* reloc->address = fixp->fx_frag->fr_address + fixp->fx_where + fixp->fx_addnumber;*/
1617 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1619 if (reloc->howto == (reloc_howto_type *) NULL)
1621 as_bad_where (fixp->fx_file, fixp->fx_line,
1622 _("reloc %d not supported by object file format"),
1623 (int) fixp->fx_r_type);
1624 return NULL;
1627 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1628 reloc->address = fixp->fx_offset;
1630 reloc->addend = fixp->fx_addnumber;
1631 return reloc;
1633 #endif