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