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