* ecoff.c: Convert to ISO-C.
[deliverable/binutils-gdb.git] / gas / config / tc-z8k.c
1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003
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 2, 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
23
24 #define DEFINE_TABLE
25 #include <stdio.h>
26
27 #include "as.h"
28 #include "bfd.h"
29 #include "safe-ctype.h"
30 #include "opcodes/z8k-opc.h"
31
32 const char comment_chars[] = "!";
33 const char line_comment_chars[] = "#";
34 const char line_separator_chars[] = ";";
35
36 extern int machine;
37 extern int coff_flags;
38 int segmented_mode;
39 const int md_reloc_size;
40
41 /* This is non-zero if target was set from the command line. */
42 static int z8k_target_from_cmdline;
43
44 static void s_segm PARAMS ((int));
45 static void even PARAMS ((int));
46 static int tohex PARAMS ((int));
47 static void sval PARAMS ((int));
48
49 static void
50 s_segm (segm)
51 int segm;
52 {
53 if (segm)
54 {
55 segmented_mode = 1;
56 machine = bfd_mach_z8001;
57 coff_flags = F_Z8001;
58 }
59 else
60 {
61 segmented_mode = 0;
62 machine = bfd_mach_z8002;
63 coff_flags = F_Z8002;
64 }
65 }
66
67 static void
68 even (ignore)
69 int ignore ATTRIBUTE_UNUSED;
70 {
71 frag_align (1, 0, 0);
72 record_alignment (now_seg, 1);
73 }
74
75 static int
76 tohex (c)
77 int c;
78 {
79 if (ISDIGIT (c))
80 return c - '0';
81 if (ISLOWER (c))
82 return c - 'a' + 10;
83 return c - 'A' + 10;
84 }
85
86 static void
87 sval (ignore)
88 int ignore ATTRIBUTE_UNUSED;
89 {
90 SKIP_WHITESPACE ();
91 if (*input_line_pointer == '\'')
92 {
93 int c;
94 input_line_pointer++;
95 c = *input_line_pointer++;
96 while (c != '\'')
97 {
98 if (c == '%')
99 {
100 c = (tohex (input_line_pointer[0]) << 4)
101 | tohex (input_line_pointer[1]);
102 input_line_pointer += 2;
103 }
104 FRAG_APPEND_1_CHAR (c);
105 c = *input_line_pointer++;
106 }
107 demand_empty_rest_of_line ();
108 }
109 }
110
111 /* This table describes all the machine specific pseudo-ops the assembler
112 has to support. The fields are:
113 pseudo-op name without dot
114 function to call to execute this pseudo-op
115 Integer arg to pass to the function
116 */
117
118 const pseudo_typeS md_pseudo_table[] = {
119 {"int" , cons , 2},
120 {"data.b" , cons , 1},
121 {"data.w" , cons , 2},
122 {"data.l" , cons , 4},
123 {"form" , listing_psize , 0},
124 {"heading", listing_title , 0},
125 {"import" , s_ignore , 0},
126 {"page" , listing_eject , 0},
127 {"program", s_ignore , 0},
128 {"z8001" , s_segm , 1},
129 {"z8002" , s_segm , 0},
130
131 {"segm" , s_segm , 1},
132 {"unsegm" , s_segm , 0},
133 {"unseg" , s_segm , 0},
134 {"name" , s_app_file , 0},
135 {"global" , s_globl , 0},
136 {"wval" , cons , 2},
137 {"lval" , cons , 4},
138 {"bval" , cons , 1},
139 {"sval" , sval , 0},
140 {"rsect" , obj_coff_section, 0},
141 {"sect" , obj_coff_section, 0},
142 {"block" , s_space , 0},
143 {"even" , even , 0},
144 {0 , 0 , 0}
145 };
146
147 const char EXP_CHARS[] = "eE";
148
149 /* Chars that mean this number is a floating point constant.
150 As in 0f12.456
151 or 0d1.2345e12 */
152 const char FLT_CHARS[] = "rRsSfFdDxXpP";
153
154 /* Opcode mnemonics. */
155 static struct hash_control *opcode_hash_control;
156
157 void
158 md_begin ()
159 {
160 const opcode_entry_type *opcode;
161 int idx = -1;
162
163 opcode_hash_control = hash_new ();
164
165 for (opcode = z8k_table; opcode->name; opcode++)
166 {
167 /* Only enter unique codes into the table. */
168 if (idx != opcode->idx)
169 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
170 idx = opcode->idx;
171 }
172
173 /* Default to z8002. */
174 if (! z8k_target_from_cmdline)
175 s_segm (0);
176
177 /* Insert the pseudo ops, too. */
178 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
179 {
180 opcode_entry_type *fake_opcode;
181 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
182 fake_opcode->name = md_pseudo_table[idx].poc_name;
183 fake_opcode->func = (void *) (md_pseudo_table + idx);
184 fake_opcode->opcode = 250;
185 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
186 }
187 }
188
189 struct z8k_exp {
190 char *e_beg;
191 char *e_end;
192 expressionS e_exp;
193 };
194
195 typedef struct z8k_op {
196 /* CLASS_REG_xxx. */
197 int regsize;
198
199 /* 0 .. 15. */
200 unsigned int reg;
201
202 int mode;
203
204 /* Any other register associated with the mode. */
205 unsigned int x_reg;
206
207 /* Any expression. */
208 expressionS exp;
209 } op_type;
210
211 static expressionS *da_operand;
212 static expressionS *imm_operand;
213
214 int reg[16];
215 int the_cc;
216 int the_ctrl;
217 int the_flags;
218 int the_interrupt;
219
220 static char *whatreg PARAMS ((int *, char *));
221 static char *parse_reg PARAMS ((char *, int *, unsigned int *));
222 static char *parse_exp PARAMS ((char *, expressionS *));
223 static char *checkfor PARAMS ((char *, char));
224 static void regword PARAMS ((int, char *));
225 static void regaddr PARAMS ((int, char *));
226 static void get_ctrl_operand
227 PARAMS ((char **, struct z8k_op *, unsigned int));
228 static void get_flags_operand
229 PARAMS ((char **, struct z8k_op *, unsigned int));
230 static void get_interrupt_operand
231 PARAMS ((char **, struct z8k_op *, unsigned int));
232 static void get_cc_operand
233 PARAMS ((char **, struct z8k_op *, unsigned int));
234 static void get_operand
235 PARAMS ((char **, struct z8k_op *, unsigned int));
236 static char *get_operands
237 PARAMS ((const opcode_entry_type *, char *, op_type *));
238 static opcode_entry_type *get_specific
239 PARAMS ((opcode_entry_type *, op_type *));
240 static void newfix
241 PARAMS ((int, int, int, expressionS *));
242 static char *apply_fix
243 PARAMS ((char *, int, expressionS *, int));
244 static void build_bytes
245 PARAMS ((opcode_entry_type *, struct z8k_op *));
246
247 static char *
248 whatreg (reg, src)
249 int *reg;
250 char *src;
251 {
252 if (ISDIGIT (src[1]))
253 {
254 *reg = (src[0] - '0') * 10 + src[1] - '0';
255 return src + 2;
256 }
257 else
258 {
259 *reg = (src[0] - '0');
260 return src + 1;
261 }
262 }
263
264 /* Parse operands
265
266 rh0-rh7, rl0-rl7
267 r0-r15
268 rr0-rr14
269 rq0--rq12
270 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
271 r0l,r0h,..r7l,r7h
272 @WREG
273 @WREG+
274 @-WREG
275 #const
276 */
277
278 /* Try to parse a reg name. Return a pointer to the first character
279 in SRC after the reg name. */
280
281 static char *
282 parse_reg (src, mode, reg)
283 char *src;
284 int *mode;
285 unsigned int *reg;
286 {
287 char *res = 0;
288 char regno;
289
290 if (src[0] == 's' && src[1] == 'p' && (src[2] == 0 || src[2] == ','))
291 {
292 if (segmented_mode)
293 {
294 *mode = CLASS_REG_LONG;
295 *reg = 14;
296 }
297 else
298 {
299 *mode = CLASS_REG_WORD;
300 *reg = 15;
301 }
302 return src + 2;
303 }
304 if (src[0] == 'r')
305 {
306 if (src[1] == 'r')
307 {
308 if (src[2] < '0' || src[2] > '9')
309 return res; /* Assume no register name but a label starting with 'rr'. */
310 *mode = CLASS_REG_LONG;
311 res = whatreg (reg, src + 2);
312 regno = *reg;
313 if (regno > 14)
314 as_bad (_("register rr%d out of range"), regno);
315 if (regno & 1)
316 as_bad (_("register rr%d does not exist"), regno);
317 }
318 else if (src[1] == 'h')
319 {
320 if (src[2] < '0' || src[2] > '9')
321 return res; /* Assume no register name but a label starting with 'rh'. */
322 *mode = CLASS_REG_BYTE;
323 res = whatreg (reg, src + 2);
324 regno = *reg;
325 if (regno > 7)
326 as_bad (_("register rh%d out of range"), regno);
327 }
328 else if (src[1] == 'l')
329 {
330 if (src[2] < '0' || src[2] > '9')
331 return res; /* Assume no register name but a label starting with 'rl'. */
332 *mode = CLASS_REG_BYTE;
333 res = whatreg (reg, src + 2);
334 regno = *reg;
335 if (regno > 7)
336 as_bad (_("register rl%d out of range"), regno);
337 *reg += 8;
338 }
339 else if (src[1] == 'q')
340 {
341 if (src[2] < '0' || src[2] > '9')
342 return res; /* Assume no register name but a label starting with 'rq'. */
343 *mode = CLASS_REG_QUAD;
344 res = whatreg (reg, src + 2);
345 regno = *reg;
346 if (regno > 12)
347 as_bad (_("register rq%d out of range"), regno);
348 if (regno & 3)
349 as_bad (_("register rq%d does not exist"), regno);
350 }
351 else
352 {
353 if (src[1] < '0' || src[1] > '9')
354 return res; /* Assume no register name but a label starting with 'r'. */
355 *mode = CLASS_REG_WORD;
356 res = whatreg (reg, src + 1);
357 regno = *reg;
358 if (regno > 15)
359 as_bad (_("register r%d out of range"), regno);
360 }
361 }
362 return res;
363 }
364
365 static char *
366 parse_exp (s, op)
367 char *s;
368 expressionS *op;
369 {
370 char *save = input_line_pointer;
371 char *new;
372
373 input_line_pointer = s;
374 expression (op);
375 if (op->X_op == O_absent)
376 as_bad (_("missing operand"));
377 new = input_line_pointer;
378 input_line_pointer = save;
379 return new;
380 }
381
382 /* The many forms of operand:
383
384 <rb>
385 <r>
386 <rr>
387 <rq>
388 @r
389 #exp
390 exp
391 exp(r)
392 r(#exp)
393 r(r)
394 */
395
396 static char *
397 checkfor (ptr, what)
398 char *ptr;
399 char what;
400 {
401 if (*ptr == what)
402 ptr++;
403 else
404 as_bad (_("expected %c"), what);
405
406 return ptr;
407 }
408
409 /* Make sure the mode supplied is the size of a word. */
410
411 static void
412 regword (mode, string)
413 int mode;
414 char *string;
415 {
416 int ok;
417
418 ok = CLASS_REG_WORD;
419 if (ok != mode)
420 {
421 as_bad (_("register is wrong size for a word %s"), string);
422 }
423 }
424
425 /* Make sure the mode supplied is the size of an address. */
426
427 static void
428 regaddr (mode, string)
429 int mode;
430 char *string;
431 {
432 int ok;
433
434 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
435 if (ok != mode)
436 {
437 as_bad (_("register is wrong size for address %s"), string);
438 }
439 }
440
441 struct ctrl_names {
442 int value;
443 char *name;
444 };
445
446 struct ctrl_names ctrl_table[] = {
447 { 0x2, "fcw" },
448 { 0x3, "refresh" },
449 { 0x4, "psapseg" },
450 { 0x5, "psapoff" },
451 { 0x5, "psap" },
452 { 0x6, "nspseg" },
453 { 0x7, "nspoff" },
454 { 0x7, "nsp" },
455 { 0 , 0 }
456 };
457
458 static void
459 get_ctrl_operand (ptr, mode, dst)
460 char **ptr;
461 struct z8k_op *mode;
462 unsigned int dst ATTRIBUTE_UNUSED;
463 {
464 char *src = *ptr;
465 int i;
466
467 while (*src == ' ')
468 src++;
469
470 mode->mode = CLASS_CTRL;
471 for (i = 0; ctrl_table[i].name; i++)
472 {
473 int j;
474
475 for (j = 0; ctrl_table[i].name[j]; j++)
476 {
477 if (ctrl_table[i].name[j] != src[j])
478 goto fail;
479 }
480 the_ctrl = ctrl_table[i].value;
481 *ptr = src + j;
482 return;
483 fail:
484 ;
485 }
486 the_ctrl = 0;
487 return;
488 }
489
490 struct flag_names {
491 int value;
492 char *name;
493
494 };
495
496 struct flag_names flag_table[] = {
497 { 0x1, "p" },
498 { 0x1, "v" },
499 { 0x2, "s" },
500 { 0x4, "z" },
501 { 0x8, "c" },
502 { 0x0, "+" },
503 { 0, 0 }
504 };
505
506 static void
507 get_flags_operand (ptr, mode, dst)
508 char **ptr;
509 struct z8k_op *mode;
510 unsigned int dst ATTRIBUTE_UNUSED;
511 {
512 char *src = *ptr;
513 int i;
514 int j;
515
516 while (*src == ' ')
517 src++;
518
519 mode->mode = CLASS_FLAGS;
520 the_flags = 0;
521 for (j = 0; j <= 9; j++)
522 {
523 if (!src[j])
524 goto done;
525 for (i = 0; flag_table[i].name; i++)
526 {
527 if (flag_table[i].name[0] == src[j])
528 {
529 the_flags = the_flags | flag_table[i].value;
530 goto match;
531 }
532 }
533 goto done;
534 match:
535 ;
536 }
537 done:
538 *ptr = src + j;
539 return;
540 }
541
542 struct interrupt_names {
543 int value;
544 char *name;
545
546 };
547
548 struct interrupt_names intr_table[] = {
549 { 0x1, "nvi" },
550 { 0x2, "vi" },
551 { 0x3, "both" },
552 { 0x3, "all" },
553 { 0, 0 }
554 };
555
556 static void
557 get_interrupt_operand (ptr, mode, dst)
558 char **ptr;
559 struct z8k_op *mode;
560 unsigned int dst ATTRIBUTE_UNUSED;
561 {
562 char *src = *ptr;
563 int i;
564
565 while (*src == ' ')
566 src++;
567
568 mode->mode = CLASS_IMM;
569 for (i = 0; intr_table[i].name; i++)
570 {
571 int j;
572
573 for (j = 0; intr_table[i].name[j]; j++)
574 {
575 if (intr_table[i].name[j] != src[j])
576 goto fail;
577 }
578 the_interrupt = intr_table[i].value;
579 *ptr = src + j;
580 return;
581 fail:
582 ;
583 }
584 /* No interrupt type specified, opcode won't do anything. */
585 as_warn (_("opcode has no effect"));
586 the_interrupt = 0x0;
587 return;
588 }
589
590 struct cc_names {
591 int value;
592 char *name;
593 };
594
595 struct cc_names table[] = {
596 { 0x0, "f" },
597 { 0x1, "lt" },
598 { 0x2, "le" },
599 { 0x3, "ule" },
600 { 0x4, "ov" },
601 { 0x4, "pe" },
602 { 0x5, "mi" },
603 { 0x6, "eq" },
604 { 0x6, "z" },
605 { 0x7, "c" },
606 { 0x7, "ult" },
607 { 0x8, "t" },
608 { 0x9, "ge" },
609 { 0xa, "gt" },
610 { 0xb, "ugt" },
611 { 0xc, "nov" },
612 { 0xc, "po" },
613 { 0xd, "pl" },
614 { 0xe, "ne" },
615 { 0xe, "nz" },
616 { 0xf, "nc" },
617 { 0xf, "uge" },
618 { 0 , 0 }
619 };
620
621 static void
622 get_cc_operand (ptr, mode, dst)
623 char **ptr;
624 struct z8k_op *mode;
625 unsigned int dst ATTRIBUTE_UNUSED;
626 {
627 char *src = *ptr;
628 int i;
629
630 while (*src == ' ')
631 src++;
632
633 mode->mode = CLASS_CC;
634 for (i = 0; table[i].name; i++)
635 {
636 int j;
637
638 for (j = 0; table[i].name[j]; j++)
639 {
640 if (table[i].name[j] != src[j])
641 goto fail;
642 }
643 the_cc = table[i].value;
644 *ptr = src + j;
645 return;
646 fail:
647 ;
648 }
649 the_cc = 0x8;
650 }
651
652 static void
653 get_operand (ptr, mode, dst)
654 char **ptr;
655 struct z8k_op *mode;
656 unsigned int dst ATTRIBUTE_UNUSED;
657 {
658 char *src = *ptr;
659 char *end;
660
661 mode->mode = 0;
662
663 while (*src == ' ')
664 src++;
665 if (*src == '#')
666 {
667 mode->mode = CLASS_IMM;
668 imm_operand = &(mode->exp);
669 src = parse_exp (src + 1, &(mode->exp));
670 }
671 else if (*src == '@')
672 {
673 mode->mode = CLASS_IR;
674 src = parse_reg (src + 1, &mode->regsize, &mode->reg);
675 }
676 else
677 {
678 int regn;
679
680 end = parse_reg (src, &mode->mode, &regn);
681
682 if (end)
683 {
684 int nw, nr;
685
686 src = end;
687 if (*src == '(')
688 {
689 src++;
690 end = parse_reg (src, &nw, &nr);
691 if (end)
692 {
693 /* Got Ra(Rb). */
694 src = end;
695
696 if (*src != ')')
697 as_bad (_("Missing ) in ra(rb)"));
698 else
699 src++;
700
701 regaddr (mode->mode, "ra(rb) ra");
702 #if 0
703 regword (mode->mode, "ra(rb) rb");
704 #endif
705 mode->mode = CLASS_BX;
706 mode->reg = regn;
707 mode->x_reg = nr;
708 reg[ARG_RX] = nr;
709 }
710 else
711 {
712 /* Got Ra(disp). */
713 if (*src == '#')
714 src++;
715 src = parse_exp (src, &(mode->exp));
716 src = checkfor (src, ')');
717 mode->mode = CLASS_BA;
718 mode->reg = regn;
719 mode->x_reg = 0;
720 imm_operand = &(mode->exp);
721 }
722 }
723 else
724 {
725 mode->reg = regn;
726 mode->x_reg = 0;
727 }
728 }
729 else
730 {
731 /* No initial reg. */
732 src = parse_exp (src, &(mode->exp));
733 if (*src == '(')
734 {
735 src++;
736 end = parse_reg (src, &(mode->mode), &regn);
737 regword (mode->mode, "addr(Ra) ra");
738 mode->mode = CLASS_X;
739 mode->reg = regn;
740 mode->x_reg = 0;
741 da_operand = &(mode->exp);
742 src = checkfor (end, ')');
743 }
744 else
745 {
746 /* Just an address. */
747 mode->mode = CLASS_DA;
748 mode->reg = 0;
749 mode->x_reg = 0;
750 da_operand = &(mode->exp);
751 }
752 }
753 }
754 *ptr = src;
755 }
756
757 static char *
758 get_operands (opcode, op_end, operand)
759 const opcode_entry_type *opcode;
760 char *op_end;
761 op_type *operand;
762 {
763 char *ptr = op_end;
764 char *savptr;
765
766 ptr++;
767 switch (opcode->noperands)
768 {
769 case 0:
770 operand[0].mode = 0;
771 operand[1].mode = 0;
772 break;
773
774 case 1:
775 if (opcode->arg_info[0] == CLASS_CC)
776 get_cc_operand (&ptr, operand + 0, 0);
777
778 else if (opcode->arg_info[0] == CLASS_FLAGS)
779 get_flags_operand (&ptr, operand + 0, 0);
780
781 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
782 get_interrupt_operand (&ptr, operand + 0, 0);
783
784 else
785 get_operand (&ptr, operand + 0, 0);
786
787 operand[1].mode = 0;
788 break;
789
790 case 2:
791 savptr = ptr;
792 if (opcode->arg_info[0] == CLASS_CC)
793 get_cc_operand (&ptr, operand + 0, 0);
794
795 else if (opcode->arg_info[0] == CLASS_CTRL)
796 {
797 get_ctrl_operand (&ptr, operand + 0, 0);
798
799 if (the_ctrl == 0)
800 {
801 ptr = savptr;
802 get_operand (&ptr, operand + 0, 0);
803
804 if (ptr == 0)
805 return NULL;
806 if (*ptr == ',')
807 ptr++;
808 get_ctrl_operand (&ptr, operand + 1, 1);
809 return ptr;
810 }
811 }
812 else
813 get_operand (&ptr, operand + 0, 0);
814
815 if (ptr == 0)
816 return NULL;
817 if (*ptr == ',')
818 ptr++;
819 get_operand (&ptr, operand + 1, 1);
820 break;
821
822 case 3:
823 get_operand (&ptr, operand + 0, 0);
824 if (*ptr == ',')
825 ptr++;
826 get_operand (&ptr, operand + 1, 1);
827 if (*ptr == ',')
828 ptr++;
829 get_operand (&ptr, operand + 2, 2);
830 break;
831
832 case 4:
833 get_operand (&ptr, operand + 0, 0);
834 if (*ptr == ',')
835 ptr++;
836 get_operand (&ptr, operand + 1, 1);
837 if (*ptr == ',')
838 ptr++;
839 get_operand (&ptr, operand + 2, 2);
840 if (*ptr == ',')
841 ptr++;
842 get_cc_operand (&ptr, operand + 3, 3);
843 break;
844
845 default:
846 abort ();
847 }
848
849 return ptr;
850 }
851
852 /* Passed a pointer to a list of opcodes which use different
853 addressing modes. Return the opcode which matches the opcodes
854 provided. */
855
856 static opcode_entry_type *
857 get_specific (opcode, operands)
858 opcode_entry_type *opcode;
859 op_type *operands;
860 {
861 opcode_entry_type *this_try = opcode;
862 int found = 0;
863 unsigned int noperands = opcode->noperands;
864
865 int this_index = opcode->idx;
866
867 while (this_index == opcode->idx && !found)
868 {
869 unsigned int i;
870
871 this_try = opcode++;
872 for (i = 0; i < noperands; i++)
873 {
874 unsigned int mode = operands[i].mode;
875
876 if (((mode & CLASS_MASK) == CLASS_IR) && ((this_try->arg_info[i] & CLASS_MASK) == CLASS_IRO))
877 {
878 mode = operands[i].mode = (operands[i].mode & ~CLASS_MASK) | CLASS_IRO;
879 }
880
881 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
882 {
883 /* It could be a pc rel operand, if this is a da mode
884 and we like disps, then insert it. */
885
886 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
887 {
888 /* This is the case. */
889 operands[i].mode = CLASS_DISP;
890 }
891 else if (mode == CLASS_BA && this_try->arg_info[i])
892 {
893 /* Can't think of a way to turn what we've been
894 given into something that's OK. */
895 goto fail;
896 }
897 else if (this_try->arg_info[i] & CLASS_PR)
898 {
899 if (mode == CLASS_REG_LONG && segmented_mode)
900 {
901 /* OK. */
902 }
903 else if (mode == CLASS_REG_WORD && !segmented_mode)
904 {
905 /* OK. */
906 }
907 else
908 goto fail;
909 }
910 else
911 goto fail;
912 }
913 switch (mode & CLASS_MASK)
914 {
915 default:
916 break;
917 case CLASS_IRO:
918 if (operands[i].regsize != CLASS_REG_WORD)
919 as_bad (_("invalid indirect register size"));
920 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
921 break;
922 case CLASS_IR:
923 if ((segmented_mode && operands[i].regsize != CLASS_REG_LONG)
924 || (!segmented_mode && operands[i].regsize != CLASS_REG_WORD))
925 as_bad (_("invalid indirect register size"));
926 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
927 break;
928 case CLASS_X:
929 case CLASS_BA:
930 case CLASS_BX:
931 case CLASS_DISP:
932 case CLASS_REG:
933 case CLASS_REG_WORD:
934 case CLASS_REG_BYTE:
935 case CLASS_REG_QUAD:
936 case CLASS_REG_LONG:
937 case CLASS_REGN0:
938 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
939 break;
940 }
941 }
942
943 found = 1;
944 fail:
945 ;
946 }
947 if (found)
948 return this_try;
949 else
950 return 0;
951 }
952
953 #if 0 /* Not used. */
954 static void
955 check_operand (operand, width, string)
956 struct z8k_op *operand;
957 unsigned int width;
958 char *string;
959 {
960 if (operand->exp.X_add_symbol == 0
961 && operand->exp.X_op_symbol == 0)
962 {
963
964 /* No symbol involved, let's look at offset, it's dangerous if
965 any of the high bits are not 0 or ff's, find out by oring or
966 anding with the width and seeing if the answer is 0 or all
967 fs. */
968 if ((operand->exp.X_add_number & ~width) != 0 &&
969 (operand->exp.X_add_number | width) != (~0))
970 {
971 as_warn (_("operand %s0x%x out of range"),
972 string, operand->exp.X_add_number);
973 }
974 }
975
976 }
977 #endif
978
979 static char buffer[20];
980
981 static void
982 newfix (ptr, type, size, operand)
983 int ptr;
984 int type;
985 int size; /* nibbles. */
986 expressionS *operand;
987 {
988 int is_pcrel = 0;
989
990 if (operand->X_add_symbol
991 || operand->X_op_symbol
992 || operand->X_add_number)
993 {
994 switch(type)
995 {
996 case R_JR:
997 case R_DISP7:
998 case R_CALLR:
999 is_pcrel = 1;
1000 }
1001 fix_new_exp (frag_now,
1002 ptr,
1003 size / 2,
1004 operand,
1005 is_pcrel,
1006 type);
1007 }
1008 }
1009
1010 static char *
1011 apply_fix (ptr, type, operand, size)
1012 char *ptr;
1013 int type;
1014 expressionS *operand;
1015 int size; /* nibbles. */
1016 {
1017 long n = operand->X_add_number;
1018
1019 newfix ((ptr - buffer) / 2, type, size + 1, operand);
1020 switch (size)
1021 {
1022 case 8: /* 8 nibbles == 32 bits. */
1023 *ptr++ = n >> 28;
1024 *ptr++ = n >> 24;
1025 *ptr++ = n >> 20;
1026 *ptr++ = n >> 16;
1027 case 4: /* 4 nibbles == 16 bits. */
1028 *ptr++ = n >> 12;
1029 *ptr++ = n >> 8;
1030 case 2:
1031 *ptr++ = n >> 4;
1032 case 1:
1033 *ptr++ = n >> 0;
1034 break;
1035 }
1036 return ptr;
1037 }
1038
1039 /* Now we know what sort of opcodes it is. Let's build the bytes. */
1040
1041 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
1042
1043 static void
1044 build_bytes (this_try, operand)
1045 opcode_entry_type *this_try;
1046 struct z8k_op *operand ATTRIBUTE_UNUSED;
1047 {
1048 char *output_ptr = buffer;
1049 int c;
1050 int nibble;
1051 unsigned int *class_ptr;
1052
1053 frag_wane (frag_now);
1054 frag_new (0);
1055
1056 memset (buffer, 0, sizeof (buffer));
1057 class_ptr = this_try->byte_info;
1058
1059 for (nibble = 0; (c = *class_ptr++); nibble++)
1060 {
1061
1062 switch (c & CLASS_MASK)
1063 {
1064 default:
1065 abort ();
1066
1067 case CLASS_ADDRESS:
1068 /* Direct address, we don't cope with the SS mode right now. */
1069 if (segmented_mode)
1070 {
1071 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
1072 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1073 }
1074 else
1075 {
1076 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1077 }
1078 da_operand = 0;
1079 break;
1080 case CLASS_DISP8:
1081 /* pc rel 8 bit */
1082 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1083 da_operand = 0;
1084 break;
1085
1086 case CLASS_0DISP7:
1087 /* pc rel 7 bit */
1088 *output_ptr = 0;
1089 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1090 da_operand = 0;
1091 break;
1092
1093 case CLASS_1DISP7:
1094 /* pc rel 7 bit */
1095 *output_ptr = 0x80;
1096 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1097 output_ptr[-2] = 0x8;
1098 da_operand = 0;
1099 break;
1100
1101 case CLASS_BIT_1OR2:
1102 *output_ptr = c & 0xf;
1103 if (imm_operand)
1104 {
1105 if (imm_operand->X_add_number == 2)
1106 *output_ptr |= 2;
1107 else if (imm_operand->X_add_number != 1)
1108 as_bad (_("immediate must be 1 or 2"));
1109 }
1110 else
1111 as_bad (_("immediate 1 or 2 expected"));
1112 output_ptr++;
1113 break;
1114 case CLASS_CC:
1115 *output_ptr++ = the_cc;
1116 break;
1117 case CLASS_0CCC:
1118 *output_ptr++ = the_ctrl;
1119 break;
1120 case CLASS_1CCC:
1121 *output_ptr++ = the_ctrl | 0x8;
1122 break;
1123 case CLASS_00II:
1124 *output_ptr++ = (~the_interrupt & 0x3);
1125 break;
1126 case CLASS_01II:
1127 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1128 break;
1129 case CLASS_FLAGS:
1130 *output_ptr++ = the_flags;
1131 break;
1132 case CLASS_IGNORE:
1133 case CLASS_BIT:
1134 *output_ptr++ = c & 0xf;
1135 break;
1136 case CLASS_REGN0:
1137 if (reg[c & 0xf] == 0)
1138 as_bad (_("can't use R0 here"));
1139 /* Fall through. */
1140 case CLASS_REG:
1141 case CLASS_REG_BYTE:
1142 case CLASS_REG_WORD:
1143 case CLASS_REG_LONG:
1144 case CLASS_REG_QUAD:
1145 /* Insert bit mattern of right reg. */
1146 *output_ptr++ = reg[c & 0xf];
1147 break;
1148 case CLASS_DISP:
1149 switch (c & ARG_MASK)
1150 {
1151 case ARG_DISP12:
1152 output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1153 break;
1154 case ARG_DISP16:
1155 output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1156 break;
1157 default:
1158 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1159 }
1160 da_operand = 0;
1161 break;
1162
1163 case CLASS_IMM:
1164 {
1165 switch (c & ARG_MASK)
1166 {
1167 case ARG_NIM4:
1168 if (imm_operand->X_add_number > 15)
1169 {
1170 as_bad (_("immediate value out of range"));
1171 }
1172 imm_operand->X_add_number = -imm_operand->X_add_number;
1173 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1174 break;
1175 /*case ARG_IMMNMINUS1: not used. */
1176 case ARG_IMM4M1:
1177 imm_operand->X_add_number--;
1178 /* Drop through. */
1179 case ARG_IMM4:
1180 if (imm_operand->X_add_number > 15)
1181 {
1182 as_bad (_("immediate value out of range"));
1183 }
1184 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1185 break;
1186 case ARG_NIM8:
1187 imm_operand->X_add_number = -imm_operand->X_add_number;
1188 /* Drop through. */
1189 case ARG_IMM8:
1190 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1191 break;
1192 case ARG_IMM16:
1193 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1194 break;
1195 case ARG_IMM32:
1196 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1197 break;
1198 default:
1199 abort ();
1200 }
1201 }
1202 }
1203 }
1204
1205 /* Copy from the nibble buffer into the frag. */
1206 {
1207 int length = (output_ptr - buffer) / 2;
1208 char *src = buffer;
1209 char *fragp = frag_more (length);
1210
1211 while (src < output_ptr)
1212 {
1213 *fragp = (src[0] << 4) | src[1];
1214 src += 2;
1215 fragp++;
1216 }
1217 }
1218 }
1219
1220 /* This is the guts of the machine-dependent assembler. STR points to a
1221 machine dependent instruction. This function is supposed to emit
1222 the frags/bytes it assembles to. */
1223
1224 void
1225 md_assemble (str)
1226 char *str;
1227 {
1228 char c;
1229 char *op_start;
1230 char *op_end;
1231 struct z8k_op operand[3];
1232 opcode_entry_type *opcode;
1233 opcode_entry_type *prev_opcode;
1234
1235 /* Drop leading whitespace. */
1236 while (*str == ' ')
1237 str++;
1238
1239 /* Find the op code end. */
1240 for (op_start = op_end = str;
1241 *op_end != 0 && *op_end != ' ';
1242 op_end++)
1243 ;
1244
1245 if (op_end == op_start)
1246 {
1247 as_bad (_("can't find opcode "));
1248 }
1249 c = *op_end;
1250
1251 *op_end = 0;
1252
1253 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1254
1255 if (opcode == NULL)
1256 {
1257 as_bad (_("unknown opcode"));
1258 return;
1259 }
1260
1261 if (opcode->opcode == 250)
1262 {
1263 pseudo_typeS *p;
1264 char oc;
1265 char *old = input_line_pointer;
1266 *op_end = c;
1267
1268 /* Was really a pseudo op. */
1269
1270 input_line_pointer = op_end;
1271
1272 oc = *old;
1273 *old = '\n';
1274 while (*input_line_pointer == ' ')
1275 input_line_pointer++;
1276 p = (pseudo_typeS *) (opcode->func);
1277
1278 (p->poc_handler) (p->poc_val);
1279 input_line_pointer = old;
1280 *old = oc;
1281 }
1282 else
1283 {
1284 char *new_input_line_pointer;
1285
1286 new_input_line_pointer = get_operands (opcode, op_end, operand);
1287 if (new_input_line_pointer)
1288 input_line_pointer = new_input_line_pointer;
1289 prev_opcode = opcode; /* XXX is this used ?? */
1290
1291 opcode = get_specific (opcode, operand);
1292
1293 if (opcode == 0)
1294 {
1295 /* Couldn't find an opcode which matched the operands. */
1296 char *where = frag_more (2);
1297
1298 where[0] = 0x0;
1299 where[1] = 0x0;
1300
1301 as_bad (_("Can't find opcode to match operands"));
1302 return;
1303 }
1304
1305 build_bytes (opcode, operand);
1306 }
1307 }
1308
1309 void
1310 tc_crawl_symbol_chain (headers)
1311 object_headers *headers ATTRIBUTE_UNUSED;
1312 {
1313 printf (_("call to tc_crawl_symbol_chain \n"));
1314 }
1315
1316 /* We have no need to default values of symbols. */
1317
1318 symbolS *
1319 md_undefined_symbol (name)
1320 char *name ATTRIBUTE_UNUSED;
1321 {
1322 return 0;
1323 }
1324
1325 void
1326 tc_headers_hook (headers)
1327 object_headers *headers ATTRIBUTE_UNUSED;
1328 {
1329 printf (_("call to tc_headers_hook \n"));
1330 }
1331
1332 /* Various routines to kill one day. */
1333 /* Equal to MAX_PRECISION in atof-ieee.c. */
1334 #define MAX_LITTLENUMS 6
1335
1336 /* Turn a string in input_line_pointer into a floating point constant
1337 of type TYPE, and store the appropriate bytes in *LITP. The number
1338 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1339 returned, or NULL on OK. */
1340
1341 char *
1342 md_atof (type, litP, sizeP)
1343 char type;
1344 char *litP;
1345 int *sizeP;
1346 {
1347 int prec;
1348 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1349 LITTLENUM_TYPE *wordP;
1350 char *t;
1351
1352 switch (type)
1353 {
1354 case 'f':
1355 case 'F':
1356 case 's':
1357 case 'S':
1358 prec = 2;
1359 break;
1360
1361 case 'd':
1362 case 'D':
1363 case 'r':
1364 case 'R':
1365 prec = 4;
1366 break;
1367
1368 case 'x':
1369 case 'X':
1370 prec = 6;
1371 break;
1372
1373 case 'p':
1374 case 'P':
1375 prec = 6;
1376 break;
1377
1378 default:
1379 *sizeP = 0;
1380 return _("Bad call to MD_ATOF()");
1381 }
1382 t = atof_ieee (input_line_pointer, type, words);
1383 if (t)
1384 input_line_pointer = t;
1385
1386 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1387 for (wordP = words; prec--;)
1388 {
1389 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1390 litP += sizeof (LITTLENUM_TYPE);
1391 }
1392 return 0;
1393 }
1394 \f
1395 const char *md_shortopts = "z:";
1396
1397 struct option md_longopts[] =
1398 {
1399 #define OPTION_RELAX (OPTION_MD_BASE)
1400 {"linkrelax", no_argument, NULL, OPTION_RELAX},
1401 {NULL, no_argument, NULL, 0}
1402 };
1403
1404 size_t md_longopts_size = sizeof (md_longopts);
1405
1406 int
1407 md_parse_option (c, arg)
1408 int c;
1409 char *arg;
1410 {
1411 switch (c)
1412 {
1413 case 'z':
1414 if (!strcmp (arg, "8001"))
1415 s_segm (1);
1416 else if (!strcmp (arg, "8002"))
1417 s_segm (0);
1418 else
1419 {
1420 as_bad (_("invalid architecture -z%s"), arg);
1421 return 0;
1422 }
1423 z8k_target_from_cmdline = 1;
1424 break;
1425
1426 case OPTION_RELAX:
1427 linkrelax = 1;
1428 break;
1429
1430 default:
1431 return 0;
1432 }
1433
1434 return 1;
1435 }
1436
1437 void
1438 md_show_usage (stream)
1439 FILE *stream;
1440 {
1441 fprintf (stream, _("\
1442 Z8K options:\n\
1443 -z8001 generate segmented code\n\
1444 -z8002 generate unsegmented code\n\
1445 -linkrelax create linker relaxable code\n"));
1446 }
1447 \f
1448 void
1449 md_convert_frag (headers, seg, fragP)
1450 object_headers *headers ATTRIBUTE_UNUSED;
1451 segT seg ATTRIBUTE_UNUSED;
1452 fragS *fragP ATTRIBUTE_UNUSED;
1453 {
1454 printf (_("call to md_convert_frag\n"));
1455 abort ();
1456 }
1457
1458 valueT
1459 md_section_align (seg, size)
1460 segT seg;
1461 valueT size;
1462 {
1463 return ((size + (1 << section_alignment[(int) seg]) - 1)
1464 & (-1 << section_alignment[(int) seg]));
1465 }
1466
1467 /* Attempt to simplify or eliminate a fixup. To indicate that a fixup
1468 has been eliminated, set fix->fx_done. If fix->fx_addsy is non-NULL,
1469 we will have to generate a reloc entry. */
1470 void
1471 md_apply_fix3 (fixP, valP, segment)
1472 fixS * fixP;
1473 valueT * valP;
1474 segT segment ATTRIBUTE_UNUSED;
1475 {
1476 long val = * (long *) valP;
1477 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1478
1479 switch (fixP->fx_r_type)
1480 {
1481 case R_IMM4L:
1482 buf[0] = (buf[0] & 0xf0) | (val & 0xf);
1483 break;
1484
1485 case R_JR:
1486 if (fixP->fx_addsy)
1487 {
1488 fixP->fx_no_overflow = 1;
1489 fixP->fx_done = 0;
1490 }
1491 else
1492 {
1493 if (val & 1)
1494 as_bad (_("cannot branch to odd address"));
1495 val /= 2;
1496 if (val > 127 || val < -128)
1497 as_warn (_("relative jump out of range"));
1498 *buf++ = val;
1499 fixP->fx_no_overflow = 1;
1500 fixP->fx_done = 1;
1501 }
1502 break;
1503
1504 case R_DISP7:
1505 if (fixP->fx_addsy)
1506 {
1507 fixP->fx_no_overflow = 1;
1508 fixP->fx_done = 0;
1509 }
1510 else
1511 {
1512 if (val & 1)
1513 as_bad (_("cannot branch to odd address"));
1514 val /= 2;
1515 if (val > 0 || val < -127)
1516 as_bad (_("relative jump out of range"));
1517 *buf = (*buf & 0x80) | (-val & 0x7f);
1518 fixP->fx_no_overflow = 1;
1519 fixP->fx_done = 1;
1520 }
1521 break;
1522
1523 case R_CALLR:
1524 if (fixP->fx_addsy)
1525 {
1526 fixP->fx_no_overflow = 1;
1527 fixP->fx_done = 0;
1528 }
1529 else
1530 {
1531 if (val & 1)
1532 as_bad (_("cannot branch to odd address"));
1533 if (val > 4096 || val < -4095)
1534 as_bad (_("relative call out of range"));
1535 val = -val / 2;
1536 *buf = (*buf & 0xf0) | ((val >> 8) & 0xf);
1537 buf++;
1538 *buf++ = val & 0xff;
1539 fixP->fx_no_overflow = 1;
1540 fixP->fx_done = 1;
1541 }
1542 break;
1543
1544 case R_IMM8:
1545 *buf++ = val;
1546 break;
1547
1548 case R_IMM16:
1549 *buf++ = (val >> 8);
1550 *buf++ = val;
1551 break;
1552
1553 case R_IMM32:
1554 *buf++ = (val >> 24);
1555 *buf++ = (val >> 16);
1556 *buf++ = (val >> 8);
1557 *buf++ = val;
1558 break;
1559
1560 case R_REL16:
1561 val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1562 if (val > 32767 || val < -32768)
1563 as_bad (_("relative address out of range"));
1564 *buf++ = (val >> 8);
1565 *buf++ = val;
1566 fixP->fx_no_overflow = 1;
1567 break;
1568
1569 #if 0
1570 case R_DA | R_SEG:
1571 *buf++ = (val >> 16);
1572 *buf++ = 0x00;
1573 *buf++ = (val >> 8);
1574 *buf++ = val;
1575 break;
1576 #endif
1577
1578 case 0:
1579 md_number_to_chars (buf, val, fixP->fx_size);
1580 break;
1581
1582 default:
1583 printf(_("md_apply_fix3: unknown r_type 0x%x\n"), fixP->fx_r_type);
1584 abort ();
1585 }
1586
1587 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1588 fixP->fx_done = 1;
1589 }
1590
1591 int
1592 md_estimate_size_before_relax (fragP, segment_type)
1593 register fragS *fragP ATTRIBUTE_UNUSED;
1594 register segT segment_type ATTRIBUTE_UNUSED;
1595 {
1596 printf (_("call to md_estimate_size_before_relax\n"));
1597 abort ();
1598 }
1599
1600 /* Put number into target byte order. */
1601
1602 void
1603 md_number_to_chars (ptr, use, nbytes)
1604 char *ptr;
1605 valueT use;
1606 int nbytes;
1607 {
1608 number_to_chars_bigendian (ptr, use, nbytes);
1609 }
1610
1611 /* On the Z8000, a PC-relative offset is relative to the address of the
1612 instruction plus its size. */
1613 long
1614 md_pcrel_from (fixP)
1615 fixS *fixP;
1616 {
1617 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1618 }
1619
1620 void
1621 tc_coff_symbol_emit_hook (s)
1622 symbolS *s ATTRIBUTE_UNUSED;
1623 {
1624 }
1625
1626 void
1627 tc_reloc_mangle (fix_ptr, intr, base)
1628 fixS *fix_ptr;
1629 struct internal_reloc *intr;
1630 bfd_vma base;
1631
1632 {
1633 symbolS *symbol_ptr;
1634
1635 if (fix_ptr->fx_addsy
1636 && fix_ptr->fx_subsy)
1637 {
1638 symbolS *add = fix_ptr->fx_addsy;
1639 symbolS *sub = fix_ptr->fx_subsy;
1640
1641 if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1642 as_bad (_("Can't subtract symbols in different sections %s %s"),
1643 S_GET_NAME (add), S_GET_NAME (sub));
1644 else
1645 {
1646 int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1647
1648 fix_ptr->fx_addsy = 0;
1649 fix_ptr->fx_subsy = 0;
1650 fix_ptr->fx_offset += diff;
1651 }
1652 }
1653 symbol_ptr = fix_ptr->fx_addsy;
1654
1655 /* If this relocation is attached to a symbol then it's ok
1656 to output it. */
1657 if (fix_ptr->fx_r_type == 0)
1658 {
1659 /* cons likes to create reloc32's whatever the size of the reloc. */
1660 switch (fix_ptr->fx_size)
1661 {
1662 case 2:
1663 intr->r_type = R_IMM16;
1664 break;
1665 case 1:
1666 intr->r_type = R_IMM8;
1667 break;
1668 case 4:
1669 intr->r_type = R_IMM32;
1670 break;
1671 default:
1672 abort ();
1673 }
1674 }
1675 else
1676 intr->r_type = fix_ptr->fx_r_type;
1677
1678 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1679 intr->r_offset = fix_ptr->fx_offset;
1680
1681 if (symbol_ptr)
1682 intr->r_symndx = symbol_ptr->sy_number;
1683 else
1684 intr->r_symndx = -1;
1685 }
This page took 0.066943 seconds and 4 git commands to generate.