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