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