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