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