daily update
[binutils.git] / gas / config / tc-dlx.c
blob4b72b564de9729c14a01b4c7040ca1315e6edcb6
1 /* tc-ldx.c -- Assemble for the DLX
2 Copyright 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 /* Initially created by Kuang Hwa Lin, 3/20/2002. */
23 #include "safe-ctype.h"
24 #include "as.h"
25 #include "tc-dlx.h"
26 #include "opcode/dlx.h"
27 #if 0
28 #include "elf/dlx.h"
29 #endif
31 /* Make it easier to clone this machine desc into another one. */
32 #define machine_opcode dlx_opcode
33 #define machine_opcodes dlx_opcodes
34 #define machine_ip dlx_ip
35 #define machine_it dlx_it
37 #define NO_RELOC BFD_RELOC_NONE
38 #define RELOC_DLX_REL26 BFD_RELOC_DLX_JMP26
39 #define RELOC_DLX_16 BFD_RELOC_16
40 #define RELOC_DLX_REL16 BFD_RELOC_16_PCREL_S2
41 #define RELOC_DLX_HI16 BFD_RELOC_HI16_S
42 #define RELOC_DLX_LO16 BFD_RELOC_LO16
43 #define RELOC_DLX_VTINHERIT BFD_RELOC_VTABLE_INHERIT
44 #define RELOC_DLX_VTENTRY BFD_RELOC_VTABLE_ENTRY
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 size;
57 int reloc_offset; /* Offset of reloc within insn. */
58 int reloc;
59 int HI;
60 int LO;
62 the_insn;
64 /* static void print_insn PARAMS ((struct machine_it *)); */
65 char * parse_operand PARAMS ((char *, expressionS *));
66 int md_chars_to_number PARAMS ((unsigned char *, int));
68 static void machine_ip PARAMS ((char *));
69 static void s_proc PARAMS ((int));
70 static void insert_sreg PARAMS ((char *, int));
71 static int hilo_modifier_ok PARAMS ((char *));
72 static int is_ldst_registers PARAMS ((char *));
73 static int match_sft_register PARAMS ((char *));
74 static void define_some_regs PARAMS ((void));
75 static char * dlx_parse_loadop PARAMS ((char *));
76 static char * dlx_parse_storeop PARAMS ((char *));
77 static char * fix_ld_st_operand PARAMS ((unsigned long, char *));
79 const pseudo_typeS
81 dlx_pseudo_table[] =
83 /* Some additional ops that are used by gcc-dlx. */
84 {"asciiz", stringer, 1},
85 {"half", cons, 2},
86 {"dword", cons, 8},
87 {"word", cons, 4},
88 {"proc", s_proc, 0},
89 {"endproc", s_proc, 1},
90 {NULL, 0, 0},
93 /* This array holds the chars that always start a comment. If the
94 pre-processor is disabled, these aren't very useful. */
95 const char comment_chars[] = ";";
97 /* This array holds the chars that only start a comment at the beginning of
98 a line. If the line seems to have the form '# 123 filename'
99 .line and .file directives will appear in the pre-processed output. */
100 /* Note that input_file.c hand checks for '#' at the beginning of the
101 first line of the input file. This is because the compiler outputs
102 #NO_APP at the beginning of its output. */
103 /* Also note that comments like this one will always work. */
104 const char line_comment_chars[] = "#";
106 /* We needed an unused char for line separation to work around the
107 lack of macros, using sed and such. */
108 const char line_separator_chars[] = "@";
110 /* Chars that can be used to separate mant from exp in floating point nums. */
111 const char EXP_CHARS[] = "eE";
113 /* Chars that mean this number is a floating point constant.
114 As in 0f12.456
115 or 0d1.2345e12. */
116 const char FLT_CHARS[] = "rRsSfFdDxXpP";
118 static void
119 insert_sreg (regname, regnum)
120 char *regname;
121 int regnum;
123 /* Must be large enough to hold the names of the special registers. */
124 char buf[80];
125 int i;
127 symbol_table_insert (symbol_new (regname, reg_section, (valueT) regnum,
128 &zero_address_frag));
129 for (i = 0; regname[i]; i++)
130 buf[i] = ISLOWER (regname[i]) ? TOUPPER (regname[i]) : regname[i];
131 buf[i] = '\0';
133 symbol_table_insert (symbol_new (buf, reg_section, (valueT) regnum,
134 &zero_address_frag));
137 /* Install symbol definitions for assorted special registers.
138 See MIPS Assembly Language Programmer's Guide page 1-4 */
140 static void
141 define_some_regs ()
143 #if 0
144 /* Hardware representation. */
145 insert_sreg ("r0", 0);
146 insert_sreg ("r1", 1);
147 insert_sreg ("r2", 2);
148 insert_sreg ("r3", 3);
149 insert_sreg ("r4", 4);
150 insert_sreg ("r5", 5);
151 insert_sreg ("r6", 6);
152 insert_sreg ("r7", 7);
153 insert_sreg ("r8", 8);
154 insert_sreg ("r9", 9);
155 insert_sreg ("r10", 10);
156 insert_sreg ("r11", 11);
157 insert_sreg ("r12", 12);
158 insert_sreg ("r13", 13);
159 insert_sreg ("r14", 14);
160 insert_sreg ("r15", 15);
161 insert_sreg ("r16", 16);
162 insert_sreg ("r17", 17);
163 insert_sreg ("r18", 18);
164 insert_sreg ("r19", 19);
165 insert_sreg ("r20", 20);
166 insert_sreg ("r21", 21);
167 insert_sreg ("r22", 22);
168 insert_sreg ("r23", 23);
169 insert_sreg ("r24", 24);
170 insert_sreg ("r25", 25);
171 insert_sreg ("r26", 26);
172 insert_sreg ("r27", 27);
173 insert_sreg ("r28", 28);
174 insert_sreg ("r29", 29);
175 insert_sreg ("r30", 30);
176 insert_sreg ("r31", 31);
177 #endif
178 /* Software representation. */
179 insert_sreg ("zero", 0);
180 insert_sreg ("at", 1);
181 insert_sreg ("v0", 2);
182 insert_sreg ("v1", 3);
183 insert_sreg ("a0", 4);
184 insert_sreg ("a1", 5);
185 insert_sreg ("a2", 6);
186 insert_sreg ("a3", 7);
187 insert_sreg ("t0", 8);
188 insert_sreg ("t1", 9);
189 insert_sreg ("t2", 10);
190 insert_sreg ("t3", 11);
191 insert_sreg ("t4", 12);
192 insert_sreg ("t5", 13);
193 insert_sreg ("t6", 14);
194 insert_sreg ("t7", 15);
195 insert_sreg ("s0", 16);
196 insert_sreg ("s1", 17);
197 insert_sreg ("s2", 18);
198 insert_sreg ("s3", 19);
199 insert_sreg ("s4", 20);
200 insert_sreg ("s5", 21);
201 insert_sreg ("s6", 22);
202 insert_sreg ("s7", 23);
203 insert_sreg ("t8", 24);
204 insert_sreg ("t9", 25);
205 insert_sreg ("k0", 26);
206 insert_sreg ("k1", 27);
207 insert_sreg ("gp", 28);
208 insert_sreg ("sp", 29);
209 insert_sreg ("fp", 30);
210 insert_sreg ("ra", 31);
211 /* Special registers. */
212 insert_sreg ("pc", 0);
213 insert_sreg ("npc", 1);
214 insert_sreg ("iad", 2);
217 /* Subroutine check the string to match an register, */
219 static int
220 match_sft_register (name)
221 char *name;
223 #define MAX_REG_NO 35
224 /* Currently we have 35 software registers defined -
225 we borrowed from MIPS. */
226 static char *soft_reg[] =
228 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
229 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9",
230 "s0", "s1", "s2", "s3", "s4", "s5", "s7", "k0", "k1",
231 "gp", "sp", "fp", "ra", "pc", "npc", "iad",
232 "EndofTab" /* End of the Table indicator */
234 char low_name[21], *ptr;
235 int idx;
237 for (ptr = name,idx = 0; *ptr != '\0'; ptr++)
238 low_name[idx++] = TOLOWER (*ptr);
240 low_name[idx] = '\0';
241 idx = 0;
243 while (idx < MAX_REG_NO && strcmp (soft_reg[idx], & low_name [0]))
244 idx += 1;
246 return idx < MAX_REG_NO;
249 /* Subroutine check the string to match an register. */
251 static int
252 is_ldst_registers (name)
253 char *name;
255 char *ptr = name;
257 /* The first character of the register name got to be either %, $, r of R. */
258 if ((ptr[0] == '%' || ptr[0] == '$' || ptr[0] == 'r' || ptr[0] == 'R')
259 && ISDIGIT ((unsigned char) ptr[1]))
260 return 1;
262 /* Now check the software register representation. */
263 return match_sft_register (ptr);
266 /* Subroutine of s_proc so targets can choose a different default prefix.
267 If DEFAULT_PREFIX is NULL, use the target's "leading char". */
269 static void
270 s_proc (end_p)
271 int end_p;
273 /* Record the current function so that we can issue an error message for
274 misplaced .func,.endfunc, and also so that .endfunc needs no
275 arguments. */
276 static char *current_name;
277 static char *current_label;
279 if (end_p)
281 if (current_name == NULL)
283 as_bad (_("missing .proc"));
284 ignore_rest_of_line ();
285 return;
288 current_name = current_label = NULL;
289 SKIP_WHITESPACE ();
290 while (!is_end_of_line[(unsigned char) *input_line_pointer])
291 input_line_pointer++;
293 else
295 char *name, *label;
296 char delim1, delim2;
298 if (current_name != NULL)
300 as_bad (_(".endfunc missing for previous .proc"));
301 ignore_rest_of_line ();
302 return;
305 name = input_line_pointer;
306 delim1 = get_symbol_end ();
307 name = xstrdup (name);
308 *input_line_pointer = delim1;
309 SKIP_WHITESPACE ();
311 if (*input_line_pointer != ',')
313 char leading_char = 0;
315 leading_char = bfd_get_symbol_leading_char (stdoutput);
316 /* Missing entry point, use function's name with the leading
317 char prepended. */
318 if (leading_char)
319 asprintf (&label, "%c%s", leading_char, name);
320 else
321 label = name;
323 else
325 ++input_line_pointer;
326 SKIP_WHITESPACE ();
327 label = input_line_pointer;
328 delim2 = get_symbol_end ();
329 label = xstrdup (label);
330 *input_line_pointer = delim2;
333 current_name = name;
334 current_label = label;
336 demand_empty_rest_of_line ();
339 /* This function is called once, at assembler startup time. It should
340 set up all the tables, etc., that the MD part of the assembler will
341 need. */
343 void
344 md_begin ()
346 const char *retval = NULL;
347 int lose = 0;
348 unsigned int i;
350 /* Create a new hash table. */
351 op_hash = hash_new ();
353 /* Hash up all the opcodes for fast use later. */
354 for (i = 0; i < num_dlx_opcodes; i++)
356 const char *name = machine_opcodes[i].name;
358 retval = hash_insert (op_hash, name, (PTR) &machine_opcodes[i]);
360 if (retval != NULL)
362 fprintf (stderr, "internal error: can't hash `%s': %s\n",
363 machine_opcodes[i].name, retval);
364 lose = 1;
368 if (lose)
369 as_fatal (_("Broken assembler. No assembly attempted."));
371 define_some_regs ();
374 /* Assemble a single instruction. Its label has already been handled
375 by the generic front end. We just parse opcode and operands, and
376 produce the bytes of data and relocation. */
378 void
379 md_assemble (str)
380 char *str;
382 char *toP;
383 fixS *fixP;
384 bit_fixS *bitP;
386 know (str);
387 machine_ip (str);
388 toP = frag_more (4);
389 /* Put out the opcode. */
390 md_number_to_chars (toP, the_insn.opcode, 4);
392 /* Put out the symbol-dependent stuff. */
393 if (the_insn.reloc != NO_RELOC)
395 fixP = fix_new_exp (frag_now,
396 (toP - frag_now->fr_literal + the_insn.reloc_offset),
397 the_insn.size, & the_insn.exp, the_insn.pcrel,
398 the_insn.reloc);
400 switch (fixP->fx_r_type)
402 case RELOC_DLX_REL26:
403 bitP = malloc (sizeof (bit_fixS));
404 bitP->fx_bit_size = 26;
405 bitP->fx_bit_offset = 25;
406 bitP->fx_bit_base = the_insn.opcode & 0xFC000000;
407 bitP->fx_bit_base_adj = 0;
408 bitP->fx_bit_max = 0;
409 bitP->fx_bit_min = 0;
410 bitP->fx_bit_add = 0x03FFFFFF;
411 fixP->fx_bit_fixP = bitP;
412 break;
413 case RELOC_DLX_REL16:
414 bitP = malloc (sizeof (bit_fixS));
415 bitP->fx_bit_size = 16;
416 bitP->fx_bit_offset = 15;
417 bitP->fx_bit_base = the_insn.opcode & 0xFFFF0000;
418 bitP->fx_bit_base_adj = 0;
419 bitP->fx_bit_max = 0;
420 bitP->fx_bit_min = 0;
421 bitP->fx_bit_add = 0x0000FFFF;
422 fixP->fx_bit_fixP = bitP;
423 break;
424 case RELOC_DLX_HI16:
425 bitP = malloc (sizeof (bit_fixS));
426 bitP->fx_bit_size = 16;
427 bitP->fx_bit_offset = 15;
428 bitP->fx_bit_base = the_insn.opcode & 0xFFFF0000;
429 bitP->fx_bit_base_adj = 0;
430 bitP->fx_bit_max = 0;
431 bitP->fx_bit_min = 0;
432 bitP->fx_bit_add = 0x0000FFFF;
433 fixP->fx_bit_fixP = bitP;
434 break;
435 default:
436 fixP->fx_bit_fixP = (bit_fixS *)NULL;
437 break;
442 static int
443 hilo_modifier_ok (s)
444 char *s;
446 char *ptr = s;
447 int idx, count = 1;
449 if (*ptr != '(')
450 return 1;
452 for (idx = 1; ptr[idx] != '\0' && ptr[idx] != '[' && idx < 73; idx += 1)
454 if (count == 0)
455 return count;
457 if (ptr[idx] == '(')
458 count += 1;
460 if (ptr[idx] == ')')
461 count -= 1;
464 return (count == 0) ? 1:0;
467 char *
468 parse_operand (s, operandp)
469 char *s;
470 expressionS *operandp;
472 char *save = input_line_pointer;
473 char *new;
475 the_insn.HI = the_insn.LO = 0;
477 /* Search for %hi and %lo, make a mark and skip it. */
478 if (strncmp (s, "%hi", 3) == 0)
480 s += 3;
481 the_insn.HI = 1;
483 else
485 if (strncmp (s, "%lo", 3) == 0)
487 s += 3;
488 the_insn.LO = 1;
490 else
491 the_insn.LO = 0;
494 if (the_insn.HI || the_insn.LO)
496 if (!hilo_modifier_ok (s))
497 as_bad (_("Expression Error for operand modifier %%hi/%%lo\n"));
500 /* Check for the % and $ register representation */
501 if ((s[0] == '%' || s[0] == '$' || s[0] == 'r' || s[0] == 'R')
502 && ISDIGIT ((unsigned char) s[1]))
504 /* We have a numeric register expression. No biggy. */
505 s += 1;
506 input_line_pointer = s;
507 (void) expression (operandp);
508 if (operandp->X_op != O_constant
509 || operandp->X_add_number > 31)
510 as_bad (_("Invalid expression after %%%%\n"));
511 operandp->X_op = O_register;
513 else
515 /* Normal operand parsing. */
516 input_line_pointer = s;
517 (void) expression (operandp);
520 new = input_line_pointer;
521 input_line_pointer = save;
522 return new;
525 /* This function will check the opcode and return 1 if the opcode is one
526 of the load/store instruction, and it will fix the operand string to
527 the standard form so we can use the standard parse_operand routine. */
529 #define READ_OP 0x100
530 #define WRITE_OP 0x200
531 static char iBuf[81];
533 static char *
534 dlx_parse_loadop (str)
535 char * str;
537 char *ptr = str;
538 int idx = 0;
540 /* The last pair of ()/[] is the register, all other are the
541 reloc displacement, and if there is a register then it ought
542 to have a pair of ()/[]
543 This is not necessarily true, what if the load instruction come
544 without the register and with %hi/%lo modifier? */
545 for (idx = 0; idx < 72 && ptr[idx] != '\0'; idx++)
548 if (idx == 72)
550 badoperand_load:
551 as_bad (_("Bad operand for a load instruction: <%s>"), str);
552 return NULL;
554 else
556 int i, pb = 0;
557 int m2 = 0;
558 char rs1[7], rd[7], endm, match = '0';
559 char imm[72];
561 idx -= 1;
562 switch (str[idx])
564 case ')':
565 match = '(';
566 endm = ')';
567 break;
568 case ']':
569 match = '[';
570 endm = ']';
571 break;
572 default:
573 /* No register indicated, fill in zero. */
574 rs1[0] = 'r';
575 rs1[1] = '0';
576 rs1[2] = '\0';
577 match = 0;
578 endm = 0;
579 m2 = 1;
582 if (!m2)
584 /* Searching for (/[ which will match the ]/). */
585 for (pb = idx - 1; str[pb] != match; pb -= 1)
586 /* Match can only be either '[' or '(', if it is
587 '(' then this can be a normal expression, we'll treat
588 it as an operand. */
589 if (str[pb] == endm || pb < (idx - 5))
590 goto load_no_rs1;
591 pb += 1;
593 for (i = 0; (pb + i) < idx; i++)
594 rs1[i] = str[pb+i];
596 rs1[i] = '\0';
598 if (is_ldst_registers (& rs1[0]))
599 /* Point to the last character of the imm. */
600 pb -= 1;
601 else
603 load_no_rs1:
604 if (match == '[')
605 goto badoperand_load;
606 /* No register indicated, fill in zero and restore the imm. */
607 rs1[0] = 'r';
608 rs1[1] = '0';
609 rs1[2] = '\0';
610 m2 = 1;
614 /* Duplicate the first register. */
615 for (i = 0; i < 7 && str[i] != ','; i++)
616 rd[i] = ptr[i];
618 if (str[i] != ',')
619 goto badoperand_load;
620 else
621 rd[i] = '\0';
623 /* Copy the immd. */
624 if (m2)
625 /* Put the '\0' back in. */
626 pb = idx + 1;
628 for (i++, m2 = 0; i < pb; m2++,i++)
629 imm[m2] = ptr[i];
631 imm[m2] = '\0';
633 /* Assemble the instruction to gas internal format. */
634 for (i = 0; rd[i] != '\0'; i++)
635 iBuf[i] = rd[i];
637 iBuf[i++] = ',';
639 for (pb = 0 ; rs1[pb] != '\0'; i++, pb++)
640 iBuf[i] = rs1[pb];
642 iBuf[i++] = ',';
644 for (pb = 0; imm[pb] != '\0'; i++, pb++)
645 iBuf[i] = imm[pb];
647 iBuf[i] = '\0';
648 return iBuf;
652 static char *
653 dlx_parse_storeop (str)
654 char * str;
656 char *ptr = str;
657 int idx = 0;
659 /* Search for the ','. */
660 for (idx = 0; idx < 72 && ptr[idx] != ','; idx++)
663 if (idx == 72)
665 badoperand_store:
666 as_bad (_("Bad operand for a store instruction: <%s>"), str);
667 return NULL;
669 else
671 /* idx now points to the ','. */
672 int i, pb = 0;
673 int comma = idx;
674 int m2 = 0;
675 char rs1[7], rd[7], endm, match = '0';
676 char imm[72];
678 /* Now parse the '(' and ')', and make idx point to ')'. */
679 idx -= 1;
680 switch (str[idx])
682 case ')':
683 match = '(';
684 endm = ')';
685 break;
686 case ']':
687 match = '[';
688 endm = ']';
689 break;
690 default:
691 /* No register indicated, fill in zero. */
692 rs1[0] = 'r';
693 rs1[1] = '0';
694 rs1[2] = '\0';
695 match = 0;
696 endm = 0;
697 m2 = 1;
700 if (!m2)
702 /* Searching for (/[ which will match the ]/). */
703 for (pb = idx - 1; str[pb] != match; pb -= 1)
704 if (pb < (idx - 5) || str[pb] == endm)
705 goto store_no_rs1;
706 pb += 1;
708 for (i = 0; (pb + i) < idx; i++)
709 rs1[i] = str[pb + i];
711 rs1[i] = '\0';
713 if (is_ldst_registers (& rs1[0]))
714 /* Point to the last character of the imm. */
715 pb -= 1;
716 else
718 store_no_rs1:
719 if (match == '[')
720 goto badoperand_store;
722 /* No register indicated, fill in zero and restore the imm. */
723 rs1[0] = 'r';
724 rs1[1] = '0';
725 rs1[2] = '\0';
726 pb = comma;
729 else
730 /* No register was specified. */
731 pb = comma;
733 /* Duplicate the first register. */
734 for (i = comma + 1; (str[i] == ' ' || str[i] == '\t'); i++)
737 for (m2 = 0; (m2 < 7 && str[i] != '\0'); i++, m2++)
739 if (str[i] != ' ' && str[i] != '\t')
740 rd[m2] = str[i];
741 else
742 goto badoperand_store;
745 if (str[i] != '\0')
746 goto badoperand_store;
747 else
748 rd[m2] = '\0';
750 /* Copy the immd. */
751 for (i = 0; i < pb; i++)
752 imm[i] = ptr[i];
754 imm[i] = '\0';
756 /* Assemble the instruction to gas internal format. */
757 for (i = 0; rd[i] != '\0'; i++)
758 iBuf[i] = rd[i];
759 iBuf[i++] = ',';
760 for (pb = 0 ; rs1[pb] != '\0'; i++, pb++)
761 iBuf[i] = rs1[pb];
762 iBuf[i++] = ',';
763 for (pb = 0; imm[pb] != '\0'; i++, pb++)
764 iBuf[i] = imm[pb];
765 iBuf[i] = '\0';
766 return iBuf;
770 static char *
771 fix_ld_st_operand (opcode, str)
772 unsigned long opcode;
773 char* str;
775 /* Check the opcode. */
776 switch ((int) opcode)
778 case LBOP:
779 case LBUOP:
780 case LSBUOP:
781 case LHOP:
782 case LHUOP:
783 case LSHUOP:
784 case LWOP:
785 case LSWOP:
786 return dlx_parse_loadop (str);
787 case SBOP:
788 case SHOP:
789 case SWOP:
790 return dlx_parse_storeop (str);
791 default:
792 return str;
796 /* Instruction parsing. Takes a string containing the opcode.
797 Operands are at input_line_pointer. Output is in the_insn.
798 Warnings or errors are generated. */
800 static void
801 machine_ip (str)
802 char *str;
804 char *s;
805 const char *args;
806 struct machine_opcode *insn;
807 char *argsStart;
808 unsigned long opcode;
809 expressionS the_operand;
810 expressionS *operand = &the_operand;
811 unsigned int reg, reg_shift = 0;
813 /* Fixup the opcode string to all lower cases, and also
814 allow numerical digits. */
815 s = str;
817 if (ISALPHA (*s))
818 for (; ISALNUM (*s); ++s)
819 if (ISUPPER (*s))
820 *s = TOLOWER (*s);
822 switch (*s)
824 case '\0':
825 break;
827 /* FIXME-SOMEDAY more whitespace. */
828 case ' ':
829 *s++ = '\0';
830 break;
832 default:
833 as_bad (_("Unknown opcode: `%s'"), str);
834 return;
837 /* Hash the opcode, insn will have the string from opcode table.
838 also initialized the_insn struct. */
839 if ((insn = (struct machine_opcode *) hash_find (op_hash, str)) == NULL)
841 /* Handle the ret and return macro here. */
842 if ((strcmp (str, "ret") == 0) || (strcmp (str, "return") == 0))
844 memset (&the_insn, '\0', sizeof (the_insn));
845 the_insn.reloc = NO_RELOC;
846 the_insn.pcrel = 0;
847 the_insn.opcode =
848 (unsigned long)(JROP | 0x03e00000); /* 0x03e00000 = r31 << 21 */
850 else
851 as_bad (_("Unknown opcode `%s'."), str);
853 return;
856 argsStart = s;
857 opcode = insn->opcode;
858 memset (&the_insn, '\0', sizeof (the_insn));
859 the_insn.reloc = NO_RELOC;
860 the_insn.pcrel = 0;
862 /* Set the sip reloc HI16 flag. */
863 if (!set_dlx_skip_hi16_flag (1))
864 as_bad (_("Can not set dlx_skip_hi16_flag"));
866 /* Fix the operand string if it is one of load store instructions. */
867 s = fix_ld_st_operand (opcode, s);
869 /* Build the opcode, checking as we go to make sure that the
870 operands match.
871 If an operand matches, we modify the_insn or opcode appropriately,
872 and do a "continue". If an operand fails to match, we "break". */
873 if (insn->args[0] != '\0' && insn->args[0] != 'N')
875 /* Prime the pump. */
876 if (*s == '\0')
878 as_bad (_("Missing arguments for opcode <%s>."), str);
879 return;
881 else
882 s = parse_operand (s, operand);
884 else if (insn->args[0] == 'N')
886 /* Clean up the insn and done! */
887 the_insn.opcode = opcode;
888 return;
891 /* Parse through the args (this is from opcode table), *s point to
892 the current character of the instruction stream. */
893 for (args = insn->args;; ++args)
895 switch (*args)
897 /* End of Line. */
898 case '\0':
899 /* End of args. */
900 if (*s == '\0')
902 /* We are truly done. */
903 the_insn.opcode = opcode;
904 /* Clean up the HI and LO mark. */
905 the_insn.HI = 0;
906 the_insn.LO = 0;
907 return;
910 the_insn.HI = 0;
911 the_insn.LO = 0;
912 as_bad (_("Too many operands: %s"), s);
913 break;
915 /* ',' Args separator */
916 case ',':
917 /* Must match a comma. */
918 if (*s++ == ',')
920 /* Parse next operand. */
921 s = parse_operand (s, operand);
922 continue;
924 break;
926 /* It can be a 'a' register or 'i' operand. */
927 case 'P':
928 /* Macro move operand/reg. */
929 if (operand->X_op == O_register)
931 /* Its a register. */
932 reg_shift = 21;
933 goto general_reg;
936 /* The immediate 16 bits literal, bit 0-15. */
937 case 'i':
938 /* offset, unsigned. */
939 case 'I':
940 /* offset, signed. */
941 if (operand->X_op == O_constant)
943 if (the_insn.HI)
944 operand->X_add_number >>= 16;
946 opcode |= operand->X_add_number & 0xFFFF;
948 if (the_insn.HI && the_insn.LO)
949 as_bad (_("Both the_insn.HI and the_insn.LO are set : %s"), s);
950 else
952 the_insn.HI = 0;
953 the_insn.LO = 0;
955 continue;
958 the_insn.reloc = (the_insn.HI) ? RELOC_DLX_HI16 : RELOC_DLX_16;
959 the_insn.reloc_offset = 2;
960 the_insn.size = 2;
961 the_insn.pcrel = 0;
962 the_insn.exp = * operand;
963 the_insn.HI = 0;
964 the_insn.LO = 0;
965 continue;
967 case 'd':
968 /* offset, signed. */
969 if (operand->X_op == O_constant)
971 opcode |= operand->X_add_number & 0xFFFF;
972 continue;
974 the_insn.reloc = RELOC_DLX_REL16;
975 the_insn.reloc_offset = 0; /* BIG-ENDIAN Byte 3 of insn. */
976 the_insn.size = 4;
977 the_insn.pcrel = 1;
978 the_insn.exp = *operand;
979 continue;
981 /* The immediate 26 bits literal, bit 0-25. */
982 case 'D':
983 /* offset, signed. */
984 if (operand->X_op == O_constant)
986 opcode |= operand->X_add_number & 0x3FFFFFF;
987 continue;
989 the_insn.reloc = RELOC_DLX_REL26;
990 the_insn.reloc_offset = 0; /* BIG-ENDIAN Byte 3 of insn. */
991 the_insn.size = 4;
992 the_insn.pcrel = 1;
993 the_insn.exp = *operand;
994 continue;
996 /* Type 'a' Register. */
997 case 'a':
998 /* A general register at bits 21-25, rs1. */
999 know (operand->X_op != O_register);
1000 reg_shift = 21;
1001 goto general_reg;
1003 /* Type 'b' Register. */
1004 case 'b':
1005 /* A general register at bits 16-20, rs2/rd. */
1006 know (operand->X_op != O_register);
1007 reg_shift = 16;
1008 goto general_reg;
1010 /* Type 'c' Register. */
1011 case 'c':
1012 /* A general register at bits 11-15, rd. */
1013 know (operand->X_op != O_register);
1014 reg_shift = 11;
1016 general_reg:
1017 know (operand->X_add_symbol == 0);
1018 know (operand->X_op_symbol == 0);
1019 reg = operand->X_add_number;
1020 if (reg & 0xffffffe0)
1021 as_fatal (_("failed regnum sanity check."));
1022 else
1023 /* Got the register, now figure out where it goes in the opcode. */
1024 opcode |= reg << reg_shift;
1026 switch (*args)
1028 case 'a':
1029 case 'b':
1030 case 'c':
1031 case 'P':
1032 continue;
1034 as_fatal (_("failed general register sanity check."));
1035 break;
1037 default:
1038 BAD_CASE (*args);
1041 /* Types or values of args don't match. */
1042 as_bad ("Invalid operands");
1043 return;
1047 /* This is identical to the md_atof in m68k.c. I think this is right,
1048 but I'm not sure.
1050 Turn a string in input_line_pointer into a floating point constant
1051 of type TYPE, and store the appropriate bytes in *LITP. The number
1052 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1053 returned, or NULL on OK. */
1054 /* Dlx will not use it anyway, so I just leave it here for now. */
1056 /* Equal to MAX_PRECISION in atof-ieee.c. */
1057 #define MAX_LITTLENUMS 6
1059 char *
1060 md_atof (type, litP, sizeP)
1061 char type;
1062 char *litP;
1063 int *sizeP;
1065 int prec;
1066 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1067 LITTLENUM_TYPE *wordP;
1068 char *t;
1070 switch (type)
1072 case 'f':
1073 case 'F':
1074 case 's':
1075 case 'S':
1076 prec = 2;
1077 break;
1079 case 'd':
1080 case 'D':
1081 case 'r':
1082 case 'R':
1083 prec = 4;
1084 break;
1086 case 'x':
1087 case 'X':
1088 prec = 6;
1089 break;
1091 case 'p':
1092 case 'P':
1093 prec = 6;
1094 break;
1096 default:
1097 *sizeP = 0;
1098 return "Bad call to MD_ATOF()";
1101 t = atof_ieee (input_line_pointer, type, words);
1102 if (t)
1103 input_line_pointer = t;
1105 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1107 for (wordP = words; prec--;)
1109 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
1110 litP += sizeof (LITTLENUM_TYPE);
1113 return 0;
1116 /* Write out big-endian. */
1117 void
1118 md_number_to_chars (buf, val, n)
1119 char *buf;
1120 valueT val;
1121 int n;
1123 number_to_chars_bigendian (buf, val, n);
1126 /* md_chars_to_number: convert from target byte order to host byte order. */
1129 md_chars_to_number (val, n)
1130 unsigned char *val; /* Value in target byte order. */
1131 int n; /* Number of bytes in the input. */
1133 int retval;
1135 for (retval = 0; n--;)
1137 retval <<= 8;
1138 retval |= val[n];
1141 return retval;
1144 bfd_boolean
1145 md_dlx_fix_adjustable (fixP)
1146 fixS *fixP;
1148 /* We need the symbol name for the VTABLE entries. */
1149 return (fixP->fx_r_type != BFD_RELOC_VTABLE_INHERIT
1150 && fixP->fx_r_type != BFD_RELOC_VTABLE_ENTRY);
1153 void
1154 md_apply_fix3 (fixP, valP, seg)
1155 fixS *fixP;
1156 valueT *valP;
1157 segT seg ATTRIBUTE_UNUSED;
1159 long val = *valP;
1160 char *place = fixP->fx_where + fixP->fx_frag->fr_literal;
1162 know (fixP->fx_size == 4);
1163 know (fixP->fx_r_type < NO_RELOC);
1165 switch (fixP->fx_r_type)
1167 case RELOC_DLX_REL16:
1168 if (fixP->fx_bit_fixP != (bit_fixS *) NULL)
1170 val = (val & 0x0000FFFF) | fixP->fx_bit_fixP->fx_bit_base;
1171 free (fixP->fx_bit_fixP);
1172 fixP->fx_bit_fixP = (bit_fixS *) NULL;
1174 #ifdef DEBUG
1175 else
1176 know ((fixP->fx_bit_fixP != (bit_fixS *) NULL));
1177 #endif
1178 break;
1180 case RELOC_DLX_HI16:
1181 if (fixP->fx_bit_fixP != (bit_fixS *) NULL)
1183 val = (val >> 16) | fixP->fx_bit_fixP->fx_bit_base;
1184 free (fixP->fx_bit_fixP);
1185 fixP->fx_bit_fixP = (bit_fixS *)NULL;
1187 #ifdef DEBUG
1188 else
1189 know ((fixP->fx_bit_fixP != (bit_fixS *) NULL));
1190 #endif
1191 break;
1193 case RELOC_DLX_REL26:
1194 if (fixP->fx_bit_fixP != (bit_fixS *) NULL)
1196 val = (val & 0x03FFFFFF) | fixP->fx_bit_fixP->fx_bit_base;
1197 free (fixP->fx_bit_fixP);
1198 fixP->fx_bit_fixP = (bit_fixS *) NULL;
1200 #ifdef DEBUG
1201 else
1202 know ((fixP->fx_bit_fixP != (bit_fixS *) NULL));
1203 #endif
1204 break;
1206 case BFD_RELOC_VTABLE_INHERIT:
1207 /* This borrowed from tc-ppc.c on a whim. */
1208 fixP->fx_done = 0;
1209 if (fixP->fx_addsy
1210 && !S_IS_DEFINED (fixP->fx_addsy)
1211 && !S_IS_WEAK (fixP->fx_addsy))
1212 S_SET_WEAK (fixP->fx_addsy);
1213 return;
1215 case BFD_RELOC_VTABLE_ENTRY:
1216 fixP->fx_done = 0;
1217 return;
1219 default:
1220 break;
1223 number_to_chars_bigendian (place, val, fixP->fx_size);
1224 if (fixP->fx_addsy == NULL)
1225 fixP->fx_done = 1;
1228 const char *md_shortopts = "";
1230 struct option md_longopts[] =
1232 {NULL, no_argument, NULL, 0}
1235 size_t md_longopts_size = sizeof (md_longopts);
1238 md_parse_option (c, arg)
1239 int c ATTRIBUTE_UNUSED;
1240 char *arg ATTRIBUTE_UNUSED;
1242 return 0;
1245 void
1246 md_show_usage (stream)
1247 FILE *stream ATTRIBUTE_UNUSED;
1251 /* This is called when a line is unrecognized. */
1254 dlx_unrecognized_line (c)
1255 int c;
1257 int lab;
1258 char *s;
1260 if (c != '$' || ! ISDIGIT ((unsigned char) input_line_pointer[0]))
1261 return 0;
1263 s = input_line_pointer;
1265 lab = 0;
1266 while (ISDIGIT ((unsigned char) *s))
1268 lab = lab * 10 + *s - '0';
1269 ++s;
1272 if (*s != ':')
1274 /* Not a label definition. */
1275 return 0;
1278 if (dollar_label_defined (lab))
1280 as_bad (_("label \"$%d\" redefined"), lab);
1281 return 0;
1284 define_dollar_label (lab);
1285 colon (dollar_label_name (lab, 0));
1286 input_line_pointer = s + 1;
1288 return 1;
1291 /* Default the values of symbols known that should be "predefined". We
1292 don't bother to predefine them unless you actually use one, since there
1293 are a lot of them. */
1295 symbolS *
1296 md_undefined_symbol (name)
1297 char *name ATTRIBUTE_UNUSED;
1299 return NULL;
1303 /* Parse an operand that is machine-specific, the function was called
1304 in expr.c by operand() function, when everything failed before it
1305 call a quit. */
1307 void
1308 md_operand (expressionP)
1309 expressionS* expressionP;
1311 /* Check for the #number representation */
1312 if (input_line_pointer[0] == '#' &&
1313 ISDIGIT ((unsigned char) input_line_pointer[1]))
1315 /* We have a numeric number expression. No biggy. */
1316 input_line_pointer += 1; /* Skip # */
1318 (void) expression (expressionP);
1320 if (expressionP->X_op != O_constant)
1321 as_bad (_("Invalid expression after # number\n"));
1324 return;
1325 #if 0
1326 else if (input_line_pointer[0] == '$'
1327 && ISDIGIT ((unsigned char) input_line_pointer[1]))
1329 long lab;
1330 char *name;
1331 symbolS *sym;
1333 /* This is a local label. */
1334 ++input_line_pointer;
1335 lab = (long) get_absolute_expression ();
1336 if (dollar_label_defined (lab))
1338 name = dollar_label_name (lab, 0);
1339 sym = symbol_find (name);
1341 else
1343 name = dollar_label_name (lab, 1);
1344 sym = symbol_find_or_make (name);
1347 expressionP->X_op = O_symbol;
1348 expressionP->X_add_symbol = sym;
1349 expressionP->X_add_number = 0;
1351 #endif
1354 /* Round up a section size to the appropriate boundary. */
1356 valueT
1357 md_section_align (segment, size)
1358 segT segment ATTRIBUTE_UNUSED;
1359 valueT size;
1361 /* Byte alignment is fine. */
1362 return size;
1365 /* Exactly what point is a PC-relative offset relative TO?
1366 On the 29000, they're relative to the address of the instruction,
1367 which we have set up as the address of the fixup too. */
1369 long
1370 md_pcrel_from (fixP)
1371 fixS* fixP;
1373 return 4 + fixP->fx_where + fixP->fx_frag->fr_address;
1376 /* From cgen.c: */
1378 #if 0
1379 static short
1380 tc_bfd_fix2rtype (fixP)
1381 fixS* fixP;
1383 #if 0
1384 if (fixP->fx_bsr)
1385 abort ();
1386 #endif
1388 if (fixP->fx_pcrel == 0 && fixP->fx_size == 4)
1389 return BFD_RELOC_32;
1391 if (fixP->fx_pcrel != 0 && fixP->fx_size == 4)
1392 return BFD_RELOC_26_PCREL;
1394 abort ();
1396 return 0;
1398 #endif
1400 /* Translate internal representation of relocation info to BFD target
1401 format.
1402 FIXME: To what extent can we get all relevant targets to use this?
1403 The above FIXME is from a29k, but I think it is also needed here. */
1405 arelent *
1406 tc_gen_reloc (section, fixP)
1407 asection *section ATTRIBUTE_UNUSED;
1408 fixS *fixP;
1410 arelent * reloc;
1412 reloc = (arelent *) xmalloc (sizeof (arelent));
1413 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1415 if (reloc->howto == (reloc_howto_type *) NULL)
1417 as_bad_where (fixP->fx_file, fixP->fx_line,
1418 "internal error: can't export reloc type %d (`%s')",
1419 fixP->fx_r_type,
1420 bfd_get_reloc_code_name (fixP->fx_r_type));
1421 return NULL;
1424 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
1426 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1427 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
1428 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
1430 if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1431 reloc->address = fixP->fx_offset;
1432 reloc->addend = 0;
1434 return reloc;
1437 extern void pop_insert PARAMS ((const pseudo_typeS *));
1439 void
1440 dlx_pop_insert ()
1442 pop_insert (dlx_pseudo_table);
1443 return ;