1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
32 int insn_size
; /* In words. */
34 unsigned int bin_opcode
;
37 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
38 {#NAME, CONSTR, SIZE, ISA, BIN},
40 struct avr_opcodes_s avr_opcodes
[] =
42 #include "opcode/avr.h"
46 const char comment_chars
[] = ";";
47 const char line_comment_chars
[] = "#";
48 const char line_separator_chars
[] = "$";
50 const char *md_shortopts
= "m:";
58 static struct mcu_type_s mcu_types
[] =
60 {"avr1", AVR_ISA_TINY1
, bfd_mach_avr1
},
61 {"avr2", AVR_ISA_2xxx
, bfd_mach_avr2
},
62 {"avr3", AVR_ISA_M103
, bfd_mach_avr3
},
63 {"avr4", AVR_ISA_M83
, bfd_mach_avr4
},
64 {"avr5", AVR_ISA_ALL
, bfd_mach_avr5
},
65 {"at90s1200", AVR_ISA_1200
, bfd_mach_avr1
},
66 {"attiny10", AVR_ISA_TINY1
, bfd_mach_avr1
},
67 {"attiny11", AVR_ISA_TINY1
, bfd_mach_avr1
},
68 {"attiny12", AVR_ISA_TINY1
, bfd_mach_avr1
},
69 {"attiny15", AVR_ISA_TINY1
, bfd_mach_avr1
},
70 {"attiny28", AVR_ISA_TINY1
, bfd_mach_avr1
},
71 {"at90s2313", AVR_ISA_2xxx
, bfd_mach_avr2
},
72 {"at90s2323", AVR_ISA_2xxx
, bfd_mach_avr2
},
73 {"at90s2333", AVR_ISA_2xxx
, bfd_mach_avr2
},
74 {"attiny22" , AVR_ISA_2xxx
, bfd_mach_avr2
},
75 {"at90s2343", AVR_ISA_2xxx
, bfd_mach_avr2
},
76 {"at90s4433", AVR_ISA_2xxx
, bfd_mach_avr2
},
77 {"at90s4414", AVR_ISA_2xxx
, bfd_mach_avr2
},
78 {"at90s4434", AVR_ISA_2xxx
, bfd_mach_avr2
},
79 {"at90s8515", AVR_ISA_2xxx
, bfd_mach_avr2
},
80 {"at90s8535", AVR_ISA_2xxx
, bfd_mach_avr2
},
81 {"at90c8534", AVR_ISA_2xxx
, bfd_mach_avr2
},
82 {"atmega603", AVR_ISA_M603
, bfd_mach_avr3
},
83 {"atmega103", AVR_ISA_M103
, bfd_mach_avr3
},
84 {"atmega83", AVR_ISA_M83
, bfd_mach_avr4
},
85 {"atmega85", AVR_ISA_M83
, bfd_mach_avr4
},
86 {"atmega161", AVR_ISA_M161
, bfd_mach_avr5
},
87 {"atmega163", AVR_ISA_M161
, bfd_mach_avr5
},
88 {"atmega32", AVR_ISA_M161
, bfd_mach_avr5
},
89 {"at94k", AVR_ISA_94K
, bfd_mach_avr5
},
93 /* Current MCU type. */
94 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_2xxx
,bfd_mach_avr2
};
95 static struct mcu_type_s
*avr_mcu
= &default_mcu
;
97 /* AVR target-specific switches. */
100 int all_opcodes
; /* -mall-opcodes: accept all known AVR opcodes */
101 int no_skip_bug
; /* -mno-skip-bug: no warnings for skipping 2-word insns */
102 int no_wrap
; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around */
105 static struct avr_opt_s avr_opt
= { 0, 0, 0 };
107 const char EXP_CHARS
[] = "eE";
108 const char FLT_CHARS
[] = "dD";
109 static void avr_set_arch (int dummy
);
111 /* The target specific pseudo-ops which we support. */
112 const pseudo_typeS md_pseudo_table
[] =
114 {"arch", avr_set_arch
, 0},
118 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
120 static void show_mcu_list (FILE *stream
);
121 static char *skip_space (char *s
);
122 static char *extract_word (char *from
, char *to
, int limit
);
123 static unsigned int avr_operand (struct avr_opcodes_s
*opcode
,
124 int where
, char *op
, char **line
);
125 static unsigned int avr_operands (struct avr_opcodes_s
*opcode
, char **line
);
126 static unsigned int avr_get_constant (char *str
, int max
);
127 static char *parse_exp (char *s
, expressionS
*op
);
128 static bfd_reloc_code_real_type
avr_ldi_expression (expressionS
*exp
);
129 long md_pcrel_from_section
PARAMS ((fixS
*, segT
));
131 #define EXP_MOD_NAME(i) exp_mod[i].name
132 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
133 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
134 #define HAVE_PM_P(i) exp_mod[i].have_pm
139 bfd_reloc_code_real_type reloc
;
140 bfd_reloc_code_real_type neg_reloc
;
144 static struct exp_mod_s exp_mod
[] =
146 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
147 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
148 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
149 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
150 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
151 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
152 {"hlo8", -BFD_RELOC_AVR_LO8_LDI
, -BFD_RELOC_AVR_LO8_LDI_NEG
, 0},
153 {"hhi8", -BFD_RELOC_AVR_HI8_LDI
, -BFD_RELOC_AVR_HI8_LDI_NEG
, 0},
156 /* Opcode hash table. */
157 static struct hash_control
*avr_hash
;
159 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
160 static struct hash_control
*avr_mod_hash
;
162 #define OPTION_MMCU 'm'
163 #define OPTION_ALL_OPCODES (OPTION_MD_BASE + 1)
164 #define OPTION_NO_SKIP_BUG (OPTION_MD_BASE + 2)
165 #define OPTION_NO_WRAP (OPTION_MD_BASE + 3)
167 struct option md_longopts
[] =
169 { "mmcu", required_argument
, NULL
, OPTION_MMCU
},
170 { "mall-opcodes", no_argument
, NULL
, OPTION_ALL_OPCODES
},
171 { "mno-skip-bug", no_argument
, NULL
, OPTION_NO_SKIP_BUG
},
172 { "mno-wrap", no_argument
, NULL
, OPTION_NO_WRAP
},
173 { NULL
, no_argument
, NULL
, 0 }
176 size_t md_longopts_size
= sizeof (md_longopts
);
178 /* Display nicely formatted list of known MCU names. */
181 show_mcu_list (stream
)
186 fprintf (stream
, _("Known MCU names:"));
189 for (i
= 0; mcu_types
[i
].name
; i
++)
191 int len
= strlen (mcu_types
[i
].name
);
196 fprintf (stream
, " %s", mcu_types
[i
].name
);
199 fprintf (stream
, "\n %s", mcu_types
[i
].name
);
204 fprintf (stream
, "\n");
211 while (*s
== ' ' || *s
== '\t')
216 /* Extract one word from FROM and copy it to TO. */
219 extract_word (char *from
, char *to
, int limit
)
225 /* Drop leading whitespace. */
226 from
= skip_space (from
);
229 /* Find the op code end. */
230 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name (*op_end
);)
232 to
[size
++] = *op_end
++;
233 if (size
+ 1 >= limit
)
242 md_estimate_size_before_relax (fragp
, seg
)
243 fragS
*fragp ATTRIBUTE_UNUSED
;
244 asection
*seg ATTRIBUTE_UNUSED
;
251 md_show_usage (stream
)
256 " -mmcu=[avr-name] select microcontroller variant\n"
257 " [avr-name] can be:\n"
258 " avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
259 " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
260 " avr3 - ATmega103, ATmega603\n"
261 " avr4 - ATmega83, ATmega85\n"
262 " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
263 " or immediate microcontroller name.\n"));
265 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
266 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
267 " (default for avr4, avr5)\n"
268 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
269 " (default for avr3, avr5)\n"));
270 show_mcu_list (stream
);
275 int dummy ATTRIBUTE_UNUSED
;
279 str
= (char *) alloca (20);
280 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
281 md_parse_option (OPTION_MMCU
, str
);
282 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
286 md_parse_option (c
, arg
)
295 char *s
= alloca (strlen (arg
) + 1);
302 *t
= tolower (*arg1
++);
306 for (i
= 0; mcu_types
[i
].name
; ++i
)
307 if (strcmp (mcu_types
[i
].name
, s
) == 0)
310 if (!mcu_types
[i
].name
)
312 show_mcu_list (stderr
);
313 as_fatal (_("unknown MCU: %s\n"), arg
);
316 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
317 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
318 as .arch ... in the asm output at the same time. */
319 if (avr_mcu
== &default_mcu
|| avr_mcu
->mach
== mcu_types
[i
].mach
)
320 avr_mcu
= &mcu_types
[i
];
322 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
323 avr_mcu
->name
, mcu_types
[i
].name
);
326 case OPTION_ALL_OPCODES
:
327 avr_opt
.all_opcodes
= 1;
329 case OPTION_NO_SKIP_BUG
:
330 avr_opt
.no_skip_bug
= 1;
341 md_undefined_symbol (name
)
342 char *name ATTRIBUTE_UNUSED
;
347 /* Turn a string in input_line_pointer into a floating point constant
348 of type TYPE, and store the appropriate bytes in *LITP. The number
349 of LITTLENUMS emitted is stored in *SIZEP. An error message is
350 returned, or NULL on OK. */
353 md_atof (type
, litP
, sizeP
)
359 LITTLENUM_TYPE words
[4];
360 LITTLENUM_TYPE
*wordP
;
373 return _("bad call to md_atof");
376 t
= atof_ieee (input_line_pointer
, type
, words
);
378 input_line_pointer
= t
;
380 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
382 /* This loop outputs the LITTLENUMs in REVERSE order. */
383 for (wordP
= words
+ prec
- 1; prec
--;)
385 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
386 litP
+= sizeof (LITTLENUM_TYPE
);
393 md_convert_frag (abfd
, sec
, fragP
)
394 bfd
*abfd ATTRIBUTE_UNUSED
;
395 asection
*sec ATTRIBUTE_UNUSED
;
396 fragS
*fragP ATTRIBUTE_UNUSED
;
405 struct avr_opcodes_s
*opcode
;
406 avr_hash
= hash_new ();
408 /* Insert unique names into hash table. This hash table then provides a
409 quick index to the first opcode with a particular name in the opcode
411 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
412 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
414 avr_mod_hash
= hash_new ();
416 for (i
= 0; i
< sizeof (exp_mod
) / sizeof (exp_mod
[0]); ++i
)
417 hash_insert (avr_mod_hash
, EXP_MOD_NAME (i
), (void *) (i
+ 10));
419 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
422 /* Resolve STR as a constant expression and return the result.
423 If result greater than MAX then error. */
426 avr_get_constant (str
, max
)
431 str
= skip_space (str
);
432 input_line_pointer
= str
;
435 if (ex
.X_op
!= O_constant
)
436 as_bad (_("constant value required"));
438 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
439 as_bad (_("number must be less than %d"), max
+ 1);
441 return ex
.X_add_number
;
444 /* Parse instruction operands.
445 Return binary opcode. */
448 avr_operands (opcode
, line
)
449 struct avr_opcodes_s
*opcode
;
452 char *op
= opcode
->constraints
;
453 unsigned int bin
= opcode
->bin_opcode
;
454 char *frag
= frag_more (opcode
->insn_size
* 2);
456 int where
= frag
- frag_now
->fr_literal
;
457 static unsigned int prev
= 0; /* Previous opcode. */
459 /* Opcode have operands. */
462 unsigned int reg1
= 0;
463 unsigned int reg2
= 0;
464 int reg1_present
= 0;
465 int reg2_present
= 0;
467 /* Parse first operand. */
468 if (REGISTER_P (*op
))
470 reg1
= avr_operand (opcode
, where
, op
, &str
);
473 /* Parse second operand. */
486 if (REGISTER_P (*op
))
489 str
= skip_space (str
);
491 as_bad (_("`,' required"));
492 str
= skip_space (str
);
494 reg2
= avr_operand (opcode
, where
, op
, &str
);
498 if (reg1_present
&& reg2_present
)
499 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
500 else if (reg2_present
)
508 if (!avr_opt
.all_opcodes
)
510 /* Detect undefined combinations (like ld r31,Z+). */
511 if (((bin
& 0xFDEF) == 0x91AD) || ((bin
& 0xFDEF) == 0x91AE) ||
512 ((bin
& 0xFDEF) == 0x91C9) || ((bin
& 0xFDEF) == 0x91CA) ||
513 ((bin
& 0xFDEF) == 0x91E1) || ((bin
& 0xFDEF) == 0x91E2) ||
514 ((bin
& 0xFFED) == 0x91E5))
515 as_warn (_("undefined combination of operands"));
518 if (opcode
->insn_size
== 2)
520 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
521 (AVR core bug, fixed in the newer devices). */
523 if (!((avr_mcu
->isa
& AVR_ISA_MUL
) || avr_opt
.no_skip_bug
))
525 if ((prev
& 0xFC00) == 0x1000
526 || (prev
& 0xFD00) == 0x9900
527 || (prev
& 0xFC08) == 0xFC00)
528 as_warn (_("skipping two-word instruction"));
531 bfd_putl32 ((bfd_vma
) bin
, frag
);
534 bfd_putl16 ((bfd_vma
) bin
, frag
);
541 /* Parse one instruction operand.
542 Return operand bitmask. Also fixups can be generated. */
545 avr_operand (opcode
, where
, op
, line
)
546 struct avr_opcodes_s
*opcode
;
552 unsigned int op_mask
= 0;
553 char *str
= skip_space (*line
);
557 /* Any register operand. */
565 if (*str
== 'r' || *str
== 'R')
569 str
= extract_word (str
, r_name
, sizeof (r_name
));
570 if (isdigit (r_name
[1]))
572 if (r_name
[2] == '\0')
573 op_mask
= r_name
[1] - '0';
574 else if (r_name
[1] != '0'
575 && isdigit (r_name
[2])
576 && r_name
[3] == '\0')
577 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
582 op_mask
= avr_get_constant (str
, 31);
583 str
= input_line_pointer
;
591 if (op_mask
< 16 || op_mask
> 23)
592 as_bad (_("register r16-r23 required"));
598 as_bad (_("register number above 15 required"));
604 as_bad (_("even register number required"));
610 if (op_mask
& 1 || op_mask
> 6)
611 as_bad (_("register r24, r26, r28 or r30 required"));
617 as_bad (_("register name or number from 0 to 31 required"));
626 str
= skip_space (str
+ 1);
635 as_bad (_("pointer register (X, Y or Z) required"));
637 str
= skip_space (str
+ 1);
642 as_bad (_("cannot both predecrement and postincrement"));
646 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
647 registers, no predecrement, no postincrement. */
648 if (!avr_opt
.all_opcodes
&& (op_mask
& 0x100F)
649 && !(avr_mcu
->isa
& AVR_ISA_SRAM
))
650 as_bad (_("addressing mode not supported"));
656 as_bad (_("can't predecrement"));
658 if (! (*str
== 'z' || *str
== 'Z'))
659 as_bad (_("pointer register Z required"));
661 str
= skip_space (str
+ 1);
672 char c
= tolower (*str
++);
677 as_bad (_("pointer register (Y or Z) required"));
678 str
= skip_space (str
);
682 x
= avr_get_constant (str
, 63);
683 str
= input_line_pointer
;
684 op_mask
|= (x
& 7) | ((x
& (3 << 3)) << 7) | ((x
& (1 << 5)) << 8);
690 str
= parse_exp (str
, &op_expr
);
691 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
692 &op_expr
, false, BFD_RELOC_AVR_CALL
);
696 str
= parse_exp (str
, &op_expr
);
697 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
698 &op_expr
, true, BFD_RELOC_AVR_13_PCREL
);
702 str
= parse_exp (str
, &op_expr
);
703 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
704 &op_expr
, true, BFD_RELOC_AVR_7_PCREL
);
708 str
= parse_exp (str
, &op_expr
);
709 fix_new_exp (frag_now
, where
+ 2, opcode
->insn_size
* 2,
710 &op_expr
, false, BFD_RELOC_16
);
715 bfd_reloc_code_real_type r_type
;
717 input_line_pointer
= str
;
718 r_type
= avr_ldi_expression (&op_expr
);
719 str
= input_line_pointer
;
720 fix_new_exp (frag_now
, where
, 3,
721 &op_expr
, false, r_type
);
729 x
= ~avr_get_constant (str
, 255);
730 str
= input_line_pointer
;
731 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
739 x
= avr_get_constant (str
, 63);
740 str
= input_line_pointer
;
741 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 2);
750 x
= avr_get_constant (str
, 7);
751 str
= input_line_pointer
;
762 x
= avr_get_constant (str
, 63);
763 str
= input_line_pointer
;
764 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
772 x
= avr_get_constant (str
, 31);
773 str
= input_line_pointer
;
782 as_bad (_("unknown constraint `%c'"), *op
);
789 /* GAS will call this function for each section at the end of the assembly,
790 to permit the CPU backend to adjust the alignment of a section. */
793 md_section_align (seg
, addr
)
797 int align
= bfd_get_section_alignment (stdoutput
, seg
);
798 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
801 /* If you define this macro, it should return the offset between the
802 address of a PC relative fixup and the position from which the PC
803 relative adjustment should be made. On many processors, the base
804 of a PC relative instruction is the next instruction, so this
805 macro would return the length of an instruction. */
808 md_pcrel_from_section (fixp
, sec
)
812 if (fixp
->fx_addsy
!= (symbolS
*) NULL
813 && (!S_IS_DEFINED (fixp
->fx_addsy
)
814 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
817 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
820 /* GAS will call this for each fixup. It should store the correct
821 value in the object file. */
824 md_apply_fix3 (fixp
, valuep
, seg
)
829 unsigned char *where
;
833 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
838 else if (fixp
->fx_pcrel
)
840 segT s
= S_GET_SEGMENT (fixp
->fx_addsy
);
842 if (fixp
->fx_addsy
&& (s
== seg
|| s
== absolute_section
))
844 value
= S_GET_VALUE (fixp
->fx_addsy
) + *valuep
;
852 value
= fixp
->fx_offset
;
854 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
856 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
858 value
-= S_GET_VALUE (fixp
->fx_subsy
);
863 /* We don't actually support subtracting a symbol. */
864 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
865 _("expression too complex"));
870 switch (fixp
->fx_r_type
)
873 fixp
->fx_no_overflow
= 1;
875 case BFD_RELOC_AVR_7_PCREL
:
876 case BFD_RELOC_AVR_13_PCREL
:
879 case BFD_RELOC_AVR_CALL
:
885 /* Fetch the instruction, insert the fully resolved operand
886 value, and stuff the instruction back again. */
887 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
888 insn
= bfd_getl16 (where
);
890 switch (fixp
->fx_r_type
)
892 case BFD_RELOC_AVR_7_PCREL
:
894 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
895 _("odd address operand: %ld"), value
);
897 /* Instruction addresses are always right-shifted by 1. */
899 --value
; /* Correct PC. */
901 if (value
< -64 || value
> 63)
902 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
903 _("operand out of range: %ld"), value
);
904 value
= (value
<< 3) & 0x3f8;
905 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
908 case BFD_RELOC_AVR_13_PCREL
:
910 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
911 _("odd address operand: %ld"), value
);
913 /* Instruction addresses are always right-shifted by 1. */
915 --value
; /* Correct PC. */
917 if (value
< -2048 || value
> 2047)
919 /* No wrap for devices with >8K of program memory. */
920 if ((avr_mcu
->isa
& AVR_ISA_MEGA
) || avr_opt
.no_wrap
)
921 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
922 _("operand out of range: %ld"), value
);
926 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
930 bfd_putl16 ((bfd_vma
) value
, where
);
934 bfd_putl16 ((bfd_vma
) value
, where
);
937 case BFD_RELOC_AVR_16_PM
:
938 bfd_putl16 ((bfd_vma
) (value
>> 1), where
);
941 case BFD_RELOC_AVR_LO8_LDI
:
942 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
945 case -BFD_RELOC_AVR_LO8_LDI
:
946 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
949 case BFD_RELOC_AVR_HI8_LDI
:
950 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
953 case -BFD_RELOC_AVR_HI8_LDI
:
954 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
957 case BFD_RELOC_AVR_HH8_LDI
:
958 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
961 case BFD_RELOC_AVR_LO8_LDI_NEG
:
962 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
965 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
966 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
969 case BFD_RELOC_AVR_HI8_LDI_NEG
:
970 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
973 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
974 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
977 case BFD_RELOC_AVR_HH8_LDI_NEG
:
978 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
981 case BFD_RELOC_AVR_LO8_LDI_PM
:
982 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
985 case BFD_RELOC_AVR_HI8_LDI_PM
:
986 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
989 case BFD_RELOC_AVR_HH8_LDI_PM
:
990 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
993 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
994 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
997 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
998 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
1001 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
1002 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
1005 case BFD_RELOC_AVR_CALL
:
1009 x
= bfd_getl16 (where
);
1011 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1012 _("odd address operand: %ld"), value
);
1014 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
1015 bfd_putl16 ((bfd_vma
) x
, where
);
1016 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+ 2);
1021 as_fatal (_("line %d: unknown relocation type: 0x%x"),
1022 fixp
->fx_line
, fixp
->fx_r_type
);
1028 switch (fixp
->fx_r_type
)
1030 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
1031 case -BFD_RELOC_AVR_HI8_LDI
:
1032 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
1033 case -BFD_RELOC_AVR_LO8_LDI
:
1034 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1035 _("only constant expression allowed"));
1041 fixp
->fx_addnumber
= value
;
1046 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1047 will pass the resulting reloc to `bfd_install_relocation'. This
1048 currently works poorly, as `bfd_install_relocation' often does the
1049 wrong thing, and instances of `tc_gen_reloc' have been written to
1050 work around the problems, which in turns makes it difficult to fix
1051 `bfd_install_relocation'. */
1053 /* If while processing a fixup, a reloc really needs to be created
1054 then it is done here. */
1057 tc_gen_reloc (seg
, fixp
)
1058 asection
*seg ATTRIBUTE_UNUSED
;
1063 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1065 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1066 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1068 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1069 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1070 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
1072 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1073 _("reloc %d not supported by object file format"),
1074 (int) fixp
->fx_r_type
);
1078 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1079 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1080 reloc
->address
= fixp
->fx_offset
;
1082 reloc
->addend
= fixp
->fx_offset
;
1091 struct avr_opcodes_s
*opcode
;
1094 str
= skip_space (extract_word (str
, op
, sizeof (op
)));
1097 as_bad (_("can't find opcode "));
1099 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1103 as_bad (_("unknown opcode `%s'"), op
);
1107 /* Special case for opcodes with optional operands (lpm, elpm) -
1108 version with operands exists in avr_opcodes[] in the next entry. */
1110 if (*str
&& *opcode
->constraints
== '?')
1113 if (!avr_opt
.all_opcodes
&& (opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1114 as_bad (_("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1116 /* We used to set input_line_pointer to the result of get_operands,
1117 but that is wrong. Our caller assumes we don't change it. */
1119 char *t
= input_line_pointer
;
1120 avr_operands (opcode
, &str
);
1121 if (*skip_space (str
))
1122 as_bad (_("garbage at end of line"));
1123 input_line_pointer
= t
;
1127 /* Parse ordinary expression. */
1134 input_line_pointer
= s
;
1136 if (op
->X_op
== O_absent
)
1137 as_bad (_("missing operand"));
1138 return input_line_pointer
;
1141 /* Parse special expressions (needed for LDI command):
1146 where xx is: hh, hi, lo. */
1148 static bfd_reloc_code_real_type
1149 avr_ldi_expression (exp
)
1152 char *str
= input_line_pointer
;
1158 str
= extract_word (str
, op
, sizeof (op
));
1162 mod
= (int) hash_find (avr_mod_hash
, op
);
1169 str
= skip_space (str
);
1177 if (strncmp ("pm(", str
, 3) == 0
1178 || strncmp ("-(pm(", str
, 5) == 0)
1180 if (HAVE_PM_P (mod
))
1186 as_bad (_("illegal expression"));
1198 if (*str
== '-' && *(str
+ 1) == '(')
1205 input_line_pointer
= str
;
1210 if (*input_line_pointer
!= ')')
1212 as_bad (_("`)' required"));
1215 input_line_pointer
++;
1219 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1224 input_line_pointer
= tmp
;
1227 /* Warn about expressions that fail to use lo8 (). */
1228 if (exp
->X_op
== O_constant
)
1230 int x
= exp
->X_add_number
;
1231 if (x
< -255 || x
> 255)
1232 as_warn (_("constant out of 8-bit range: %d"), x
);
1235 as_warn (_("expression possibly out of 8-bit range"));
1237 return BFD_RELOC_AVR_LO8_LDI
;
1240 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1241 `avr_cons_fix_new'. */
1242 static int exp_mod_pm
= 0;
1244 /* Parse special CONS expression: pm (expression)
1245 which is used for addressing to a program memory.
1246 Relocation: BFD_RELOC_AVR_16_PM. */
1249 avr_parse_cons_expression (exp
, nbytes
)
1257 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1261 char *pm_name
= "pm";
1262 int len
= strlen (pm_name
);
1264 if (strncasecmp (input_line_pointer
, pm_name
, len
) == 0)
1266 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1268 if (*input_line_pointer
== '(')
1270 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1274 if (*input_line_pointer
== ')')
1275 ++input_line_pointer
;
1278 as_bad (_("`)' required"));
1285 input_line_pointer
= tmp
;
1293 avr_cons_fix_new (frag
, where
, nbytes
, exp
)
1299 if (exp_mod_pm
== 0)
1302 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_16
);
1303 else if (nbytes
== 4)
1304 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_32
);
1306 as_bad (_("illegal %srelocation size: %d"), "", nbytes
);
1311 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_AVR_16_PM
);
1313 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes
);