1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
5 Contributed by Denis Chertykov <denisc@overta.ru>
7 This file is part of GAS, the GNU Assembler.
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
22 Boston, MA 02110-1301, 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. */
61 static struct mcu_type_s mcu_types
[] =
63 {"avr1", AVR_ISA_AVR1
, bfd_mach_avr1
},
64 /* TODO: insruction set for avr2 architecture should be AVR_ISA_AVR2,
65 but set to AVR_ISA_AVR25 for some following version
66 of GCC (from 4.3) for backward compatibility. */
67 {"avr2", AVR_ISA_AVR25
, bfd_mach_avr2
},
68 {"avr25", AVR_ISA_AVR25
, bfd_mach_avr25
},
69 /* TODO: insruction set for avr3 architecture should be AVR_ISA_AVR3,
70 but set to AVR_ISA_AVR3_ALL for some following version
71 of GCC (from 4.3) for backward compatibility. */
72 {"avr3", AVR_ISA_AVR3_ALL
, bfd_mach_avr3
},
73 {"avr31", AVR_ISA_AVR31
, bfd_mach_avr31
},
74 {"avr35", AVR_ISA_AVR35
, bfd_mach_avr35
},
75 {"avr4", AVR_ISA_AVR4
, bfd_mach_avr4
},
76 /* TODO: insruction set for avr5 architecture should be AVR_ISA_AVR5,
77 but set to AVR_ISA_AVR51 for some following version
78 of GCC (from 4.3) for backward compatibility. */
79 {"avr5", AVR_ISA_AVR51
, bfd_mach_avr5
},
80 {"avr51", AVR_ISA_AVR51
, bfd_mach_avr51
},
81 {"avr6", AVR_ISA_AVR6
, bfd_mach_avr6
},
82 {"at90s1200", AVR_ISA_1200
, bfd_mach_avr1
},
83 {"attiny11", AVR_ISA_AVR1
, bfd_mach_avr1
},
84 {"attiny12", AVR_ISA_AVR1
, bfd_mach_avr1
},
85 {"attiny15", AVR_ISA_AVR1
, bfd_mach_avr1
},
86 {"attiny28", AVR_ISA_AVR1
, bfd_mach_avr1
},
87 {"at90s2313", AVR_ISA_AVR2
, bfd_mach_avr2
},
88 {"at90s2323", AVR_ISA_AVR2
, bfd_mach_avr2
},
89 {"at90s2333", AVR_ISA_AVR2
, bfd_mach_avr2
}, /* XXX -> 4433 */
90 {"at90s2343", AVR_ISA_AVR2
, bfd_mach_avr2
},
91 {"attiny22", AVR_ISA_AVR2
, bfd_mach_avr2
}, /* XXX -> 2343 */
92 {"attiny26", AVR_ISA_2xxe
, bfd_mach_avr2
},
93 {"at90s4414", AVR_ISA_AVR2
, bfd_mach_avr2
}, /* XXX -> 8515 */
94 {"at90s4433", AVR_ISA_AVR2
, bfd_mach_avr2
},
95 {"at90s4434", AVR_ISA_AVR2
, bfd_mach_avr2
}, /* XXX -> 8535 */
96 {"at90s8515", AVR_ISA_AVR2
, bfd_mach_avr2
},
97 {"at90c8534", AVR_ISA_AVR2
, bfd_mach_avr2
},
98 {"at90s8535", AVR_ISA_AVR2
, bfd_mach_avr2
},
99 {"attiny13", AVR_ISA_AVR25
, bfd_mach_avr25
},
100 {"attiny13a", AVR_ISA_AVR25
, bfd_mach_avr25
},
101 {"attiny2313", AVR_ISA_AVR25
, bfd_mach_avr25
},
102 {"attiny2313a",AVR_ISA_AVR25
, bfd_mach_avr25
},
103 {"attiny24", AVR_ISA_AVR25
, bfd_mach_avr25
},
104 {"attiny24a", AVR_ISA_AVR25
, bfd_mach_avr25
},
105 {"attiny4313", AVR_ISA_AVR25
, bfd_mach_avr25
},
106 {"attiny44", AVR_ISA_AVR25
, bfd_mach_avr25
},
107 {"attiny44a", AVR_ISA_AVR25
, bfd_mach_avr25
},
108 {"attiny84", AVR_ISA_AVR25
, bfd_mach_avr25
},
109 {"attiny25", AVR_ISA_AVR25
, bfd_mach_avr25
},
110 {"attiny45", AVR_ISA_AVR25
, bfd_mach_avr25
},
111 {"attiny85", AVR_ISA_AVR25
, bfd_mach_avr25
},
112 {"attiny261", AVR_ISA_AVR25
, bfd_mach_avr25
},
113 {"attiny261a", AVR_ISA_AVR25
, bfd_mach_avr25
},
114 {"attiny461", AVR_ISA_AVR25
, bfd_mach_avr25
},
115 {"attiny861", AVR_ISA_AVR25
, bfd_mach_avr25
},
116 {"attiny861a", AVR_ISA_AVR25
, bfd_mach_avr25
},
117 {"attiny87", AVR_ISA_AVR25
, bfd_mach_avr25
},
118 {"attiny43u", AVR_ISA_AVR25
, bfd_mach_avr25
},
119 {"attiny48", AVR_ISA_AVR25
, bfd_mach_avr25
},
120 {"attiny88", AVR_ISA_AVR25
, bfd_mach_avr25
},
121 {"at86rf401", AVR_ISA_RF401
, bfd_mach_avr25
},
122 {"ata6289", AVR_ISA_AVR25
, bfd_mach_avr25
},
123 {"at43usb355", AVR_ISA_AVR3
, bfd_mach_avr3
},
124 {"at76c711", AVR_ISA_AVR3
, bfd_mach_avr3
},
125 {"atmega103", AVR_ISA_AVR31
, bfd_mach_avr31
},
126 {"at43usb320", AVR_ISA_AVR31
, bfd_mach_avr31
},
127 {"attiny167", AVR_ISA_AVR35
, bfd_mach_avr35
},
128 {"attiny327", AVR_ISA_AVR35
, bfd_mach_avr35
},
129 {"at90usb82", AVR_ISA_AVR35
, bfd_mach_avr35
},
130 {"at90usb162", AVR_ISA_AVR35
, bfd_mach_avr35
},
131 {"atmega8u2", AVR_ISA_AVR35
, bfd_mach_avr35
},
132 {"atmega16u2", AVR_ISA_AVR35
, bfd_mach_avr35
},
133 {"atmega32u2", AVR_ISA_AVR35
, bfd_mach_avr35
},
134 {"atmega8", AVR_ISA_M8
, bfd_mach_avr4
},
135 {"atmega48", AVR_ISA_AVR4
, bfd_mach_avr4
},
136 {"atmega48p", AVR_ISA_AVR4
, bfd_mach_avr4
},
137 {"atmega88", AVR_ISA_AVR4
, bfd_mach_avr4
},
138 {"atmega88p", AVR_ISA_AVR4
, bfd_mach_avr4
},
139 {"atmega8515", AVR_ISA_M8
, bfd_mach_avr4
},
140 {"atmega8535", AVR_ISA_M8
, bfd_mach_avr4
},
141 {"atmega8hva", AVR_ISA_AVR4
, bfd_mach_avr4
},
142 {"atmega4hvd", AVR_ISA_AVR4
, bfd_mach_avr4
},
143 {"atmega8hvd", AVR_ISA_AVR4
, bfd_mach_avr4
},
144 {"atmega8c1", AVR_ISA_AVR4
, bfd_mach_avr4
},
145 {"atmega8m1", AVR_ISA_AVR4
, bfd_mach_avr4
},
146 {"at90pwm1", AVR_ISA_AVR4
, bfd_mach_avr4
},
147 {"at90pwm2", AVR_ISA_AVR4
, bfd_mach_avr4
},
148 {"at90pwm2b", AVR_ISA_AVR4
, bfd_mach_avr4
},
149 {"at90pwm3", AVR_ISA_AVR4
, bfd_mach_avr4
},
150 {"at90pwm3b", AVR_ISA_AVR4
, bfd_mach_avr4
},
151 {"at90pwm81", AVR_ISA_AVR4
, bfd_mach_avr4
},
152 {"atmega16", AVR_ISA_AVR5
, bfd_mach_avr5
},
153 {"atmega161", AVR_ISA_M161
, bfd_mach_avr5
},
154 {"atmega162", AVR_ISA_AVR5
, bfd_mach_avr5
},
155 {"atmega163", AVR_ISA_M161
, bfd_mach_avr5
},
156 {"atmega164p", AVR_ISA_AVR5
, bfd_mach_avr5
},
157 {"atmega165", AVR_ISA_AVR5
, bfd_mach_avr5
},
158 {"atmega165p", AVR_ISA_AVR5
, bfd_mach_avr5
},
159 {"atmega168", AVR_ISA_AVR5
, bfd_mach_avr5
},
160 {"atmega168p", AVR_ISA_AVR5
, bfd_mach_avr5
},
161 {"atmega169", AVR_ISA_AVR5
, bfd_mach_avr5
},
162 {"atmega169p", AVR_ISA_AVR5
, bfd_mach_avr5
},
163 {"atmega16c1", AVR_ISA_AVR5
, bfd_mach_avr5
},
164 {"atmega32", AVR_ISA_AVR5
, bfd_mach_avr5
},
165 {"atmega323", AVR_ISA_AVR5
, bfd_mach_avr5
},
166 {"atmega324p", AVR_ISA_AVR5
, bfd_mach_avr5
},
167 {"atmega325", AVR_ISA_AVR5
, bfd_mach_avr5
},
168 {"atmega325p", AVR_ISA_AVR5
, bfd_mach_avr5
},
169 {"atmega3250", AVR_ISA_AVR5
, bfd_mach_avr5
},
170 {"atmega3250p",AVR_ISA_AVR5
, bfd_mach_avr5
},
171 {"atmega328p", AVR_ISA_AVR5
, bfd_mach_avr5
},
172 {"atmega329", AVR_ISA_AVR5
, bfd_mach_avr5
},
173 {"atmega329p", AVR_ISA_AVR5
, bfd_mach_avr5
},
174 {"atmega3290", AVR_ISA_AVR5
, bfd_mach_avr5
},
175 {"atmega3290p",AVR_ISA_AVR5
, bfd_mach_avr5
},
176 {"atmega406", AVR_ISA_AVR5
, bfd_mach_avr5
},
177 {"atmega64", AVR_ISA_AVR5
, bfd_mach_avr5
},
178 {"atmega640", AVR_ISA_AVR5
, bfd_mach_avr5
},
179 {"atmega644", AVR_ISA_AVR5
, bfd_mach_avr5
},
180 {"atmega644p", AVR_ISA_AVR5
, bfd_mach_avr5
},
181 {"atmega644pa",AVR_ISA_AVR5
, bfd_mach_avr5
},
182 {"atmega645", AVR_ISA_AVR5
, bfd_mach_avr5
},
183 {"atmega649", AVR_ISA_AVR5
, bfd_mach_avr5
},
184 {"atmega6450", AVR_ISA_AVR5
, bfd_mach_avr5
},
185 {"atmega6490", AVR_ISA_AVR5
, bfd_mach_avr5
},
186 {"atmega16hva",AVR_ISA_AVR5
, bfd_mach_avr5
},
187 {"atmega16hvb",AVR_ISA_AVR5
, bfd_mach_avr5
},
188 {"atmega32hvb",AVR_ISA_AVR5
, bfd_mach_avr5
},
189 {"at90can32" , AVR_ISA_AVR5
, bfd_mach_avr5
},
190 {"at90can64" , AVR_ISA_AVR5
, bfd_mach_avr5
},
191 {"at90pwm216", AVR_ISA_AVR5
, bfd_mach_avr5
},
192 {"at90pwm316", AVR_ISA_AVR5
, bfd_mach_avr5
},
193 {"atmega32c1", AVR_ISA_AVR5
, bfd_mach_avr5
},
194 {"atmega64c1", AVR_ISA_AVR5
, bfd_mach_avr5
},
195 {"atmega16m1", AVR_ISA_AVR5
, bfd_mach_avr5
},
196 {"atmega32m1", AVR_ISA_AVR5
, bfd_mach_avr5
},
197 {"atmega64m1", AVR_ISA_AVR5
, bfd_mach_avr5
},
198 {"atmega16u4", AVR_ISA_AVR5
, bfd_mach_avr5
},
199 {"atmega32u4", AVR_ISA_AVR5
, bfd_mach_avr5
},
200 {"atmega32u6", AVR_ISA_AVR5
, bfd_mach_avr5
},
201 {"at90usb646", AVR_ISA_AVR5
, bfd_mach_avr5
},
202 {"at90usb647", AVR_ISA_AVR5
, bfd_mach_avr5
},
203 {"at90scr100", AVR_ISA_AVR5
, bfd_mach_avr5
},
204 {"at94k", AVR_ISA_94K
, bfd_mach_avr5
},
205 {"atmega128", AVR_ISA_AVR51
, bfd_mach_avr51
},
206 {"atmega1280", AVR_ISA_AVR51
, bfd_mach_avr51
},
207 {"atmega1281", AVR_ISA_AVR51
, bfd_mach_avr51
},
208 {"atmega1284p",AVR_ISA_AVR51
, bfd_mach_avr51
},
209 {"atmega128rfa1",AVR_ISA_AVR51
, bfd_mach_avr51
},
210 {"at90can128", AVR_ISA_AVR51
, bfd_mach_avr51
},
211 {"at90usb1286",AVR_ISA_AVR51
, bfd_mach_avr51
},
212 {"at90usb1287",AVR_ISA_AVR51
, bfd_mach_avr51
},
213 {"m3000f", AVR_ISA_AVR51
, bfd_mach_avr51
},
214 {"m3000s", AVR_ISA_AVR51
, bfd_mach_avr51
},
215 {"m3001b", AVR_ISA_AVR51
, bfd_mach_avr51
},
216 {"atmega2560", AVR_ISA_AVR6
, bfd_mach_avr6
},
217 {"atmega2561", AVR_ISA_AVR6
, bfd_mach_avr6
},
221 /* Current MCU type. */
222 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_AVR2
, bfd_mach_avr2
};
223 static struct mcu_type_s
* avr_mcu
= & default_mcu
;
225 /* AVR target-specific switches. */
228 int all_opcodes
; /* -mall-opcodes: accept all known AVR opcodes. */
229 int no_skip_bug
; /* -mno-skip-bug: no warnings for skipping 2-word insns. */
230 int no_wrap
; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around. */
233 static struct avr_opt_s avr_opt
= { 0, 0, 0 };
235 const char EXP_CHARS
[] = "eE";
236 const char FLT_CHARS
[] = "dD";
238 static void avr_set_arch (int);
240 /* The target specific pseudo-ops which we support. */
241 const pseudo_typeS md_pseudo_table
[] =
243 {"arch", avr_set_arch
, 0},
247 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
249 #define EXP_MOD_NAME(i) exp_mod[i].name
250 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
251 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
252 #define HAVE_PM_P(i) exp_mod[i].have_pm
257 bfd_reloc_code_real_type reloc
;
258 bfd_reloc_code_real_type neg_reloc
;
262 static struct exp_mod_s exp_mod
[] =
264 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
265 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
266 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
267 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
268 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
269 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
270 {"hlo8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 0},
271 {"hhi8", BFD_RELOC_AVR_MS8_LDI
, BFD_RELOC_AVR_MS8_LDI_NEG
, 0},
274 /* A union used to store indicies into the exp_mod[] array
275 in a hash table which expects void * data types. */
282 /* Opcode hash table. */
283 static struct hash_control
*avr_hash
;
285 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
286 static struct hash_control
*avr_mod_hash
;
288 #define OPTION_MMCU 'm'
291 OPTION_ALL_OPCODES
= OPTION_MD_BASE
+ 1,
296 struct option md_longopts
[] =
298 { "mmcu", required_argument
, NULL
, OPTION_MMCU
},
299 { "mall-opcodes", no_argument
, NULL
, OPTION_ALL_OPCODES
},
300 { "mno-skip-bug", no_argument
, NULL
, OPTION_NO_SKIP_BUG
},
301 { "mno-wrap", no_argument
, NULL
, OPTION_NO_WRAP
},
302 { NULL
, no_argument
, NULL
, 0 }
305 size_t md_longopts_size
= sizeof (md_longopts
);
307 /* Display nicely formatted list of known MCU names. */
310 show_mcu_list (FILE *stream
)
314 fprintf (stream
, _("Known MCU names:"));
317 for (i
= 0; mcu_types
[i
].name
; i
++)
319 int len
= strlen (mcu_types
[i
].name
);
324 fprintf (stream
, " %s", mcu_types
[i
].name
);
327 fprintf (stream
, "\n %s", mcu_types
[i
].name
);
332 fprintf (stream
, "\n");
338 while (*s
== ' ' || *s
== '\t')
343 /* Extract one word from FROM and copy it to TO. */
346 extract_word (char *from
, char *to
, int limit
)
352 /* Drop leading whitespace. */
353 from
= skip_space (from
);
356 /* Find the op code end. */
357 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name (*op_end
);)
359 to
[size
++] = *op_end
++;
360 if (size
+ 1 >= limit
)
369 md_estimate_size_before_relax (fragS
*fragp ATTRIBUTE_UNUSED
,
370 asection
*seg ATTRIBUTE_UNUSED
)
377 md_show_usage (FILE *stream
)
381 " -mmcu=[avr-name] select microcontroller variant\n"
382 " [avr-name] can be:\n"
383 " avr1 - classic AVR core without data RAM\n"
384 " avr2 - classic AVR core with up to 8K program memory\n"
385 " avr25 - classic AVR core with up to 8K program memory\n"
386 " plus the MOVW instruction\n"
387 " avr3 - classic AVR core with up to 64K program memory\n"
388 " avr31 - classic AVR core with up to 128K program memory\n"
389 " avr35 - classic AVR core with up to 64K program memory\n"
390 " plus the MOVW instruction\n"
391 " avr4 - enhanced AVR core with up to 8K program memory\n"
392 " avr5 - enhanced AVR core with up to 64K program memory\n"
393 " avr51 - enhanced AVR core with up to 128K program memory\n"
394 " avr6 - enhanced AVR core with up to 256K program memory\n"
395 " or immediate microcontroller name.\n"));
397 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
398 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
399 " (default for avr4, avr5)\n"
400 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
401 " (default for avr3, avr5)\n"));
402 show_mcu_list (stream
);
406 avr_set_arch (int dummy ATTRIBUTE_UNUSED
)
410 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
411 md_parse_option (OPTION_MMCU
, str
);
412 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
416 md_parse_option (int c
, char *arg
)
423 char *s
= alloca (strlen (arg
) + 1);
430 *t
= TOLOWER (*arg1
++);
434 for (i
= 0; mcu_types
[i
].name
; ++i
)
435 if (strcmp (mcu_types
[i
].name
, s
) == 0)
438 if (!mcu_types
[i
].name
)
440 show_mcu_list (stderr
);
441 as_fatal (_("unknown MCU: %s\n"), arg
);
444 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
445 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
446 as .arch ... in the asm output at the same time. */
447 if (avr_mcu
== &default_mcu
|| avr_mcu
->mach
== mcu_types
[i
].mach
)
448 avr_mcu
= &mcu_types
[i
];
450 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
451 avr_mcu
->name
, mcu_types
[i
].name
);
454 case OPTION_ALL_OPCODES
:
455 avr_opt
.all_opcodes
= 1;
457 case OPTION_NO_SKIP_BUG
:
458 avr_opt
.no_skip_bug
= 1;
469 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
475 md_atof (int type
, char *litP
, int *sizeP
)
477 return ieee_md_atof (type
, litP
, sizeP
, FALSE
);
481 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
,
482 asection
*sec ATTRIBUTE_UNUSED
,
483 fragS
*fragP ATTRIBUTE_UNUSED
)
492 struct avr_opcodes_s
*opcode
;
494 avr_hash
= hash_new ();
496 /* Insert unique names into hash table. This hash table then provides a
497 quick index to the first opcode with a particular name in the opcode
499 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
500 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
502 avr_mod_hash
= hash_new ();
504 for (i
= 0; i
< ARRAY_SIZE (exp_mod
); ++i
)
509 hash_insert (avr_mod_hash
, EXP_MOD_NAME (i
), m
.ptr
);
512 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
515 /* Resolve STR as a constant expression and return the result.
516 If result greater than MAX then error. */
519 avr_get_constant (char *str
, int max
)
523 str
= skip_space (str
);
524 input_line_pointer
= str
;
527 if (ex
.X_op
!= O_constant
)
528 as_bad (_("constant value required"));
530 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
531 as_bad (_("number must be positive and less than %d"), max
+ 1);
533 return ex
.X_add_number
;
536 /* Parse for ldd/std offset. */
539 avr_offset_expression (expressionS
*exp
)
541 char *str
= input_line_pointer
;
546 str
= extract_word (str
, op
, sizeof (op
));
548 input_line_pointer
= tmp
;
551 /* Warn about expressions that fail to use lo8 (). */
552 if (exp
->X_op
== O_constant
)
554 int x
= exp
->X_add_number
;
556 if (x
< -255 || x
> 255)
557 as_warn (_("constant out of 8-bit range: %d"), x
);
561 /* Parse ordinary expression. */
564 parse_exp (char *s
, expressionS
*op
)
566 input_line_pointer
= s
;
568 if (op
->X_op
== O_absent
)
569 as_bad (_("missing operand"));
570 return input_line_pointer
;
573 /* Parse special expressions (needed for LDI command):
578 where xx is: hh, hi, lo. */
580 static bfd_reloc_code_real_type
581 avr_ldi_expression (expressionS
*exp
)
583 char *str
= input_line_pointer
;
587 int linker_stubs_should_be_generated
= 0;
591 str
= extract_word (str
, op
, sizeof (op
));
597 m
.ptr
= hash_find (avr_mod_hash
, op
);
605 str
= skip_space (str
);
609 bfd_reloc_code_real_type reloc_to_return
;
614 if (strncmp ("pm(", str
, 3) == 0
615 || strncmp ("gs(",str
,3) == 0
616 || strncmp ("-(gs(",str
,5) == 0
617 || strncmp ("-(pm(", str
, 5) == 0)
625 as_bad (_("illegal expression"));
627 if (str
[0] == 'g' || str
[2] == 'g')
628 linker_stubs_should_be_generated
= 1;
640 if (*str
== '-' && *(str
+ 1) == '(')
647 input_line_pointer
= str
;
652 if (*input_line_pointer
!= ')')
654 as_bad (_("`)' required"));
657 input_line_pointer
++;
662 neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
663 if (linker_stubs_should_be_generated
)
665 switch (reloc_to_return
)
667 case BFD_RELOC_AVR_LO8_LDI_PM
:
668 reloc_to_return
= BFD_RELOC_AVR_LO8_LDI_GS
;
670 case BFD_RELOC_AVR_HI8_LDI_PM
:
671 reloc_to_return
= BFD_RELOC_AVR_HI8_LDI_GS
;
675 /* PR 5523: Do not generate a warning here,
676 legitimate code can trigger this case. */
680 return reloc_to_return
;
685 input_line_pointer
= tmp
;
688 /* Warn about expressions that fail to use lo8 (). */
689 if (exp
->X_op
== O_constant
)
691 int x
= exp
->X_add_number
;
693 if (x
< -255 || x
> 255)
694 as_warn (_("constant out of 8-bit range: %d"), x
);
697 return BFD_RELOC_AVR_LDI
;
700 /* Parse one instruction operand.
701 Return operand bitmask. Also fixups can be generated. */
704 avr_operand (struct avr_opcodes_s
*opcode
,
710 unsigned int op_mask
= 0;
711 char *str
= skip_space (*line
);
715 /* Any register operand. */
721 if (*str
== 'r' || *str
== 'R')
725 str
= extract_word (str
, r_name
, sizeof (r_name
));
727 if (ISDIGIT (r_name
[1]))
729 if (r_name
[2] == '\0')
730 op_mask
= r_name
[1] - '0';
731 else if (r_name
[1] != '0'
732 && ISDIGIT (r_name
[2])
733 && r_name
[3] == '\0')
734 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
739 op_mask
= avr_get_constant (str
, 31);
740 str
= input_line_pointer
;
748 if (op_mask
< 16 || op_mask
> 23)
749 as_bad (_("register r16-r23 required"));
755 as_bad (_("register number above 15 required"));
761 as_bad (_("even register number required"));
766 if ((op_mask
& 1) || op_mask
< 24)
767 as_bad (_("register r24, r26, r28 or r30 required"));
768 op_mask
= (op_mask
- 24) >> 1;
773 as_bad (_("register name or number from 0 to 31 required"));
782 str
= skip_space (str
+ 1);
791 as_bad (_("pointer register (X, Y or Z) required"));
793 str
= skip_space (str
+ 1);
798 as_bad (_("cannot both predecrement and postincrement"));
802 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
803 registers, no predecrement, no postincrement. */
804 if (!avr_opt
.all_opcodes
&& (op_mask
& 0x100F)
805 && !(avr_mcu
->isa
& AVR_ISA_SRAM
))
806 as_bad (_("addressing mode not supported"));
812 as_bad (_("can't predecrement"));
814 if (! (*str
== 'z' || *str
== 'Z'))
815 as_bad (_("pointer register Z required"));
817 str
= skip_space (str
+ 1);
825 /* attiny26 can do "lpm" and "lpm r,Z" but not "lpm r,Z+". */
826 if (!avr_opt
.all_opcodes
827 && (op_mask
& 0x0001)
828 && !(avr_mcu
->isa
& AVR_ISA_MOVW
))
829 as_bad (_("postincrement not supported"));
834 char c
= TOLOWER (*str
++);
839 as_bad (_("pointer register (Y or Z) required"));
840 str
= skip_space (str
);
843 input_line_pointer
= str
;
844 avr_offset_expression (& op_expr
);
845 str
= input_line_pointer
;
846 fix_new_exp (frag_now
, where
, 3,
847 &op_expr
, FALSE
, BFD_RELOC_AVR_6
);
853 str
= parse_exp (str
, &op_expr
);
854 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
855 &op_expr
, FALSE
, BFD_RELOC_AVR_CALL
);
859 str
= parse_exp (str
, &op_expr
);
860 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
861 &op_expr
, TRUE
, BFD_RELOC_AVR_13_PCREL
);
865 str
= parse_exp (str
, &op_expr
);
866 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
867 &op_expr
, TRUE
, BFD_RELOC_AVR_7_PCREL
);
871 str
= parse_exp (str
, &op_expr
);
872 fix_new_exp (frag_now
, where
+ 2, opcode
->insn_size
* 2,
873 &op_expr
, FALSE
, BFD_RELOC_16
);
878 bfd_reloc_code_real_type r_type
;
880 input_line_pointer
= str
;
881 r_type
= avr_ldi_expression (&op_expr
);
882 str
= input_line_pointer
;
883 fix_new_exp (frag_now
, where
, 3,
884 &op_expr
, FALSE
, r_type
);
892 x
= ~avr_get_constant (str
, 255);
893 str
= input_line_pointer
;
894 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
899 input_line_pointer
= str
;
900 avr_offset_expression (& op_expr
);
901 str
= input_line_pointer
;
902 fix_new_exp (frag_now
, where
, 3,
903 & op_expr
, FALSE
, BFD_RELOC_AVR_6_ADIW
);
911 x
= avr_get_constant (str
, 7);
912 str
= input_line_pointer
;
923 x
= avr_get_constant (str
, 63);
924 str
= input_line_pointer
;
925 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
933 x
= avr_get_constant (str
, 31);
934 str
= input_line_pointer
;
943 as_bad (_("unknown constraint `%c'"), *op
);
950 /* Parse instruction operands.
951 Return binary opcode. */
954 avr_operands (struct avr_opcodes_s
*opcode
, char **line
)
956 char *op
= opcode
->constraints
;
957 unsigned int bin
= opcode
->bin_opcode
;
958 char *frag
= frag_more (opcode
->insn_size
* 2);
960 int where
= frag
- frag_now
->fr_literal
;
961 static unsigned int prev
= 0; /* Previous opcode. */
963 /* Opcode have operands. */
966 unsigned int reg1
= 0;
967 unsigned int reg2
= 0;
968 int reg1_present
= 0;
969 int reg2_present
= 0;
971 /* Parse first operand. */
972 if (REGISTER_P (*op
))
974 reg1
= avr_operand (opcode
, where
, op
, &str
);
977 /* Parse second operand. */
990 if (REGISTER_P (*op
))
993 str
= skip_space (str
);
995 as_bad (_("`,' required"));
996 str
= skip_space (str
);
998 reg2
= avr_operand (opcode
, where
, op
, &str
);
1001 if (reg1_present
&& reg2_present
)
1002 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
1003 else if (reg2_present
)
1011 /* Detect undefined combinations (like ld r31,Z+). */
1012 if (!avr_opt
.all_opcodes
&& AVR_UNDEF_P (bin
))
1013 as_warn (_("undefined combination of operands"));
1015 if (opcode
->insn_size
== 2)
1017 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
1018 (AVR core bug, fixed in the newer devices). */
1019 if (!(avr_opt
.no_skip_bug
||
1020 (avr_mcu
->isa
& (AVR_ISA_MUL
| AVR_ISA_MOVW
)))
1021 && AVR_SKIP_P (prev
))
1022 as_warn (_("skipping two-word instruction"));
1024 bfd_putl32 ((bfd_vma
) bin
, frag
);
1027 bfd_putl16 ((bfd_vma
) bin
, frag
);
1034 /* GAS will call this function for each section at the end of the assembly,
1035 to permit the CPU backend to adjust the alignment of a section. */
1038 md_section_align (asection
*seg
, valueT addr
)
1040 int align
= bfd_get_section_alignment (stdoutput
, seg
);
1041 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
1044 /* If you define this macro, it should return the offset between the
1045 address of a PC relative fixup and the position from which the PC
1046 relative adjustment should be made. On many processors, the base
1047 of a PC relative instruction is the next instruction, so this
1048 macro would return the length of an instruction. */
1051 md_pcrel_from_section (fixS
*fixp
, segT sec
)
1053 if (fixp
->fx_addsy
!= (symbolS
*) NULL
1054 && (!S_IS_DEFINED (fixp
->fx_addsy
)
1055 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
1058 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1061 /* GAS will call this for each fixup. It should store the correct
1062 value in the object file. */
1065 md_apply_fix (fixS
*fixP
, valueT
* valP
, segT seg
)
1067 unsigned char *where
;
1071 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
1074 else if (fixP
->fx_pcrel
)
1076 segT s
= S_GET_SEGMENT (fixP
->fx_addsy
);
1078 if (s
== seg
|| s
== absolute_section
)
1080 value
+= S_GET_VALUE (fixP
->fx_addsy
);
1085 /* We don't actually support subtracting a symbol. */
1086 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
1087 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
1089 switch (fixP
->fx_r_type
)
1092 fixP
->fx_no_overflow
= 1;
1094 case BFD_RELOC_AVR_7_PCREL
:
1095 case BFD_RELOC_AVR_13_PCREL
:
1098 case BFD_RELOC_AVR_CALL
:
1104 /* Fetch the instruction, insert the fully resolved operand
1105 value, and stuff the instruction back again. */
1106 where
= (unsigned char *) fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1107 insn
= bfd_getl16 (where
);
1109 switch (fixP
->fx_r_type
)
1111 case BFD_RELOC_AVR_7_PCREL
:
1113 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1114 _("odd address operand: %ld"), value
);
1116 /* Instruction addresses are always right-shifted by 1. */
1118 --value
; /* Correct PC. */
1120 if (value
< -64 || value
> 63)
1121 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1122 _("operand out of range: %ld"), value
);
1123 value
= (value
<< 3) & 0x3f8;
1124 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
1127 case BFD_RELOC_AVR_13_PCREL
:
1129 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1130 _("odd address operand: %ld"), value
);
1132 /* Instruction addresses are always right-shifted by 1. */
1134 --value
; /* Correct PC. */
1136 if (value
< -2048 || value
> 2047)
1138 /* No wrap for devices with >8K of program memory. */
1139 if ((avr_mcu
->isa
& AVR_ISA_MEGA
) || avr_opt
.no_wrap
)
1140 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1141 _("operand out of range: %ld"), value
);
1145 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
1149 bfd_putl16 ((bfd_vma
) value
, where
);
1153 bfd_putl16 ((bfd_vma
) value
, where
);
1157 if (value
> 255 || value
< -128)
1158 as_warn_where (fixP
->fx_file
, fixP
->fx_line
,
1159 _("operand out of range: %ld"), value
);
1163 case BFD_RELOC_AVR_16_PM
:
1164 bfd_putl16 ((bfd_vma
) (value
>> 1), where
);
1167 case BFD_RELOC_AVR_LDI
:
1169 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1170 _("operand out of range: %ld"), value
);
1171 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
1174 case BFD_RELOC_AVR_6
:
1175 if ((value
> 63) || (value
< 0))
1176 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1177 _("operand out of range: %ld"), value
);
1178 bfd_putl16 ((bfd_vma
) insn
| ((value
& 7) | ((value
& (3 << 3)) << 7) | ((value
& (1 << 5)) << 8)), where
);
1181 case BFD_RELOC_AVR_6_ADIW
:
1182 if ((value
> 63) || (value
< 0))
1183 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1184 _("operand out of range: %ld"), value
);
1185 bfd_putl16 ((bfd_vma
) insn
| (value
& 0xf) | ((value
& 0x30) << 2), where
);
1188 case BFD_RELOC_AVR_LO8_LDI
:
1189 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
1192 case BFD_RELOC_AVR_HI8_LDI
:
1193 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
1196 case BFD_RELOC_AVR_MS8_LDI
:
1197 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
1200 case BFD_RELOC_AVR_HH8_LDI
:
1201 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
1204 case BFD_RELOC_AVR_LO8_LDI_NEG
:
1205 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
1208 case BFD_RELOC_AVR_HI8_LDI_NEG
:
1209 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
1212 case BFD_RELOC_AVR_MS8_LDI_NEG
:
1213 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
1216 case BFD_RELOC_AVR_HH8_LDI_NEG
:
1217 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
1220 case BFD_RELOC_AVR_LO8_LDI_PM
:
1221 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
1224 case BFD_RELOC_AVR_HI8_LDI_PM
:
1225 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
1228 case BFD_RELOC_AVR_HH8_LDI_PM
:
1229 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
1232 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
1233 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
1236 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
1237 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
1240 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
1241 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
1244 case BFD_RELOC_AVR_CALL
:
1248 x
= bfd_getl16 (where
);
1250 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1251 _("odd address operand: %ld"), value
);
1253 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
1254 bfd_putl16 ((bfd_vma
) x
, where
);
1255 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+ 2);
1260 as_fatal (_("line %d: unknown relocation type: 0x%x"),
1261 fixP
->fx_line
, fixP
->fx_r_type
);
1267 switch ((int) fixP
->fx_r_type
)
1269 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
1270 case -BFD_RELOC_AVR_HI8_LDI
:
1271 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
1272 case -BFD_RELOC_AVR_LO8_LDI
:
1273 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1274 _("only constant expression allowed"));
1283 /* GAS will call this to generate a reloc, passing the resulting reloc
1284 to `bfd_install_relocation'. This currently works poorly, as
1285 `bfd_install_relocation' often does the wrong thing, and instances of
1286 `tc_gen_reloc' have been written to work around the problems, which
1287 in turns makes it difficult to fix `bfd_install_relocation'. */
1289 /* If while processing a fixup, a reloc really needs to be created
1290 then it is done here. */
1293 tc_gen_reloc (asection
*seg ATTRIBUTE_UNUSED
,
1298 if (fixp
->fx_addsy
&& fixp
->fx_subsy
)
1302 if ((S_GET_SEGMENT (fixp
->fx_addsy
) != S_GET_SEGMENT (fixp
->fx_subsy
))
1303 || S_GET_SEGMENT (fixp
->fx_addsy
) == undefined_section
)
1305 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1306 "Difference of symbols in different sections is not supported");
1310 /* We are dealing with two symbols defined in the same section.
1311 Let us fix-up them here. */
1312 value
+= S_GET_VALUE (fixp
->fx_addsy
);
1313 value
-= S_GET_VALUE (fixp
->fx_subsy
);
1315 /* When fx_addsy and fx_subsy both are zero, md_apply_fix
1316 only takes it's second operands for the fixup value. */
1317 fixp
->fx_addsy
= NULL
;
1318 fixp
->fx_subsy
= NULL
;
1319 md_apply_fix (fixp
, (valueT
*) &value
, NULL
);
1324 reloc
= xmalloc (sizeof (arelent
));
1326 reloc
->sym_ptr_ptr
= xmalloc (sizeof (asymbol
*));
1327 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1329 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1330 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1331 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
1333 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1334 _("reloc %d not supported by object file format"),
1335 (int) fixp
->fx_r_type
);
1339 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1340 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1341 reloc
->address
= fixp
->fx_offset
;
1343 reloc
->addend
= fixp
->fx_offset
;
1349 md_assemble (char *str
)
1351 struct avr_opcodes_s
*opcode
;
1354 str
= skip_space (extract_word (str
, op
, sizeof (op
)));
1357 as_bad (_("can't find opcode "));
1359 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1363 as_bad (_("unknown opcode `%s'"), op
);
1367 /* Special case for opcodes with optional operands (lpm, elpm) -
1368 version with operands exists in avr_opcodes[] in the next entry. */
1370 if (*str
&& *opcode
->constraints
== '?')
1373 if (!avr_opt
.all_opcodes
&& (opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1374 as_bad (_("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1376 dwarf2_emit_insn (0);
1378 /* We used to set input_line_pointer to the result of get_operands,
1379 but that is wrong. Our caller assumes we don't change it. */
1381 char *t
= input_line_pointer
;
1383 avr_operands (opcode
, &str
);
1384 if (*skip_space (str
))
1385 as_bad (_("garbage at end of line"));
1386 input_line_pointer
= t
;
1390 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1391 `avr_cons_fix_new'. */
1392 static int exp_mod_pm
= 0;
1394 /* Parse special CONS expression: pm (expression)
1395 or alternatively: gs (expression).
1396 These are used for addressing program memory.
1397 Relocation: BFD_RELOC_AVR_16_PM. */
1400 avr_parse_cons_expression (expressionS
*exp
, int nbytes
)
1406 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1410 char *pm_name1
= "pm";
1411 char *pm_name2
= "gs";
1412 int len
= strlen (pm_name1
);
1413 /* len must be the same for both pm identifiers. */
1415 if (strncasecmp (input_line_pointer
, pm_name1
, len
) == 0
1416 || strncasecmp (input_line_pointer
, pm_name2
, len
) == 0)
1418 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1420 if (*input_line_pointer
== '(')
1422 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1426 if (*input_line_pointer
== ')')
1427 ++input_line_pointer
;
1430 as_bad (_("`)' required"));
1437 input_line_pointer
= tmp
;
1445 avr_cons_fix_new (fragS
*frag
,
1450 if (exp_mod_pm
== 0)
1453 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_8
);
1454 else if (nbytes
== 2)
1455 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_16
);
1456 else if (nbytes
== 4)
1457 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_32
);
1459 as_bad (_("illegal %srelocation size: %d"), "", nbytes
);
1464 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_AVR_16_PM
);
1466 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes
);