2004-06-29 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gas / config / tc-h8500.c
blobcd4d76f8831cf936b5c763f575bf3fd482c9e7d7
1 /* tc-h8500.c -- Assemble code for the Renesas H8/500
2 Copyright 1993, 1994, 1995, 1998, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
24 #include <stdio.h>
25 #include "as.h"
26 #include "bfd.h"
27 #include "subsegs.h"
28 #define DEFINE_TABLE
29 #define ASSEMBLER_TABLE
30 #include "opcodes/h8500-opc.h"
31 #include "safe-ctype.h"
33 const char comment_chars[] = "!";
34 const char line_separator_chars[] = ";";
35 const char line_comment_chars[] = "!#";
37 /* This table describes all the machine specific pseudo-ops the assembler
38 has to support. The fields are:
39 pseudo-op name without dot
40 function to call to execute this pseudo-op
41 Integer arg to pass to the function
44 const pseudo_typeS md_pseudo_table[] =
46 {"int", cons, 2},
47 {"data.b", cons, 1},
48 {"data.w", cons, 2},
49 {"data.l", cons, 4},
50 {"form", listing_psize, 0},
51 {"heading", listing_title, 0},
52 {"import", s_ignore, 0},
53 {"page", listing_eject, 0},
54 {"program", s_ignore, 0},
55 {0, 0, 0}
58 const int md_reloc_size;
60 const char EXP_CHARS[] = "eE";
62 /* Chars that mean this number is a floating point constant */
63 /* As in 0f12.456 */
64 /* or 0d1.2345e12 */
65 const char FLT_CHARS[] = "rRsSfFdDxXpP";
67 #define C(a,b) ENCODE_RELAX(a,b)
68 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
70 #define GET_WHAT(x) ((x>>2))
72 #define BYTE_DISP 1
73 #define WORD_DISP 2
74 #define UNDEF_BYTE_DISP 0
75 #define UNDEF_WORD_DISP 3
77 #define BRANCH 1
78 #define SCB_F 2
79 #define SCB_TST 3
80 #define END 4
82 #define BYTE_F 127
83 #define BYTE_B -126
84 #define WORD_F 32767
85 #define WORD_B 32768
87 relax_typeS md_relax_table[C (END, 0)] = {
88 { 0, 0, 0, 0 },
89 { 0, 0, 0, 0 },
90 { 0, 0, 0, 0 },
91 { 0, 0, 0, 0 },
93 /* BRANCH */
94 { 0, 0, 0, 0 },
95 { BYTE_F, BYTE_B, 2, C (BRANCH, WORD_DISP) },
96 { WORD_F, WORD_B, 3, 0 },
97 { 0, 0, 3, 0 },
99 /* SCB_F */
100 { 0, 0, 0, 0 },
101 { BYTE_F, BYTE_B, 3, C (SCB_F, WORD_DISP) },
102 { WORD_F, WORD_B, 8, 0 },
103 { 0, 0, 8, 0 },
105 /* SCB_TST */
106 { 0, 0, 0, 0 },
107 { BYTE_F, BYTE_B, 3, C (SCB_TST, WORD_DISP) },
108 { WORD_F, WORD_B, 10, 0 },
109 { 0, 0, 10, 0 }
113 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
116 This function is called once, at assembler startup time. This should
117 set up all the tables, etc. that the MD part of the assembler needs
120 void
121 md_begin ()
123 const h8500_opcode_info *opcode;
124 char prev_buffer[100];
125 int idx = 0;
127 opcode_hash_control = hash_new ();
128 prev_buffer[0] = 0;
130 /* Insert unique names into hash table */
131 for (opcode = h8500_table; opcode->name; opcode++)
133 if (idx != opcode->idx)
135 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
136 idx++;
141 static int rn; /* register number used by RN */
142 static int rs; /* register number used by RS */
143 static int rd; /* register number used by RD */
144 static int crb; /* byte size cr */
145 static int crw; /* word sized cr */
146 static int cr; /* unknown size cr */
148 static expressionS displacement;/* displacement expression */
150 static int immediate_inpage;
151 static expressionS immediate; /* immediate expression */
153 static expressionS absolute; /* absolute expression */
155 typedef struct
157 int type;
158 int reg;
159 expressionS exp;
160 int page;
163 h8500_operand_info;
165 /* Try to parse a reg name. Return the number of chars consumed. */
167 static int parse_reg PARAMS ((char *, int *, int *));
169 static int
170 parse_reg (src, mode, reg)
171 char *src;
172 int *mode;
173 int *reg;
175 char *end;
176 int len;
178 /* Cribbed from get_symbol_end(). */
179 if (!is_name_beginner (*src) || *src == '\001')
180 return 0;
181 end = src + 1;
182 while (is_part_of_name (*end) || *end == '\001')
183 end++;
184 len = end - src;
186 if (len == 2 && src[0] == 'r')
188 if (src[1] >= '0' && src[1] <= '7')
190 *mode = RN;
191 *reg = (src[1] - '0');
192 return len;
195 if (len == 2 && src[0] == 's' && src[1] == 'p')
197 *mode = RN;
198 *reg = 7;
199 return len;
201 if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
203 *mode = CRB;
204 *reg = 1;
205 return len;
207 if (len == 2 && src[0] == 's' && src[1] == 'r')
209 *mode = CRW;
210 *reg = 0;
211 return len;
213 if (len == 2 && src[0] == 'b' && src[1] == 'r')
215 *mode = CRB;
216 *reg = 3;
217 return len;
219 if (len == 2 && src[0] == 'e' && src[1] == 'p')
221 *mode = CRB;
222 *reg = 4;
223 return len;
225 if (len == 2 && src[0] == 'd' && src[1] == 'p')
227 *mode = CRB;
228 *reg = 5;
229 return len;
231 if (len == 2 && src[0] == 't' && src[1] == 'p')
233 *mode = CRB;
234 *reg = 7;
235 return len;
237 if (len == 2 && src[0] == 'f' && src[1] == 'p')
239 *mode = RN;
240 *reg = 6;
241 return len;
243 return 0;
246 static char *parse_exp PARAMS ((char *, expressionS *, int *));
248 static char *
249 parse_exp (s, op, page)
250 char *s;
251 expressionS *op;
252 int *page;
254 char *save;
255 char *new;
257 save = input_line_pointer;
259 *page = 0;
260 if (s[0] == '%')
262 if (s[1] == 'p' && s[2] == 'a' && s[3] == 'g' && s[4] == 'e')
264 s += 5;
265 *page = 'p';
267 if (s[1] == 'h' && s[2] == 'i' && s[3] == '1' && s[4] == '6')
269 s += 5;
270 *page = 'h';
272 else if (s[1] == 'o' && s[2] == 'f' && s[3] == 'f')
274 s += 4;
275 *page = 'o';
279 input_line_pointer = s;
281 expression (op);
282 if (op->X_op == O_absent)
283 as_bad (_("missing operand"));
284 new = input_line_pointer;
285 input_line_pointer = save;
286 return new;
289 typedef enum
291 exp_signed, exp_unsigned, exp_sandu
292 } sign_type;
294 static char *skip_colonthing
295 PARAMS ((sign_type, char *, h8500_operand_info *, int, int, int, int));
297 static char *
298 skip_colonthing (sign, ptr, exp, def, size8, size16, size24)
299 sign_type sign;
300 char *ptr;
301 h8500_operand_info *exp;
302 int def;
303 int size8;
304 int size16;
305 int size24;
307 ptr = parse_exp (ptr, &exp->exp, &exp->page);
308 if (*ptr == ':')
310 ptr++;
311 if (*ptr == '8')
313 ptr++;
314 exp->type = size8;
316 else if (ptr[0] == '1' && ptr[1] == '6')
318 ptr += 2;
319 exp->type = size16;
321 else if (ptr[0] == '2' && ptr[1] == '4')
323 if (!size24)
325 as_bad (_(":24 not valid for this opcode"));
327 ptr += 2;
328 exp->type = size24;
330 else
332 as_bad (_("expect :8,:16 or :24"));
333 exp->type = size16;
336 else
338 if (exp->page == 'p')
340 exp->type = IMM8;
342 else if (exp->page == 'h')
344 exp->type = IMM16;
346 else
348 /* Let's work out the size from the context */
349 int n = exp->exp.X_add_number;
350 if (size8
351 && exp->exp.X_op == O_constant
352 && ((sign == exp_signed && (n >= -128 && n <= 127))
353 || (sign == exp_unsigned && (n >= 0 && (n <= 255)))
354 || (sign == exp_sandu && (n >= -128 && (n <= 255)))))
356 exp->type = size8;
358 else
360 exp->type = def;
364 return ptr;
367 static int parse_reglist PARAMS ((char *, h8500_operand_info *));
369 static int
370 parse_reglist (src, op)
371 char *src;
372 h8500_operand_info *op;
374 int mode;
375 int rn;
376 int mask = 0;
377 int rm;
378 int idx = 1; /* skip ( */
380 while (src[idx] && src[idx] != ')')
382 int done = parse_reg (src + idx, &mode, &rn);
384 if (done)
386 idx += done;
387 mask |= 1 << rn;
389 else
391 as_bad (_("syntax error in reg list"));
392 return 0;
394 if (src[idx] == '-')
396 idx++;
397 done = parse_reg (src + idx, &mode, &rm);
398 if (done)
400 idx += done;
401 while (rn <= rm)
403 mask |= 1 << rn;
404 rn++;
407 else
409 as_bad (_("missing final register in range"));
412 if (src[idx] == ',')
413 idx++;
415 idx++;
416 op->exp.X_add_symbol = 0;
417 op->exp.X_op_symbol = 0;
418 op->exp.X_add_number = mask;
419 op->exp.X_op = O_constant;
420 op->exp.X_unsigned = 1;
421 op->type = IMM8;
422 return idx;
426 /* The many forms of operand:
428 Rn Register direct
429 @Rn Register indirect
430 @(disp[:size], Rn) Register indirect with displacement
431 @Rn+
432 @-Rn
433 @aa[:size] absolute
434 #xx[:size] immediate data
438 static void get_operand PARAMS ((char **, h8500_operand_info *, char));
440 static void
441 get_operand (ptr, op, ispage)
442 char **ptr;
443 h8500_operand_info *op;
444 char ispage;
446 char *src = *ptr;
447 int mode;
448 unsigned int num;
449 unsigned int len;
450 op->page = 0;
451 if (src[0] == '(' && src[1] == 'r')
453 /* This is a register list */
454 *ptr = src + parse_reglist (src, op);
455 return;
458 len = parse_reg (src, &op->type, &op->reg);
460 if (len)
462 *ptr = src + len;
463 return;
466 if (*src == '@')
468 src++;
469 if (*src == '-')
471 src++;
472 len = parse_reg (src, &mode, &num);
473 if (len == 0)
475 /* Oops, not a reg after all, must be ordinary exp */
476 src--;
477 /* must be a symbol */
478 *ptr = skip_colonthing (exp_unsigned, src,
479 op, ABS16, ABS8, ABS16, ABS24);
480 return;
483 op->type = RNDEC;
484 op->reg = num;
485 *ptr = src + len;
486 return;
488 if (*src == '(')
490 /* Disp */
491 src++;
493 src = skip_colonthing (exp_signed, src,
494 op, RNIND_D16, RNIND_D8, RNIND_D16, 0);
496 if (*src != ',')
498 as_bad (_("expected @(exp, Rn)"));
499 return;
501 src++;
502 len = parse_reg (src, &mode, &op->reg);
503 if (len == 0 || mode != RN)
505 as_bad (_("expected @(exp, Rn)"));
506 return;
508 src += len;
509 if (*src != ')')
511 as_bad (_("expected @(exp, Rn)"));
512 return;
514 *ptr = src + 1;
515 return;
517 len = parse_reg (src, &mode, &num);
519 if (len)
521 src += len;
522 if (*src == '+')
524 src++;
525 if (mode != RN)
527 as_bad (_("@Rn+ needs word register"));
528 return;
530 op->type = RNINC;
531 op->reg = num;
532 *ptr = src;
533 return;
535 if (mode != RN)
537 as_bad (_("@Rn needs word register"));
538 return;
540 op->type = RNIND;
541 op->reg = num;
542 *ptr = src;
543 return;
545 else
547 /* must be a symbol */
548 *ptr =
549 skip_colonthing (exp_unsigned, src, op,
550 ispage ? ABS24 : ABS16, ABS8, ABS16, ABS24);
551 return;
555 if (*src == '#')
557 src++;
558 *ptr = skip_colonthing (exp_sandu, src, op, IMM16, IMM8, IMM16, ABS24);
559 return;
561 else
563 *ptr = skip_colonthing (exp_signed, src, op,
564 ispage ? ABS24 : PCREL8, PCREL8, PCREL16, ABS24);
568 static char *get_operands
569 PARAMS ((h8500_opcode_info *, char *, h8500_operand_info *));
571 static char *
572 get_operands (info, args, operand)
573 h8500_opcode_info *info;
574 char *args;
575 h8500_operand_info *operand;
577 char *ptr = args;
579 switch (info->nargs)
581 case 0:
582 operand[0].type = 0;
583 operand[1].type = 0;
584 break;
586 case 1:
587 ptr++;
588 get_operand (&ptr, operand + 0, info->name[0] == 'p');
589 operand[1].type = 0;
590 break;
592 case 2:
593 ptr++;
594 get_operand (&ptr, operand + 0, 0);
595 if (*ptr == ',')
596 ptr++;
597 get_operand (&ptr, operand + 1, 0);
598 break;
600 default:
601 abort ();
604 return ptr;
607 /* Passed a pointer to a list of opcodes which use different
608 addressing modes, return the opcode which matches the opcodes
609 provided. */
611 int pcrel8; /* Set when we've seen a pcrel operand */
613 static h8500_opcode_info *get_specific
614 PARAMS ((h8500_opcode_info *, h8500_operand_info *));
616 static h8500_opcode_info *
617 get_specific (opcode, operands)
618 h8500_opcode_info *opcode;
619 h8500_operand_info *operands;
621 h8500_opcode_info *this_try = opcode;
622 int found = 0;
623 unsigned int noperands = opcode->nargs;
624 int this_index = opcode->idx;
626 while (this_index == opcode->idx && !found)
628 unsigned int i;
630 this_try = opcode++;
632 /* look at both operands needed by the opcodes and provided by
633 the user*/
634 for (i = 0; i < noperands; i++)
636 h8500_operand_info *user = operands + i;
638 switch (this_try->arg_type[i])
640 case FPIND_D8:
641 /* Opcode needs (disp:8,fp) */
642 if (user->type == RNIND_D8 && user->reg == 6)
644 displacement = user->exp;
645 continue;
647 break;
648 case RDIND_D16:
649 if (user->type == RNIND_D16)
651 displacement = user->exp;
652 rd = user->reg;
653 continue;
655 break;
656 case RDIND_D8:
657 if (user->type == RNIND_D8)
659 displacement = user->exp;
660 rd = user->reg;
661 continue;
663 break;
664 case RNIND_D16:
665 case RNIND_D8:
666 if (user->type == this_try->arg_type[i])
668 displacement = user->exp;
669 rn = user->reg;
670 continue;
672 break;
674 case SPDEC:
675 if (user->type == RNDEC && user->reg == 7)
677 continue;
679 break;
680 case SPINC:
681 if (user->type == RNINC && user->reg == 7)
683 continue;
685 break;
686 case ABS16:
687 if (user->type == ABS16)
689 absolute = user->exp;
690 continue;
692 break;
693 case ABS8:
694 if (user->type == ABS8)
696 absolute = user->exp;
697 continue;
699 break;
700 case ABS24:
701 if (user->type == ABS24)
703 absolute = user->exp;
704 continue;
706 break;
708 case CRB:
709 if ((user->type == CRB || user->type == CR) && user->reg != 0)
711 crb = user->reg;
712 continue;
714 break;
715 case CRW:
716 if ((user->type == CRW || user->type == CR) && user->reg == 0)
718 crw = user->reg;
719 continue;
721 break;
722 case DISP16:
723 if (user->type == DISP16)
725 displacement = user->exp;
726 continue;
728 break;
729 case DISP8:
730 if (user->type == DISP8)
732 displacement = user->exp;
733 continue;
735 break;
736 case FP:
737 if (user->type == RN && user->reg == 6)
739 continue;
741 break;
742 case PCREL16:
743 if (user->type == PCREL16)
745 displacement = user->exp;
746 continue;
748 break;
749 case PCREL8:
750 if (user->type == PCREL8)
752 displacement = user->exp;
753 pcrel8 = 1;
754 continue;
756 break;
758 case IMM16:
759 if (user->type == IMM16
760 || user->type == IMM8)
762 immediate_inpage = user->page;
763 immediate = user->exp;
764 continue;
766 break;
767 case RLIST:
768 case IMM8:
769 if (user->type == IMM8)
771 immediate_inpage = user->page;
772 immediate = user->exp;
773 continue;
775 break;
776 case IMM4:
777 if (user->type == IMM8)
779 immediate_inpage = user->page;
780 immediate = user->exp;
781 continue;
783 break;
784 case QIM:
785 if (user->type == IMM8
786 && user->exp.X_op == O_constant
788 (user->exp.X_add_number == -2
789 || user->exp.X_add_number == -1
790 || user->exp.X_add_number == 1
791 || user->exp.X_add_number == 2))
793 immediate_inpage = user->page;
794 immediate = user->exp;
795 continue;
797 break;
798 case RD:
799 if (user->type == RN)
801 rd = user->reg;
802 continue;
804 break;
805 case RS:
806 if (user->type == RN)
808 rs = user->reg;
809 continue;
811 break;
812 case RDIND:
813 if (user->type == RNIND)
815 rd = user->reg;
816 continue;
819 break;
820 case RNINC:
821 case RNIND:
822 case RNDEC:
823 case RN:
825 if (user->type == this_try->arg_type[i])
827 rn = user->reg;
828 continue;
830 break;
831 case SP:
832 if (user->type == RN && user->reg == 7)
834 continue;
836 break;
837 default:
838 printf (_("unhandled %d\n"), this_try->arg_type[i]);
839 break;
842 /* If we get here this didn't work out */
843 goto fail;
845 found = 1;
846 fail:;
850 if (found)
851 return this_try;
852 else
853 return 0;
856 static int check PARAMS ((expressionS *, int, int));
858 static int
859 check (operand, low, high)
860 expressionS *operand;
861 int low;
862 int high;
864 if (operand->X_op != O_constant
865 || operand->X_add_number < low
866 || operand->X_add_number > high)
868 as_bad (_("operand must be absolute in range %d..%d"), low, high);
870 return operand->X_add_number;
873 static void insert PARAMS ((char *, int, expressionS *, int, int));
875 static void
876 insert (output, index, exp, reloc, pcrel)
877 char *output;
878 int index;
879 expressionS *exp;
880 int reloc;
881 int pcrel;
883 fix_new_exp (frag_now,
884 output - frag_now->fr_literal + index,
885 4, /* always say size is 4, but we know better */
886 exp,
887 pcrel,
888 reloc);
891 static void build_relaxable_instruction
892 PARAMS ((h8500_opcode_info *, h8500_operand_info *));
894 static void
895 build_relaxable_instruction (opcode, operand)
896 h8500_opcode_info *opcode;
897 h8500_operand_info *operand ATTRIBUTE_UNUSED;
899 /* All relaxable instructions start life as two bytes but can become
900 three bytes long if a lonely branch and up to 9 bytes if long
901 scb. */
902 char *p;
903 int len;
904 int type;
906 if (opcode->bytes[0].contents == 0x01)
908 type = SCB_F;
910 else if (opcode->bytes[0].contents == 0x06
911 || opcode->bytes[0].contents == 0x07)
913 type = SCB_TST;
915 else
917 type = BRANCH;
920 p = frag_var (rs_machine_dependent,
921 md_relax_table[C (type, WORD_DISP)].rlx_length,
922 len = md_relax_table[C (type, BYTE_DISP)].rlx_length,
923 C (type, UNDEF_BYTE_DISP),
924 displacement.X_add_symbol,
925 displacement.X_add_number,
928 p[0] = opcode->bytes[0].contents;
929 if (type != BRANCH)
931 p[1] = opcode->bytes[1].contents | rs;
935 /* Now we know what sort of opcodes it is, let's build the bytes. */
937 static void build_bytes PARAMS ((h8500_opcode_info *, h8500_operand_info *));
939 static void
940 build_bytes (opcode, operand)
941 h8500_opcode_info *opcode;
942 h8500_operand_info *operand;
944 int index;
946 if (pcrel8)
948 pcrel8 = 0;
949 build_relaxable_instruction (opcode, operand);
951 else
953 char *output = frag_more (opcode->length);
955 memset (output, 0, opcode->length);
956 for (index = 0; index < opcode->length; index++)
958 output[index] = opcode->bytes[index].contents;
960 switch (opcode->bytes[index].insert)
962 default:
963 printf (_("failed for %d\n"), opcode->bytes[index].insert);
964 break;
965 case 0:
966 break;
967 case RN:
968 output[index] |= rn;
969 break;
970 case RD:
971 case RDIND:
972 output[index] |= rd;
973 break;
974 case RS:
975 output[index] |= rs;
976 break;
977 case DISP16:
978 insert (output, index, &displacement, R_H8500_IMM16, 0);
979 index++;
980 break;
981 case DISP8:
982 case FPIND_D8:
983 insert (output, index, &displacement, R_H8500_IMM8, 0);
984 break;
985 case IMM16:
987 int p;
989 switch (immediate_inpage)
991 case 'p':
992 p = R_H8500_HIGH16;
993 break;
994 case 'h':
995 p = R_H8500_HIGH16;
996 break;
997 default:
998 p = R_H8500_IMM16;
999 break;
1001 insert (output, index, &immediate, p, 0);
1003 index++;
1004 break;
1005 case RLIST:
1006 case IMM8:
1007 if (immediate_inpage)
1008 insert (output, index, &immediate, R_H8500_HIGH8, 0);
1009 else
1010 insert (output, index, &immediate, R_H8500_IMM8, 0);
1011 break;
1012 case PCREL16:
1013 insert (output, index, &displacement, R_H8500_PCREL16, 1);
1014 index++;
1015 break;
1016 case PCREL8:
1017 insert (output, index, &displacement, R_H8500_PCREL8, 1);
1018 break;
1019 case IMM4:
1020 output[index] |= check (&immediate, 0, 15);
1021 break;
1022 case CR:
1023 output[index] |= cr;
1024 if (cr == 0)
1025 output[0] |= 0x8;
1026 else
1027 output[0] &= ~0x8;
1028 break;
1029 case CRB:
1030 output[index] |= crb;
1031 output[0] &= ~0x8;
1032 break;
1033 case CRW:
1034 output[index] |= crw;
1035 output[0] |= 0x8;
1036 break;
1037 case ABS24:
1038 insert (output, index, &absolute, R_H8500_IMM24, 0);
1039 index += 2;
1040 break;
1041 case ABS16:
1042 insert (output, index, &absolute, R_H8500_IMM16, 0);
1043 index++;
1044 break;
1045 case ABS8:
1046 insert (output, index, &absolute, R_H8500_IMM8, 0);
1047 break;
1048 case QIM:
1049 switch (immediate.X_add_number)
1051 case -2:
1052 output[index] |= 0x5;
1053 break;
1054 case -1:
1055 output[index] |= 0x4;
1056 break;
1057 case 1:
1058 output[index] |= 0;
1059 break;
1060 case 2:
1061 output[index] |= 1;
1062 break;
1064 break;
1070 /* This is the guts of the machine-dependent assembler. STR points to
1071 a machine dependent instruction. This function is supposed to emit
1072 the frags/bytes it assembles to. */
1074 void
1075 md_assemble (str)
1076 char *str;
1078 char *op_start;
1079 char *op_end;
1080 h8500_operand_info operand[2];
1081 h8500_opcode_info *opcode;
1082 h8500_opcode_info *prev_opcode;
1083 char name[11];
1085 int nlen = 0;
1087 /* Drop leading whitespace. */
1088 while (*str == ' ')
1089 str++;
1091 /* Find the op code end. */
1092 for (op_start = op_end = str;
1093 !is_end_of_line[(unsigned char) *op_end] && *op_end != ' ';
1094 op_end++)
1096 if ( /**op_end != '.'
1097 && *op_end != ':'
1098 && */ nlen < 10)
1100 name[nlen++] = *op_end;
1103 name[nlen] = 0;
1105 if (op_end == op_start)
1106 as_bad (_("can't find opcode "));
1108 opcode = (h8500_opcode_info *) hash_find (opcode_hash_control, name);
1110 if (opcode == NULL)
1112 as_bad (_("unknown opcode"));
1113 return;
1116 get_operands (opcode, op_end, operand);
1117 prev_opcode = opcode;
1119 opcode = get_specific (opcode, operand);
1121 if (opcode == 0)
1123 /* Couldn't find an opcode which matched the operands */
1124 char *where = frag_more (2);
1126 where[0] = 0x0;
1127 where[1] = 0x0;
1128 as_bad (_("invalid operands for opcode"));
1129 return;
1132 build_bytes (opcode, operand);
1135 void
1136 tc_crawl_symbol_chain (headers)
1137 object_headers *headers ATTRIBUTE_UNUSED;
1139 printf (_("call to tc_crawl_symbol_chain \n"));
1142 symbolS *
1143 md_undefined_symbol (name)
1144 char *name ATTRIBUTE_UNUSED;
1146 return 0;
1149 void
1150 tc_headers_hook (headers)
1151 object_headers *headers ATTRIBUTE_UNUSED;
1153 printf (_("call to tc_headers_hook \n"));
1156 /* Various routines to kill one day. */
1157 /* Equal to MAX_PRECISION in atof-ieee.c. */
1158 #define MAX_LITTLENUMS 6
1160 /* Turn a string in input_line_pointer into a floating point constant
1161 of type type, and store the appropriate bytes in *LITP. The number
1162 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1163 returned, or NULL on OK. */
1165 char *
1166 md_atof (type, litP, sizeP)
1167 char type;
1168 char *litP;
1169 int *sizeP;
1171 int prec;
1172 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1173 LITTLENUM_TYPE *wordP;
1174 char *t;
1176 switch (type)
1178 case 'f':
1179 case 'F':
1180 case 's':
1181 case 'S':
1182 prec = 2;
1183 break;
1185 case 'd':
1186 case 'D':
1187 case 'r':
1188 case 'R':
1189 prec = 4;
1190 break;
1192 case 'x':
1193 case 'X':
1194 prec = 6;
1195 break;
1197 case 'p':
1198 case 'P':
1199 prec = 6;
1200 break;
1202 default:
1203 *sizeP = 0;
1204 return _("Bad call to MD_ATOF()");
1206 t = atof_ieee (input_line_pointer, type, words);
1207 if (t)
1208 input_line_pointer = t;
1210 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1211 for (wordP = words; prec--;)
1213 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1214 litP += sizeof (LITTLENUM_TYPE);
1216 return 0;
1219 const char *md_shortopts = "";
1220 struct option md_longopts[] = {
1221 {NULL, no_argument, NULL, 0}
1223 size_t md_longopts_size = sizeof (md_longopts);
1226 md_parse_option (c, arg)
1227 int c ATTRIBUTE_UNUSED;
1228 char *arg ATTRIBUTE_UNUSED;
1230 return 0;
1233 void
1234 md_show_usage (stream)
1235 FILE *stream ATTRIBUTE_UNUSED;
1239 static void wordify_scb PARAMS ((char *, int *, int *));
1241 static void
1242 wordify_scb (buffer, disp_size, inst_size)
1243 char *buffer;
1244 int *disp_size;
1245 int *inst_size;
1247 int rn = buffer[1] & 0x7;
1249 switch (buffer[0])
1251 case 0x0e: /* BSR */
1252 case 0x20:
1253 case 0x21:
1254 case 0x22:
1255 case 0x23:
1256 case 0x24:
1257 case 0x25:
1258 case 0x26:
1259 case 0x27:
1260 case 0x28:
1261 case 0x29:
1262 case 0x2a:
1263 case 0x2b:
1264 case 0x2c:
1265 case 0x2d:
1266 case 0x2e:
1267 case 0x2f:
1268 buffer[0] |= 0x10;
1269 buffer[1] = 0;
1270 buffer[2] = 0;
1271 *disp_size = 2;
1272 *inst_size = 1;
1273 return;
1274 default:
1275 abort ();
1277 case 0x01:
1278 *inst_size = 6;
1279 *disp_size = 2;
1280 break;
1281 case 0x06:
1282 *inst_size = 8;
1283 *disp_size = 2;
1285 *buffer++ = 0x26; /* bne + 8 */
1286 *buffer++ = 0x08;
1287 break;
1288 case 0x07:
1289 *inst_size = 8;
1290 *disp_size = 2;
1291 *buffer++ = 0x27; /* bne + 8 */
1292 *buffer++ = 0x08;
1293 break;
1296 *buffer++ = 0xa8 | rn; /* addq -1,rn */
1297 *buffer++ = 0x0c;
1298 *buffer++ = 0x04; /* cmp #0xff:8, rn */
1299 *buffer++ = 0xff;
1300 *buffer++ = 0x70 | rn;
1301 *buffer++ = 0x36; /* bne ... */
1302 *buffer++ = 0;
1303 *buffer++ = 0;
1306 /* Called after relaxing, change the frags so they know how big they
1307 are. */
1309 void
1310 md_convert_frag (headers, seg, fragP)
1311 object_headers *headers ATTRIBUTE_UNUSED;
1312 segT seg ATTRIBUTE_UNUSED;
1313 fragS *fragP;
1315 int disp_size = 0;
1316 int inst_size = 0;
1317 char *buffer = fragP->fr_fix + fragP->fr_literal;
1319 switch (fragP->fr_subtype)
1321 case C (BRANCH, BYTE_DISP):
1322 disp_size = 1;
1323 inst_size = 1;
1324 break;
1326 case C (SCB_F, BYTE_DISP):
1327 case C (SCB_TST, BYTE_DISP):
1328 disp_size = 1;
1329 inst_size = 2;
1330 break;
1332 /* Branches to a known 16 bit displacement. */
1334 /* Turn on the 16bit bit. */
1335 case C (BRANCH, WORD_DISP):
1336 case C (SCB_F, WORD_DISP):
1337 case C (SCB_TST, WORD_DISP):
1338 wordify_scb (buffer, &disp_size, &inst_size);
1339 break;
1341 case C (BRANCH, UNDEF_WORD_DISP):
1342 case C (SCB_F, UNDEF_WORD_DISP):
1343 case C (SCB_TST, UNDEF_WORD_DISP):
1344 /* This tried to be relaxed, but didn't manage it, it now needs
1345 a fix. */
1346 wordify_scb (buffer, &disp_size, &inst_size);
1348 /* Make a reloc */
1349 fix_new (fragP,
1350 fragP->fr_fix + inst_size,
1352 fragP->fr_symbol,
1353 fragP->fr_offset,
1355 R_H8500_PCREL16);
1357 fragP->fr_fix += disp_size + inst_size;
1358 return;
1359 break;
1360 default:
1361 abort ();
1363 if (inst_size)
1365 /* Get the address of the end of the instruction */
1366 int next_inst = fragP->fr_fix + fragP->fr_address + disp_size + inst_size;
1367 int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
1368 fragP->fr_offset);
1369 int disp = targ_addr - next_inst;
1371 md_number_to_chars (buffer + inst_size, disp, disp_size);
1372 fragP->fr_fix += disp_size + inst_size;
1376 valueT
1377 md_section_align (seg, size)
1378 segT seg ;
1379 valueT size;
1381 return ((size + (1 << section_alignment[(int) seg]) - 1)
1382 & (-1 << section_alignment[(int) seg]));
1386 void
1387 md_apply_fix3 (fixP, valP, seg)
1388 fixS *fixP;
1389 valueT * valP;
1390 segT seg ATTRIBUTE_UNUSED;
1392 long val = * (long *) valP;
1393 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1395 if (fixP->fx_r_type == 0)
1396 fixP->fx_r_type = fixP->fx_size == 4 ? R_H8500_IMM32 : R_H8500_IMM16;
1398 switch (fixP->fx_r_type)
1400 case R_H8500_IMM8:
1401 case R_H8500_PCREL8:
1402 *buf++ = val;
1403 break;
1404 case R_H8500_IMM16:
1405 case R_H8500_LOW16:
1406 case R_H8500_PCREL16:
1407 *buf++ = (val >> 8);
1408 *buf++ = val;
1409 break;
1410 case R_H8500_HIGH8:
1411 *buf++ = val >> 16;
1412 break;
1413 case R_H8500_HIGH16:
1414 *buf++ = val >> 24;
1415 *buf++ = val >> 16;
1416 break;
1417 case R_H8500_IMM24:
1418 *buf++ = (val >> 16);
1419 *buf++ = (val >> 8);
1420 *buf++ = val;
1421 break;
1422 case R_H8500_IMM32:
1423 *buf++ = (val >> 24);
1424 *buf++ = (val >> 16);
1425 *buf++ = (val >> 8);
1426 *buf++ = val;
1427 break;
1428 default:
1429 abort ();
1432 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1433 fixP->fx_done = 1;
1436 /* Called just before address relaxation, return the length
1437 by which a fragment must grow to reach it's destination. */
1440 md_estimate_size_before_relax (fragP, segment_type)
1441 register fragS *fragP;
1442 register segT segment_type;
1444 int what;
1446 switch (fragP->fr_subtype)
1448 default:
1449 abort ();
1451 case C (BRANCH, UNDEF_BYTE_DISP):
1452 case C (SCB_F, UNDEF_BYTE_DISP):
1453 case C (SCB_TST, UNDEF_BYTE_DISP):
1454 what = GET_WHAT (fragP->fr_subtype);
1455 /* used to be a branch to somewhere which was unknown */
1456 if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1458 /* Got a symbol and it's defined in this segment, become byte
1459 sized - maybe it will fix up. */
1460 fragP->fr_subtype = C (what, BYTE_DISP);
1462 else
1464 /* Its got a segment, but its not ours, so it will always be
1465 long. */
1466 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1468 break;
1470 case C (BRANCH, BYTE_DISP):
1471 case C (BRANCH, WORD_DISP):
1472 case C (BRANCH, UNDEF_WORD_DISP):
1473 case C (SCB_F, BYTE_DISP):
1474 case C (SCB_F, WORD_DISP):
1475 case C (SCB_F, UNDEF_WORD_DISP):
1476 case C (SCB_TST, BYTE_DISP):
1477 case C (SCB_TST, WORD_DISP):
1478 case C (SCB_TST, UNDEF_WORD_DISP):
1479 /* When relaxing a section for the second time, we don't need to
1480 do anything besides return the current size. */
1481 break;
1484 return md_relax_table[fragP->fr_subtype].rlx_length;
1487 /* Put number into target byte order. */
1489 void
1490 md_number_to_chars (ptr, use, nbytes)
1491 char *ptr;
1492 valueT use;
1493 int nbytes;
1495 number_to_chars_bigendian (ptr, use, nbytes);
1498 long
1499 md_pcrel_from (fixP)
1500 fixS *fixP;
1502 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1505 void
1506 tc_coff_symbol_emit_hook (ignore)
1507 symbolS *ignore ATTRIBUTE_UNUSED;
1511 short
1512 tc_coff_fix2rtype (fix_ptr)
1513 fixS *fix_ptr;
1515 if (fix_ptr->fx_r_type == RELOC_32)
1517 /* cons likes to create reloc32's whatever the size of the reloc..
1519 switch (fix_ptr->fx_size)
1521 case 2:
1522 return R_H8500_IMM16;
1523 break;
1524 case 1:
1525 return R_H8500_IMM8;
1526 break;
1527 default:
1528 abort ();
1531 return fix_ptr->fx_r_type;
1534 void
1535 tc_reloc_mangle (fix_ptr, intr, base)
1536 fixS *fix_ptr;
1537 struct internal_reloc *intr;
1538 bfd_vma base;
1541 symbolS *symbol_ptr;
1543 symbol_ptr = fix_ptr->fx_addsy;
1545 /* If this relocation is attached to a symbol then it's ok
1546 to output it */
1547 if (fix_ptr->fx_r_type == RELOC_32)
1549 /* cons likes to create reloc32's whatever the size of the reloc..
1551 switch (fix_ptr->fx_size)
1553 case 2:
1554 intr->r_type = R_IMM16;
1555 break;
1556 case 1:
1557 intr->r_type = R_IMM8;
1558 break;
1559 default:
1560 abort ();
1563 else
1565 intr->r_type = fix_ptr->fx_r_type;
1568 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1569 intr->r_offset = fix_ptr->fx_offset;
1571 /* Turn the segment of the symbol into an offset. */
1572 if (symbol_ptr)
1574 symbolS *dot;
1576 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1577 if (dot)
1579 #if 0
1580 intr->r_offset -=
1581 segment_info[S_GET_SEGMENT (symbol_ptr)].scnhdr.s_paddr;
1582 #endif
1583 intr->r_offset += S_GET_VALUE (symbol_ptr);
1584 intr->r_symndx = dot->sy_number;
1586 else
1588 intr->r_symndx = symbol_ptr->sy_number;
1592 else
1594 intr->r_symndx = -1;
1600 start_label (ptr)
1601 char *ptr;
1603 /* Check for :s.w */
1604 if (ISALPHA (ptr[1]) && ptr[2] == '.')
1605 return 0;
1606 /* Check for :s */
1607 if (ISALPHA (ptr[1]) && !ISALPHA (ptr[2]))
1608 return 0;
1609 return 1;
1613 tc_coff_sizemachdep (frag)
1614 fragS *frag;
1616 return md_relax_table[frag->fr_subtype].rlx_length;