Commit | Line | Data |
---|---|---|
adde6300 AM |
1 | /* tc-avr.c -- Assembler code for the ATMEL AVR |
2 | ||
8ad7c533 | 3 | Copyright 1999, 2000, 2001, 2002, 2004, 2005, 2006 |
2132e3a3 | 4 | Free Software Foundation, Inc. |
adde6300 AM |
5 | Contributed by Denis Chertykov <denisc@overta.ru> |
6 | ||
7 | This file is part of GAS, the GNU Assembler. | |
8 | ||
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 2, or (at your option) | |
12 | any later version. | |
13 | ||
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. | |
18 | ||
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 | |
4b4da160 NC |
21 | the Free Software Foundation, 51 Franklin Street - Fifth Floor, |
22 | Boston, MA 02110-1301, USA. */ | |
adde6300 AM |
23 | |
24 | #include <stdio.h> | |
adde6300 | 25 | #include "as.h" |
3882b010 | 26 | #include "safe-ctype.h" |
adde6300 | 27 | #include "subsegs.h" |
dc191a8f | 28 | #include "libiberty.h" |
adde6300 | 29 | |
1188e082 DC |
30 | struct avr_opcodes_s |
31 | { | |
dc191a8f NC |
32 | char * name; |
33 | char * constraints; | |
34 | int insn_size; /* In words. */ | |
35 | int isa; | |
36 | unsigned int bin_opcode; | |
1188e082 DC |
37 | }; |
38 | ||
39 | #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \ | |
40 | {#NAME, CONSTR, SIZE, ISA, BIN}, | |
41 | ||
42 | struct avr_opcodes_s avr_opcodes[] = | |
43 | { | |
44 | #include "opcode/avr.h" | |
45 | {NULL, NULL, 0, 0, 0} | |
46 | }; | |
47 | ||
adde6300 AM |
48 | const char comment_chars[] = ";"; |
49 | const char line_comment_chars[] = "#"; | |
50 | const char line_separator_chars[] = "$"; | |
51 | ||
adde6300 AM |
52 | const char *md_shortopts = "m:"; |
53 | struct mcu_type_s | |
54 | { | |
55 | char *name; | |
56 | int isa; | |
57 | int mach; | |
58 | }; | |
59 | ||
1f8ae5e6 DC |
60 | /* XXX - devices that don't seem to exist (renamed, replaced with larger |
61 | ones, or planned but never produced), left here for compatibility. | |
62 | TODO: hide them in show_mcu_list output? */ | |
63 | ||
adde6300 AM |
64 | static struct mcu_type_s mcu_types[] = |
65 | { | |
b170af93 | 66 | {"avr1", AVR_ISA_TINY1, bfd_mach_avr1}, |
71d7ecf2 | 67 | {"avr2", AVR_ISA_TINY2, bfd_mach_avr2}, |
b170af93 | 68 | {"avr3", AVR_ISA_M103, bfd_mach_avr3}, |
1f8ae5e6 | 69 | {"avr4", AVR_ISA_M8, bfd_mach_avr4}, |
65aa24b6 | 70 | {"avr5", AVR_ISA_ALL, bfd_mach_avr5}, |
adde6300 | 71 | {"at90s1200", AVR_ISA_1200, bfd_mach_avr1}, |
1f8ae5e6 | 72 | {"attiny10", AVR_ISA_TINY1, bfd_mach_avr1}, /* XXX -> tn11 */ |
b170af93 DC |
73 | {"attiny11", AVR_ISA_TINY1, bfd_mach_avr1}, |
74 | {"attiny12", AVR_ISA_TINY1, bfd_mach_avr1}, | |
75 | {"attiny15", AVR_ISA_TINY1, bfd_mach_avr1}, | |
76 | {"attiny28", AVR_ISA_TINY1, bfd_mach_avr1}, | |
adde6300 AM |
77 | {"at90s2313", AVR_ISA_2xxx, bfd_mach_avr2}, |
78 | {"at90s2323", AVR_ISA_2xxx, bfd_mach_avr2}, | |
1f8ae5e6 | 79 | {"at90s2333", AVR_ISA_2xxx, bfd_mach_avr2}, /* XXX -> 4433 */ |
adde6300 | 80 | {"at90s2343", AVR_ISA_2xxx, bfd_mach_avr2}, |
bdfdba87 MM |
81 | {"attiny22", AVR_ISA_2xxx, bfd_mach_avr2}, /* XXX -> 2343 */ |
82 | {"attiny26", AVR_ISA_2xxx, bfd_mach_avr2}, | |
adde6300 | 83 | {"at90s4433", AVR_ISA_2xxx, bfd_mach_avr2}, |
1f8ae5e6 DC |
84 | {"at90s4414", AVR_ISA_2xxx, bfd_mach_avr2}, /* XXX -> 8515 */ |
85 | {"at90s4434", AVR_ISA_2xxx, bfd_mach_avr2}, /* XXX -> 8535 */ | |
65aa24b6 NC |
86 | {"at90s8515", AVR_ISA_2xxx, bfd_mach_avr2}, |
87 | {"at90s8535", AVR_ISA_2xxx, bfd_mach_avr2}, | |
88 | {"at90c8534", AVR_ISA_2xxx, bfd_mach_avr2}, | |
8a6def3b | 89 | {"at86rf401", AVR_ISA_2xxx, bfd_mach_avr2}, |
71d7ecf2 MM |
90 | {"attiny13", AVR_ISA_TINY2, bfd_mach_avr2}, |
91 | {"attiny2313",AVR_ISA_TINY2, bfd_mach_avr2}, | |
1f8ae5e6 | 92 | {"atmega603", AVR_ISA_M603, bfd_mach_avr3}, /* XXX -> m103 */ |
b170af93 | 93 | {"atmega103", AVR_ISA_M103, bfd_mach_avr3}, |
1f8ae5e6 | 94 | {"at43usb320",AVR_ISA_M103, bfd_mach_avr3}, |
bdfdba87 | 95 | {"at43usb355",AVR_ISA_M603, bfd_mach_avr3}, |
1f8ae5e6 | 96 | {"at76c711", AVR_ISA_M603, bfd_mach_avr3}, |
23794b24 | 97 | {"atmega48", AVR_ISA_M8, bfd_mach_avr4}, |
1f8ae5e6 | 98 | {"atmega8", AVR_ISA_M8, bfd_mach_avr4}, |
8a6def3b | 99 | {"atmega83", AVR_ISA_M8, bfd_mach_avr4}, /* XXX -> m8535 */ |
1f8ae5e6 | 100 | {"atmega85", AVR_ISA_M8, bfd_mach_avr4}, /* XXX -> m8 */ |
23794b24 | 101 | {"atmega88", AVR_ISA_M8, bfd_mach_avr4}, |
bdfdba87 | 102 | {"atmega8515",AVR_ISA_M8, bfd_mach_avr4}, |
8a6def3b | 103 | {"atmega8535",AVR_ISA_M8, bfd_mach_avr4}, |
1f8ae5e6 | 104 | {"atmega16", AVR_ISA_M323, bfd_mach_avr5}, |
65aa24b6 | 105 | {"atmega161", AVR_ISA_M161, bfd_mach_avr5}, |
bdfdba87 | 106 | {"atmega162", AVR_ISA_M323, bfd_mach_avr5}, |
65aa24b6 | 107 | {"atmega163", AVR_ISA_M161, bfd_mach_avr5}, |
5c54fd37 | 108 | {"atmega165", AVR_ISA_M323, bfd_mach_avr5}, |
23794b24 | 109 | {"atmega168", AVR_ISA_M323, bfd_mach_avr5}, |
8a6def3b | 110 | {"atmega169", AVR_ISA_M323, bfd_mach_avr5}, |
1f8ae5e6 DC |
111 | {"atmega32", AVR_ISA_M323, bfd_mach_avr5}, |
112 | {"atmega323", AVR_ISA_M323, bfd_mach_avr5}, | |
5c54fd37 NC |
113 | {"atmega325", AVR_ISA_M323, bfd_mach_avr5}, |
114 | {"atmega3250",AVR_ISA_M323, bfd_mach_avr5}, | |
1f8ae5e6 DC |
115 | {"atmega64", AVR_ISA_M323, bfd_mach_avr5}, |
116 | {"atmega128", AVR_ISA_M128, bfd_mach_avr5}, | |
5c54fd37 NC |
117 | {"atmega645", AVR_ISA_M323, bfd_mach_avr5}, |
118 | {"atmega6450",AVR_ISA_M323, bfd_mach_avr5}, | |
23794b24 | 119 | {"at90can128",AVR_ISA_M128, bfd_mach_avr5}, |
65aa24b6 | 120 | {"at94k", AVR_ISA_94K, bfd_mach_avr5}, |
adde6300 AM |
121 | {NULL, 0, 0} |
122 | }; | |
123 | ||
adde6300 | 124 | /* Current MCU type. */ |
dc191a8f NC |
125 | static struct mcu_type_s default_mcu = {"avr2", AVR_ISA_2xxx,bfd_mach_avr2}; |
126 | static struct mcu_type_s * avr_mcu = & default_mcu; | |
adde6300 | 127 | |
00d2865b NC |
128 | /* AVR target-specific switches. */ |
129 | struct avr_opt_s | |
130 | { | |
dc191a8f NC |
131 | int all_opcodes; /* -mall-opcodes: accept all known AVR opcodes. */ |
132 | int no_skip_bug; /* -mno-skip-bug: no warnings for skipping 2-word insns. */ | |
133 | int no_wrap; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around. */ | |
00d2865b NC |
134 | }; |
135 | ||
136 | static struct avr_opt_s avr_opt = { 0, 0, 0 }; | |
137 | ||
adde6300 AM |
138 | const char EXP_CHARS[] = "eE"; |
139 | const char FLT_CHARS[] = "dD"; | |
dc191a8f NC |
140 | |
141 | static void avr_set_arch (int); | |
adde6300 AM |
142 | |
143 | /* The target specific pseudo-ops which we support. */ | |
144 | const pseudo_typeS md_pseudo_table[] = | |
145 | { | |
146 | {"arch", avr_set_arch, 0}, | |
147 | { NULL, NULL, 0} | |
148 | }; | |
149 | ||
150 | #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00)) | |
adde6300 | 151 | |
dc191a8f NC |
152 | #define EXP_MOD_NAME(i) exp_mod[i].name |
153 | #define EXP_MOD_RELOC(i) exp_mod[i].reloc | |
154 | #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc | |
155 | #define HAVE_PM_P(i) exp_mod[i].have_pm | |
adde6300 AM |
156 | |
157 | struct exp_mod_s | |
158 | { | |
dc191a8f NC |
159 | char * name; |
160 | bfd_reloc_code_real_type reloc; | |
161 | bfd_reloc_code_real_type neg_reloc; | |
162 | int have_pm; | |
adde6300 AM |
163 | }; |
164 | ||
c6a7ab1f NC |
165 | static struct exp_mod_s exp_mod[] = |
166 | { | |
adde6300 AM |
167 | {"hh8", BFD_RELOC_AVR_HH8_LDI, BFD_RELOC_AVR_HH8_LDI_NEG, 1}, |
168 | {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM, BFD_RELOC_AVR_HH8_LDI_PM_NEG, 0}, | |
169 | {"hi8", BFD_RELOC_AVR_HI8_LDI, BFD_RELOC_AVR_HI8_LDI_NEG, 1}, | |
170 | {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM, BFD_RELOC_AVR_HI8_LDI_PM_NEG, 0}, | |
171 | {"lo8", BFD_RELOC_AVR_LO8_LDI, BFD_RELOC_AVR_LO8_LDI_NEG, 1}, | |
172 | {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM, BFD_RELOC_AVR_LO8_LDI_PM_NEG, 0}, | |
df406460 NC |
173 | {"hlo8", BFD_RELOC_AVR_HH8_LDI, BFD_RELOC_AVR_HH8_LDI_NEG, 0}, |
174 | {"hhi8", BFD_RELOC_AVR_MS8_LDI, BFD_RELOC_AVR_MS8_LDI_NEG, 0}, | |
adde6300 AM |
175 | }; |
176 | ||
8ad7c533 NC |
177 | /* A union used to store indicies into the exp_mod[] array |
178 | in a hash table which expects void * data types. */ | |
179 | typedef union | |
180 | { | |
181 | void * ptr; | |
182 | int index; | |
183 | } mod_index; | |
184 | ||
adde6300 AM |
185 | /* Opcode hash table. */ |
186 | static struct hash_control *avr_hash; | |
187 | ||
188 | /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */ | |
189 | static struct hash_control *avr_mod_hash; | |
190 | ||
00d2865b | 191 | #define OPTION_MMCU 'm' |
dc191a8f NC |
192 | enum options |
193 | { | |
194 | OPTION_ALL_OPCODES = OPTION_MD_BASE + 1, | |
195 | OPTION_NO_SKIP_BUG, | |
196 | OPTION_NO_WRAP | |
197 | }; | |
adde6300 | 198 | |
c6a7ab1f NC |
199 | struct option md_longopts[] = |
200 | { | |
00d2865b NC |
201 | { "mmcu", required_argument, NULL, OPTION_MMCU }, |
202 | { "mall-opcodes", no_argument, NULL, OPTION_ALL_OPCODES }, | |
203 | { "mno-skip-bug", no_argument, NULL, OPTION_NO_SKIP_BUG }, | |
204 | { "mno-wrap", no_argument, NULL, OPTION_NO_WRAP }, | |
205 | { NULL, no_argument, NULL, 0 } | |
adde6300 | 206 | }; |
adde6300 | 207 | |
c6a7ab1f | 208 | size_t md_longopts_size = sizeof (md_longopts); |
00d2865b NC |
209 | |
210 | /* Display nicely formatted list of known MCU names. */ | |
c6a7ab1f | 211 | |
00d2865b | 212 | static void |
dc191a8f | 213 | show_mcu_list (FILE *stream) |
00d2865b NC |
214 | { |
215 | int i, x; | |
216 | ||
217 | fprintf (stream, _("Known MCU names:")); | |
218 | x = 1000; | |
1dab94dd | 219 | |
00d2865b NC |
220 | for (i = 0; mcu_types[i].name; i++) |
221 | { | |
222 | int len = strlen (mcu_types[i].name); | |
1dab94dd | 223 | |
00d2865b | 224 | x += len + 1; |
1dab94dd | 225 | |
00d2865b | 226 | if (x < 75) |
c6a7ab1f | 227 | fprintf (stream, " %s", mcu_types[i].name); |
00d2865b NC |
228 | else |
229 | { | |
230 | fprintf (stream, "\n %s", mcu_types[i].name); | |
231 | x = len + 2; | |
232 | } | |
233 | } | |
1dab94dd | 234 | |
c6a7ab1f | 235 | fprintf (stream, "\n"); |
00d2865b NC |
236 | } |
237 | ||
adde6300 | 238 | static inline char * |
dc191a8f | 239 | skip_space (char *s) |
adde6300 AM |
240 | { |
241 | while (*s == ' ' || *s == '\t') | |
242 | ++s; | |
243 | return s; | |
244 | } | |
245 | ||
246 | /* Extract one word from FROM and copy it to TO. */ | |
c6a7ab1f | 247 | |
adde6300 AM |
248 | static char * |
249 | extract_word (char *from, char *to, int limit) | |
250 | { | |
251 | char *op_start; | |
252 | char *op_end; | |
253 | int size = 0; | |
254 | ||
255 | /* Drop leading whitespace. */ | |
256 | from = skip_space (from); | |
257 | *to = 0; | |
c6a7ab1f | 258 | |
adde6300 | 259 | /* Find the op code end. */ |
c6a7ab1f | 260 | for (op_start = op_end = from; *op_end != 0 && is_part_of_name (*op_end);) |
adde6300 AM |
261 | { |
262 | to[size++] = *op_end++; | |
263 | if (size + 1 >= limit) | |
264 | break; | |
265 | } | |
1dab94dd | 266 | |
adde6300 AM |
267 | to[size] = 0; |
268 | return op_end; | |
269 | } | |
270 | ||
271 | int | |
dc191a8f NC |
272 | md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED, |
273 | asection *seg ATTRIBUTE_UNUSED) | |
adde6300 AM |
274 | { |
275 | abort (); | |
276 | return 0; | |
277 | } | |
278 | ||
279 | void | |
dc191a8f | 280 | md_show_usage (FILE *stream) |
adde6300 | 281 | { |
00d2865b NC |
282 | fprintf (stream, |
283 | _("AVR options:\n" | |
adde6300 AM |
284 | " -mmcu=[avr-name] select microcontroller variant\n" |
285 | " [avr-name] can be:\n" | |
65aa24b6 NC |
286 | " avr1 - AT90S1200, ATtiny1x, ATtiny28\n" |
287 | " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n" | |
288 | " avr3 - ATmega103, ATmega603\n" | |
289 | " avr4 - ATmega83, ATmega85\n" | |
290 | " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n" | |
adde6300 | 291 | " or immediate microcontroller name.\n")); |
00d2865b NC |
292 | fprintf (stream, |
293 | _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n" | |
294 | " -mno-skip-bug disable warnings for skipping two-word instructions\n" | |
295 | " (default for avr4, avr5)\n" | |
296 | " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n" | |
297 | " (default for avr3, avr5)\n")); | |
298 | show_mcu_list (stream); | |
adde6300 AM |
299 | } |
300 | ||
301 | static void | |
dc191a8f | 302 | avr_set_arch (int dummy ATTRIBUTE_UNUSED) |
adde6300 | 303 | { |
dc191a8f | 304 | char str[20]; |
1dab94dd | 305 | |
adde6300 | 306 | input_line_pointer = extract_word (input_line_pointer, str, 20); |
00d2865b | 307 | md_parse_option (OPTION_MMCU, str); |
adde6300 AM |
308 | bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach); |
309 | } | |
310 | ||
311 | int | |
dc191a8f | 312 | md_parse_option (int c, char *arg) |
adde6300 | 313 | { |
00d2865b | 314 | switch (c) |
adde6300 | 315 | { |
00d2865b NC |
316 | case OPTION_MMCU: |
317 | { | |
318 | int i; | |
319 | char *s = alloca (strlen (arg) + 1); | |
adde6300 | 320 | |
00d2865b NC |
321 | { |
322 | char *t = s; | |
323 | char *arg1 = arg; | |
324 | ||
325 | do | |
3882b010 | 326 | *t = TOLOWER (*arg1++); |
00d2865b NC |
327 | while (*t++); |
328 | } | |
329 | ||
330 | for (i = 0; mcu_types[i].name; ++i) | |
331 | if (strcmp (mcu_types[i].name, s) == 0) | |
332 | break; | |
adde6300 | 333 | |
00d2865b NC |
334 | if (!mcu_types[i].name) |
335 | { | |
336 | show_mcu_list (stderr); | |
337 | as_fatal (_("unknown MCU: %s\n"), arg); | |
338 | } | |
65aa24b6 | 339 | |
00d2865b NC |
340 | /* It is OK to redefine mcu type within the same avr[1-5] bfd machine |
341 | type - this for allows passing -mmcu=... via gcc ASM_SPEC as well | |
342 | as .arch ... in the asm output at the same time. */ | |
00d2865b NC |
343 | if (avr_mcu == &default_mcu || avr_mcu->mach == mcu_types[i].mach) |
344 | avr_mcu = &mcu_types[i]; | |
345 | else | |
346 | as_fatal (_("redefinition of mcu type `%s' to `%s'"), | |
347 | avr_mcu->name, mcu_types[i].name); | |
348 | return 1; | |
349 | } | |
350 | case OPTION_ALL_OPCODES: | |
351 | avr_opt.all_opcodes = 1; | |
352 | return 1; | |
353 | case OPTION_NO_SKIP_BUG: | |
354 | avr_opt.no_skip_bug = 1; | |
355 | return 1; | |
356 | case OPTION_NO_WRAP: | |
357 | avr_opt.no_wrap = 1; | |
adde6300 AM |
358 | return 1; |
359 | } | |
1dab94dd | 360 | |
adde6300 AM |
361 | return 0; |
362 | } | |
363 | ||
364 | symbolS * | |
dc191a8f | 365 | md_undefined_symbol (char *name ATTRIBUTE_UNUSED) |
adde6300 | 366 | { |
dc191a8f | 367 | return NULL; |
adde6300 AM |
368 | } |
369 | ||
c6a7ab1f NC |
370 | /* Turn a string in input_line_pointer into a floating point constant |
371 | of type TYPE, and store the appropriate bytes in *LITP. The number | |
372 | of LITTLENUMS emitted is stored in *SIZEP. An error message is | |
373 | returned, or NULL on OK. */ | |
374 | ||
adde6300 | 375 | char * |
dc191a8f | 376 | md_atof (int type, char *litP, int *sizeP) |
adde6300 AM |
377 | { |
378 | int prec; | |
379 | LITTLENUM_TYPE words[4]; | |
380 | LITTLENUM_TYPE *wordP; | |
381 | char *t; | |
382 | ||
383 | switch (type) | |
384 | { | |
385 | case 'f': | |
386 | prec = 2; | |
387 | break; | |
388 | case 'd': | |
389 | prec = 4; | |
390 | break; | |
391 | default: | |
392 | *sizeP = 0; | |
393 | return _("bad call to md_atof"); | |
394 | } | |
395 | ||
396 | t = atof_ieee (input_line_pointer, type, words); | |
397 | if (t) | |
398 | input_line_pointer = t; | |
399 | ||
400 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
1dab94dd | 401 | |
adde6300 AM |
402 | /* This loop outputs the LITTLENUMs in REVERSE order. */ |
403 | for (wordP = words + prec - 1; prec--;) | |
404 | { | |
405 | md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE)); | |
406 | litP += sizeof (LITTLENUM_TYPE); | |
407 | } | |
1dab94dd | 408 | |
adde6300 AM |
409 | return NULL; |
410 | } | |
411 | ||
412 | void | |
dc191a8f NC |
413 | md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, |
414 | asection *sec ATTRIBUTE_UNUSED, | |
415 | fragS *fragP ATTRIBUTE_UNUSED) | |
adde6300 AM |
416 | { |
417 | abort (); | |
418 | } | |
419 | ||
adde6300 | 420 | void |
dc191a8f | 421 | md_begin (void) |
adde6300 | 422 | { |
df136245 | 423 | unsigned int i; |
adde6300 | 424 | struct avr_opcodes_s *opcode; |
dc191a8f | 425 | |
c6a7ab1f | 426 | avr_hash = hash_new (); |
adde6300 AM |
427 | |
428 | /* Insert unique names into hash table. This hash table then provides a | |
429 | quick index to the first opcode with a particular name in the opcode | |
430 | table. */ | |
adde6300 AM |
431 | for (opcode = avr_opcodes; opcode->name; opcode++) |
432 | hash_insert (avr_hash, opcode->name, (char *) opcode); | |
433 | ||
434 | avr_mod_hash = hash_new (); | |
435 | ||
dc191a8f | 436 | for (i = 0; i < ARRAY_SIZE (exp_mod); ++i) |
8ad7c533 NC |
437 | { |
438 | mod_index m; | |
439 | ||
440 | m.index = i + 10; | |
441 | hash_insert (avr_mod_hash, EXP_MOD_NAME (i), m.ptr); | |
442 | } | |
c6a7ab1f | 443 | |
adde6300 AM |
444 | bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach); |
445 | } | |
446 | ||
df136245 | 447 | /* Resolve STR as a constant expression and return the result. |
c6a7ab1f | 448 | If result greater than MAX then error. */ |
df136245 DC |
449 | |
450 | static unsigned int | |
dc191a8f | 451 | avr_get_constant (char *str, int max) |
df136245 DC |
452 | { |
453 | expressionS ex; | |
dc191a8f | 454 | |
df136245 DC |
455 | str = skip_space (str); |
456 | input_line_pointer = str; | |
dc191a8f | 457 | expression (& ex); |
df136245 DC |
458 | |
459 | if (ex.X_op != O_constant) | |
460 | as_bad (_("constant value required")); | |
461 | ||
462 | if (ex.X_add_number > max || ex.X_add_number < 0) | |
c6a7ab1f | 463 | as_bad (_("number must be less than %d"), max + 1); |
1dab94dd | 464 | |
df136245 DC |
465 | return ex.X_add_number; |
466 | } | |
467 | ||
dc191a8f | 468 | /* Parse for ldd/std offset. */ |
df136245 | 469 | |
dc191a8f NC |
470 | static void |
471 | avr_offset_expression (expressionS *exp) | |
adde6300 | 472 | { |
dc191a8f NC |
473 | char *str = input_line_pointer; |
474 | char *tmp; | |
475 | char op[8]; | |
adde6300 | 476 | |
dc191a8f NC |
477 | tmp = str; |
478 | str = extract_word (str, op, sizeof (op)); | |
479 | ||
480 | input_line_pointer = tmp; | |
481 | expression (exp); | |
482 | ||
483 | /* Warn about expressions that fail to use lo8 (). */ | |
484 | if (exp->X_op == O_constant) | |
adde6300 | 485 | { |
dc191a8f NC |
486 | int x = exp->X_add_number; |
487 | ||
488 | if (x < -255 || x > 255) | |
489 | as_warn (_("constant out of 8-bit range: %d"), x); | |
490 | } | |
491 | } | |
adde6300 | 492 | |
dc191a8f | 493 | /* Parse ordinary expression. */ |
adde6300 | 494 | |
dc191a8f NC |
495 | static char * |
496 | parse_exp (char *s, expressionS *op) | |
497 | { | |
498 | input_line_pointer = s; | |
499 | expression (op); | |
500 | if (op->X_op == O_absent) | |
501 | as_bad (_("missing operand")); | |
502 | return input_line_pointer; | |
503 | } | |
1dab94dd | 504 | |
dc191a8f NC |
505 | /* Parse special expressions (needed for LDI command): |
506 | xx8 (address) | |
507 | xx8 (-address) | |
508 | pm_xx8 (address) | |
509 | pm_xx8 (-address) | |
510 | where xx is: hh, hi, lo. */ | |
adde6300 | 511 | |
dc191a8f NC |
512 | static bfd_reloc_code_real_type |
513 | avr_ldi_expression (expressionS *exp) | |
514 | { | |
515 | char *str = input_line_pointer; | |
516 | char *tmp; | |
517 | char op[8]; | |
518 | int mod; | |
519 | tmp = str; | |
adde6300 | 520 | |
dc191a8f | 521 | str = extract_word (str, op, sizeof (op)); |
adde6300 | 522 | |
dc191a8f NC |
523 | if (op[0]) |
524 | { | |
8ad7c533 NC |
525 | mod_index m; |
526 | ||
527 | m.ptr = hash_find (avr_mod_hash, op); | |
528 | mod = m.index; | |
1dab94dd | 529 | |
dc191a8f NC |
530 | if (mod) |
531 | { | |
532 | int closes = 0; | |
b170af93 | 533 | |
dc191a8f NC |
534 | mod -= 10; |
535 | str = skip_space (str); | |
00d2865b | 536 | |
dc191a8f NC |
537 | if (*str == '(') |
538 | { | |
539 | int neg_p = 0; | |
00d2865b | 540 | |
dc191a8f | 541 | ++str; |
00d2865b | 542 | |
dc191a8f NC |
543 | if (strncmp ("pm(", str, 3) == 0 |
544 | || strncmp ("-(pm(", str, 5) == 0) | |
545 | { | |
546 | if (HAVE_PM_P (mod)) | |
547 | { | |
548 | ++mod; | |
549 | ++closes; | |
550 | } | |
551 | else | |
552 | as_bad (_("illegal expression")); | |
b170af93 | 553 | |
dc191a8f NC |
554 | if (*str == '-') |
555 | { | |
556 | neg_p = 1; | |
557 | ++closes; | |
558 | str += 5; | |
559 | } | |
560 | else | |
561 | str += 3; | |
562 | } | |
adde6300 | 563 | |
dc191a8f NC |
564 | if (*str == '-' && *(str + 1) == '(') |
565 | { | |
566 | neg_p ^= 1; | |
567 | ++closes; | |
568 | str += 2; | |
569 | } | |
750bce0e | 570 | |
dc191a8f NC |
571 | input_line_pointer = str; |
572 | expression (exp); | |
750bce0e | 573 | |
dc191a8f NC |
574 | do |
575 | { | |
576 | if (*input_line_pointer != ')') | |
577 | { | |
578 | as_bad (_("`)' required")); | |
579 | break; | |
580 | } | |
581 | input_line_pointer++; | |
582 | } | |
583 | while (closes--); | |
584 | ||
585 | return neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod); | |
586 | } | |
587 | } | |
588 | } | |
750bce0e NC |
589 | |
590 | input_line_pointer = tmp; | |
591 | expression (exp); | |
592 | ||
593 | /* Warn about expressions that fail to use lo8 (). */ | |
594 | if (exp->X_op == O_constant) | |
595 | { | |
596 | int x = exp->X_add_number; | |
dc191a8f | 597 | |
750bce0e NC |
598 | if (x < -255 || x > 255) |
599 | as_warn (_("constant out of 8-bit range: %d"), x); | |
600 | } | |
dc191a8f NC |
601 | |
602 | return BFD_RELOC_AVR_LDI; | |
750bce0e NC |
603 | } |
604 | ||
df136245 | 605 | /* Parse one instruction operand. |
c6a7ab1f NC |
606 | Return operand bitmask. Also fixups can be generated. */ |
607 | ||
adde6300 | 608 | static unsigned int |
dc191a8f NC |
609 | avr_operand (struct avr_opcodes_s *opcode, |
610 | int where, | |
611 | char *op, | |
612 | char **line) | |
adde6300 | 613 | { |
adde6300 | 614 | expressionS op_expr; |
df136245 DC |
615 | unsigned int op_mask = 0; |
616 | char *str = skip_space (*line); | |
adde6300 | 617 | |
adde6300 AM |
618 | switch (*op) |
619 | { | |
620 | /* Any register operand. */ | |
621 | case 'w': | |
622 | case 'd': | |
623 | case 'r': | |
b170af93 DC |
624 | case 'a': |
625 | case 'v': | |
c6a7ab1f NC |
626 | if (*str == 'r' || *str == 'R') |
627 | { | |
628 | char r_name[20]; | |
1dab94dd | 629 | |
c6a7ab1f | 630 | str = extract_word (str, r_name, sizeof (r_name)); |
65b1d096 | 631 | op_mask = 0xff; |
3882b010 | 632 | if (ISDIGIT (r_name[1])) |
c6a7ab1f NC |
633 | { |
634 | if (r_name[2] == '\0') | |
635 | op_mask = r_name[1] - '0'; | |
636 | else if (r_name[1] != '0' | |
3882b010 | 637 | && ISDIGIT (r_name[2]) |
c6a7ab1f NC |
638 | && r_name[3] == '\0') |
639 | op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0'; | |
640 | } | |
641 | } | |
642 | else | |
643 | { | |
644 | op_mask = avr_get_constant (str, 31); | |
645 | str = input_line_pointer; | |
646 | } | |
1dab94dd | 647 | |
c6a7ab1f NC |
648 | if (op_mask <= 31) |
649 | { | |
650 | switch (*op) | |
651 | { | |
652 | case 'a': | |
653 | if (op_mask < 16 || op_mask > 23) | |
654 | as_bad (_("register r16-r23 required")); | |
655 | op_mask -= 16; | |
656 | break; | |
1dab94dd | 657 | |
c6a7ab1f NC |
658 | case 'd': |
659 | if (op_mask < 16) | |
660 | as_bad (_("register number above 15 required")); | |
661 | op_mask -= 16; | |
662 | break; | |
1dab94dd | 663 | |
c6a7ab1f NC |
664 | case 'v': |
665 | if (op_mask & 1) | |
666 | as_bad (_("even register number required")); | |
667 | op_mask >>= 1; | |
668 | break; | |
1dab94dd | 669 | |
c6a7ab1f | 670 | case 'w': |
65b1d096 | 671 | if ((op_mask & 1) || op_mask < 24) |
c6a7ab1f | 672 | as_bad (_("register r24, r26, r28 or r30 required")); |
65b1d096 | 673 | op_mask = (op_mask - 24) >> 1; |
c6a7ab1f NC |
674 | break; |
675 | } | |
676 | break; | |
677 | } | |
678 | as_bad (_("register name or number from 0 to 31 required")); | |
adde6300 AM |
679 | break; |
680 | ||
681 | case 'e': | |
682 | { | |
683 | char c; | |
1dab94dd | 684 | |
adde6300 AM |
685 | if (*str == '-') |
686 | { | |
c6a7ab1f | 687 | str = skip_space (str + 1); |
adde6300 AM |
688 | op_mask = 0x1002; |
689 | } | |
3882b010 | 690 | c = TOLOWER (*str); |
adde6300 AM |
691 | if (c == 'x') |
692 | op_mask |= 0x100c; | |
693 | else if (c == 'y') | |
694 | op_mask |= 0x8; | |
695 | else if (c != 'z') | |
00d2865b | 696 | as_bad (_("pointer register (X, Y or Z) required")); |
adde6300 | 697 | |
c6a7ab1f | 698 | str = skip_space (str + 1); |
adde6300 AM |
699 | if (*str == '+') |
700 | { | |
701 | ++str; | |
702 | if (op_mask & 2) | |
00d2865b | 703 | as_bad (_("cannot both predecrement and postincrement")); |
adde6300 AM |
704 | op_mask |= 0x1001; |
705 | } | |
e38c9cc2 | 706 | |
1188e082 | 707 | /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer |
e38c9cc2 | 708 | registers, no predecrement, no postincrement. */ |
00d2865b NC |
709 | if (!avr_opt.all_opcodes && (op_mask & 0x100F) |
710 | && !(avr_mcu->isa & AVR_ISA_SRAM)) | |
711 | as_bad (_("addressing mode not supported")); | |
adde6300 AM |
712 | } |
713 | break; | |
714 | ||
b170af93 | 715 | case 'z': |
c6a7ab1f NC |
716 | if (*str == '-') |
717 | as_bad (_("can't predecrement")); | |
1dab94dd | 718 | |
c6a7ab1f NC |
719 | if (! (*str == 'z' || *str == 'Z')) |
720 | as_bad (_("pointer register Z required")); | |
1dab94dd | 721 | |
c6a7ab1f NC |
722 | str = skip_space (str + 1); |
723 | ||
724 | if (*str == '+') | |
725 | { | |
726 | ++str; | |
727 | op_mask |= 1; | |
728 | } | |
b170af93 DC |
729 | break; |
730 | ||
adde6300 AM |
731 | case 'b': |
732 | { | |
3882b010 | 733 | char c = TOLOWER (*str++); |
1dab94dd | 734 | |
adde6300 AM |
735 | if (c == 'y') |
736 | op_mask |= 0x8; | |
737 | else if (c != 'z') | |
00d2865b | 738 | as_bad (_("pointer register (Y or Z) required")); |
adde6300 AM |
739 | str = skip_space (str); |
740 | if (*str++ == '+') | |
741 | { | |
750bce0e NC |
742 | input_line_pointer = str; |
743 | avr_offset_expression (& op_expr); | |
adde6300 | 744 | str = input_line_pointer; |
750bce0e NC |
745 | fix_new_exp (frag_now, where, 3, |
746 | &op_expr, FALSE, BFD_RELOC_AVR_6); | |
adde6300 AM |
747 | } |
748 | } | |
749 | break; | |
750 | ||
751 | case 'h': | |
c6a7ab1f NC |
752 | str = parse_exp (str, &op_expr); |
753 | fix_new_exp (frag_now, where, opcode->insn_size * 2, | |
b34976b6 | 754 | &op_expr, FALSE, BFD_RELOC_AVR_CALL); |
adde6300 AM |
755 | break; |
756 | ||
757 | case 'L': | |
c6a7ab1f NC |
758 | str = parse_exp (str, &op_expr); |
759 | fix_new_exp (frag_now, where, opcode->insn_size * 2, | |
b34976b6 | 760 | &op_expr, TRUE, BFD_RELOC_AVR_13_PCREL); |
adde6300 AM |
761 | break; |
762 | ||
763 | case 'l': | |
c6a7ab1f NC |
764 | str = parse_exp (str, &op_expr); |
765 | fix_new_exp (frag_now, where, opcode->insn_size * 2, | |
b34976b6 | 766 | &op_expr, TRUE, BFD_RELOC_AVR_7_PCREL); |
adde6300 AM |
767 | break; |
768 | ||
769 | case 'i': | |
c6a7ab1f NC |
770 | str = parse_exp (str, &op_expr); |
771 | fix_new_exp (frag_now, where + 2, opcode->insn_size * 2, | |
b34976b6 | 772 | &op_expr, FALSE, BFD_RELOC_16); |
adde6300 AM |
773 | break; |
774 | ||
775 | case 'M': | |
776 | { | |
777 | bfd_reloc_code_real_type r_type; | |
1dab94dd | 778 | |
c6a7ab1f NC |
779 | input_line_pointer = str; |
780 | r_type = avr_ldi_expression (&op_expr); | |
781 | str = input_line_pointer; | |
adde6300 | 782 | fix_new_exp (frag_now, where, 3, |
b34976b6 | 783 | &op_expr, FALSE, r_type); |
adde6300 AM |
784 | } |
785 | break; | |
786 | ||
787 | case 'n': | |
788 | { | |
789 | unsigned int x; | |
1dab94dd | 790 | |
adde6300 AM |
791 | x = ~avr_get_constant (str, 255); |
792 | str = input_line_pointer; | |
793 | op_mask |= (x & 0xf) | ((x << 4) & 0xf00); | |
794 | } | |
795 | break; | |
796 | ||
797 | case 'K': | |
750bce0e NC |
798 | input_line_pointer = str; |
799 | avr_offset_expression (& op_expr); | |
800 | str = input_line_pointer; | |
801 | fix_new_exp (frag_now, where, 3, | |
802 | & op_expr, FALSE, BFD_RELOC_AVR_6_ADIW); | |
adde6300 AM |
803 | break; |
804 | ||
805 | case 'S': | |
806 | case 's': | |
807 | { | |
808 | unsigned int x; | |
1dab94dd | 809 | |
adde6300 AM |
810 | x = avr_get_constant (str, 7); |
811 | str = input_line_pointer; | |
812 | if (*op == 'S') | |
813 | x <<= 4; | |
814 | op_mask |= x; | |
815 | } | |
816 | break; | |
817 | ||
818 | case 'P': | |
819 | { | |
820 | unsigned int x; | |
1dab94dd | 821 | |
adde6300 AM |
822 | x = avr_get_constant (str, 63); |
823 | str = input_line_pointer; | |
824 | op_mask |= (x & 0xf) | ((x & 0x30) << 5); | |
825 | } | |
826 | break; | |
827 | ||
828 | case 'p': | |
829 | { | |
830 | unsigned int x; | |
1dab94dd | 831 | |
adde6300 AM |
832 | x = avr_get_constant (str, 31); |
833 | str = input_line_pointer; | |
834 | op_mask |= x << 3; | |
835 | } | |
836 | break; | |
1dab94dd | 837 | |
1188e082 DC |
838 | case '?': |
839 | break; | |
1dab94dd | 840 | |
adde6300 | 841 | default: |
00d2865b | 842 | as_bad (_("unknown constraint `%c'"), *op); |
adde6300 | 843 | } |
1dab94dd | 844 | |
adde6300 AM |
845 | *line = str; |
846 | return op_mask; | |
847 | } | |
848 | ||
dc191a8f NC |
849 | /* Parse instruction operands. |
850 | Return binary opcode. */ | |
851 | ||
852 | static unsigned int | |
853 | avr_operands (struct avr_opcodes_s *opcode, char **line) | |
854 | { | |
855 | char *op = opcode->constraints; | |
856 | unsigned int bin = opcode->bin_opcode; | |
857 | char *frag = frag_more (opcode->insn_size * 2); | |
858 | char *str = *line; | |
859 | int where = frag - frag_now->fr_literal; | |
860 | static unsigned int prev = 0; /* Previous opcode. */ | |
861 | ||
862 | /* Opcode have operands. */ | |
863 | if (*op) | |
864 | { | |
865 | unsigned int reg1 = 0; | |
866 | unsigned int reg2 = 0; | |
867 | int reg1_present = 0; | |
868 | int reg2_present = 0; | |
869 | ||
870 | /* Parse first operand. */ | |
871 | if (REGISTER_P (*op)) | |
872 | reg1_present = 1; | |
873 | reg1 = avr_operand (opcode, where, op, &str); | |
874 | ++op; | |
875 | ||
876 | /* Parse second operand. */ | |
877 | if (*op) | |
878 | { | |
879 | if (*op == ',') | |
880 | ++op; | |
881 | ||
882 | if (*op == '=') | |
883 | { | |
884 | reg2 = reg1; | |
885 | reg2_present = 1; | |
886 | } | |
887 | else | |
888 | { | |
889 | if (REGISTER_P (*op)) | |
890 | reg2_present = 1; | |
891 | ||
892 | str = skip_space (str); | |
893 | if (*str++ != ',') | |
894 | as_bad (_("`,' required")); | |
895 | str = skip_space (str); | |
896 | ||
897 | reg2 = avr_operand (opcode, where, op, &str); | |
898 | } | |
899 | ||
900 | if (reg1_present && reg2_present) | |
901 | reg2 = (reg2 & 0xf) | ((reg2 << 5) & 0x200); | |
902 | else if (reg2_present) | |
903 | reg2 <<= 4; | |
904 | } | |
905 | if (reg1_present) | |
906 | reg1 <<= 4; | |
907 | bin |= reg1 | reg2; | |
908 | } | |
909 | ||
910 | /* Detect undefined combinations (like ld r31,Z+). */ | |
911 | if (!avr_opt.all_opcodes && AVR_UNDEF_P (bin)) | |
912 | as_warn (_("undefined combination of operands")); | |
913 | ||
914 | if (opcode->insn_size == 2) | |
915 | { | |
916 | /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs | |
917 | (AVR core bug, fixed in the newer devices). */ | |
918 | if (!(avr_opt.no_skip_bug || | |
919 | (avr_mcu->isa & (AVR_ISA_MUL | AVR_ISA_MOVW))) | |
920 | && AVR_SKIP_P (prev)) | |
921 | as_warn (_("skipping two-word instruction")); | |
922 | ||
923 | bfd_putl32 ((bfd_vma) bin, frag); | |
924 | } | |
925 | else | |
926 | bfd_putl16 ((bfd_vma) bin, frag); | |
927 | ||
928 | prev = bin; | |
929 | *line = str; | |
930 | return bin; | |
931 | } | |
932 | ||
adde6300 AM |
933 | /* GAS will call this function for each section at the end of the assembly, |
934 | to permit the CPU backend to adjust the alignment of a section. */ | |
c6a7ab1f | 935 | |
adde6300 | 936 | valueT |
dc191a8f | 937 | md_section_align (asection *seg, valueT addr) |
adde6300 AM |
938 | { |
939 | int align = bfd_get_section_alignment (stdoutput, seg); | |
940 | return ((addr + (1 << align) - 1) & (-1 << align)); | |
941 | } | |
942 | ||
943 | /* If you define this macro, it should return the offset between the | |
944 | address of a PC relative fixup and the position from which the PC | |
945 | relative adjustment should be made. On many processors, the base | |
946 | of a PC relative instruction is the next instruction, so this | |
947 | macro would return the length of an instruction. */ | |
c6a7ab1f | 948 | |
adde6300 | 949 | long |
dc191a8f | 950 | md_pcrel_from_section (fixS *fixp, segT sec) |
adde6300 | 951 | { |
c6a7ab1f | 952 | if (fixp->fx_addsy != (symbolS *) NULL |
adde6300 AM |
953 | && (!S_IS_DEFINED (fixp->fx_addsy) |
954 | || (S_GET_SEGMENT (fixp->fx_addsy) != sec))) | |
955 | return 0; | |
1dab94dd | 956 | |
adde6300 AM |
957 | return fixp->fx_frag->fr_address + fixp->fx_where; |
958 | } | |
959 | ||
960 | /* GAS will call this for each fixup. It should store the correct | |
c6a7ab1f NC |
961 | value in the object file. */ |
962 | ||
94f592af | 963 | void |
dc191a8f | 964 | md_apply_fix (fixS *fixP, valueT * valP, segT seg) |
adde6300 AM |
965 | { |
966 | unsigned char *where; | |
967 | unsigned long insn; | |
a161fe53 | 968 | long value = *valP; |
adde6300 | 969 | |
94f592af NC |
970 | if (fixP->fx_addsy == (symbolS *) NULL) |
971 | fixP->fx_done = 1; | |
972 | ||
87733541 AM |
973 | else if (fixP->fx_pcrel) |
974 | { | |
975 | segT s = S_GET_SEGMENT (fixP->fx_addsy); | |
976 | ||
977 | if (s == seg || s == absolute_section) | |
978 | { | |
979 | value += S_GET_VALUE (fixP->fx_addsy); | |
980 | fixP->fx_done = 1; | |
981 | } | |
982 | } | |
983 | ||
a161fe53 AM |
984 | /* We don't actually support subtracting a symbol. */ |
985 | if (fixP->fx_subsy != (symbolS *) NULL) | |
986 | as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex")); | |
1dab94dd | 987 | |
94f592af | 988 | switch (fixP->fx_r_type) |
adde6300 AM |
989 | { |
990 | default: | |
94f592af | 991 | fixP->fx_no_overflow = 1; |
adde6300 AM |
992 | break; |
993 | case BFD_RELOC_AVR_7_PCREL: | |
994 | case BFD_RELOC_AVR_13_PCREL: | |
995 | case BFD_RELOC_32: | |
996 | case BFD_RELOC_16: | |
997 | case BFD_RELOC_AVR_CALL: | |
998 | break; | |
999 | } | |
1000 | ||
94f592af | 1001 | if (fixP->fx_done) |
adde6300 AM |
1002 | { |
1003 | /* Fetch the instruction, insert the fully resolved operand | |
1004 | value, and stuff the instruction back again. */ | |
2132e3a3 | 1005 | where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where; |
adde6300 AM |
1006 | insn = bfd_getl16 (where); |
1007 | ||
94f592af | 1008 | switch (fixP->fx_r_type) |
adde6300 AM |
1009 | { |
1010 | case BFD_RELOC_AVR_7_PCREL: | |
1011 | if (value & 1) | |
94f592af | 1012 | as_bad_where (fixP->fx_file, fixP->fx_line, |
adde6300 | 1013 | _("odd address operand: %ld"), value); |
1dab94dd | 1014 | |
adde6300 AM |
1015 | /* Instruction addresses are always right-shifted by 1. */ |
1016 | value >>= 1; | |
1017 | --value; /* Correct PC. */ | |
1dab94dd | 1018 | |
adde6300 | 1019 | if (value < -64 || value > 63) |
94f592af | 1020 | as_bad_where (fixP->fx_file, fixP->fx_line, |
adde6300 AM |
1021 | _("operand out of range: %ld"), value); |
1022 | value = (value << 3) & 0x3f8; | |
1023 | bfd_putl16 ((bfd_vma) (value | insn), where); | |
1024 | break; | |
1025 | ||
1026 | case BFD_RELOC_AVR_13_PCREL: | |
1027 | if (value & 1) | |
94f592af | 1028 | as_bad_where (fixP->fx_file, fixP->fx_line, |
adde6300 | 1029 | _("odd address operand: %ld"), value); |
1dab94dd | 1030 | |
adde6300 AM |
1031 | /* Instruction addresses are always right-shifted by 1. */ |
1032 | value >>= 1; | |
1033 | --value; /* Correct PC. */ | |
adde6300 AM |
1034 | |
1035 | if (value < -2048 || value > 2047) | |
1036 | { | |
65aa24b6 | 1037 | /* No wrap for devices with >8K of program memory. */ |
00d2865b | 1038 | if ((avr_mcu->isa & AVR_ISA_MEGA) || avr_opt.no_wrap) |
94f592af | 1039 | as_bad_where (fixP->fx_file, fixP->fx_line, |
adde6300 AM |
1040 | _("operand out of range: %ld"), value); |
1041 | } | |
1042 | ||
1043 | value &= 0xfff; | |
1044 | bfd_putl16 ((bfd_vma) (value | insn), where); | |
1045 | break; | |
1046 | ||
1047 | case BFD_RELOC_32: | |
1048 | bfd_putl16 ((bfd_vma) value, where); | |
1049 | break; | |
1050 | ||
1051 | case BFD_RELOC_16: | |
1052 | bfd_putl16 ((bfd_vma) value, where); | |
1053 | break; | |
1054 | ||
1055 | case BFD_RELOC_AVR_16_PM: | |
c6a7ab1f | 1056 | bfd_putl16 ((bfd_vma) (value >> 1), where); |
adde6300 AM |
1057 | break; |
1058 | ||
750bce0e NC |
1059 | case BFD_RELOC_AVR_LDI: |
1060 | if (value > 255) | |
1061 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
1062 | _("operand out of range: %ld"), value); | |
1063 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where); | |
1064 | break; | |
1065 | ||
1066 | case BFD_RELOC_AVR_6: | |
1067 | if ((value > 63) || (value < 0)) | |
1068 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
1069 | _("operand out of range: %ld"), value); | |
1070 | bfd_putl16 ((bfd_vma) insn | ((value & 7) | ((value & (3 << 3)) << 7) | ((value & (1 << 5)) << 8)), where); | |
1071 | break; | |
1072 | ||
1073 | case BFD_RELOC_AVR_6_ADIW: | |
1074 | if ((value > 63) || (value < 0)) | |
1075 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
1076 | _("operand out of range: %ld"), value); | |
1077 | bfd_putl16 ((bfd_vma) insn | (value & 0xf) | ((value & 0x30) << 2), where); | |
1078 | break; | |
1079 | ||
adde6300 AM |
1080 | case BFD_RELOC_AVR_LO8_LDI: |
1081 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where); | |
1082 | break; | |
1083 | ||
adde6300 AM |
1084 | case BFD_RELOC_AVR_HI8_LDI: |
1085 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where); | |
1086 | break; | |
1087 | ||
df406460 | 1088 | case BFD_RELOC_AVR_MS8_LDI: |
adde6300 AM |
1089 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where); |
1090 | break; | |
1091 | ||
1092 | case BFD_RELOC_AVR_HH8_LDI: | |
1093 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where); | |
1094 | break; | |
1095 | ||
1096 | case BFD_RELOC_AVR_LO8_LDI_NEG: | |
1097 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where); | |
1098 | break; | |
1099 | ||
adde6300 AM |
1100 | case BFD_RELOC_AVR_HI8_LDI_NEG: |
1101 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where); | |
1102 | break; | |
1103 | ||
df406460 | 1104 | case BFD_RELOC_AVR_MS8_LDI_NEG: |
adde6300 AM |
1105 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where); |
1106 | break; | |
1107 | ||
1108 | case BFD_RELOC_AVR_HH8_LDI_NEG: | |
1109 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where); | |
1110 | break; | |
1111 | ||
1112 | case BFD_RELOC_AVR_LO8_LDI_PM: | |
1113 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where); | |
1114 | break; | |
1115 | ||
1116 | case BFD_RELOC_AVR_HI8_LDI_PM: | |
1117 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where); | |
1118 | break; | |
1119 | ||
1120 | case BFD_RELOC_AVR_HH8_LDI_PM: | |
1121 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where); | |
1122 | break; | |
1123 | ||
1124 | case BFD_RELOC_AVR_LO8_LDI_PM_NEG: | |
1125 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where); | |
1126 | break; | |
1127 | ||
1128 | case BFD_RELOC_AVR_HI8_LDI_PM_NEG: | |
1129 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where); | |
1130 | break; | |
1131 | ||
1132 | case BFD_RELOC_AVR_HH8_LDI_PM_NEG: | |
1133 | bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where); | |
1134 | break; | |
1135 | ||
1136 | case BFD_RELOC_AVR_CALL: | |
1137 | { | |
1138 | unsigned long x; | |
1dab94dd | 1139 | |
adde6300 AM |
1140 | x = bfd_getl16 (where); |
1141 | if (value & 1) | |
94f592af | 1142 | as_bad_where (fixP->fx_file, fixP->fx_line, |
adde6300 AM |
1143 | _("odd address operand: %ld"), value); |
1144 | value >>= 1; | |
1145 | x |= ((value & 0x10000) | ((value << 3) & 0x1f00000)) >> 16; | |
1146 | bfd_putl16 ((bfd_vma) x, where); | |
c6a7ab1f | 1147 | bfd_putl16 ((bfd_vma) (value & 0xffff), where + 2); |
adde6300 AM |
1148 | } |
1149 | break; | |
1150 | ||
1151 | default: | |
c6a7ab1f | 1152 | as_fatal (_("line %d: unknown relocation type: 0x%x"), |
94f592af | 1153 | fixP->fx_line, fixP->fx_r_type); |
adde6300 AM |
1154 | break; |
1155 | } | |
1156 | } | |
1157 | else | |
1158 | { | |
94f592af | 1159 | switch (fixP->fx_r_type) |
adde6300 AM |
1160 | { |
1161 | case -BFD_RELOC_AVR_HI8_LDI_NEG: | |
1162 | case -BFD_RELOC_AVR_HI8_LDI: | |
1163 | case -BFD_RELOC_AVR_LO8_LDI_NEG: | |
1164 | case -BFD_RELOC_AVR_LO8_LDI: | |
94f592af | 1165 | as_bad_where (fixP->fx_file, fixP->fx_line, |
adde6300 | 1166 | _("only constant expression allowed")); |
94f592af | 1167 | fixP->fx_done = 1; |
adde6300 AM |
1168 | break; |
1169 | default: | |
1170 | break; | |
1171 | } | |
adde6300 | 1172 | } |
adde6300 AM |
1173 | } |
1174 | ||
7be1c489 AM |
1175 | /* GAS will call this to generate a reloc, passing the resulting reloc |
1176 | to `bfd_install_relocation'. This currently works poorly, as | |
1177 | `bfd_install_relocation' often does the wrong thing, and instances of | |
1178 | `tc_gen_reloc' have been written to work around the problems, which | |
1179 | in turns makes it difficult to fix `bfd_install_relocation'. */ | |
adde6300 AM |
1180 | |
1181 | /* If while processing a fixup, a reloc really needs to be created | |
1182 | then it is done here. */ | |
1183 | ||
1184 | arelent * | |
dc191a8f NC |
1185 | tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, |
1186 | fixS *fixp) | |
adde6300 AM |
1187 | { |
1188 | arelent *reloc; | |
1189 | ||
df406460 NC |
1190 | if (fixp->fx_addsy && fixp->fx_subsy) |
1191 | { | |
1192 | long value = 0; | |
1193 | ||
1194 | if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy)) | |
1195 | || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section) | |
1196 | { | |
1197 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
1198 | "Difference of symbols in different sections is not supported"); | |
1199 | return NULL; | |
1200 | } | |
1201 | ||
1202 | /* We are dealing with two symbols defined in the same section. | |
1203 | Let us fix-up them here. */ | |
1204 | value += S_GET_VALUE (fixp->fx_addsy); | |
1205 | value -= S_GET_VALUE (fixp->fx_subsy); | |
1206 | ||
1207 | /* When fx_addsy and fx_subsy both are zero, md_apply_fix | |
1208 | only takes it's second operands for the fixup value. */ | |
1209 | fixp->fx_addsy = NULL; | |
1210 | fixp->fx_subsy = NULL; | |
1211 | md_apply_fix (fixp, (valueT *) &value, NULL); | |
1212 | ||
1213 | return NULL; | |
1214 | } | |
1215 | ||
dc191a8f | 1216 | reloc = xmalloc (sizeof (arelent)); |
adde6300 | 1217 | |
dc191a8f | 1218 | reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *)); |
adde6300 AM |
1219 | *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); |
1220 | ||
1221 | reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
1222 | reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
1223 | if (reloc->howto == (reloc_howto_type *) NULL) | |
1224 | { | |
1225 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
c6a7ab1f NC |
1226 | _("reloc %d not supported by object file format"), |
1227 | (int) fixp->fx_r_type); | |
adde6300 AM |
1228 | return NULL; |
1229 | } | |
1230 | ||
1231 | if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT | |
1232 | || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY) | |
1233 | reloc->address = fixp->fx_offset; | |
1234 | ||
1235 | reloc->addend = fixp->fx_offset; | |
1236 | ||
1237 | return reloc; | |
1238 | } | |
1239 | ||
adde6300 | 1240 | void |
dc191a8f | 1241 | md_assemble (char *str) |
adde6300 | 1242 | { |
c6a7ab1f | 1243 | struct avr_opcodes_s *opcode; |
adde6300 AM |
1244 | char op[11]; |
1245 | ||
c6a7ab1f | 1246 | str = skip_space (extract_word (str, op, sizeof (op))); |
adde6300 AM |
1247 | |
1248 | if (!op[0]) | |
00d2865b | 1249 | as_bad (_("can't find opcode ")); |
adde6300 AM |
1250 | |
1251 | opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op); | |
1252 | ||
1253 | if (opcode == NULL) | |
1254 | { | |
00d2865b | 1255 | as_bad (_("unknown opcode `%s'"), op); |
adde6300 AM |
1256 | return; |
1257 | } | |
1258 | ||
b170af93 | 1259 | /* Special case for opcodes with optional operands (lpm, elpm) - |
1188e082 | 1260 | version with operands exists in avr_opcodes[] in the next entry. */ |
c6a7ab1f | 1261 | |
1188e082 DC |
1262 | if (*str && *opcode->constraints == '?') |
1263 | ++opcode; | |
b170af93 | 1264 | |
00d2865b NC |
1265 | if (!avr_opt.all_opcodes && (opcode->isa & avr_mcu->isa) != opcode->isa) |
1266 | as_bad (_("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name); | |
adde6300 AM |
1267 | |
1268 | /* We used to set input_line_pointer to the result of get_operands, | |
1269 | but that is wrong. Our caller assumes we don't change it. */ | |
1270 | { | |
1271 | char *t = input_line_pointer; | |
dc191a8f | 1272 | |
adde6300 | 1273 | avr_operands (opcode, &str); |
b170af93 | 1274 | if (*skip_space (str)) |
00d2865b | 1275 | as_bad (_("garbage at end of line")); |
adde6300 AM |
1276 | input_line_pointer = t; |
1277 | } | |
1278 | } | |
1279 | ||
adde6300 | 1280 | /* Flag to pass `pm' mode between `avr_parse_cons_expression' and |
c6a7ab1f | 1281 | `avr_cons_fix_new'. */ |
adde6300 AM |
1282 | static int exp_mod_pm = 0; |
1283 | ||
1284 | /* Parse special CONS expression: pm (expression) | |
1285 | which is used for addressing to a program memory. | |
c6a7ab1f NC |
1286 | Relocation: BFD_RELOC_AVR_16_PM. */ |
1287 | ||
adde6300 | 1288 | void |
dc191a8f | 1289 | avr_parse_cons_expression (expressionS *exp, int nbytes) |
adde6300 | 1290 | { |
c6a7ab1f | 1291 | char *tmp; |
adde6300 AM |
1292 | |
1293 | exp_mod_pm = 0; | |
1294 | ||
1295 | tmp = input_line_pointer = skip_space (input_line_pointer); | |
1296 | ||
1297 | if (nbytes == 2) | |
1298 | { | |
c6a7ab1f | 1299 | char *pm_name = "pm"; |
adde6300 | 1300 | int len = strlen (pm_name); |
1dab94dd | 1301 | |
adde6300 AM |
1302 | if (strncasecmp (input_line_pointer, pm_name, len) == 0) |
1303 | { | |
1304 | input_line_pointer = skip_space (input_line_pointer + len); | |
1dab94dd | 1305 | |
adde6300 AM |
1306 | if (*input_line_pointer == '(') |
1307 | { | |
1308 | input_line_pointer = skip_space (input_line_pointer + 1); | |
1309 | exp_mod_pm = 1; | |
1310 | expression (exp); | |
1dab94dd | 1311 | |
adde6300 AM |
1312 | if (*input_line_pointer == ')') |
1313 | ++input_line_pointer; | |
1314 | else | |
1315 | { | |
00d2865b | 1316 | as_bad (_("`)' required")); |
adde6300 AM |
1317 | exp_mod_pm = 0; |
1318 | } | |
1dab94dd | 1319 | |
adde6300 AM |
1320 | return; |
1321 | } | |
1dab94dd | 1322 | |
adde6300 AM |
1323 | input_line_pointer = tmp; |
1324 | } | |
1325 | } | |
1dab94dd | 1326 | |
adde6300 AM |
1327 | expression (exp); |
1328 | } | |
1329 | ||
1330 | void | |
dc191a8f NC |
1331 | avr_cons_fix_new (fragS *frag, |
1332 | int where, | |
1333 | int nbytes, | |
1334 | expressionS *exp) | |
adde6300 AM |
1335 | { |
1336 | if (exp_mod_pm == 0) | |
1337 | { | |
1338 | if (nbytes == 2) | |
b34976b6 | 1339 | fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_16); |
adde6300 | 1340 | else if (nbytes == 4) |
b34976b6 | 1341 | fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_32); |
adde6300 | 1342 | else |
00d2865b | 1343 | as_bad (_("illegal %srelocation size: %d"), "", nbytes); |
adde6300 AM |
1344 | } |
1345 | else | |
1346 | { | |
1347 | if (nbytes == 2) | |
b34976b6 | 1348 | fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_AVR_16_PM); |
adde6300 | 1349 | else |
00d2865b | 1350 | as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes); |
adde6300 AM |
1351 | exp_mod_pm = 0; |
1352 | } | |
1353 | } |