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