* config/tc-mips.c (move_register): New function.
[deliverable/binutils-gdb.git] / gas / config / tc-w65.c
1 /* tc-w65.c -- Assemble code for the W65816
2 Copyright 1995, 1998, 2000, 2001 Free Software Foundation, Inc.
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 /* Written By Steve Chamberlain <sac@cygnus.com>. */
22
23 #include <stdio.h>
24 #include "as.h"
25 #include "bfd.h"
26 #include "subsegs.h"
27 #define DEFINE_TABLE
28 #include "../opcodes/w65-opc.h"
29 #include <ctype.h>
30
31 const char comment_chars[] = "!";
32 CONST char line_separator_chars[] = ";";
33 const char line_comment_chars[] = "!#";
34
35 /* This table describes all the machine specific pseudo-ops the assembler
36 has to support. The fields are:
37
38 pseudo-op name without dot
39 function to call to execute this pseudo-op
40 Integer arg to pass to the function */
41
42 #define OP_BCC 0x90
43 #define OP_BCS 0xB0
44 #define OP_BEQ 0xF0
45 #define OP_BMI 0x30
46 #define OP_BNE 0xD0
47 #define OP_BPL 0x10
48 #define OP_BRA 0x80
49 #define OP_BRL 0x82
50 #define OP_BVC 0x50
51 #define OP_BVS 0x70
52
53 void s_longa ();
54
55 const pseudo_typeS md_pseudo_table[] = {
56 {"int", cons, 2},
57 {"word", cons, 2},
58 {"longa", s_longa, 0},
59 {"longi", s_longa, 1},
60 {0, 0, 0}
61 };
62
63 void cons ();
64 void s_align_bytes ();
65
66 #if 0
67 int md_reloc_size;
68 #endif
69
70 static int relax; /* set if -relax seen */
71
72 const char EXP_CHARS[] = "eE";
73
74 /* Chars that mean this number is a floating point constant. */
75 /* As in 0f12.456 */
76 /* or 0d1.2345e12 */
77 const char FLT_CHARS[] = "rRsSfFdDxXpP";
78
79 /* Opcode mnemonics */
80 static struct hash_control *opcode_hash_control;
81
82 int M; /* M flag */
83 int X; /* X flag */
84
85 #define C(a,b) ENCODE_RELAX(a,b)
86 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
87
88 #define GET_WHAT(x) ((x>>2))
89
90 #define BYTE_DISP 1
91 #define WORD_DISP 2
92 #define UNDEF_BYTE_DISP 0
93 #define UNDEF_WORD_DISP 3
94
95 #define COND_BRANCH 1
96 #define UNCOND_BRANCH 2
97 #define END 3
98
99 #define BYTE_F 127 /* How far we can branch forwards */
100 #define BYTE_B -126 /* How far we can branch backwards */
101 #define WORD_F 32767
102 #define WORD_B 32768
103
104 relax_typeS md_relax_table[C (END, 0)] = {
105 { 0, 0, 0, 0 },
106 { 0, 0, 0, 0 },
107 { 0, 0, 0, 0 },
108 { 0, 0, 0, 0 },
109
110 /* COND_BRANCH */
111 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
112 { BYTE_F, BYTE_B, 2, C (COND_BRANCH, WORD_DISP) }, /* BYTE_DISP */
113 { WORD_F, WORD_B, 5, 0 }, /* WORD_DISP */
114 { 0, 0, 5, 0 }, /* UNDEF_WORD_DISP */
115
116 /* UNCOND_BRANCH */
117 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
118 { BYTE_F, BYTE_B, 2, C (UNCOND_BRANCH, WORD_DISP) }, /* BYTE_DISP */
119 { WORD_F, WORD_B, 3, 0 }, /* WORD_DISP */
120 { 0, 0, 3, 0 } /* UNDEF_WORD_DISP */
121
122 };
123
124 /* This function is called once, at assembler startup time. This
125 should set up all the tables, etc that the MD part of the assembler
126 needs. */
127
128 void
129 s_longa (xmode)
130 {
131 int *p = xmode ? &X : &M;
132 while (*input_line_pointer == ' ')
133 input_line_pointer++;
134 if (strncmp (input_line_pointer, "on", 2) == 0)
135 {
136 input_line_pointer += 2;
137 *p = 0;
138 }
139 else if (strncmp (input_line_pointer, "off", 3) == 0)
140 {
141 *p = 1;
142 input_line_pointer += 3;
143 }
144 else
145 as_bad (_("need on or off."));
146 demand_empty_rest_of_line ();
147 }
148
149 void
150 md_begin ()
151 {
152 struct opinfo *opcode;
153 char *prev_name = "";
154
155 opcode_hash_control = hash_new ();
156
157 /* Insert unique names into hash table. */
158 for (opcode = optable; opcode->name; opcode++)
159 {
160 if (strcmp (prev_name, opcode->name))
161 {
162 prev_name = opcode->name;
163 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
164 }
165 else
166 {
167 /* Make all the opcodes with the same name point to the same
168 string. */
169 opcode->name = prev_name;
170 }
171 }
172
173 flag_signed_overflow_ok = 1;
174 }
175
176 static expressionS immediate; /* absolute expression */
177 static expressionS immediate1; /* absolute expression */
178
179 static symbolS *
180 dot ()
181 {
182 const char *fake;
183
184 /* JF: '.' is pseudo symbol with value of current location
185 in current segment. */
186 fake = FAKE_LABEL_NAME;
187 return symbol_new (fake,
188 now_seg,
189 (valueT) frag_now_fix (),
190 frag_now);
191
192 }
193
194 int expr_size;
195 int expr_shift;
196 int tc_cons_reloc;
197
198 void
199 w65_expression (dest, bytes)
200 expressionS *dest;
201 unsigned int bytes;
202 {
203 expr_size = 0;
204 expr_shift = 0;
205 tc_cons_reloc = 0;
206 while (*input_line_pointer == ' ')
207 input_line_pointer++;
208
209 if (*input_line_pointer == '<')
210 {
211 expr_size = 1;
212 input_line_pointer++;
213 }
214 else if (*input_line_pointer == '>')
215 {
216 expr_shift = 1;
217 input_line_pointer++;
218 }
219 else if (*input_line_pointer == '^')
220 {
221 expr_shift = 2;
222 input_line_pointer++;
223 }
224
225 expr (0, dest);
226 }
227
228 int amode;
229
230 static char *
231 parse_exp (s, bytes)
232 char *s;
233 int bytes;
234 {
235 char *save;
236 char *new;
237
238 save = input_line_pointer;
239 input_line_pointer = s;
240 w65_expression (&immediate, bytes);
241 if (immediate.X_op == O_absent)
242 as_bad (_("missing operand"));
243 new = input_line_pointer;
244 input_line_pointer = save;
245 return new;
246 }
247
248 static char *
249 get_operands (info, ptr)
250 struct opinfo *info;
251 char *ptr;
252 {
253 register int override_len = 0;
254 register int bytes = 0;
255
256 while (*ptr == ' ')
257 ptr++;
258
259 if (ptr[0] == '#')
260 {
261 ptr++;
262 switch (info->amode)
263 {
264 case ADDR_IMMTOI:
265 bytes = X ? 1 : 2;
266 amode = ADDR_IMMTOI;
267 break;
268 case ADDR_IMMTOA:
269 bytes = M ? 1 : 2;
270 amode = ADDR_IMMTOA;
271 break;
272 case ADDR_IMMCOP:
273 bytes = 1;
274 amode = ADDR_IMMCOP;
275 break;
276 case ADDR_DIR:
277 bytes = 2;
278 amode = ADDR_ABS;
279 break;
280 default:
281 abort ();
282 break;
283 }
284 ptr = parse_exp (ptr);
285 }
286 else if (ptr[0] == '!')
287 {
288 ptr = parse_exp (ptr + 1);
289 if (ptr[0] == ',')
290 {
291 if (ptr[1] == 'y')
292 {
293 amode = ADDR_ABS_IDX_Y;
294 bytes = 2;
295 ptr += 2;
296 }
297 else if (ptr[1] == 'x')
298 {
299 amode = ADDR_ABS_IDX_X;
300 bytes = 2;
301 ptr += 2;
302 }
303 else
304 {
305 as_bad (_("syntax error after <exp"));
306 }
307 }
308 else
309 {
310 amode = ADDR_ABS;
311 bytes = 2;
312 }
313 }
314 else if (ptr[0] == '>')
315 {
316 ptr = parse_exp (ptr + 1);
317 if (ptr[0] == ',' && ptr[1] == 'x')
318 {
319 amode = ADDR_ABS_LONG_IDX_X;
320 bytes = 3;
321 ptr += 2;
322 }
323 else
324 {
325 amode = ADDR_ABS_LONG;
326 bytes = 3;
327 }
328 }
329 else if (ptr[0] == '<')
330 {
331 ptr = parse_exp (ptr + 1);
332 if (ptr[0] == ',')
333 {
334 if (ptr[1] == 'y')
335 {
336 amode = ADDR_DIR_IDX_Y;
337 ptr += 2;
338 bytes = 2;
339 }
340 else if (ptr[1] == 'x')
341 {
342 amode = ADDR_DIR_IDX_X;
343 ptr += 2;
344 bytes = 2;
345 }
346 else
347 {
348 as_bad (_("syntax error after <exp"));
349 }
350 }
351 else
352 {
353 amode = ADDR_DIR;
354 bytes = 1;
355 }
356 }
357 else if (ptr[0] == 'a')
358 {
359 amode = ADDR_ACC;
360 }
361 else if (ptr[0] == '(')
362 {
363 /* Look for (exp),y
364 (<exp),y
365 (exp,x)
366 (<exp,x)
367 (exp)
368 (!exp)
369 (exp)
370 (<exp)
371 (exp,x)
372 (!exp,x)
373 (exp,s)
374 (exp,s),y */
375
376 ptr++;
377 if (ptr[0] == '<')
378 {
379 override_len = 1;
380 ptr++;
381 }
382 else if (ptr[0] == '!')
383 {
384 override_len = 2;
385 ptr++;
386 }
387 else if (ptr[0] == '>')
388 {
389 override_len = 3;
390 ptr++;
391 }
392 else
393 {
394 override_len = 0;
395 }
396 ptr = parse_exp (ptr);
397
398 if (ptr[0] == ',')
399 {
400 ptr++;
401 if (ptr[0] == 'x' && ptr[1] == ')')
402 {
403 ptr += 2;
404
405 if (override_len == 1)
406 {
407 amode = ADDR_DIR_IDX_IND_X;
408 bytes = 2;
409 }
410 else
411 {
412 amode = ADDR_ABS_IND_IDX;
413 bytes = 2;
414 }
415 }
416 else if (ptr[0] == 's' && ptr[1] == ')'
417 && ptr[2] == ',' && ptr[3] == 'y')
418 {
419 amode = ADDR_STACK_REL_INDX_IDX;
420 bytes = 1;
421 ptr += 4;
422 }
423 }
424 else if (ptr[0] == ')')
425 {
426 if (ptr[1] == ',' && ptr[2] == 'y')
427 {
428 amode = ADDR_DIR_IND_IDX_Y;
429 ptr += 3;
430 bytes = 2;
431 }
432 else
433 {
434 if (override_len == 1)
435 {
436 amode = ADDR_DIR_IND;
437 bytes = 1;
438 }
439 else
440 {
441 amode = ADDR_ABS_IND;
442 bytes = 2;
443 }
444 ptr++;
445
446 }
447 }
448 }
449 else if (ptr[0] == '[')
450 {
451 ptr = parse_exp (ptr + 1);
452 if (ptr[0] == ']')
453 {
454 ptr++;
455 if (ptr[0] == ',' && ptr[1] == 'y')
456 {
457 bytes = 1;
458 amode = ADDR_DIR_IND_IDX_Y_LONG;
459 ptr += 2;
460 }
461 else
462 {
463 if (info->code == O_jmp)
464 {
465 bytes = 2;
466 amode = ADDR_ABS_IND_LONG;
467 }
468 else
469 {
470 bytes = 1;
471 amode = ADDR_DIR_IND_LONG;
472 }
473 }
474 }
475 }
476 else
477 {
478 ptr = parse_exp (ptr, 2);
479 if (ptr[0] == ',')
480 {
481 if (ptr[1] == 'y')
482 {
483 if (override_len == 1)
484 {
485 bytes = 1;
486 amode = ADDR_DIR_IDX_Y;
487 }
488 else
489 {
490 amode = ADDR_ABS_IDX_Y;
491 bytes = 2;
492 }
493 ptr += 2;
494 }
495 else if (ptr[1] == 'x')
496 {
497 if (override_len == 1)
498 {
499 amode = ADDR_DIR_IDX_X;
500 bytes = 1;
501 }
502 else
503 {
504 amode = ADDR_ABS_IDX_X;
505 bytes = 2;
506 }
507 ptr += 2;
508 }
509 else if (ptr[1] == 's')
510 {
511 bytes = 1;
512 amode = ADDR_STACK_REL;
513 ptr += 2;
514 }
515 else
516 {
517 bytes = 1;
518 immediate1 = immediate;
519 ptr = parse_exp (ptr + 1);
520 amode = ADDR_BLOCK_MOVE;
521 }
522 }
523 else
524 {
525 switch (info->amode)
526 {
527 case ADDR_PC_REL:
528 amode = ADDR_PC_REL;
529 bytes = 1;
530 break;
531 case ADDR_PC_REL_LONG:
532 amode = ADDR_PC_REL_LONG;
533 bytes = 2;
534 break;
535 default:
536 if (override_len == 1)
537 {
538 amode = ADDR_DIR;
539 bytes = 1;
540 }
541 else if (override_len == 3)
542 {
543 bytes = 3;
544 amode = ADDR_ABS_LONG;
545 }
546 else
547 {
548 amode = ADDR_ABS;
549 bytes = 2;
550 }
551 }
552 }
553 }
554
555 switch (bytes)
556 {
557 case 1:
558 switch (expr_shift)
559 {
560 case 0:
561 if (amode == ADDR_DIR)
562 tc_cons_reloc = R_W65_DP;
563 else
564 tc_cons_reloc = R_W65_ABS8;
565 break;
566 case 1:
567 tc_cons_reloc = R_W65_ABS8S8;
568 break;
569 case 2:
570 tc_cons_reloc = R_W65_ABS8S16;
571 break;
572 }
573 break;
574 case 2:
575 switch (expr_shift)
576 {
577 case 0:
578 tc_cons_reloc = R_W65_ABS16;
579 break;
580 case 1:
581 tc_cons_reloc = R_W65_ABS16S8;
582 break;
583 case 2:
584 tc_cons_reloc = R_W65_ABS16S16;
585 break;
586 }
587 }
588 return ptr;
589 }
590
591 /* Passed a pointer to a list of opcodes which use different
592 addressing modes, return the opcode which matches the opcodes
593 provided. */
594
595 static struct opinfo *
596 get_specific (opcode)
597 struct opinfo *opcode;
598 {
599 int ocode = opcode->code;
600
601 for (; opcode->code == ocode; opcode++)
602 {
603 if (opcode->amode == amode)
604 return opcode;
605 }
606 return 0;
607 }
608
609 int
610 check (operand, low, high)
611 expressionS *operand;
612 int low;
613 int high;
614 {
615 if (operand->X_op != O_constant
616 || operand->X_add_number < low
617 || operand->X_add_number > high)
618 {
619 as_bad ("operand must be absolute in range %d..%d", low, high);
620 }
621 return operand->X_add_number;
622 }
623
624 static int log2[] = { 0, 0, 1, 0, 2 };
625
626 /* Now we know what sort of opcodes it is, let's build the bytes. */
627
628 static void
629 build_Mytes (opcode)
630 struct opinfo *opcode;
631 {
632 int size;
633 int type;
634 int pcrel;
635 char *output;
636
637 if (opcode->amode == ADDR_IMPLIED)
638 {
639 output = frag_more (1);
640 }
641 else if (opcode->amode == ADDR_PC_REL)
642 {
643 int type;
644
645 /* This is a relaxable insn, so we do some special handling. */
646 type = opcode->val == OP_BRA ? UNCOND_BRANCH : COND_BRANCH;
647 output = frag_var (rs_machine_dependent,
648 md_relax_table[C (type, WORD_DISP)].rlx_length,
649 md_relax_table[C (type, BYTE_DISP)].rlx_length,
650 C (type, UNDEF_BYTE_DISP),
651 immediate.X_add_symbol,
652 immediate.X_add_number,
653 0);
654 }
655 else
656 {
657 switch (opcode->amode)
658 {
659 GETINFO (size, type, pcrel);
660 }
661
662 /* If something special was done in the expression modify the
663 reloc type. */
664 if (tc_cons_reloc)
665 type = tc_cons_reloc;
666
667 /* 1 byte for the opcode + the bytes for the addrmode. */
668 output = frag_more (size + 1);
669
670 if (opcode->amode == ADDR_BLOCK_MOVE)
671 {
672 /* Two relocs for this one. */
673 fix_new_exp (frag_now,
674 output + 1 - frag_now->fr_literal,
675 1,
676 &immediate,
677 0,
678 R_W65_ABS8S16);
679
680 fix_new_exp (frag_now,
681 output + 2 - frag_now->fr_literal,
682 1,
683 &immediate1,
684 0,
685 R_W65_ABS8S16);
686 }
687 else if (type >= 0
688 && opcode->amode != ADDR_IMPLIED
689 && opcode->amode != ADDR_ACC
690 && opcode->amode != ADDR_STACK)
691 {
692 fix_new_exp (frag_now,
693 output + 1 - frag_now->fr_literal,
694 size,
695 &immediate,
696 pcrel,
697 type);
698 }
699 }
700 output[0] = opcode->val;
701 }
702
703 /* This is the guts of the machine-dependent assembler. STR points to
704 a machine dependent instruction. This function is supposed to emit
705 the frags/bytes it assembles to. */
706
707 void
708 md_assemble (str)
709 char *str;
710 {
711 unsigned char *op_start;
712 unsigned char *op_end;
713 struct opinfo *opcode;
714 char name[20];
715 int nlen = 0;
716 char *p;
717
718 /* Drop leading whitespace */
719 while (*str == ' ')
720 str++;
721
722 /* all opcodes are three letters */
723 name[0] = str[0];
724 name[1] = str[1];
725 name[2] = str[2];
726 name[3] = 0;
727
728 tc_cons_reloc = 0;
729 str += 3;
730 opcode = (struct opinfo *) hash_find (opcode_hash_control, name);
731
732 if (opcode == NULL)
733 {
734 as_bad (_("unknown opcode"));
735 return;
736 }
737
738 if (opcode->amode != ADDR_IMPLIED
739 && opcode->amode != ADDR_STACK)
740 {
741 get_operands (opcode, str);
742 opcode = get_specific (opcode);
743 }
744
745 if (opcode == 0)
746 {
747 /* Couldn't find an opcode which matched the operands. */
748
749 char *where = frag_more (1);
750
751 where[0] = 0x0;
752 where[1] = 0x0;
753 as_bad (_("invalid operands for opcode"));
754 return;
755 }
756
757 build_Mytes (opcode);
758 }
759
760 void
761 tc_crawl_symbol_chain (headers)
762 object_headers *headers;
763 {
764 printf (_("call to tc_crawl_symbol_chain \n"));
765 }
766
767 symbolS *
768 md_undefined_symbol (name)
769 char *name;
770 {
771 return 0;
772 }
773
774 void
775 tc_headers_hook (headers)
776 object_headers *headers;
777 {
778 printf (_("call to tc_headers_hook \n"));
779 }
780
781 /* Various routines to kill one day. */
782 /* Equal to MAX_PRECISION in atof-ieee.c. */
783 #define MAX_LITTLENUMS 6
784
785 /* Turn a string in input_line_pointer into a floating point constant
786 of type TYPE, and store the appropriate bytes in *LITP. The number
787 of LITTLENUMS emitted is stored in *SIZEP. An error message is
788 returned, or NULL on OK. */
789
790 char *
791 md_atof (type, litP, sizeP)
792 char type;
793 char *litP;
794 int *sizeP;
795 {
796 int prec;
797 LITTLENUM_TYPE words[MAX_LITTLENUMS];
798 LITTLENUM_TYPE *wordP;
799 char *t;
800 char *atof_ieee ();
801
802 switch (type)
803 {
804 case 'f':
805 case 'F':
806 case 's':
807 case 'S':
808 prec = 2;
809 break;
810
811 case 'd':
812 case 'D':
813 case 'r':
814 case 'R':
815 prec = 4;
816 break;
817
818 case 'x':
819 case 'X':
820 prec = 6;
821 break;
822
823 case 'p':
824 case 'P':
825 prec = 6;
826 break;
827
828 default:
829 *sizeP = 0;
830 return _("Bad call to MD_NTOF()");
831 }
832 t = atof_ieee (input_line_pointer, type, words);
833 if (t)
834 input_line_pointer = t;
835
836 *sizeP = prec * sizeof (LITTLENUM_TYPE);
837 for (wordP = words + prec - 1; prec--;)
838 {
839 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
840 litP += sizeof (LITTLENUM_TYPE);
841 }
842 return 0;
843 }
844
845 int
846 md_parse_option (c, a)
847 int c;
848 char *a;
849 {
850 return 1;
851 }
852
853 void
854 tc_Nout_fix_to_chars ()
855 {
856 printf (_("call to tc_Nout_fix_to_chars \n"));
857 abort ();
858 }
859
860 /* Called after relaxing, change the frags so they know how big they
861 are. */
862
863 void
864 md_convert_frag (headers, seg, fragP)
865 object_headers *headers;
866 segT seg;
867 fragS *fragP;
868 {
869 int disp_size = 0;
870 int inst_size = 0;
871 unsigned char *buffer =
872 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
873
874 switch (fragP->fr_subtype)
875 {
876 case C (COND_BRANCH, BYTE_DISP):
877 case C (UNCOND_BRANCH, BYTE_DISP):
878 disp_size = 1;
879 inst_size = 1;
880 break;
881
882 /* Conditional branches to a known 16 bit displacement. */
883 case C (COND_BRANCH, WORD_DISP):
884 switch (buffer[0])
885 {
886 case OP_BCC:
887 case OP_BCS:
888 case OP_BEQ:
889 case OP_BMI:
890 case OP_BNE:
891 case OP_BPL:
892 case OP_BVS:
893 case OP_BVC:
894 /* Invert the sense of the test */
895 buffer[0] ^= 0x20;
896 buffer[1] = 3; /* Jump over following brl */
897 buffer[2] = OP_BRL;
898 buffer[3] = 0;
899 buffer[4] = 0;
900 disp_size = 2;
901 inst_size = 3;
902 break;
903 default:
904 abort ();
905 }
906 break;
907 case C (UNCOND_BRANCH, WORD_DISP):
908 /* Unconditional branches to a known 16 bit displacement. */
909
910 switch (buffer[0])
911 {
912 case OP_BRA:
913 buffer[0] = OP_BRL;
914 disp_size = 2;
915 inst_size = 1;
916 break;
917 default:
918 abort ();
919 }
920 break;
921 /* Got to create a branch over a reloc here. */
922 case C (COND_BRANCH, UNDEF_WORD_DISP):
923 buffer[0] ^= 0x20; /* invert test */
924 buffer[1] = 3;
925 buffer[2] = OP_BRL;
926 buffer[3] = 0;
927 buffer[4] = 0;
928 fix_new (fragP,
929 fragP->fr_fix + 3,
930 4,
931 fragP->fr_symbol,
932 fragP->fr_offset,
933 0,
934 R_W65_PCR16);
935
936 fragP->fr_fix += disp_size + inst_size;
937 fragP->fr_var = 0;
938 break;
939 case C (UNCOND_BRANCH, UNDEF_WORD_DISP):
940 buffer[0] = OP_BRL;
941 buffer[1] = 0;
942 buffer[2] = 0;
943 fix_new (fragP,
944 fragP->fr_fix + 1,
945 4,
946 fragP->fr_symbol,
947 fragP->fr_offset,
948 0,
949 R_W65_PCR16);
950
951 fragP->fr_fix += disp_size + inst_size;
952 fragP->fr_var = 0;
953 break;
954 default:
955 abort ();
956 }
957 if (inst_size)
958 {
959 /* Get the address of the end of the instruction. */
960 int next_inst = (fragP->fr_fix + fragP->fr_address
961 + disp_size + inst_size);
962 int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
963 fragP->fr_offset);
964 int disp = targ_addr - next_inst;
965
966 md_number_to_chars (buffer + inst_size, disp, disp_size);
967 fragP->fr_fix += disp_size + inst_size;
968 fragP->fr_var = 0;
969 }
970 }
971
972 valueT
973 md_section_align (seg, size)
974 segT seg;
975 valueT size;
976 {
977 return ((size + (1 << section_alignment[(int) seg]) - 1)
978 & (-1 << section_alignment[(int) seg]));
979 }
980
981 void
982 md_apply_fix (fixP, val)
983 fixS *fixP;
984 long val;
985 {
986 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
987 int addr = fixP->fx_frag->fr_address + fixP->fx_where;
988
989 if (fixP->fx_r_type == 0)
990 {
991 if (fixP->fx_size == 1)
992 fixP->fx_r_type = R_W65_ABS8;
993 else
994 fixP->fx_r_type = R_W65_ABS16;
995 }
996
997 switch (fixP->fx_r_type)
998 {
999 case R_W65_ABS8S16:
1000 val >>= 8;
1001 case R_W65_ABS8S8:
1002 val >>= 8;
1003 case R_W65_ABS8:
1004 *buf++ = val;
1005 break;
1006 case R_W65_ABS16S16:
1007 val >>= 8;
1008 case R_W65_ABS16S8:
1009 val >>= 8;
1010 case R_W65_ABS16:
1011 *buf++ = val >> 0;
1012 *buf++ = val >> 8;
1013 break;
1014 case R_W65_ABS24:
1015 *buf++ = val >> 0;
1016 *buf++ = val >> 8;
1017 *buf++ = val >> 16;
1018 break;
1019 case R_W65_PCR8:
1020 *buf++ = val - addr - 1;
1021 break;
1022 case R_W65_PCR16:
1023 val = val - addr - 1;
1024 *buf++ = val;
1025 *buf++ = val >> 8;
1026 break;
1027 case R_W65_DP:
1028 *buf++ = val;
1029 break;
1030
1031 default:
1032 abort ();
1033 }
1034 }
1035
1036 /* Put number into target byte order */
1037
1038 void
1039 md_number_to_chars (ptr, use, nbytes)
1040 char *ptr;
1041 valueT use;
1042 int nbytes;
1043 {
1044 number_to_chars_littleendian (ptr, use, nbytes);
1045 }
1046
1047 long
1048 md_pcrel_from (fixP)
1049 fixS *fixP;
1050 {
1051 int gap = fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address - 1;
1052 return gap;
1053 }
1054
1055 void
1056 tc_coff_symbol_emit_hook (x)
1057 symbolS *x;
1058 {
1059 }
1060
1061 short
1062 tc_coff_fix2rtype (fix_ptr)
1063 fixS *fix_ptr;
1064 {
1065 return fix_ptr->fx_r_type;
1066 }
1067
1068 void
1069 tc_reloc_mangle (fix_ptr, intr, base)
1070 fixS *fix_ptr;
1071 struct internal_reloc *intr;
1072 bfd_vma base;
1073
1074 {
1075 symbolS *symbol_ptr;
1076
1077 symbol_ptr = fix_ptr->fx_addsy;
1078
1079 /* If this relocation is attached to a symbol then it's ok
1080 to output it */
1081 if (fix_ptr->fx_r_type == RELOC_32)
1082 {
1083 /* cons likes to create reloc32's whatever the size of the reloc..
1084 */
1085 switch (fix_ptr->fx_size)
1086 {
1087 case 2:
1088 intr->r_type = R_IMM16;
1089 break;
1090 case 1:
1091 intr->r_type = R_IMM8;
1092 break;
1093 default:
1094 abort ();
1095 }
1096 }
1097 else
1098 {
1099 if (fix_ptr->fx_size == 4)
1100 intr->r_type = R_W65_ABS24;
1101 else
1102 intr->r_type = fix_ptr->fx_r_type;
1103 }
1104
1105 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1106 intr->r_offset = fix_ptr->fx_offset;
1107
1108 /* Turn the segment of the symbol into an offset. */
1109 if (symbol_ptr)
1110 {
1111 symbolS *dot;
1112
1113 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1114 if (dot)
1115 {
1116 intr->r_offset += S_GET_VALUE (symbol_ptr);
1117 intr->r_symndx = dot->sy_number;
1118 }
1119 else
1120 {
1121 intr->r_symndx = symbol_ptr->sy_number;
1122 }
1123 }
1124 else
1125 {
1126 intr->r_symndx = -1;
1127 }
1128 }
1129
1130 int
1131 tc_coff_sizemachdep (frag)
1132 fragS *frag;
1133 {
1134 return md_relax_table[frag->fr_subtype].rlx_length;
1135 }
1136
1137 /* Called just before address relaxation, return the length by which a
1138 fragment must grow to reach it's destination. */
1139
1140 int
1141 md_estimate_size_before_relax (fragP, segment_type)
1142 register fragS *fragP;
1143 register segT segment_type;
1144 {
1145 int what;
1146
1147 switch (fragP->fr_subtype)
1148 {
1149 default:
1150 abort ();
1151
1152 case C (COND_BRANCH, UNDEF_BYTE_DISP):
1153 case C (UNCOND_BRANCH, UNDEF_BYTE_DISP):
1154 what = GET_WHAT (fragP->fr_subtype);
1155 /* Used to be a branch to somewhere which was unknown. */
1156 if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1157 {
1158 /* Got a symbol and it's defined in this segment, become byte
1159 sized - maybe it will fix up. */
1160 fragP->fr_subtype = C (what, BYTE_DISP);
1161 }
1162 else
1163 {
1164 /* Its got a segment, but its not ours, so it will always be
1165 long. */
1166 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1167 }
1168 break;
1169
1170 case C (COND_BRANCH, BYTE_DISP):
1171 case C (COND_BRANCH, WORD_DISP):
1172 case C (COND_BRANCH, UNDEF_WORD_DISP):
1173 case C (UNCOND_BRANCH, BYTE_DISP):
1174 case C (UNCOND_BRANCH, WORD_DISP):
1175 case C (UNCOND_BRANCH, UNDEF_WORD_DISP):
1176 /* When relaxing a section for the second time, we don't need to
1177 do anything besides return the current size. */
1178 break;
1179 }
1180
1181 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
1182 return fragP->fr_var;
1183 }
1184
1185 CONST char *md_shortopts = "";
1186 struct option md_longopts[] = {
1187 #define OPTION_RELAX (OPTION_MD_BASE)
1188 {NULL, no_argument, NULL, 0}
1189 };
1190
1191 void
1192 md_show_usage (stream)
1193 FILE *stream;
1194 {
1195 }
1196
1197 size_t md_longopts_size = sizeof (md_longopts);
This page took 0.05339 seconds and 4 git commands to generate.