* config/tc-dlx.c (s_proc): Avoid warning about ignoring asprintf
[external/binutils.git] / gas / config / tc-dlx.c
1 /* tc-dlx.c -- Assemble for the DLX
2    Copyright 2002, 2003, 2004, 2005, 2007, 2009, 2010, 2012
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21
22 /* Initially created by Kuang Hwa Lin, 3/20/2002.  */
23
24 #include "as.h"
25 #include "safe-ctype.h"
26 #include "tc-dlx.h"
27 #include "opcode/dlx.h"
28
29 /* Make it easier to clone this machine desc into another one.  */
30 #define machine_opcode      dlx_opcode
31 #define machine_opcodes     dlx_opcodes
32 #define machine_ip          dlx_ip
33 #define machine_it          dlx_it
34
35 #define NO_RELOC            BFD_RELOC_NONE
36 #define RELOC_DLX_REL26     BFD_RELOC_DLX_JMP26
37 #define RELOC_DLX_16        BFD_RELOC_16
38 #define RELOC_DLX_REL16     BFD_RELOC_16_PCREL_S2
39 #define RELOC_DLX_HI16      BFD_RELOC_HI16_S
40 #define RELOC_DLX_LO16      BFD_RELOC_LO16
41 #define RELOC_DLX_VTINHERIT BFD_RELOC_VTABLE_INHERIT
42 #define RELOC_DLX_VTENTRY   BFD_RELOC_VTABLE_ENTRY
43
44 /* handle of the OPCODE hash table */
45 static struct hash_control *op_hash = NULL;
46
47 struct machine_it
48 {
49   char *error;
50   unsigned long opcode;
51   struct nlist *nlistp;
52   expressionS exp;
53   int pcrel;
54   int size;
55   int reloc_offset;             /* Offset of reloc within insn.  */
56   int reloc;
57   int HI;
58   int LO;
59 }
60 the_insn;
61
62 /* This array holds the chars that always start a comment.  If the
63    pre-processor is disabled, these aren't very useful.  */
64 const char comment_chars[] = ";";
65
66 /* This array holds the chars that only start a comment at the beginning of
67    a line.  If the line seems to have the form '# 123 filename'
68    .line and .file directives will appear in the pre-processed output.  */
69 /* Note that input_file.c hand checks for '#' at the beginning of the
70    first line of the input file.  This is because the compiler outputs
71    #NO_APP at the beginning of its output.  */
72 /* Also note that comments like this one will always work.  */
73 const char line_comment_chars[] = "#";
74
75 /* We needed an unused char for line separation to work around the
76    lack of macros, using sed and such.  */
77 const char line_separator_chars[] = "@";
78
79 /* Chars that can be used to separate mant from exp in floating point nums.  */
80 const char EXP_CHARS[] = "eE";
81
82 /* Chars that mean this number is a floating point constant.
83    As in 0f12.456
84    or    0d1.2345e12.  */
85 const char FLT_CHARS[] = "rRsSfFdDxXpP";
86
87 static void
88 insert_sreg (char *regname, int regnum)
89 {
90   /* Must be large enough to hold the names of the special registers.  */
91   char buf[80];
92   int i;
93
94   symbol_table_insert (symbol_new (regname, reg_section, (valueT) regnum,
95                                    &zero_address_frag));
96   for (i = 0; regname[i]; i++)
97     buf[i] = ISLOWER (regname[i]) ? TOUPPER (regname[i]) : regname[i];
98   buf[i] = '\0';
99
100   symbol_table_insert (symbol_new (buf, reg_section, (valueT) regnum,
101                                    &zero_address_frag));
102 }
103
104 /* Install symbol definitions for assorted special registers.
105    See MIPS Assembly Language Programmer's Guide page 1-4   */
106
107 static void
108 define_some_regs (void)
109 {
110   /* Software representation.  */
111   insert_sreg ("zero",  0);
112   insert_sreg ("at",    1);
113   insert_sreg ("v0",    2);
114   insert_sreg ("v1",    3);
115   insert_sreg ("a0",    4);
116   insert_sreg ("a1",    5);
117   insert_sreg ("a2",    6);
118   insert_sreg ("a3",    7);
119   insert_sreg ("t0",    8);
120   insert_sreg ("t1",    9);
121   insert_sreg ("t2",    10);
122   insert_sreg ("t3",    11);
123   insert_sreg ("t4",    12);
124   insert_sreg ("t5",    13);
125   insert_sreg ("t6",    14);
126   insert_sreg ("t7",    15);
127   insert_sreg ("s0",    16);
128   insert_sreg ("s1",    17);
129   insert_sreg ("s2",    18);
130   insert_sreg ("s3",    19);
131   insert_sreg ("s4",    20);
132   insert_sreg ("s5",    21);
133   insert_sreg ("s6",    22);
134   insert_sreg ("s7",    23);
135   insert_sreg ("t8",    24);
136   insert_sreg ("t9",    25);
137   insert_sreg ("k0",    26);
138   insert_sreg ("k1",    27);
139   insert_sreg ("gp",    28);
140   insert_sreg ("sp",    29);
141   insert_sreg ("fp",    30);
142   insert_sreg ("ra",    31);
143   /* Special registers.  */
144   insert_sreg ("pc",    0);
145   insert_sreg ("npc",   1);
146   insert_sreg ("iad",   2);
147 }
148
149 /* Subroutine check the string to match an register.  */
150
151 static int
152 match_sft_register (char *name)
153 {
154 #define MAX_REG_NO  35
155 /* Currently we have 35 software registers defined -
156    we borrowed from MIPS.   */
157   static char *soft_reg[] =
158     {
159       "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
160       "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9",
161       "s0", "s1", "s2", "s3", "s4", "s5", "s7", "k0", "k1",
162       "gp", "sp", "fp", "ra", "pc", "npc", "iad",
163       "EndofTab"  /* End of the Table indicator */
164     };
165   char low_name[21], *ptr;
166   int idx;
167
168   for (ptr = name,idx = 0; *ptr != '\0'; ptr++)
169     low_name[idx++] = TOLOWER (*ptr);
170
171   low_name[idx] = '\0';
172   idx = 0;
173
174   while (idx < MAX_REG_NO && strcmp (soft_reg[idx], & low_name [0]))
175     idx += 1;
176
177   return idx < MAX_REG_NO;
178 }
179
180 /* Subroutine check the string to match an register.  */
181
182 static int
183 is_ldst_registers (char *name)
184 {
185   char *ptr = name;
186
187   /* The first character of the register name got to be either %, $, r of R.  */
188   if ((ptr[0] == '%' || ptr[0] == '$' || ptr[0] == 'r' || ptr[0] == 'R')
189       && ISDIGIT ((unsigned char) ptr[1]))
190     return 1;
191
192   /* Now check the software register representation.  */
193   return match_sft_register (ptr);
194 }
195
196 /* Subroutine of s_proc so targets can choose a different default prefix.
197    If DEFAULT_PREFIX is NULL, use the target's "leading char".  */
198
199 static void
200 s_proc (int end_p)
201 {
202   /* Record the current function so that we can issue an error message for
203      misplaced .func,.endfunc, and also so that .endfunc needs no
204      arguments.  */
205   static char *current_name;
206   static char *current_label;
207
208   if (end_p)
209     {
210       if (current_name == NULL)
211         {
212           as_bad (_("missing .proc"));
213           ignore_rest_of_line ();
214           return;
215         }
216
217       current_name = current_label = NULL;
218       SKIP_WHITESPACE ();
219       while (!is_end_of_line[(unsigned char) *input_line_pointer])
220         input_line_pointer++;
221     }
222   else
223     {
224       char *name, *label;
225       char delim1, delim2;
226
227       if (current_name != NULL)
228         {
229           as_bad (_(".endfunc missing for previous .proc"));
230           ignore_rest_of_line ();
231           return;
232         }
233
234       name = input_line_pointer;
235       delim1 = get_symbol_end ();
236       name = xstrdup (name);
237       *input_line_pointer = delim1;
238       SKIP_WHITESPACE ();
239
240       if (*input_line_pointer != ',')
241         {
242           char leading_char = 0;
243
244           leading_char = bfd_get_symbol_leading_char (stdoutput);
245           /* Missing entry point, use function's name with the leading
246              char prepended.  */
247           if (leading_char)
248             {
249               if (asprintf (&label, "%c%s", leading_char, name))
250                 { /* Avoid warning */ }
251             }
252           else
253             label = name;
254         }
255       else
256         {
257           ++input_line_pointer;
258           SKIP_WHITESPACE ();
259           label = input_line_pointer;
260           delim2 = get_symbol_end ();
261           label = xstrdup (label);
262           *input_line_pointer = delim2;
263         }
264
265       current_name = name;
266       current_label = label;
267     }
268   demand_empty_rest_of_line ();
269 }
270
271 /* This function is called once, at assembler startup time.  It should
272    set up all the tables, etc., that the MD part of the assembler will
273    need.  */
274
275 void
276 md_begin (void)
277 {
278   const char *retval = NULL;
279   int lose = 0;
280   unsigned int i;
281
282   /* Create a new hash table.  */
283   op_hash = hash_new ();
284
285   /* Hash up all the opcodes for fast use later.  */
286   for (i = 0; i < num_dlx_opcodes; i++)
287     {
288       const char *name = machine_opcodes[i].name;
289
290       retval = hash_insert (op_hash, name, (void *) &machine_opcodes[i]);
291
292       if (retval != NULL)
293         {
294           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
295                    machine_opcodes[i].name, retval);
296           lose = 1;
297         }
298     }
299
300   if (lose)
301     as_fatal (_("Broken assembler.  No assembly attempted."));
302
303   define_some_regs ();
304 }
305
306 /* This function will check the opcode and return 1 if the opcode is one
307    of the load/store instruction, and it will fix the operand string to
308    the standard form so we can use the standard parse_operand routine.  */
309
310 #define READ_OP     0x100
311 #define WRITE_OP    0x200
312 static char iBuf[81];
313
314 static char *
315 dlx_parse_loadop (char * str)
316 {
317   char *ptr = str;
318   int   idx = 0;
319
320   /* The last pair of ()/[] is the register, all other are the
321      reloc displacement, and if there is a register then it ought
322      to have a pair of ()/[]
323      This is not necessarily true, what if the load instruction come
324      without the register and with %hi/%lo modifier?  */
325   for (idx = 0; idx < 72 && ptr[idx] != '\0'; idx++)
326     ;
327
328   if (idx == 72)
329     {
330     badoperand_load:
331       as_bad (_("Bad operand for a load instruction: <%s>"), str);
332       return NULL;
333     }
334   else
335     {
336       int i, pb = 0;
337       int m2 = 0;
338       char rs1[7], rd[7], endm, match = '0';
339       char imm[72];
340
341       idx -= 1;
342       switch (str[idx])
343         {
344         case ')':
345           match = '(';
346           endm  = ')';
347           break;
348         case ']':
349           match = '[';
350           endm  = ']';
351           break;
352         default:
353           /* No register indicated, fill in zero.  */
354           rs1[0] = 'r';
355           rs1[1] = '0';
356           rs1[2] = '\0';
357           match  = 0;
358           endm = 0;
359           m2 = 1;
360         }
361
362       if (!m2)
363         {
364           /* Searching for (/[ which will match the ]/).  */
365           for (pb = idx - 1; str[pb] != match; pb -= 1)
366             /* Match can only be either '[' or '(', if it is
367                '(' then this can be a normal expression, we'll treat
368                it as an operand.  */
369             if (str[pb] == endm || pb < (idx - 5))
370               goto load_no_rs1;
371           pb += 1;
372
373           for (i = 0; (pb + i) < idx; i++)
374             rs1[i] = str[pb+i];
375
376           rs1[i] = '\0';
377
378           if (is_ldst_registers (& rs1[0]))
379             /* Point to the last character of the imm.  */
380             pb -= 1;
381           else
382             {
383             load_no_rs1:
384               if (match == '[')
385                 goto badoperand_load;
386               /* No register indicated, fill in zero and restore the imm.  */
387               rs1[0] = 'r';
388               rs1[1] = '0';
389               rs1[2] = '\0';
390               m2 = 1;
391             }
392         }
393
394       /* Duplicate the first register.  */
395       for (i = 0; i < 7 && str[i] != ','; i++)
396         rd[i] = ptr[i];
397
398       if (str[i] != ',')
399         goto badoperand_load;
400       else
401         rd[i] = '\0';
402
403       /* Copy the immd.  */
404       if (m2)
405         /* Put the '\0' back in.  */
406         pb = idx + 1;
407
408       for (i++, m2 = 0; i < pb; m2++,i++)
409         imm[m2] = ptr[i];
410
411       imm[m2] = '\0';
412
413       /* Assemble the instruction to gas internal format.  */
414       for (i = 0; rd[i] != '\0'; i++)
415         iBuf[i] = rd[i];
416
417       iBuf[i++] = ',';
418
419       for (pb = 0 ; rs1[pb] != '\0'; i++, pb++)
420         iBuf[i] = rs1[pb];
421
422       iBuf[i++] = ',';
423
424       for (pb = 0; imm[pb] != '\0'; i++, pb++)
425         iBuf[i] = imm[pb];
426
427       iBuf[i] = '\0';
428       return iBuf;
429     }
430 }
431
432 static char *
433 dlx_parse_storeop (char * str)
434 {
435   char *ptr = str;
436   int   idx = 0;
437
438   /* Search for the ','.  */
439   for (idx = 0; idx < 72 && ptr[idx] != ','; idx++)
440     ;
441
442   if (idx == 72)
443     {
444     badoperand_store:
445       as_bad (_("Bad operand for a store instruction: <%s>"), str);
446       return NULL;
447     }
448   else
449     {
450       /* idx now points to the ','.  */
451       int i, pb = 0;
452       int comma = idx;
453       int m2 = 0;
454       char rs1[7], rd[7], endm, match = '0';
455       char imm[72];
456
457       /* Now parse the '(' and ')', and make idx point to ')'.  */
458       idx -= 1;
459       switch (str[idx])
460         {
461         case ')':
462           match = '(';
463           endm  = ')';
464           break;
465         case ']':
466           match = '[';
467           endm  = ']';
468           break;
469         default:
470           /* No register indicated, fill in zero.  */
471           rs1[0] = 'r';
472           rs1[1] = '0';
473           rs1[2] = '\0';
474           match  = 0;
475           endm = 0;
476           m2 = 1;
477         }
478
479       if (!m2)
480         {
481           /* Searching for (/[ which will match the ]/).  */
482           for (pb = idx - 1; str[pb] != match; pb -= 1)
483             if (pb < (idx - 5) || str[pb] == endm)
484               goto store_no_rs1;
485           pb += 1;
486
487           for (i = 0; (pb + i) < idx; i++)
488             rs1[i] = str[pb + i];
489
490           rs1[i] = '\0';
491
492           if (is_ldst_registers (& rs1[0]))
493             /* Point to the last character of the imm.  */
494             pb -= 1;
495           else
496             {
497             store_no_rs1:
498               if (match == '[')
499                 goto badoperand_store;
500
501               /* No register indicated, fill in zero and restore the imm.  */
502               rs1[0] = 'r';
503               rs1[1] = '0';
504               rs1[2] = '\0';
505               pb = comma;
506             }
507         }
508       else
509         /* No register was specified.  */
510         pb = comma;
511
512       /* Duplicate the first register.  */
513       for (i = comma + 1; (str[i] == ' ' || str[i] == '\t'); i++)
514         ;
515
516       for (m2 = 0; (m2 < 7 && str[i] != '\0'); i++, m2++)
517         {
518           if (str[i] != ' ' && str[i] != '\t')
519             rd[m2] = str[i];
520           else
521             goto badoperand_store;
522         }
523
524       if (str[i] != '\0')
525         goto badoperand_store;
526       else
527         rd[m2] = '\0';
528
529       /* Copy the immd.  */
530       for (i = 0; i < pb; i++)
531         imm[i] = ptr[i];
532
533       imm[i] = '\0';
534
535       /* Assemble the instruction to gas internal format.  */
536       for (i = 0; rd[i] != '\0'; i++)
537         iBuf[i] = rd[i];
538       iBuf[i++] = ',';
539       for (pb = 0 ; rs1[pb] != '\0'; i++, pb++)
540         iBuf[i] = rs1[pb];
541       iBuf[i++] = ',';
542       for (pb = 0; imm[pb] != '\0'; i++, pb++)
543         iBuf[i] = imm[pb];
544       iBuf[i] = '\0';
545       return iBuf;
546     }
547 }
548
549 static char *
550 fix_ld_st_operand (unsigned long opcode, char* str)
551 {
552   /* Check the opcode.  */
553   switch ((int) opcode)
554     {
555     case  LBOP:
556     case  LBUOP:
557     case  LSBUOP:
558     case  LHOP:
559     case  LHUOP:
560     case  LSHUOP:
561     case  LWOP:
562     case  LSWOP:
563       return dlx_parse_loadop (str);
564     case  SBOP:
565     case  SHOP:
566     case  SWOP:
567       return dlx_parse_storeop (str);
568     default:
569       return str;
570     }
571 }
572
573 static int
574 hilo_modifier_ok (char *s)
575 {
576   char *ptr = s;
577   int   idx, count = 1;
578
579   if (*ptr != '(')
580     return 1;
581
582   for (idx = 1; ptr[idx] != '\0' && ptr[idx] != '[' && idx < 73; idx += 1)
583     {
584       if (count == 0)
585         return count;
586
587       if (ptr[idx] == '(')
588         count += 1;
589
590       if (ptr[idx] == ')')
591         count -= 1;
592     }
593
594   return (count == 0) ? 1:0;
595 }
596
597 static char *
598 parse_operand (char *s, expressionS *operandp)
599 {
600   char *save = input_line_pointer;
601   char *new_pos;
602
603   the_insn.HI = the_insn.LO = 0;
604
605   /* Search for %hi and %lo, make a mark and skip it.  */
606   if (strncmp (s, "%hi", 3) == 0)
607     {
608       s += 3;
609       the_insn.HI = 1;
610     }
611   else
612     {
613       if (strncmp (s, "%lo", 3) == 0)
614         {
615           s += 3;
616           the_insn.LO = 1;
617         }
618       else
619         the_insn.LO = 0;
620     }
621
622   if (the_insn.HI || the_insn.LO)
623     {
624       if (!hilo_modifier_ok (s))
625         as_bad (_("Expression Error for operand modifier %%hi/%%lo\n"));
626     }
627
628   /* Check for the % and $ register representation    */
629   if ((s[0] == '%' || s[0] == '$' || s[0] == 'r' || s[0] == 'R')
630       && ISDIGIT ((unsigned char) s[1]))
631     {
632       /* We have a numeric register expression.  No biggy.  */
633       s += 1;
634       input_line_pointer = s;
635       (void) expression (operandp);
636       if (operandp->X_op != O_constant
637           || operandp->X_add_number > 31)
638         as_bad (_("Invalid expression after %%%%\n"));
639       operandp->X_op = O_register;
640     }
641   else
642     {
643       /* Normal operand parsing.  */
644       input_line_pointer = s;
645       (void) expression (operandp);
646     }
647
648   new_pos = input_line_pointer;
649   input_line_pointer = save;
650   return new_pos;
651 }
652
653 /* Instruction parsing.  Takes a string containing the opcode.
654    Operands are at input_line_pointer.  Output is in the_insn.
655    Warnings or errors are generated.  */
656
657 static void
658 machine_ip (char *str)
659 {
660   char *s;
661   const char *args;
662   struct machine_opcode *insn;
663   unsigned long opcode;
664   expressionS the_operand;
665   expressionS *operand = &the_operand;
666   unsigned int reg, reg_shift = 0;
667
668   /* Fixup the opcode string to all lower cases, and also
669      allow numerical digits.  */
670   s = str;
671
672   if (ISALPHA (*s))
673     for (; ISALNUM (*s); ++s)
674       if (ISUPPER (*s))
675         *s = TOLOWER (*s);
676
677   switch (*s)
678     {
679     case '\0':
680       break;
681
682       /* FIXME-SOMEDAY more whitespace.  */
683     case ' ':
684       *s++ = '\0';
685       break;
686
687     default:
688       as_bad (_("Unknown opcode: `%s'"), str);
689       return;
690     }
691
692   /* Hash the opcode, insn will have the string from opcode table.
693      also initialized the_insn struct.  */
694   if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
695     {
696       /* Handle the ret and return macro here.  */
697       if ((strcmp (str, "ret") == 0) || (strcmp (str, "return") == 0))
698         {
699           memset (&the_insn, '\0', sizeof (the_insn));
700           the_insn.reloc = NO_RELOC;
701           the_insn.pcrel = 0;
702           the_insn.opcode =
703             (unsigned long)(JROP | 0x03e00000);    /* 0x03e00000 = r31 << 21 */
704         }
705       else
706         as_bad (_("Unknown opcode `%s'."), str);
707
708       return;
709     }
710
711   opcode = insn->opcode;
712   memset (&the_insn, '\0', sizeof (the_insn));
713   the_insn.reloc = NO_RELOC;
714   the_insn.pcrel = 0;
715
716   /* Set the sip reloc HI16 flag.  */
717   if (!set_dlx_skip_hi16_flag (1))
718     as_bad (_("Can not set dlx_skip_hi16_flag"));
719
720   /* Fix the operand string if it is one of load store instructions.  */
721   s = fix_ld_st_operand (opcode, s);
722
723   /* Build the opcode, checking as we go to make sure that the
724      operands match.
725      If an operand matches, we modify the_insn or opcode appropriately,
726      and do a "continue".  If an operand fails to match, we "break".  */
727   if (insn->args[0] != '\0' && insn->args[0] != 'N')
728     {
729       /* Prime the pump.  */
730       if (*s == '\0')
731         {
732           as_bad (_("Missing arguments for opcode <%s>."), str);
733           return;
734         }
735       else
736         s = parse_operand (s, operand);
737     }
738   else if (insn->args[0] == 'N')
739     {
740       /* Clean up the insn and done!  */
741       the_insn.opcode = opcode;
742       return;
743     }
744
745   /* Parse through the args (this is from opcode table), *s point to
746      the current character of the instruction stream.  */
747   for (args = insn->args;; ++args)
748     {
749       switch (*args)
750         {
751           /* End of Line.  */
752         case '\0':
753           /* End of args.  */
754           if (*s == '\0')
755             {
756               /* We are truly done.  */
757               the_insn.opcode = opcode;
758               /* Clean up the HI and LO mark.  */
759               the_insn.HI = 0;
760               the_insn.LO = 0;
761               return;
762             }
763
764           the_insn.HI = 0;
765           the_insn.LO = 0;
766           as_bad (_("Too many operands: %s"), s);
767           break;
768
769           /* ',' Args separator */
770         case ',':
771           /* Must match a comma.  */
772           if (*s++ == ',')
773             {
774               /* Parse next operand.  */
775               s = parse_operand (s, operand);
776               continue;
777             }
778           break;
779
780           /* It can be a 'a' register or 'i' operand.  */
781         case 'P':
782           /* Macro move operand/reg.  */
783           if (operand->X_op == O_register)
784             {
785               /* Its a register.  */
786               reg_shift = 21;
787               goto general_reg;
788             }
789
790           /* The immediate 16 bits literal, bit 0-15.  */
791         case 'i':
792           /* offset, unsigned.  */
793         case 'I':
794           /* offset, signed.  */
795           if (operand->X_op == O_constant)
796             {
797               if (the_insn.HI)
798                 operand->X_add_number >>= 16;
799
800               opcode |= operand->X_add_number & 0xFFFF;
801
802               if (the_insn.HI && the_insn.LO)
803                 as_bad (_("Both the_insn.HI and the_insn.LO are set : %s"), s);
804               else
805                 {
806                   the_insn.HI = 0;
807                   the_insn.LO = 0;
808                 }
809               continue;
810             }
811
812           the_insn.reloc        = (the_insn.HI) ? RELOC_DLX_HI16 
813             : (the_insn.LO ? RELOC_DLX_LO16 : RELOC_DLX_16);
814           the_insn.reloc_offset = 2;
815           the_insn.size         = 2;
816           the_insn.pcrel        = 0;
817           the_insn.exp          = * operand;
818           the_insn.HI           = 0;
819           the_insn.LO           = 0;
820           continue;
821
822         case 'd':
823           /* offset, signed.  */
824           if (operand->X_op == O_constant)
825             {
826               opcode |= operand->X_add_number & 0xFFFF;
827               continue;
828             }
829           the_insn.reloc        = RELOC_DLX_REL16;
830           the_insn.reloc_offset = 0;    /* BIG-ENDIAN Byte 3 of insn.  */
831           the_insn.size         = 4;
832           the_insn.pcrel        = 1;
833           the_insn.exp          = *operand;
834           continue;
835
836           /* The immediate 26 bits literal, bit 0-25.  */
837         case 'D':
838           /* offset, signed.  */
839           if (operand->X_op == O_constant)
840             {
841               opcode |= operand->X_add_number & 0x3FFFFFF;
842               continue;
843             }
844           the_insn.reloc = RELOC_DLX_REL26;
845           the_insn.reloc_offset = 0;    /* BIG-ENDIAN Byte 3 of insn.  */
846           the_insn.size  = 4;
847           the_insn.pcrel = 1;
848           the_insn.exp = *operand;
849           continue;
850
851           /* Type 'a' Register.  */
852         case 'a':
853           /* A general register at bits 21-25, rs1.  */
854           reg_shift = 21;
855           goto general_reg;
856
857           /* Type 'b' Register.  */
858         case 'b':
859           /* A general register at bits 16-20, rs2/rd.  */
860           reg_shift = 16;
861           goto general_reg;
862
863           /* Type 'c' Register.  */
864         case 'c':
865           /* A general register at bits 11-15, rd.  */
866           reg_shift = 11;
867
868         general_reg:
869           know (operand->X_add_symbol == 0);
870           know (operand->X_op_symbol == 0);
871           reg = operand->X_add_number;
872           if (reg & 0xffffffe0)
873             as_fatal (_("failed regnum sanity check."));
874           else
875             /* Got the register, now figure out where it goes in the opcode.  */
876             opcode |= reg << reg_shift;
877
878           switch (*args)
879             {
880             case 'a':
881             case 'b':
882             case 'c':
883             case 'P':
884               continue;
885             }
886           as_fatal (_("failed general register sanity check."));
887           break;
888
889         default:
890           BAD_CASE (*args);
891         }
892
893       /* Types or values of args don't match.  */
894       as_bad (_("Invalid operands"));
895       return;
896     }
897 }
898
899 /* Assemble a single instruction.  Its label has already been handled
900    by the generic front end.  We just parse opcode and operands, and
901    produce the bytes of data and relocation.  */
902
903 void
904 md_assemble (char *str)
905 {
906   char *toP;
907   fixS *fixP;
908   bit_fixS *bitP;
909
910   know (str);
911   machine_ip (str);
912   toP = frag_more (4);
913   dwarf2_emit_insn (4);
914
915   /* Put out the opcode.  */
916   md_number_to_chars (toP, the_insn.opcode, 4);
917
918   /* Put out the symbol-dependent stuff.  */
919   if (the_insn.reloc != NO_RELOC)
920     {
921       fixP = fix_new_exp (frag_now,
922                           (toP - frag_now->fr_literal + the_insn.reloc_offset),
923                           the_insn.size, & the_insn.exp, the_insn.pcrel,
924                           the_insn.reloc);
925
926       /* Turn off complaints that the addend is
927          too large for things like foo+100000@ha.  */
928       switch (the_insn.reloc)
929         {
930         case RELOC_DLX_HI16:
931         case RELOC_DLX_LO16:
932           fixP->fx_no_overflow = 1;
933           break;
934         default:
935           break;
936         }
937
938       switch (fixP->fx_r_type)
939         {
940         case RELOC_DLX_REL26:
941           bitP = malloc (sizeof (bit_fixS));
942           bitP->fx_bit_size = 26;
943           bitP->fx_bit_offset = 25;
944           bitP->fx_bit_base = the_insn.opcode & 0xFC000000;
945           bitP->fx_bit_base_adj = 0;
946           bitP->fx_bit_max = 0;
947           bitP->fx_bit_min = 0;
948           bitP->fx_bit_add = 0x03FFFFFF;
949           fixP->fx_bit_fixP = bitP;
950           break;
951         case RELOC_DLX_LO16:
952         case RELOC_DLX_REL16:
953           bitP = malloc (sizeof (bit_fixS));
954           bitP->fx_bit_size = 16;
955           bitP->fx_bit_offset = 15;
956           bitP->fx_bit_base = the_insn.opcode & 0xFFFF0000;
957           bitP->fx_bit_base_adj = 0;
958           bitP->fx_bit_max = 0;
959           bitP->fx_bit_min = 0;
960           bitP->fx_bit_add = 0x0000FFFF;
961           fixP->fx_bit_fixP = bitP;
962           break;
963         case RELOC_DLX_HI16:
964           bitP = malloc (sizeof (bit_fixS));
965           bitP->fx_bit_size = 16;
966           bitP->fx_bit_offset = 15;
967           bitP->fx_bit_base = the_insn.opcode & 0xFFFF0000;
968           bitP->fx_bit_base_adj = 0;
969           bitP->fx_bit_max = 0;
970           bitP->fx_bit_min = 0;
971           bitP->fx_bit_add = 0x0000FFFF;
972           fixP->fx_bit_fixP = bitP;
973           break;
974         default:
975           fixP->fx_bit_fixP = NULL;
976           break;
977         }
978     }
979 }
980
981 /* This is identical to the md_atof in m68k.c.  I think this is right,
982    but I'm not sure.  Dlx will not use it anyway, so I just leave it
983    here for now.  */
984
985 char *
986 md_atof (int type, char *litP, int *sizeP)
987 {
988   return ieee_md_atof (type, litP, sizeP, TRUE);
989 }
990
991 /* Write out big-endian.  */
992 void
993 md_number_to_chars (char *buf, valueT val, int n)
994 {
995   number_to_chars_bigendian (buf, val, n);
996 }
997
998 bfd_boolean
999 md_dlx_fix_adjustable (fixS *fixP)
1000 {
1001   /* We need the symbol name for the VTABLE entries.  */
1002   return (fixP->fx_r_type != BFD_RELOC_VTABLE_INHERIT
1003           && fixP->fx_r_type != BFD_RELOC_VTABLE_ENTRY);
1004 }
1005
1006 void
1007 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1008 {
1009   long val = *valP;
1010   char *place = fixP->fx_where + fixP->fx_frag->fr_literal;
1011
1012   switch (fixP->fx_r_type)
1013     {
1014     case RELOC_DLX_LO16:
1015     case RELOC_DLX_REL16:
1016       if (fixP->fx_bit_fixP != NULL)
1017         {
1018           val = (val & 0x0000FFFF) | fixP->fx_bit_fixP->fx_bit_base;
1019           free (fixP->fx_bit_fixP);
1020           fixP->fx_bit_fixP = NULL;
1021         }
1022 #ifdef DEBUG
1023       else
1024         know ((fixP->fx_bit_fixP != NULL));
1025 #endif
1026       break;
1027
1028     case RELOC_DLX_HI16:
1029       if (fixP->fx_bit_fixP != NULL)
1030         {
1031           val = (val >> 16) | fixP->fx_bit_fixP->fx_bit_base;
1032           free (fixP->fx_bit_fixP);
1033           fixP->fx_bit_fixP = NULL;
1034         }
1035 #ifdef DEBUG
1036       else
1037         know ((fixP->fx_bit_fixP != NULL));
1038 #endif
1039       break;
1040
1041     case RELOC_DLX_REL26:
1042       if (fixP->fx_bit_fixP != NULL)
1043         {
1044           val = (val & 0x03FFFFFF) | fixP->fx_bit_fixP->fx_bit_base;
1045           free (fixP->fx_bit_fixP);
1046           fixP->fx_bit_fixP = NULL;
1047         }
1048 #ifdef DEBUG
1049       else
1050         know ((fixP->fx_bit_fixP != NULL));
1051 #endif
1052       break;
1053
1054     case BFD_RELOC_VTABLE_INHERIT:
1055       /* This borrowed from tc-ppc.c on a whim.  */
1056       fixP->fx_done = 0;
1057       if (fixP->fx_addsy
1058           && !S_IS_DEFINED (fixP->fx_addsy)
1059           && !S_IS_WEAK (fixP->fx_addsy))
1060         S_SET_WEAK (fixP->fx_addsy);
1061       return;
1062
1063     case BFD_RELOC_VTABLE_ENTRY:
1064       fixP->fx_done = 0;
1065       return;
1066
1067     default:
1068       break;
1069     }
1070
1071   number_to_chars_bigendian (place, val, fixP->fx_size);
1072   if (fixP->fx_addsy == NULL)
1073     fixP->fx_done = 1;
1074 }
1075
1076 const char *md_shortopts = "";
1077
1078 struct option md_longopts[] =
1079   {
1080     {NULL, no_argument, NULL, 0}
1081   };
1082
1083 size_t md_longopts_size = sizeof (md_longopts);
1084
1085 int
1086 md_parse_option (int c     ATTRIBUTE_UNUSED,
1087                  char *arg ATTRIBUTE_UNUSED)
1088 {
1089   return 0;
1090 }
1091
1092 void
1093 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
1094 {
1095 }
1096
1097 /* This is called when a line is unrecognized.  */
1098
1099 int
1100 dlx_unrecognized_line (int c)
1101 {
1102   int lab;
1103   char *s;
1104
1105   if (c != '$' || ! ISDIGIT ((unsigned char) input_line_pointer[0]))
1106     return 0;
1107
1108   s = input_line_pointer;
1109
1110   lab = 0;
1111   while (ISDIGIT ((unsigned char) *s))
1112     {
1113       lab = lab * 10 + *s - '0';
1114       ++s;
1115     }
1116
1117   if (*s != ':')
1118     /* Not a label definition.  */
1119     return 0;
1120
1121   if (dollar_label_defined (lab))
1122     {
1123       as_bad (_("label \"$%d\" redefined"), lab);
1124       return 0;
1125     }
1126
1127   define_dollar_label (lab);
1128   colon (dollar_label_name (lab, 0));
1129   input_line_pointer = s + 1;
1130
1131   return 1;
1132 }
1133
1134 /* Default the values of symbols known that should be "predefined".  We
1135    don't bother to predefine them unless you actually use one, since there
1136    are a lot of them.  */
1137
1138 symbolS *
1139 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1140 {
1141   return NULL;
1142 }
1143
1144 /* Parse an operand that is machine-specific, the function was called
1145    in expr.c by operand() function, when everything failed before it
1146    call a quit.  */
1147
1148 void
1149 md_operand (expressionS* expressionP)
1150 {
1151   /* Check for the #number representation    */
1152   if (input_line_pointer[0] == '#' &&
1153       ISDIGIT ((unsigned char) input_line_pointer[1]))
1154     {
1155       /* We have a numeric number expression.  No biggy.  */
1156       input_line_pointer += 1;  /* Skip # */
1157
1158       (void) expression (expressionP);
1159
1160       if (expressionP->X_op != O_constant)
1161         as_bad (_("Invalid expression after # number\n"));
1162     }
1163
1164   return;
1165 }
1166
1167 /* Round up a section size to the appropriate boundary.  */
1168
1169 valueT
1170 md_section_align (segT segment ATTRIBUTE_UNUSED,
1171                   valueT size)
1172 {
1173   /* Byte alignment is fine.  */
1174   return size;
1175 }
1176
1177 /* Exactly what point is a PC-relative offset relative TO?
1178    On the 29000, they're relative to the address of the instruction,
1179    which we have set up as the address of the fixup too.  */
1180
1181 long
1182 md_pcrel_from (fixS* fixP)
1183 {
1184   return 4 + fixP->fx_where + fixP->fx_frag->fr_address;
1185 }
1186
1187 /* Translate internal representation of relocation info to BFD target
1188    format.
1189    FIXME: To what extent can we get all relevant targets to use this?
1190    The above FIXME is from a29k, but I think it is also needed here.    */
1191
1192 arelent *
1193 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED,
1194               fixS *fixP)
1195 {
1196   arelent * reloc;
1197
1198   reloc = xmalloc (sizeof (arelent));
1199   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1200
1201   if (reloc->howto == NULL)
1202     {
1203       as_bad_where (fixP->fx_file, fixP->fx_line,
1204                     _("internal error: can't export reloc type %d (`%s')"),
1205                     fixP->fx_r_type,
1206                     bfd_get_reloc_code_name (fixP->fx_r_type));
1207       return NULL;
1208     }
1209
1210   gas_assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
1211
1212   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1213   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
1214   reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
1215
1216   if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1217     reloc->address = fixP->fx_offset;
1218   reloc->addend = 0;
1219
1220   return reloc;
1221 }
1222
1223 const pseudo_typeS
1224 dlx_pseudo_table[] =
1225 {
1226   /* Some additional ops that are used by gcc-dlx.  */
1227   {"asciiz", stringer, 8 + 1},
1228   {"half", cons, 2},
1229   {"dword", cons, 8},
1230   {"word", cons, 4},
1231   {"proc", s_proc, 0},
1232   {"endproc", s_proc, 1},
1233   {NULL, NULL, 0}
1234 };
1235
1236 void
1237 dlx_pop_insert (void)
1238 {
1239   pop_insert (dlx_pseudo_table);
1240   return ;
1241 }