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