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