Fix comments.
[binutils-gdb.git] / gas / config / tc-h8500.c
1 /* tc-h8500.c -- Assemble code for the Hitachi H8/500
2 Copyright (C) 1993, 94, 95, 98, 2000 Free Software Foundation.
3
4 This file is part of GAS, the GNU Assembler.
5
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.
10
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.
15
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. */
20
21 /*
22 Written By Steve Chamberlain
23 sac@cygnus.com
24 */
25
26 #include <stdio.h>
27 #include "as.h"
28 #include "bfd.h"
29 #include "subsegs.h"
30 #define DEFINE_TABLE
31 #define ASSEMBLER_TABLE
32 #include "opcodes/h8500-opc.h"
33 #include <ctype.h>
34
35 const char comment_chars[] = "!";
36 const char line_separator_chars[] = ";";
37 const char line_comment_chars[] = "!#";
38
39 /* This table describes all the machine specific pseudo-ops the assembler
40 has to support. The fields are:
41 pseudo-op name without dot
42 function to call to execute this pseudo-op
43 Integer arg to pass to the function
44 */
45
46 void cons ();
47
48 const pseudo_typeS md_pseudo_table[] =
49 {
50 {"int", cons, 2},
51 {"data.b", cons, 1},
52 {"data.w", cons, 2},
53 {"data.l", cons, 4},
54 {"form", listing_psize, 0},
55 {"heading", listing_title, 0},
56 {"import", s_ignore, 0},
57 {"page", listing_eject, 0},
58 {"program", s_ignore, 0},
59 {0, 0, 0}
60 };
61
62 const int md_reloc_size;
63
64 const char EXP_CHARS[] = "eE";
65
66 /* Chars that mean this number is a floating point constant */
67 /* As in 0f12.456 */
68 /* or 0d1.2345e12 */
69 const char FLT_CHARS[] = "rRsSfFdDxXpP";
70
71 #define C(a,b) ENCODE_RELAX(a,b)
72 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
73
74 #define GET_WHAT(x) ((x>>2))
75
76 #define BYTE_DISP 1
77 #define WORD_DISP 2
78 #define UNDEF_BYTE_DISP 0
79 #define UNDEF_WORD_DISP 3
80
81 #define BRANCH 1
82 #define SCB_F 2
83 #define SCB_TST 3
84 #define END 4
85
86 #define BYTE_F 127
87 #define BYTE_B -126
88 #define WORD_F 32767
89 #define WORD_B 32768
90
91 relax_typeS md_relax_table[C (END, 0)];
92
93 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
94
95 /*
96 This function is called once, at assembler startup time. This should
97 set up all the tables, etc. that the MD part of the assembler needs
98 */
99
100 void
101 md_begin ()
102 {
103 h8500_opcode_info *opcode;
104 char prev_buffer[100];
105 int idx = 0;
106 register relax_typeS *table;
107
108 opcode_hash_control = hash_new ();
109 prev_buffer[0] = 0;
110
111 /* Insert unique names into hash table */
112 for (opcode = h8500_table; opcode->name; opcode++)
113 {
114 if (idx != opcode->idx)
115 {
116 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
117 idx++;
118 }
119 }
120
121 /* Initialize the relax table. We use a local variable to avoid
122 warnings about modifying a supposedly const data structure. */
123 table = (relax_typeS *) md_relax_table;
124 table[C (BRANCH, BYTE_DISP)].rlx_forward = BYTE_F;
125 table[C (BRANCH, BYTE_DISP)].rlx_backward = BYTE_B;
126 table[C (BRANCH, BYTE_DISP)].rlx_length = 2;
127 table[C (BRANCH, BYTE_DISP)].rlx_more = C (BRANCH, WORD_DISP);
128
129 table[C (BRANCH, WORD_DISP)].rlx_forward = WORD_F;
130 table[C (BRANCH, WORD_DISP)].rlx_backward = WORD_B;
131 table[C (BRANCH, WORD_DISP)].rlx_length = 3;
132 table[C (BRANCH, WORD_DISP)].rlx_more = 0;
133
134 table[C (SCB_F, BYTE_DISP)].rlx_forward = BYTE_F;
135 table[C (SCB_F, BYTE_DISP)].rlx_backward = BYTE_B;
136 table[C (SCB_F, BYTE_DISP)].rlx_length = 3;
137 table[C (SCB_F, BYTE_DISP)].rlx_more = C (SCB_F, WORD_DISP);
138
139 table[C (SCB_F, WORD_DISP)].rlx_forward = WORD_F;
140 table[C (SCB_F, WORD_DISP)].rlx_backward = WORD_B;
141 table[C (SCB_F, WORD_DISP)].rlx_length = 8;
142 table[C (SCB_F, WORD_DISP)].rlx_more = 0;
143
144 table[C (SCB_TST, BYTE_DISP)].rlx_forward = BYTE_F;
145 table[C (SCB_TST, BYTE_DISP)].rlx_backward = BYTE_B;
146 table[C (SCB_TST, BYTE_DISP)].rlx_length = 3;
147 table[C (SCB_TST, BYTE_DISP)].rlx_more = C (SCB_TST, WORD_DISP);
148
149 table[C (SCB_TST, WORD_DISP)].rlx_forward = WORD_F;
150 table[C (SCB_TST, WORD_DISP)].rlx_backward = WORD_B;
151 table[C (SCB_TST, WORD_DISP)].rlx_length = 10;
152 table[C (SCB_TST, WORD_DISP)].rlx_more = 0;
153
154 }
155
156 static int rn; /* register number used by RN */
157 static int rs; /* register number used by RS */
158 static int rd; /* register number used by RD */
159 static int crb; /* byte size cr */
160 static int crw; /* word sized cr */
161 static int cr; /* unknown size cr */
162
163 static expressionS displacement;/* displacement expression */
164 static int displacement_size; /* and size if given */
165
166 static int immediate_inpage;
167 static expressionS immediate; /* immediate expression */
168 static int immediate_size; /* and size if given */
169
170 static expressionS absolute; /* absolute expression */
171 static int absolute_size; /* and size if given */
172
173 typedef struct
174 {
175 int type;
176 int reg;
177 expressionS exp;
178 int page;
179 }
180
181 h8500_operand_info;
182
183 /* Try to parse a reg name. Return the number of chars consumed. */
184
185 static int
186 parse_reg (src, mode, reg)
187 char *src;
188 int *mode;
189 int *reg;
190 {
191 char *end;
192 int len;
193
194 /* Cribbed from get_symbol_end(). */
195 if (!is_name_beginner (*src) || *src == '\001')
196 return 0;
197 end = src+1;
198 while (is_part_of_name (*end) || *end == '\001')
199 end++;
200 len = end - src;
201
202 if (len == 2 && src[0] == 'r')
203 {
204 if (src[1] >= '0' && src[1] <= '7')
205 {
206 *mode = RN;
207 *reg = (src[1] - '0');
208 return len;
209 }
210 }
211 if (len == 2 && src[0] == 's' && src[1] == 'p')
212 {
213 *mode = RN;
214 *reg = 7;
215 return len;
216 }
217 if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
218 {
219 *mode = CRB;
220 *reg = 1;
221 return len;
222 }
223 if (len == 2 && src[0] == 's' && src[1] == 'r')
224 {
225 *mode = CRW;
226 *reg = 0;
227 return len;
228 }
229 if (len == 2 && src[0] == 'b' && src[1] == 'r')
230 {
231 *mode = CRB;
232 *reg = 3;
233 return len;
234 }
235 if (len == 2 && src[0] == 'e' && src[1] == 'p')
236 {
237 *mode = CRB;
238 *reg = 4;
239 return len;
240 }
241 if (len == 2 && src[0] == 'd' && src[1] == 'p')
242 {
243 *mode = CRB;
244 *reg = 5;
245 return len;
246 }
247 if (len == 2 && src[0] == 't' && src[1] == 'p')
248 {
249 *mode = CRB;
250 *reg = 7;
251 return len;
252 }
253 if (len == 2 && src[0] == 'f' && src[1] == 'p')
254 {
255 *mode = RN;
256 *reg = 6;
257 return len;
258 }
259 return 0;
260 }
261
262 static
263 char *
264 parse_exp (s, op, page)
265 char *s;
266 expressionS *op;
267 int *page;
268 {
269 char *save;
270 char *new;
271
272 save = input_line_pointer;
273
274 *page = 0;
275 if (s[0] == '%')
276 {
277 if (s[1] == 'p' && s[2] == 'a' && s[3] == 'g' && s[4] == 'e')
278 {
279 s += 5;
280 *page = 'p';
281 }
282 if (s[1] == 'h' && s[2] == 'i' && s[3] == '1' && s[4] == '6')
283 {
284 s += 5;
285 *page = 'h';
286 }
287 else if (s[1] == 'o' && s[2] == 'f' && s[3] == 'f')
288 {
289 s += 4;
290 *page = 'o';
291 }
292 }
293
294 input_line_pointer = s;
295
296 expression (op);
297 if (op->X_op == O_absent)
298 as_bad (_("missing operand"));
299 new = input_line_pointer;
300 input_line_pointer = save;
301 return new;
302 }
303
304 typedef enum
305 {
306 exp_signed, exp_unsigned, exp_sandu
307 } sign_type;
308
309
310 static char *
311 skip_colonthing (sign, ptr, exp, def, size8, size16, size24)
312 sign_type sign;
313 char *ptr;
314 h8500_operand_info *exp;
315 int def;
316 int size8;
317 int size16;
318 int size24;
319 {
320 ptr = parse_exp (ptr, &exp->exp, &exp->page);
321 if (*ptr == ':')
322 {
323 ptr++;
324 if (*ptr == '8')
325 {
326 ptr++;
327 exp->type = size8;
328 }
329 else if (ptr[0] == '1' & ptr[1] == '6')
330 {
331 ptr += 2;
332 exp->type = size16;
333 }
334 else if (ptr[0] == '2' & ptr[1] == '4')
335 {
336 if (!size24)
337 {
338 as_bad (_(":24 not valid for this opcode"));
339 }
340 ptr += 2;
341 exp->type = size24;
342 }
343 else
344 {
345 as_bad (_("expect :8,:16 or :24"));
346 exp->type = size16;
347 }
348 }
349 else
350 {
351 if (exp->page == 'p')
352 {
353 exp->type = IMM8;
354 }
355 else if (exp->page == 'h')
356 {
357 exp->type = IMM16;
358 }
359 else
360 {
361 /* Let's work out the size from the context */
362 int n = exp->exp.X_add_number;
363 if (size8
364 && exp->exp.X_op == O_constant
365 && ((sign == exp_signed && (n >= -128 && n <= 127))
366 || (sign == exp_unsigned && (n >= 0 && (n <= 255)))
367 || (sign == exp_sandu && (n >= -128 && (n <= 255)))))
368 {
369 exp->type = size8;
370 }
371 else
372 {
373 exp->type = def;
374 }
375 }
376 }
377 return ptr;
378 }
379
380 static int
381 parse_reglist (src, op)
382 char *src;
383 h8500_operand_info *op;
384 {
385 int mode;
386 int rn;
387 int mask = 0;
388 int rm;
389 int idx = 1; /* skip ( */
390
391 while (src[idx] && src[idx] != ')')
392 {
393 int done = parse_reg (src + idx, &mode, &rn);
394
395 if (done)
396 {
397 idx += done;
398 mask |= 1 << rn;
399 }
400 else
401 {
402 as_bad (_("syntax error in reg list"));
403 return 0;
404 }
405 if (src[idx] == '-')
406 {
407 idx++;
408 done = parse_reg (src + idx, &mode, &rm);
409 if (done)
410 {
411 idx += done;
412 while (rn <= rm)
413 {
414 mask |= 1 << rn;
415 rn++;
416 }
417 }
418 else
419 {
420 as_bad (_("missing final register in range"));
421 }
422 }
423 if (src[idx] == ',')
424 idx++;
425 }
426 idx++;
427 op->exp.X_add_symbol = 0;
428 op->exp.X_op_symbol = 0;
429 op->exp.X_add_number = mask;
430 op->exp.X_op = O_constant;
431 op->exp.X_unsigned = 1;
432 op->type = IMM8;
433 return idx;
434
435 }
436
437 /* The many forms of operand:
438
439 Rn Register direct
440 @Rn Register indirect
441 @(disp[:size], Rn) Register indirect with displacement
442 @Rn+
443 @-Rn
444 @aa[:size] absolute
445 #xx[:size] immediate data
446
447 */
448
449 static void
450 get_operand (ptr, op, ispage)
451 char **ptr;
452 h8500_operand_info *op;
453 char ispage;
454 {
455 char *src = *ptr;
456 int mode;
457 unsigned int num;
458 unsigned int len;
459 op->page = 0;
460 if (src[0] == '(' && src[1] == 'r')
461 {
462 /* This is a register list */
463 *ptr = src + parse_reglist (src, op);
464 return;
465 }
466
467 len = parse_reg (src, &op->type, &op->reg);
468
469 if (len)
470 {
471 *ptr = src + len;
472 return;
473 }
474
475 if (*src == '@')
476 {
477 src++;
478 if (*src == '-')
479 {
480 src++;
481 len = parse_reg (src, &mode, &num);
482 if (len == 0)
483 {
484 /* Oops, not a reg after all, must be ordinary exp */
485 src--;
486 /* must be a symbol */
487 *ptr = skip_colonthing (exp_unsigned, src,
488 op, ABS16, ABS8, ABS16, ABS24);
489 return;
490 }
491
492 op->type = RNDEC;
493 op->reg = num;
494 *ptr = src + len;
495 return;
496 }
497 if (*src == '(')
498 {
499 /* Disp */
500 src++;
501
502 src = skip_colonthing (exp_signed, src,
503 op, RNIND_D16, RNIND_D8, RNIND_D16, 0);
504
505 if (*src != ',')
506 {
507 as_bad (_("expected @(exp, Rn)"));
508 return;
509 }
510 src++;
511 len = parse_reg (src, &mode, &op->reg);
512 if (len == 0 || mode != RN)
513 {
514 as_bad (_("expected @(exp, Rn)"));
515 return;
516 }
517 src += len;
518 if (*src != ')')
519 {
520 as_bad (_("expected @(exp, Rn)"));
521 return;
522 }
523 *ptr = src + 1;
524 return;
525 }
526 len = parse_reg (src, &mode, &num);
527
528 if (len)
529 {
530 src += len;
531 if (*src == '+')
532 {
533 src++;
534 if (mode != RN)
535 {
536 as_bad (_("@Rn+ needs word register"));
537 return;
538 }
539 op->type = RNINC;
540 op->reg = num;
541 *ptr = src;
542 return;
543 }
544 if (mode != RN)
545 {
546 as_bad (_("@Rn needs word register"));
547 return;
548 }
549 op->type = RNIND;
550 op->reg = num;
551 *ptr = src;
552 return;
553 }
554 else
555 {
556 /* must be a symbol */
557 *ptr =
558 skip_colonthing (exp_unsigned, src, op,
559 ispage ? ABS24 : ABS16, ABS8, ABS16, ABS24);
560 return;
561 }
562 }
563
564 if (*src == '#')
565 {
566 src++;
567 *ptr = skip_colonthing (exp_sandu, src, op, IMM16, IMM8, IMM16, ABS24);
568 return;
569 }
570 else
571 {
572 *ptr = skip_colonthing (exp_signed, src, op,
573 ispage ? ABS24 : PCREL8, PCREL8, PCREL16, ABS24);
574 }
575 }
576
577 static
578 char *
579 get_operands (info, args, operand)
580 h8500_opcode_info *info;
581 char *args;
582 h8500_operand_info *operand;
583
584 {
585 char *ptr = args;
586
587 switch (info->nargs)
588 {
589 case 0:
590 operand[0].type = 0;
591 operand[1].type = 0;
592 break;
593
594 case 1:
595 ptr++;
596 get_operand (&ptr, operand + 0, info->name[0] == 'p');
597 operand[1].type = 0;
598 break;
599
600 case 2:
601 ptr++;
602 get_operand (&ptr, operand + 0, 0);
603 if (*ptr == ',')
604 ptr++;
605 get_operand (&ptr, operand + 1, 0);
606 break;
607
608 default:
609 abort ();
610 }
611
612 return ptr;
613 }
614
615 /* Passed a pointer to a list of opcodes which use different
616 addressing modes, return the opcode which matches the opcodes
617 provided
618 */
619
620 int pcrel8; /* Set when we've seen a pcrel operand */
621
622 static
623 h8500_opcode_info *
624 get_specific (opcode, operands)
625 h8500_opcode_info *opcode;
626 h8500_operand_info *operands;
627 {
628 h8500_opcode_info *this_try = opcode;
629 int found = 0;
630 unsigned int noperands = opcode->nargs;
631
632 unsigned int this_index = opcode->idx;
633
634 while (this_index == opcode->idx && !found)
635 {
636 unsigned int i;
637
638 this_try = opcode++;
639
640 /* look at both operands needed by the opcodes and provided by
641 the user*/
642 for (i = 0; i < noperands; i++)
643 {
644 h8500_operand_info *user = operands + i;
645
646 switch (this_try->arg_type[i])
647 {
648 case FPIND_D8:
649 /* Opcode needs (disp:8,fp) */
650 if (user->type == RNIND_D8 && user->reg == 6)
651 {
652 displacement = user->exp;
653 continue;
654 }
655 break;
656 case RDIND_D16:
657 if (user->type == RNIND_D16)
658 {
659 displacement = user->exp;
660 rd = user->reg;
661 continue;
662 }
663 break;
664 case RDIND_D8:
665 if (user->type == RNIND_D8)
666 {
667 displacement = user->exp;
668 rd = user->reg;
669 continue;
670 }
671 break;
672 case RNIND_D16:
673 case RNIND_D8:
674 if (user->type == this_try->arg_type[i])
675 {
676 displacement = user->exp;
677 rn = user->reg;
678 continue;
679 }
680 break;
681
682 case SPDEC:
683 if (user->type == RNDEC && user->reg == 7)
684 {
685 continue;
686 }
687 break;
688 case SPINC:
689 if (user->type == RNINC && user->reg == 7)
690 {
691 continue;
692 }
693 break;
694 case ABS16:
695 if (user->type == ABS16)
696 {
697 absolute = user->exp;
698 continue;
699 }
700 break;
701 case ABS8:
702 if (user->type == ABS8)
703 {
704 absolute = user->exp;
705 continue;
706 }
707 break;
708 case ABS24:
709 if (user->type == ABS24)
710 {
711 absolute = user->exp;
712 continue;
713 }
714 break;
715
716 case CRB:
717 if ((user->type == CRB || user->type == CR) && user->reg != 0)
718 {
719 crb = user->reg;
720 continue;
721 }
722 break;
723 case CRW:
724 if ((user->type == CRW || user->type == CR) && user->reg == 0)
725 {
726 crw = user->reg;
727 continue;
728 }
729 break;
730 case DISP16:
731 if (user->type == DISP16)
732 {
733 displacement = user->exp;
734 continue;
735 }
736 break;
737 case DISP8:
738 if (user->type == DISP8)
739 {
740 displacement = user->exp;
741 continue;
742 }
743 break;
744 case FP:
745 if (user->type == RN && user->reg == 6)
746 {
747 continue;
748 }
749 break;
750 case PCREL16:
751 if (user->type == PCREL16)
752 {
753 displacement = user->exp;
754 continue;
755 }
756 break;
757 case PCREL8:
758 if (user->type == PCREL8)
759 {
760 displacement = user->exp;
761 pcrel8 = 1;
762 continue;
763 }
764 break;
765
766 case IMM16:
767 if (user->type == IMM16
768 || user->type == IMM8)
769 {
770 immediate_inpage = user->page;
771 immediate = user->exp;
772 continue;
773 }
774 break;
775 case RLIST:
776 case IMM8:
777 if (user->type == IMM8)
778 {
779 immediate_inpage = user->page;
780 immediate = user->exp;
781 continue;
782 }
783 break;
784 case IMM4:
785 if (user->type == IMM8)
786 {
787 immediate_inpage = user->page;
788 immediate = user->exp;
789 continue;
790 }
791 break;
792 case QIM:
793 if (user->type == IMM8
794 && user->exp.X_op == O_constant
795 &&
796 (user->exp.X_add_number == -2
797 || user->exp.X_add_number == -1
798 || user->exp.X_add_number == 1
799 || user->exp.X_add_number == 2))
800 {
801 immediate_inpage = user->page;
802 immediate = user->exp;
803 continue;
804 }
805 break;
806 case RD:
807 if (user->type == RN)
808 {
809 rd = user->reg;
810 continue;
811 }
812 break;
813 case RS:
814 if (user->type == RN)
815 {
816 rs = user->reg;
817 continue;
818 }
819 break;
820 case RDIND:
821 if (user->type == RNIND)
822 {
823 rd = user->reg;
824 continue;
825
826 }
827 break;
828 case RNINC:
829 case RNIND:
830 case RNDEC:
831 case RN:
832
833 if (user->type == this_try->arg_type[i])
834 {
835 rn = user->reg;
836 continue;
837 }
838 break;
839 case SP:
840 if (user->type == RN && user->reg == 7)
841 {
842 continue;
843 }
844 break;
845 default:
846 printf (_("unhandled %d\n"), this_try->arg_type[i]);
847 break;
848 }
849
850 /* If we get here this didn't work out */
851 goto fail;
852 }
853 found = 1;
854 fail:;
855
856 }
857
858 if (found)
859 return this_try;
860 else
861 return 0;
862 }
863
864 int
865 check (operand, low, high)
866 expressionS *operand;
867 int low;
868 int high;
869 {
870 if (operand->X_op != O_constant
871 || operand->X_add_number < low
872 || operand->X_add_number > high)
873 {
874 as_bad (_("operand must be absolute in range %d..%d"), low, high);
875 }
876 return operand->X_add_number;
877 }
878
879 static
880 void
881 insert (output, index, exp, reloc, pcrel)
882 char *output;
883 int index;
884 expressionS *exp;
885 int reloc;
886 int pcrel;
887 {
888 fix_new_exp (frag_now,
889 output - frag_now->fr_literal + index,
890 4, /* always say size is 4, but we know better */
891 exp,
892 pcrel,
893 reloc);
894 }
895
896 void
897 build_relaxable_instruction (opcode, operand)
898 h8500_opcode_info *opcode;
899 h8500_operand_info *operand;
900 {
901 /* All relaxable instructions start life as two bytes but can become
902 three bytes long if a lonely branch and up to 9 bytes if long scb
903 */
904 char *p;
905 int len;
906 int type;
907
908 if (opcode->bytes[0].contents == 0x01)
909 {
910 type = SCB_F;
911 }
912 else if (opcode->bytes[0].contents == 0x06
913 || opcode->bytes[0].contents == 0x07)
914 {
915 type = SCB_TST;
916 }
917 else
918 {
919 type = BRANCH;
920 }
921
922 p = frag_var (rs_machine_dependent,
923 md_relax_table[C (type, WORD_DISP)].rlx_length,
924 len = md_relax_table[C (type, BYTE_DISP)].rlx_length,
925 C (type, UNDEF_BYTE_DISP),
926 displacement.X_add_symbol,
927 displacement.X_add_number,
928 0);
929
930 p[0] = opcode->bytes[0].contents;
931 if (type != BRANCH)
932 {
933 p[1] = opcode->bytes[1].contents | rs;
934 }
935 }
936
937 /* Now we know what sort of opcodes it is, let's build the bytes -
938 */
939 static void
940 build_bytes (opcode, operand)
941 h8500_opcode_info *opcode;
942 h8500_operand_info *operand;
943
944 {
945 int index;
946
947 if (pcrel8)
948 {
949 pcrel8 = 0;
950 build_relaxable_instruction (opcode, operand);
951 }
952 else
953 {
954 char *output = frag_more (opcode->length);
955
956 memset (output, 0, opcode->length);
957 for (index = 0; index < opcode->length; index++)
958 {
959 output[index] = opcode->bytes[index].contents;
960
961 switch (opcode->bytes[index].insert)
962 {
963 default:
964 printf (_("failed for %d\n"), opcode->bytes[index].insert);
965 break;
966 case 0:
967 break;
968 case RN:
969 output[index] |= rn;
970 break;
971 case RD:
972 case RDIND:
973
974 output[index] |= rd;
975 break;
976 case RS:
977 output[index] |= rs;
978 break;
979 case DISP16:
980 insert (output, index, &displacement, R_H8500_IMM16, 0);
981 index++;
982 break;
983 case DISP8:
984 case FPIND_D8:
985 insert (output, index, &displacement, R_H8500_IMM8, 0);
986 break;
987
988 case IMM16:
989 {
990 int p;
991 switch (immediate_inpage) {
992 case 'p':
993 p = R_H8500_HIGH16;
994 break;
995 case 'h':
996 p = R_H8500_HIGH16;
997 break;
998 default:
999 p = R_H8500_IMM16;
1000 break;
1001 }
1002
1003 insert (output, index, &immediate,p, 0);
1004 }
1005
1006 index++;
1007 break;
1008 case RLIST:
1009 case IMM8:
1010 if (immediate_inpage)
1011 {
1012 insert (output, index, &immediate, R_H8500_HIGH8, 0);
1013 }
1014 else
1015 {
1016 insert (output, index, &immediate, R_H8500_IMM8, 0);
1017 }
1018 break;
1019 case PCREL16:
1020 insert (output, index, &displacement, R_H8500_PCREL16, 1);
1021 index++;
1022 break;
1023 case PCREL8:
1024 insert (output, index, &displacement, R_H8500_PCREL8, 1);
1025 break;
1026 case IMM4:
1027 output[index] |= check (&immediate, 0, 15);
1028 break;
1029 case CR:
1030
1031 output[index] |= cr;
1032 if (cr == 0)
1033 {
1034 output[0] |= 0x8;
1035 }
1036 else
1037 {
1038 output[0] &= ~0x8;
1039 }
1040
1041 break;
1042
1043 case CRB:
1044 output[index] |= crb;
1045 output[0] &= ~0x8;
1046 break;
1047 case CRW:
1048 output[index] |= crw;
1049 output[0] |= 0x8;
1050 break;
1051 case ABS24:
1052 insert (output, index, &absolute, R_H8500_IMM24, 0);
1053 index += 2;
1054 break;
1055 case ABS16:
1056 insert (output, index, &absolute, R_H8500_IMM16, 0);
1057 index++;
1058 break;
1059 case ABS8:
1060 insert (output, index, &absolute, R_H8500_IMM8, 0);
1061 break;
1062 case QIM:
1063 switch (immediate.X_add_number)
1064 {
1065 case -2:
1066 output[index] |= 0x5;
1067 break;
1068 case -1:
1069 output[index] |= 0x4;
1070 break;
1071 case 1:
1072 output[index] |= 0;
1073 break;
1074 case 2:
1075 output[index] |= 1;
1076 break;
1077 }
1078 break;
1079 }
1080 }
1081 }
1082 }
1083
1084 /* This is the guts of the machine-dependent assembler. STR points to a
1085 machine dependent instruction. This function is supposed to emit
1086 the frags/bytes it assembles to.
1087 */
1088
1089 void
1090 md_assemble (str)
1091 char *str;
1092 {
1093 char *op_start;
1094 char *op_end;
1095 h8500_operand_info operand[2];
1096 h8500_opcode_info *opcode;
1097 h8500_opcode_info *prev_opcode;
1098 char name[11];
1099
1100 int nlen = 0;
1101
1102 /* Drop leading whitespace */
1103 while (*str == ' ')
1104 str++;
1105
1106 /* find the op code end */
1107 for (op_start = op_end = str;
1108 !is_end_of_line[(unsigned char) *op_end] && *op_end != ' ';
1109 op_end++)
1110 {
1111 if ( /**op_end != '.'
1112 && *op_end != ':'
1113 && */ nlen < 10)
1114 {
1115 name[nlen++] = *op_end;
1116 }
1117 }
1118 name[nlen] = 0;
1119
1120 if (op_end == op_start)
1121 {
1122 as_bad (_("can't find opcode "));
1123 }
1124
1125 opcode = (h8500_opcode_info *) hash_find (opcode_hash_control, name);
1126
1127 if (opcode == NULL)
1128 {
1129 as_bad (_("unknown opcode"));
1130 return;
1131 }
1132
1133 get_operands (opcode, op_end, operand);
1134 prev_opcode = opcode;
1135
1136 opcode = get_specific (opcode, operand);
1137
1138 if (opcode == 0)
1139 {
1140 /* Couldn't find an opcode which matched the operands */
1141 char *where = frag_more (2);
1142
1143 where[0] = 0x0;
1144 where[1] = 0x0;
1145 as_bad (_("invalid operands for opcode"));
1146 return;
1147 }
1148
1149 build_bytes (opcode, operand);
1150
1151 }
1152
1153 void
1154 tc_crawl_symbol_chain (headers)
1155 object_headers *headers;
1156 {
1157 printf (_("call to tc_crawl_symbol_chain \n"));
1158 }
1159
1160 symbolS *
1161 md_undefined_symbol (name)
1162 char *name;
1163 {
1164 return 0;
1165 }
1166
1167 void
1168 tc_headers_hook (headers)
1169 object_headers *headers;
1170 {
1171 printf (_("call to tc_headers_hook \n"));
1172 }
1173
1174 /* Various routines to kill one day */
1175 /* Equal to MAX_PRECISION in atof-ieee.c */
1176 #define MAX_LITTLENUMS 6
1177
1178 /* Turn a string in input_line_pointer into a floating point constant
1179 of type type, and store the appropriate bytes in *LITP. The number
1180 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1181 returned, or NULL on OK. */
1182
1183 char *
1184 md_atof (type, litP, sizeP)
1185 char type;
1186 char *litP;
1187 int *sizeP;
1188 {
1189 int prec;
1190 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1191 LITTLENUM_TYPE *wordP;
1192 char *t;
1193 char *atof_ieee ();
1194
1195 switch (type)
1196 {
1197 case 'f':
1198 case 'F':
1199 case 's':
1200 case 'S':
1201 prec = 2;
1202 break;
1203
1204 case 'd':
1205 case 'D':
1206 case 'r':
1207 case 'R':
1208 prec = 4;
1209 break;
1210
1211 case 'x':
1212 case 'X':
1213 prec = 6;
1214 break;
1215
1216 case 'p':
1217 case 'P':
1218 prec = 6;
1219 break;
1220
1221 default:
1222 *sizeP = 0;
1223 return _("Bad call to MD_ATOF()");
1224 }
1225 t = atof_ieee (input_line_pointer, type, words);
1226 if (t)
1227 input_line_pointer = t;
1228
1229 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1230 for (wordP = words; prec--;)
1231 {
1232 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1233 litP += sizeof (LITTLENUM_TYPE);
1234 }
1235 return 0;
1236 }
1237 \f
1238 CONST char *md_shortopts = "";
1239 struct option md_longopts[] = {
1240 {NULL, no_argument, NULL, 0}
1241 };
1242 size_t md_longopts_size = sizeof(md_longopts);
1243
1244 int
1245 md_parse_option (c, arg)
1246 int c;
1247 char *arg;
1248 {
1249 return 0;
1250 }
1251
1252 void
1253 md_show_usage (stream)
1254 FILE *stream;
1255 {
1256 }
1257 \f
1258 void
1259 tc_aout_fix_to_chars ()
1260 {
1261 printf (_("call to tc_aout_fix_to_chars \n"));
1262 abort ();
1263 }
1264
1265 static
1266 void
1267 wordify_scb (buffer, disp_size, inst_size)
1268 char *buffer;
1269 int *disp_size;
1270 int *inst_size;
1271 {
1272 int rn = buffer[1] & 0x7;
1273
1274 switch (buffer[0])
1275 {
1276 case 0x0e: /* BSR */
1277 case 0x20:
1278 case 0x21:
1279 case 0x22:
1280 case 0x23:
1281 case 0x24:
1282 case 0x25:
1283 case 0x26:
1284 case 0x27:
1285 case 0x28:
1286 case 0x29:
1287 case 0x2a:
1288 case 0x2b:
1289 case 0x2c:
1290 case 0x2d:
1291 case 0x2e:
1292 case 0x2f:
1293 buffer[0] |= 0x10;
1294 buffer[1] = 0;
1295 buffer[2] = 0;
1296 *disp_size = 2;
1297 *inst_size = 1;
1298 return;
1299 default:
1300 abort ();
1301
1302 case 0x01:
1303 *inst_size = 6;
1304 *disp_size = 2;
1305 break;
1306 case 0x06:
1307 *inst_size = 8;
1308 *disp_size = 2;
1309
1310 *buffer++ = 0x26; /* bne + 8 */
1311 *buffer++ = 0x08;
1312 break;
1313 case 0x07:
1314 *inst_size = 8;
1315 *disp_size = 2;
1316 *buffer++ = 0x27; /* bne + 8 */
1317 *buffer++ = 0x08;
1318 break;
1319
1320 }
1321 *buffer++ = 0xa8 | rn; /* addq -1,rn */
1322 *buffer++ = 0x0c;
1323 *buffer++ = 0x04; /* cmp #0xff:8, rn */
1324 *buffer++ = 0xff;
1325 *buffer++ = 0x70 | rn;
1326 *buffer++ = 0x36; /* bne ... */
1327 *buffer++ = 0;
1328 *buffer++ = 0;
1329 }
1330
1331 /*
1332 called after relaxing, change the frags so they know how big they are
1333 */
1334 void
1335 md_convert_frag (headers, seg, fragP)
1336 object_headers *headers;
1337 segT seg;
1338 fragS *fragP;
1339 {
1340 int disp_size = 0;
1341 int inst_size = 0;
1342 char *buffer = fragP->fr_fix + fragP->fr_literal;
1343
1344 switch (fragP->fr_subtype)
1345 {
1346 case C (BRANCH, BYTE_DISP):
1347 disp_size = 1;
1348 inst_size = 1;
1349 break;
1350
1351 case C (SCB_F, BYTE_DISP):
1352 case C (SCB_TST, BYTE_DISP):
1353 disp_size = 1;
1354 inst_size = 2;
1355 break;
1356
1357 /* Branches to a known 16 bit displacement */
1358
1359 /* Turn on the 16bit bit */
1360 case C (BRANCH, WORD_DISP):
1361 case C (SCB_F, WORD_DISP):
1362 case C (SCB_TST, WORD_DISP):
1363 wordify_scb (buffer, &disp_size, &inst_size);
1364 break;
1365
1366 case C (BRANCH, UNDEF_WORD_DISP):
1367 case C (SCB_F, UNDEF_WORD_DISP):
1368 case C (SCB_TST, UNDEF_WORD_DISP):
1369 /* This tried to be relaxed, but didn't manage it, it now needs a
1370 fix */
1371 wordify_scb (buffer, &disp_size, &inst_size);
1372
1373 /* Make a reloc */
1374 fix_new (fragP,
1375 fragP->fr_fix + inst_size,
1376 4,
1377 fragP->fr_symbol,
1378 fragP->fr_offset,
1379 0,
1380 R_H8500_PCREL16);
1381
1382 fragP->fr_fix += disp_size + inst_size;
1383 fragP->fr_var = 0;
1384 return;
1385 break;
1386 default:
1387 abort ();
1388 }
1389 if (inst_size)
1390 {
1391 /* Get the address of the end of the instruction */
1392 int next_inst = fragP->fr_fix + fragP->fr_address + disp_size + inst_size;
1393 int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
1394 fragP->fr_offset);
1395 int disp = targ_addr - next_inst;
1396
1397 md_number_to_chars (buffer + inst_size, disp, disp_size);
1398 fragP->fr_fix += disp_size + inst_size;
1399 fragP->fr_var = 0;
1400 }
1401 }
1402
1403 valueT
1404 md_section_align (seg, size)
1405 segT seg ;
1406 valueT size;
1407 {
1408 return ((size + (1 << section_alignment[(int) seg]) - 1)
1409 & (-1 << section_alignment[(int) seg]));
1410
1411 }
1412
1413 void
1414 md_apply_fix (fixP, val)
1415 fixS *fixP;
1416 long val;
1417 {
1418 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1419
1420 if (fixP->fx_r_type == 0)
1421 {
1422 fixP->fx_r_type = fixP->fx_size == 4 ? R_H8500_IMM32 : R_H8500_IMM16;
1423 }
1424
1425 switch (fixP->fx_r_type)
1426 {
1427
1428 case R_H8500_IMM8:
1429 case R_H8500_PCREL8:
1430 *buf++ = val;
1431 break;
1432 case R_H8500_IMM16:
1433 case R_H8500_LOW16:
1434 case R_H8500_PCREL16:
1435 *buf++ = (val >> 8);
1436 *buf++ = val;
1437 break;
1438 case R_H8500_HIGH8:
1439 *buf++ = val >> 16;
1440 break;
1441 case R_H8500_HIGH16:
1442 *buf++ = val >> 24;
1443 *buf++ = val >> 16;
1444 break;
1445 case R_H8500_IMM24:
1446 *buf++ = (val >> 16);
1447 *buf++ = (val >> 8);
1448 *buf++ = val;
1449 break;
1450 case R_H8500_IMM32:
1451 *buf++ = (val >> 24);
1452 *buf++ = (val >> 16);
1453 *buf++ = (val >> 8);
1454 *buf++ = val;
1455 break;
1456 default:
1457 abort ();
1458
1459 }
1460 }
1461
1462 /*
1463 called just before address relaxation, return the length
1464 by which a fragment must grow to reach it's destination
1465 */
1466 int
1467 md_estimate_size_before_relax (fragP, segment_type)
1468 register fragS *fragP;
1469 register segT segment_type;
1470 {
1471 int what = GET_WHAT (fragP->fr_subtype);
1472
1473 switch (fragP->fr_subtype)
1474 {
1475 default:
1476 abort ();
1477 case C (BRANCH, UNDEF_BYTE_DISP):
1478 case C (SCB_F, UNDEF_BYTE_DISP):
1479 case C (SCB_TST, UNDEF_BYTE_DISP):
1480 /* used to be a branch to somewhere which was unknown */
1481 if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1482 {
1483 /* Got a symbol and it's defined in this segment, become byte
1484 sized - maybe it will fix up */
1485 fragP->fr_subtype = C (what, BYTE_DISP);
1486 fragP->fr_var = md_relax_table[C (what, BYTE_DISP)].rlx_length;
1487 }
1488 else
1489 {
1490 /* Its got a segment, but its not ours, so it will always be long */
1491 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1492 fragP->fr_var = md_relax_table[C (what, WORD_DISP)].rlx_length;
1493 return md_relax_table[C (what, WORD_DISP)].rlx_length;
1494 }
1495 }
1496 return fragP->fr_var;
1497 }
1498
1499 /* Put number into target byte order */
1500
1501 void
1502 md_number_to_chars (ptr, use, nbytes)
1503 char *ptr;
1504 valueT use;
1505 int nbytes;
1506 {
1507 number_to_chars_bigendian (ptr, use, nbytes);
1508 }
1509
1510 long
1511 md_pcrel_from (fixP)
1512 fixS *fixP;
1513 {
1514 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1515 }
1516
1517 /*ARGSUSED*/
1518 void
1519 tc_coff_symbol_emit_hook (ignore)
1520 symbolS *ignore;
1521 {
1522 }
1523
1524 short
1525 tc_coff_fix2rtype (fix_ptr)
1526 fixS *fix_ptr;
1527 {
1528 if (fix_ptr->fx_r_type == RELOC_32)
1529 {
1530 /* cons likes to create reloc32's whatever the size of the reloc..
1531 */
1532 switch (fix_ptr->fx_size)
1533 {
1534 case 2:
1535 return R_H8500_IMM16;
1536 break;
1537 case 1:
1538 return R_H8500_IMM8;
1539 break;
1540 default:
1541 abort ();
1542 }
1543 }
1544 return fix_ptr->fx_r_type;
1545 }
1546
1547 void
1548 tc_reloc_mangle (fix_ptr, intr, base)
1549 fixS *fix_ptr;
1550 struct internal_reloc *intr;
1551 bfd_vma base;
1552
1553 {
1554 symbolS *symbol_ptr;
1555
1556 symbol_ptr = fix_ptr->fx_addsy;
1557
1558 /* If this relocation is attached to a symbol then it's ok
1559 to output it */
1560 if (fix_ptr->fx_r_type == RELOC_32)
1561 {
1562 /* cons likes to create reloc32's whatever the size of the reloc..
1563 */
1564 switch (fix_ptr->fx_size)
1565 {
1566 case 2:
1567 intr->r_type = R_IMM16;
1568 break;
1569 case 1:
1570 intr->r_type = R_IMM8;
1571 break;
1572 default:
1573 abort ();
1574 }
1575 }
1576 else
1577 {
1578 intr->r_type = fix_ptr->fx_r_type;
1579 }
1580
1581 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1582 intr->r_offset = fix_ptr->fx_offset;
1583
1584 /* Turn the segment of the symbol into an offset. */
1585 if (symbol_ptr)
1586 {
1587 symbolS *dot;
1588
1589 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1590 if (dot)
1591 {
1592 /* intr->r_offset -=
1593 segment_info[S_GET_SEGMENT(symbol_ptr)].scnhdr.s_paddr;*/
1594 intr->r_offset += S_GET_VALUE (symbol_ptr);
1595 intr->r_symndx = dot->sy_number;
1596 }
1597 else
1598 {
1599 intr->r_symndx = symbol_ptr->sy_number;
1600 }
1601
1602 }
1603 else
1604 {
1605 intr->r_symndx = -1;
1606 }
1607
1608 }
1609
1610
1611
1612 int
1613 start_label (ptr)
1614 char *ptr;
1615 {
1616 /* Check for :s.w */
1617 if (isalpha (ptr[1]) && ptr[2] == '.')
1618 return 0;
1619 /* Check for :s */
1620 if (isalpha (ptr[1]) && !isalpha (ptr[2]))
1621 return 0;
1622 return 1;
1623 }
1624
1625
1626 int
1627 tc_coff_sizemachdep (frag)
1628 fragS *frag;
1629 {
1630 return md_relax_table[frag->fr_subtype].rlx_length;
1631 }
1632
1633 /* end of tc-h8500.c */
1634