1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright 1999, 2000, 2001, 2002 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. */
25 #include "safe-ctype.h"
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 /* XXX - devices that don't seem to exist (renamed, replaced with larger
59 ones, or planned but never produced), left here for compatibility.
60 TODO: hide them in show_mcu_list output? */
62 static struct mcu_type_s mcu_types
[] =
64 {"avr1", AVR_ISA_TINY1
, bfd_mach_avr1
},
65 {"avr2", AVR_ISA_2xxx
, bfd_mach_avr2
},
66 {"avr3", AVR_ISA_M103
, bfd_mach_avr3
},
67 {"avr4", AVR_ISA_M8
, bfd_mach_avr4
},
68 {"avr5", AVR_ISA_ALL
, bfd_mach_avr5
},
69 {"at90s1200", AVR_ISA_1200
, bfd_mach_avr1
},
70 {"attiny10", AVR_ISA_TINY1
, bfd_mach_avr1
}, /* XXX -> tn11 */
71 {"attiny11", AVR_ISA_TINY1
, bfd_mach_avr1
},
72 {"attiny12", AVR_ISA_TINY1
, bfd_mach_avr1
},
73 {"attiny15", AVR_ISA_TINY1
, bfd_mach_avr1
},
74 {"attiny28", AVR_ISA_TINY1
, bfd_mach_avr1
},
75 {"at90s2313", AVR_ISA_2xxx
, bfd_mach_avr2
},
76 {"at90s2323", AVR_ISA_2xxx
, bfd_mach_avr2
},
77 {"at90s2333", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 4433 */
78 {"at90s2343", AVR_ISA_2xxx
, bfd_mach_avr2
},
79 {"attiny22", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 2343 */
80 {"attiny26", AVR_ISA_2xxx
, bfd_mach_avr2
},
81 {"at90s4433", AVR_ISA_2xxx
, bfd_mach_avr2
},
82 {"at90s4414", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 8515 */
83 {"at90s4434", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 8535 */
84 {"at90s8515", AVR_ISA_2xxx
, bfd_mach_avr2
},
85 {"at90s8535", AVR_ISA_2xxx
, bfd_mach_avr2
},
86 {"at90c8534", AVR_ISA_2xxx
, bfd_mach_avr2
},
87 {"at86rf401", AVR_ISA_2xxx
, bfd_mach_avr2
},
88 {"atmega603", AVR_ISA_M603
, bfd_mach_avr3
}, /* XXX -> m103 */
89 {"atmega103", AVR_ISA_M103
, bfd_mach_avr3
},
90 {"at43usb320",AVR_ISA_M103
, bfd_mach_avr3
},
91 {"at43usb355",AVR_ISA_M603
, bfd_mach_avr3
},
92 {"at76c711", AVR_ISA_M603
, bfd_mach_avr3
},
93 {"atmega8", AVR_ISA_M8
, bfd_mach_avr4
},
94 {"atmega83", AVR_ISA_M8
, bfd_mach_avr4
}, /* XXX -> m8535 */
95 {"atmega85", AVR_ISA_M8
, bfd_mach_avr4
}, /* XXX -> m8 */
96 {"atmega8515",AVR_ISA_M8
, bfd_mach_avr4
},
97 {"atmega8535",AVR_ISA_M8
, bfd_mach_avr4
},
98 {"atmega16", AVR_ISA_M323
, bfd_mach_avr5
},
99 {"atmega161", AVR_ISA_M161
, bfd_mach_avr5
},
100 {"atmega162", AVR_ISA_M323
, bfd_mach_avr5
},
101 {"atmega163", AVR_ISA_M161
, bfd_mach_avr5
},
102 {"atmega169", AVR_ISA_M323
, bfd_mach_avr5
},
103 {"atmega32", AVR_ISA_M323
, bfd_mach_avr5
},
104 {"atmega323", AVR_ISA_M323
, bfd_mach_avr5
},
105 {"atmega64", AVR_ISA_M323
, bfd_mach_avr5
},
106 {"atmega128", AVR_ISA_M128
, bfd_mach_avr5
},
107 {"at94k", AVR_ISA_94K
, bfd_mach_avr5
},
111 /* Current MCU type. */
112 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_2xxx
,bfd_mach_avr2
};
113 static struct mcu_type_s
*avr_mcu
= &default_mcu
;
115 /* AVR target-specific switches. */
118 int all_opcodes
; /* -mall-opcodes: accept all known AVR opcodes */
119 int no_skip_bug
; /* -mno-skip-bug: no warnings for skipping 2-word insns */
120 int no_wrap
; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around */
123 static struct avr_opt_s avr_opt
= { 0, 0, 0 };
125 const char EXP_CHARS
[] = "eE";
126 const char FLT_CHARS
[] = "dD";
127 static void avr_set_arch (int dummy
);
129 /* The target specific pseudo-ops which we support. */
130 const pseudo_typeS md_pseudo_table
[] =
132 {"arch", avr_set_arch
, 0},
136 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
138 static void show_mcu_list
PARAMS ((FILE *));
139 static char *skip_space
PARAMS ((char *));
140 static char *extract_word
PARAMS ((char *, char *, int));
141 static unsigned int avr_operand
PARAMS ((struct avr_opcodes_s
*,
142 int, char *, char **));
143 static unsigned int avr_operands
PARAMS ((struct avr_opcodes_s
*, char **));
144 static unsigned int avr_get_constant
PARAMS ((char *, int));
145 static char *parse_exp
PARAMS ((char *, expressionS
*));
146 static bfd_reloc_code_real_type avr_ldi_expression
PARAMS ((expressionS
*));
148 #define EXP_MOD_NAME(i) exp_mod[i].name
149 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
150 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
151 #define HAVE_PM_P(i) exp_mod[i].have_pm
156 bfd_reloc_code_real_type reloc
;
157 bfd_reloc_code_real_type neg_reloc
;
161 static struct exp_mod_s exp_mod
[] =
163 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
164 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
165 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
166 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
167 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
168 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
169 {"hlo8", -BFD_RELOC_AVR_LO8_LDI
, -BFD_RELOC_AVR_LO8_LDI_NEG
, 0},
170 {"hhi8", -BFD_RELOC_AVR_HI8_LDI
, -BFD_RELOC_AVR_HI8_LDI_NEG
, 0},
173 /* Opcode hash table. */
174 static struct hash_control
*avr_hash
;
176 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
177 static struct hash_control
*avr_mod_hash
;
179 #define OPTION_MMCU 'm'
180 #define OPTION_ALL_OPCODES (OPTION_MD_BASE + 1)
181 #define OPTION_NO_SKIP_BUG (OPTION_MD_BASE + 2)
182 #define OPTION_NO_WRAP (OPTION_MD_BASE + 3)
184 struct option md_longopts
[] =
186 { "mmcu", required_argument
, NULL
, OPTION_MMCU
},
187 { "mall-opcodes", no_argument
, NULL
, OPTION_ALL_OPCODES
},
188 { "mno-skip-bug", no_argument
, NULL
, OPTION_NO_SKIP_BUG
},
189 { "mno-wrap", no_argument
, NULL
, OPTION_NO_WRAP
},
190 { NULL
, no_argument
, NULL
, 0 }
193 size_t md_longopts_size
= sizeof (md_longopts
);
195 /* Display nicely formatted list of known MCU names. */
198 show_mcu_list (stream
)
203 fprintf (stream
, _("Known MCU names:"));
206 for (i
= 0; mcu_types
[i
].name
; i
++)
208 int len
= strlen (mcu_types
[i
].name
);
213 fprintf (stream
, " %s", mcu_types
[i
].name
);
216 fprintf (stream
, "\n %s", mcu_types
[i
].name
);
221 fprintf (stream
, "\n");
228 while (*s
== ' ' || *s
== '\t')
233 /* Extract one word from FROM and copy it to TO. */
236 extract_word (char *from
, char *to
, int limit
)
242 /* Drop leading whitespace. */
243 from
= skip_space (from
);
246 /* Find the op code end. */
247 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name (*op_end
);)
249 to
[size
++] = *op_end
++;
250 if (size
+ 1 >= limit
)
259 md_estimate_size_before_relax (fragp
, seg
)
260 fragS
*fragp ATTRIBUTE_UNUSED
;
261 asection
*seg ATTRIBUTE_UNUSED
;
268 md_show_usage (stream
)
273 " -mmcu=[avr-name] select microcontroller variant\n"
274 " [avr-name] can be:\n"
275 " avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
276 " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
277 " avr3 - ATmega103, ATmega603\n"
278 " avr4 - ATmega83, ATmega85\n"
279 " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
280 " or immediate microcontroller name.\n"));
282 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
283 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
284 " (default for avr4, avr5)\n"
285 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
286 " (default for avr3, avr5)\n"));
287 show_mcu_list (stream
);
292 int dummy ATTRIBUTE_UNUSED
;
296 str
= (char *) alloca (20);
297 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
298 md_parse_option (OPTION_MMCU
, str
);
299 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
303 md_parse_option (c
, arg
)
312 char *s
= alloca (strlen (arg
) + 1);
319 *t
= TOLOWER (*arg1
++);
323 for (i
= 0; mcu_types
[i
].name
; ++i
)
324 if (strcmp (mcu_types
[i
].name
, s
) == 0)
327 if (!mcu_types
[i
].name
)
329 show_mcu_list (stderr
);
330 as_fatal (_("unknown MCU: %s\n"), arg
);
333 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
334 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
335 as .arch ... in the asm output at the same time. */
336 if (avr_mcu
== &default_mcu
|| avr_mcu
->mach
== mcu_types
[i
].mach
)
337 avr_mcu
= &mcu_types
[i
];
339 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
340 avr_mcu
->name
, mcu_types
[i
].name
);
343 case OPTION_ALL_OPCODES
:
344 avr_opt
.all_opcodes
= 1;
346 case OPTION_NO_SKIP_BUG
:
347 avr_opt
.no_skip_bug
= 1;
358 md_undefined_symbol (name
)
359 char *name ATTRIBUTE_UNUSED
;
364 /* Turn a string in input_line_pointer into a floating point constant
365 of type TYPE, and store the appropriate bytes in *LITP. The number
366 of LITTLENUMS emitted is stored in *SIZEP. An error message is
367 returned, or NULL on OK. */
370 md_atof (type
, litP
, sizeP
)
376 LITTLENUM_TYPE words
[4];
377 LITTLENUM_TYPE
*wordP
;
390 return _("bad call to md_atof");
393 t
= atof_ieee (input_line_pointer
, type
, words
);
395 input_line_pointer
= t
;
397 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
399 /* This loop outputs the LITTLENUMs in REVERSE order. */
400 for (wordP
= words
+ prec
- 1; prec
--;)
402 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
403 litP
+= sizeof (LITTLENUM_TYPE
);
410 md_convert_frag (abfd
, sec
, fragP
)
411 bfd
*abfd ATTRIBUTE_UNUSED
;
412 asection
*sec ATTRIBUTE_UNUSED
;
413 fragS
*fragP ATTRIBUTE_UNUSED
;
422 struct avr_opcodes_s
*opcode
;
423 avr_hash
= hash_new ();
425 /* Insert unique names into hash table. This hash table then provides a
426 quick index to the first opcode with a particular name in the opcode
428 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
429 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
431 avr_mod_hash
= hash_new ();
433 for (i
= 0; i
< sizeof (exp_mod
) / sizeof (exp_mod
[0]); ++i
)
434 hash_insert (avr_mod_hash
, EXP_MOD_NAME (i
), (void *) (i
+ 10));
436 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
439 /* Resolve STR as a constant expression and return the result.
440 If result greater than MAX then error. */
443 avr_get_constant (str
, max
)
448 str
= skip_space (str
);
449 input_line_pointer
= str
;
452 if (ex
.X_op
!= O_constant
)
453 as_bad (_("constant value required"));
455 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
456 as_bad (_("number must be less than %d"), max
+ 1);
458 return ex
.X_add_number
;
461 /* Parse instruction operands.
462 Return binary opcode. */
465 avr_operands (opcode
, line
)
466 struct avr_opcodes_s
*opcode
;
469 char *op
= opcode
->constraints
;
470 unsigned int bin
= opcode
->bin_opcode
;
471 char *frag
= frag_more (opcode
->insn_size
* 2);
473 int where
= frag
- frag_now
->fr_literal
;
474 static unsigned int prev
= 0; /* Previous opcode. */
476 /* Opcode have operands. */
479 unsigned int reg1
= 0;
480 unsigned int reg2
= 0;
481 int reg1_present
= 0;
482 int reg2_present
= 0;
484 /* Parse first operand. */
485 if (REGISTER_P (*op
))
487 reg1
= avr_operand (opcode
, where
, op
, &str
);
490 /* Parse second operand. */
503 if (REGISTER_P (*op
))
506 str
= skip_space (str
);
508 as_bad (_("`,' required"));
509 str
= skip_space (str
);
511 reg2
= avr_operand (opcode
, where
, op
, &str
);
515 if (reg1_present
&& reg2_present
)
516 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
517 else if (reg2_present
)
525 /* Detect undefined combinations (like ld r31,Z+). */
526 if (!avr_opt
.all_opcodes
&& AVR_UNDEF_P (bin
))
527 as_warn (_("undefined combination of operands"));
529 if (opcode
->insn_size
== 2)
531 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
532 (AVR core bug, fixed in the newer devices). */
534 if (!(avr_opt
.no_skip_bug
|| (avr_mcu
->isa
& AVR_ISA_MUL
))
535 && AVR_SKIP_P (prev
))
536 as_warn (_("skipping two-word instruction"));
538 bfd_putl32 ((bfd_vma
) bin
, frag
);
541 bfd_putl16 ((bfd_vma
) bin
, frag
);
548 /* Parse one instruction operand.
549 Return operand bitmask. Also fixups can be generated. */
552 avr_operand (opcode
, where
, op
, line
)
553 struct avr_opcodes_s
*opcode
;
559 unsigned int op_mask
= 0;
560 char *str
= skip_space (*line
);
564 /* Any register operand. */
570 if (*str
== 'r' || *str
== 'R')
574 str
= extract_word (str
, r_name
, sizeof (r_name
));
576 if (ISDIGIT (r_name
[1]))
578 if (r_name
[2] == '\0')
579 op_mask
= r_name
[1] - '0';
580 else if (r_name
[1] != '0'
581 && ISDIGIT (r_name
[2])
582 && r_name
[3] == '\0')
583 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
588 op_mask
= avr_get_constant (str
, 31);
589 str
= input_line_pointer
;
597 if (op_mask
< 16 || op_mask
> 23)
598 as_bad (_("register r16-r23 required"));
604 as_bad (_("register number above 15 required"));
610 as_bad (_("even register number required"));
615 if ((op_mask
& 1) || op_mask
< 24)
616 as_bad (_("register r24, r26, r28 or r30 required"));
617 op_mask
= (op_mask
- 24) >> 1;
622 as_bad (_("register name or number from 0 to 31 required"));
631 str
= skip_space (str
+ 1);
640 as_bad (_("pointer register (X, Y or Z) required"));
642 str
= skip_space (str
+ 1);
647 as_bad (_("cannot both predecrement and postincrement"));
651 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
652 registers, no predecrement, no postincrement. */
653 if (!avr_opt
.all_opcodes
&& (op_mask
& 0x100F)
654 && !(avr_mcu
->isa
& AVR_ISA_SRAM
))
655 as_bad (_("addressing mode not supported"));
661 as_bad (_("can't predecrement"));
663 if (! (*str
== 'z' || *str
== 'Z'))
664 as_bad (_("pointer register Z required"));
666 str
= skip_space (str
+ 1);
677 char c
= TOLOWER (*str
++);
682 as_bad (_("pointer register (Y or Z) required"));
683 str
= skip_space (str
);
687 x
= avr_get_constant (str
, 63);
688 str
= input_line_pointer
;
689 op_mask
|= (x
& 7) | ((x
& (3 << 3)) << 7) | ((x
& (1 << 5)) << 8);
695 str
= parse_exp (str
, &op_expr
);
696 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
697 &op_expr
, false, BFD_RELOC_AVR_CALL
);
701 str
= parse_exp (str
, &op_expr
);
702 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
703 &op_expr
, true, BFD_RELOC_AVR_13_PCREL
);
707 str
= parse_exp (str
, &op_expr
);
708 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
709 &op_expr
, true, BFD_RELOC_AVR_7_PCREL
);
713 str
= parse_exp (str
, &op_expr
);
714 fix_new_exp (frag_now
, where
+ 2, opcode
->insn_size
* 2,
715 &op_expr
, false, BFD_RELOC_16
);
720 bfd_reloc_code_real_type r_type
;
722 input_line_pointer
= str
;
723 r_type
= avr_ldi_expression (&op_expr
);
724 str
= input_line_pointer
;
725 fix_new_exp (frag_now
, where
, 3,
726 &op_expr
, false, r_type
);
734 x
= ~avr_get_constant (str
, 255);
735 str
= input_line_pointer
;
736 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
744 x
= avr_get_constant (str
, 63);
745 str
= input_line_pointer
;
746 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 2);
755 x
= avr_get_constant (str
, 7);
756 str
= input_line_pointer
;
767 x
= avr_get_constant (str
, 63);
768 str
= input_line_pointer
;
769 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
777 x
= avr_get_constant (str
, 31);
778 str
= input_line_pointer
;
787 as_bad (_("unknown constraint `%c'"), *op
);
794 /* GAS will call this function for each section at the end of the assembly,
795 to permit the CPU backend to adjust the alignment of a section. */
798 md_section_align (seg
, addr
)
802 int align
= bfd_get_section_alignment (stdoutput
, seg
);
803 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
806 /* If you define this macro, it should return the offset between the
807 address of a PC relative fixup and the position from which the PC
808 relative adjustment should be made. On many processors, the base
809 of a PC relative instruction is the next instruction, so this
810 macro would return the length of an instruction. */
813 md_pcrel_from_section (fixp
, sec
)
817 if (fixp
->fx_addsy
!= (symbolS
*) NULL
818 && (!S_IS_DEFINED (fixp
->fx_addsy
)
819 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
822 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
825 /* GAS will call this for each fixup. It should store the correct
826 value in the object file. */
829 md_apply_fix3 (fixP
, valP
, seg
)
834 unsigned char *where
;
838 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
841 /* We don't actually support subtracting a symbol. */
842 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
843 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
845 switch (fixP
->fx_r_type
)
848 fixP
->fx_no_overflow
= 1;
850 case BFD_RELOC_AVR_7_PCREL
:
851 case BFD_RELOC_AVR_13_PCREL
:
854 case BFD_RELOC_AVR_CALL
:
860 /* Fetch the instruction, insert the fully resolved operand
861 value, and stuff the instruction back again. */
862 where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
863 insn
= bfd_getl16 (where
);
865 switch (fixP
->fx_r_type
)
867 case BFD_RELOC_AVR_7_PCREL
:
869 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
870 _("odd address operand: %ld"), value
);
872 /* Instruction addresses are always right-shifted by 1. */
874 --value
; /* Correct PC. */
876 if (value
< -64 || value
> 63)
877 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
878 _("operand out of range: %ld"), value
);
879 value
= (value
<< 3) & 0x3f8;
880 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
883 case BFD_RELOC_AVR_13_PCREL
:
885 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
886 _("odd address operand: %ld"), value
);
888 /* Instruction addresses are always right-shifted by 1. */
890 --value
; /* Correct PC. */
892 if (value
< -2048 || value
> 2047)
894 /* No wrap for devices with >8K of program memory. */
895 if ((avr_mcu
->isa
& AVR_ISA_MEGA
) || avr_opt
.no_wrap
)
896 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
897 _("operand out of range: %ld"), value
);
901 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
905 bfd_putl16 ((bfd_vma
) value
, where
);
909 bfd_putl16 ((bfd_vma
) value
, where
);
912 case BFD_RELOC_AVR_16_PM
:
913 bfd_putl16 ((bfd_vma
) (value
>> 1), where
);
916 case BFD_RELOC_AVR_LO8_LDI
:
917 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
920 case -BFD_RELOC_AVR_LO8_LDI
:
921 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
924 case BFD_RELOC_AVR_HI8_LDI
:
925 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
928 case -BFD_RELOC_AVR_HI8_LDI
:
929 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
932 case BFD_RELOC_AVR_HH8_LDI
:
933 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
936 case BFD_RELOC_AVR_LO8_LDI_NEG
:
937 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
940 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
941 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
944 case BFD_RELOC_AVR_HI8_LDI_NEG
:
945 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
948 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
949 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
952 case BFD_RELOC_AVR_HH8_LDI_NEG
:
953 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
956 case BFD_RELOC_AVR_LO8_LDI_PM
:
957 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
960 case BFD_RELOC_AVR_HI8_LDI_PM
:
961 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
964 case BFD_RELOC_AVR_HH8_LDI_PM
:
965 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
968 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
969 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
972 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
973 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
976 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
977 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
980 case BFD_RELOC_AVR_CALL
:
984 x
= bfd_getl16 (where
);
986 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
987 _("odd address operand: %ld"), value
);
989 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
990 bfd_putl16 ((bfd_vma
) x
, where
);
991 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+ 2);
996 as_fatal (_("line %d: unknown relocation type: 0x%x"),
997 fixP
->fx_line
, fixP
->fx_r_type
);
1003 switch (fixP
->fx_r_type
)
1005 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
1006 case -BFD_RELOC_AVR_HI8_LDI
:
1007 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
1008 case -BFD_RELOC_AVR_LO8_LDI
:
1009 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1010 _("only constant expression allowed"));
1019 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1020 will pass the resulting reloc to `bfd_install_relocation'. This
1021 currently works poorly, as `bfd_install_relocation' often does the
1022 wrong thing, and instances of `tc_gen_reloc' have been written to
1023 work around the problems, which in turns makes it difficult to fix
1024 `bfd_install_relocation'. */
1026 /* If while processing a fixup, a reloc really needs to be created
1027 then it is done here. */
1030 tc_gen_reloc (seg
, fixp
)
1031 asection
*seg ATTRIBUTE_UNUSED
;
1036 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1038 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1039 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1041 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1042 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1043 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
1045 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1046 _("reloc %d not supported by object file format"),
1047 (int) fixp
->fx_r_type
);
1051 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1052 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1053 reloc
->address
= fixp
->fx_offset
;
1055 reloc
->addend
= fixp
->fx_offset
;
1064 struct avr_opcodes_s
*opcode
;
1067 str
= skip_space (extract_word (str
, op
, sizeof (op
)));
1070 as_bad (_("can't find opcode "));
1072 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1076 as_bad (_("unknown opcode `%s'"), op
);
1080 /* Special case for opcodes with optional operands (lpm, elpm) -
1081 version with operands exists in avr_opcodes[] in the next entry. */
1083 if (*str
&& *opcode
->constraints
== '?')
1086 if (!avr_opt
.all_opcodes
&& (opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1087 as_bad (_("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1089 /* We used to set input_line_pointer to the result of get_operands,
1090 but that is wrong. Our caller assumes we don't change it. */
1092 char *t
= input_line_pointer
;
1093 avr_operands (opcode
, &str
);
1094 if (*skip_space (str
))
1095 as_bad (_("garbage at end of line"));
1096 input_line_pointer
= t
;
1100 /* Parse ordinary expression. */
1107 input_line_pointer
= s
;
1109 if (op
->X_op
== O_absent
)
1110 as_bad (_("missing operand"));
1111 return input_line_pointer
;
1114 /* Parse special expressions (needed for LDI command):
1119 where xx is: hh, hi, lo. */
1121 static bfd_reloc_code_real_type
1122 avr_ldi_expression (exp
)
1125 char *str
= input_line_pointer
;
1131 str
= extract_word (str
, op
, sizeof (op
));
1135 mod
= (int) hash_find (avr_mod_hash
, op
);
1142 str
= skip_space (str
);
1150 if (strncmp ("pm(", str
, 3) == 0
1151 || strncmp ("-(pm(", str
, 5) == 0)
1153 if (HAVE_PM_P (mod
))
1159 as_bad (_("illegal expression"));
1171 if (*str
== '-' && *(str
+ 1) == '(')
1178 input_line_pointer
= str
;
1183 if (*input_line_pointer
!= ')')
1185 as_bad (_("`)' required"));
1188 input_line_pointer
++;
1192 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1197 input_line_pointer
= tmp
;
1200 /* Warn about expressions that fail to use lo8 (). */
1201 if (exp
->X_op
== O_constant
)
1203 int x
= exp
->X_add_number
;
1204 if (x
< -255 || x
> 255)
1205 as_warn (_("constant out of 8-bit range: %d"), x
);
1208 as_warn (_("expression possibly out of 8-bit range"));
1210 return BFD_RELOC_AVR_LO8_LDI
;
1213 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1214 `avr_cons_fix_new'. */
1215 static int exp_mod_pm
= 0;
1217 /* Parse special CONS expression: pm (expression)
1218 which is used for addressing to a program memory.
1219 Relocation: BFD_RELOC_AVR_16_PM. */
1222 avr_parse_cons_expression (exp
, nbytes
)
1230 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1234 char *pm_name
= "pm";
1235 int len
= strlen (pm_name
);
1237 if (strncasecmp (input_line_pointer
, pm_name
, len
) == 0)
1239 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1241 if (*input_line_pointer
== '(')
1243 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1247 if (*input_line_pointer
== ')')
1248 ++input_line_pointer
;
1251 as_bad (_("`)' required"));
1258 input_line_pointer
= tmp
;
1266 avr_cons_fix_new (frag
, where
, nbytes
, exp
)
1272 if (exp_mod_pm
== 0)
1275 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_16
);
1276 else if (nbytes
== 4)
1277 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_32
);
1279 as_bad (_("illegal %srelocation size: %d"), "", nbytes
);
1284 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_AVR_16_PM
);
1286 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes
);