Add support for a __gcc_isr pseudo isntruction to the AVR assembler.
[deliverable/binutils-gdb.git] / gas / config / tc-avr.c
CommitLineData
adde6300
AM
1/* tc-avr.c -- Assembler code for the ATMEL AVR
2
2571583a 3 Copyright (C) 1999-2017 Free Software Foundation, Inc.
adde6300
AM
4 Contributed by Denis Chertykov <denisc@overta.ru>
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
ec2655a6 10 the Free Software Foundation; either version 3, or (at your option)
adde6300
AM
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
4b4da160
NC
20 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
adde6300 22
adde6300 23#include "as.h"
3882b010 24#include "safe-ctype.h"
adde6300 25#include "subsegs.h"
32f76c67 26#include "struc-symbol.h"
fb5b7503 27#include "dwarf2dbg.h"
af3ecb4a 28#include "dw2gencfi.h"
eac7440d 29#include "elf/avr.h"
fdd410ac
AB
30#include "elf32-avr.h"
31
32/* For building a linked list of AVR_PROPERTY_RECORD structures. */
33struct avr_property_record_link
34{
35 struct avr_property_record record;
36 struct avr_property_record_link *next;
37};
adde6300 38
1188e082
DC
39struct avr_opcodes_s
40{
e0471c16
TS
41 const char * name;
42 const char * constraints;
43 const char * opcode;
dc191a8f
NC
44 int insn_size; /* In words. */
45 int isa;
46 unsigned int bin_opcode;
1188e082
DC
47};
48
49#define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
8cc66334 50{#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
1188e082
DC
51
52struct avr_opcodes_s avr_opcodes[] =
53{
54 #include "opcode/avr.h"
8cc66334 55 {NULL, NULL, NULL, 0, 0, 0}
1188e082
DC
56};
57
32f76c67
GJL
58
59/* Stuff for the `__gcc_isr' pseudo instruction.
60
61 Purpose of the pseudo instruction is to emit more efficient ISR prologues
62 and epilogues than GCC currently does. GCC has no explicit (on RTL level)
63 modelling of SREG, TMP_REG or ZERO_REG. These regs are used implicitly
64 during instruction printing. That doesn't hurt too much for ordinary
65 functions, however for small ISRs there might be some overhead.
66
67 As implementing http://gcc.gnu.org/PR20296 would imply an almost complete
68 rewite of GCC's AVR back-end (which might pop up less optimized code in
69 other places), we provide a pseudo-instruction which is resolved by GAS
70 into ISR prologue / epilogue as expected by GCC.
71
72 Using GAS for this purpose has the additional benefit that it can scan
73 code emit by inline asm which is opaque to GCC.
74
75 The pseudo-instruction is only supposed to handle the starting of
76 prologue and the ending of epilogues (without RETI) which deal with
77 SREG, TMP_REG and ZERO_REG and one additional, optional general purpose
78 register.
79
80 __gcc_isr consists of 3 different "chunks":
81
82 __gcc_isr 1
83 Chunk 1 (ISR_CHUNK_Prologue)
84 Start the ISR code. Will be replaced by ISR prologue by next Done chunk.
85 Must be the 1st chunk in a file or follow a Done chunk from previous
86 ISR (which has been patched already).
87
88 It will finish the current frag and emit a new frag of
89 type rs_machine_dependent, subtype ISR_CHUNK_Prologue.
90
91 __gcc_isr 2
92 Chunk 2 (ISR_CHUNK_Epilogue)
93 Will be replaced by ISR epilogue by next Done chunk. Must follow
94 chunk 1 (Prologue) or chunk 2 (Epilogue). Functions might come
95 without epilogue or with more than one epilogue, and even code
96 located statically after the last epilogue might belong to a function.
97
98 It will finish the current frag and emit a new frag of
99 type rs_machine_dependent, subtype ISR_CHUNK_Epilogue.
100
101 __gcc_isr 0, Rx
102 Chunk 0 (ISR_CHUNK_Done)
103 Must follow chunk 1 (Prologue) or chunk 2 (Epilogue) and finishes
104 the ISR code. Only GCC can know where a function's code ends.
105
106 It triggers the patch-up of all rs_machine_dependent frags in the
107 current frag chain and turns them into ordinary rs_fill code frags.
108
109 If Rx is a register > ZERO_REG then GCC also wants to push / pop Rx.
110 If neither TMP_REG nor ZERO_REG are needed, Rx will be used in
111 the push / pop sequence avoiding the need for TMP_REG / ZERO_REG.
112 If Rx <= ZERO_REG then GCC doesn't assume anything about Rx.
113
114 Assumptions:
115
116 o GCC takes care of code that is opaque to GAS like tail calls
117 or non-local goto.
118
119 o Using SEI / CLI does not count as clobbering SREG. This is
120 because a final RETI will restore the I-flag.
121
122 o Using OUT or ST* is supposed not to clobber SREG. Sequences like
123
124 IN-SREG + CLI + Atomic-Code + OUT-SREG
125
126 will still work as expected because the scan will reveal any
127 clobber of SREG other than I-flag and emit PUSH / POP of SREG.
128*/
129
130enum
131 {
132 ISR_CHUNK_Done = 0,
133 ISR_CHUNK_Prologue = 1,
134 ISR_CHUNK_Epilogue = 2
135 };
136
137static struct
138{
139 /* Previous __gcc_isr chunk (one of the enums above)
140 and it's location for diagnostics. */
141 int prev_chunk;
142 unsigned line;
143 const char *file;
144 /* Replacer for __gcc_isr.n_pushed once we know how many regs are
145 pushed by the Prologue chunk. */
146 symbolS *sym_n_pushed;
147
148 /* Set and used during parse from chunk 1 (Prologue) up to chunk 0 (Done).
149 Set by `avr_update_gccisr' and used by `avr_patch_gccisr_frag'. */
150 int need_reg_tmp;
151 int need_reg_zero;
152 int need_sreg;
153} avr_isr;
154
155static void avr_gccisr_operands (struct avr_opcodes_s*, char**);
156static void avr_update_gccisr (struct avr_opcodes_s*, int, int);
157static struct avr_opcodes_s *avr_gccisr_opcode;
158
adde6300
AM
159const char comment_chars[] = ";";
160const char line_comment_chars[] = "#";
161const char line_separator_chars[] = "$";
162
adde6300
AM
163const char *md_shortopts = "m:";
164struct mcu_type_s
165{
e0471c16 166 const char *name;
adde6300
AM
167 int isa;
168 int mach;
169};
170
1f8ae5e6 171/* XXX - devices that don't seem to exist (renamed, replaced with larger
7b21ac3f 172 ones, or planned but never produced), left here for compatibility. */
1f8ae5e6 173
adde6300
AM
174static struct mcu_type_s mcu_types[] =
175{
7b21ac3f 176 {"avr1", AVR_ISA_AVR1, bfd_mach_avr1},
33eaf5de 177/* TODO: instruction set for avr2 architecture should be AVR_ISA_AVR2,
99700d6f
NC
178 but set to AVR_ISA_AVR25 for some following version
179 of GCC (from 4.3) for backward compatibility. */
7b21ac3f
EW
180 {"avr2", AVR_ISA_AVR25, bfd_mach_avr2},
181 {"avr25", AVR_ISA_AVR25, bfd_mach_avr25},
33eaf5de 182/* TODO: instruction set for avr3 architecture should be AVR_ISA_AVR3,
99700d6f 183 but set to AVR_ISA_AVR3_ALL for some following version
7b21ac3f
EW
184 of GCC (from 4.3) for backward compatibility. */
185 {"avr3", AVR_ISA_AVR3_ALL, bfd_mach_avr3},
186 {"avr31", AVR_ISA_AVR31, bfd_mach_avr31},
187 {"avr35", AVR_ISA_AVR35, bfd_mach_avr35},
188 {"avr4", AVR_ISA_AVR4, bfd_mach_avr4},
33eaf5de 189/* TODO: instruction set for avr5 architecture should be AVR_ISA_AVR5,
99700d6f 190 but set to AVR_ISA_AVR51 for some following version
7b21ac3f
EW
191 of GCC (from 4.3) for backward compatibility. */
192 {"avr5", AVR_ISA_AVR51, bfd_mach_avr5},
193 {"avr51", AVR_ISA_AVR51, bfd_mach_avr51},
194 {"avr6", AVR_ISA_AVR6, bfd_mach_avr6},
8cc66334
EW
195 {"avrxmega1", AVR_ISA_XMEGA, bfd_mach_avrxmega1},
196 {"avrxmega2", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
197 {"avrxmega3", AVR_ISA_XMEGA, bfd_mach_avrxmega3},
198 {"avrxmega4", AVR_ISA_XMEGA, bfd_mach_avrxmega4},
199 {"avrxmega5", AVR_ISA_XMEGA, bfd_mach_avrxmega5},
200 {"avrxmega6", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
201 {"avrxmega7", AVR_ISA_XMEGA, bfd_mach_avrxmega7},
f36e8886 202 {"avrtiny", AVR_ISA_AVRTINY, bfd_mach_avrtiny},
28c9d252 203 {"at90s1200", AVR_ISA_1200, bfd_mach_avr1},
7b21ac3f
EW
204 {"attiny11", AVR_ISA_AVR1, bfd_mach_avr1},
205 {"attiny12", AVR_ISA_AVR1, bfd_mach_avr1},
206 {"attiny15", AVR_ISA_AVR1, bfd_mach_avr1},
207 {"attiny28", AVR_ISA_AVR1, bfd_mach_avr1},
208 {"at90s2313", AVR_ISA_AVR2, bfd_mach_avr2},
209 {"at90s2323", AVR_ISA_AVR2, bfd_mach_avr2},
210 {"at90s2333", AVR_ISA_AVR2, bfd_mach_avr2}, /* XXX -> 4433 */
211 {"at90s2343", AVR_ISA_AVR2, bfd_mach_avr2},
212 {"attiny22", AVR_ISA_AVR2, bfd_mach_avr2}, /* XXX -> 2343 */
d669d37f 213 {"attiny26", AVR_ISA_2xxe, bfd_mach_avr2},
7b21ac3f
EW
214 {"at90s4414", AVR_ISA_AVR2, bfd_mach_avr2}, /* XXX -> 8515 */
215 {"at90s4433", AVR_ISA_AVR2, bfd_mach_avr2},
216 {"at90s4434", AVR_ISA_AVR2, bfd_mach_avr2}, /* XXX -> 8535 */
217 {"at90s8515", AVR_ISA_AVR2, bfd_mach_avr2},
218 {"at90c8534", AVR_ISA_AVR2, bfd_mach_avr2},
219 {"at90s8535", AVR_ISA_AVR2, bfd_mach_avr2},
255d9eec 220 {"ata5272", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f
EW
221 {"attiny13", AVR_ISA_AVR25, bfd_mach_avr25},
222 {"attiny13a", AVR_ISA_AVR25, bfd_mach_avr25},
223 {"attiny2313", AVR_ISA_AVR25, bfd_mach_avr25},
8453da2e 224 {"attiny2313a",AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f 225 {"attiny24", AVR_ISA_AVR25, bfd_mach_avr25},
8453da2e
EW
226 {"attiny24a", AVR_ISA_AVR25, bfd_mach_avr25},
227 {"attiny4313", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f 228 {"attiny44", AVR_ISA_AVR25, bfd_mach_avr25},
8453da2e 229 {"attiny44a", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f 230 {"attiny84", AVR_ISA_AVR25, bfd_mach_avr25},
e760a81b 231 {"attiny84a", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f
EW
232 {"attiny25", AVR_ISA_AVR25, bfd_mach_avr25},
233 {"attiny45", AVR_ISA_AVR25, bfd_mach_avr25},
234 {"attiny85", AVR_ISA_AVR25, bfd_mach_avr25},
235 {"attiny261", AVR_ISA_AVR25, bfd_mach_avr25},
8453da2e 236 {"attiny261a", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f 237 {"attiny461", AVR_ISA_AVR25, bfd_mach_avr25},
e760a81b 238 {"attiny461a", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f 239 {"attiny861", AVR_ISA_AVR25, bfd_mach_avr25},
8453da2e 240 {"attiny861a", AVR_ISA_AVR25, bfd_mach_avr25},
2b02f87c 241 {"attiny87", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f
EW
242 {"attiny43u", AVR_ISA_AVR25, bfd_mach_avr25},
243 {"attiny48", AVR_ISA_AVR25, bfd_mach_avr25},
244 {"attiny88", AVR_ISA_AVR25, bfd_mach_avr25},
255d9eec 245 {"attiny828", AVR_ISA_AVR25, bfd_mach_avr25},
7b21ac3f
EW
246 {"at86rf401", AVR_ISA_RF401, bfd_mach_avr25},
247 {"at43usb355", AVR_ISA_AVR3, bfd_mach_avr3},
248 {"at76c711", AVR_ISA_AVR3, bfd_mach_avr3},
249 {"atmega103", AVR_ISA_AVR31, bfd_mach_avr31},
250 {"at43usb320", AVR_ISA_AVR31, bfd_mach_avr31},
251 {"attiny167", AVR_ISA_AVR35, bfd_mach_avr35},
252 {"at90usb82", AVR_ISA_AVR35, bfd_mach_avr35},
253 {"at90usb162", AVR_ISA_AVR35, bfd_mach_avr35},
255d9eec 254 {"ata5505", AVR_ISA_AVR35, bfd_mach_avr35},
11908008
EW
255 {"atmega8u2", AVR_ISA_AVR35, bfd_mach_avr35},
256 {"atmega16u2", AVR_ISA_AVR35, bfd_mach_avr35},
257 {"atmega32u2", AVR_ISA_AVR35, bfd_mach_avr35},
255d9eec 258 {"attiny1634", AVR_ISA_AVR35, bfd_mach_avr35},
28c9d252 259 {"atmega8", AVR_ISA_M8, bfd_mach_avr4},
8be59acb 260 {"ata6289", AVR_ISA_AVR4, bfd_mach_avr4},
255d9eec
DC
261 {"atmega8a", AVR_ISA_M8, bfd_mach_avr4},
262 {"ata6285", AVR_ISA_AVR4, bfd_mach_avr4},
263 {"ata6286", AVR_ISA_AVR4, bfd_mach_avr4},
7b21ac3f 264 {"atmega48", AVR_ISA_AVR4, bfd_mach_avr4},
e760a81b 265 {"atmega48a", AVR_ISA_AVR4, bfd_mach_avr4},
255d9eec 266 {"atmega48pa", AVR_ISA_AVR4, bfd_mach_avr4},
7b21ac3f
EW
267 {"atmega48p", AVR_ISA_AVR4, bfd_mach_avr4},
268 {"atmega88", AVR_ISA_AVR4, bfd_mach_avr4},
e760a81b 269 {"atmega88a", AVR_ISA_AVR4, bfd_mach_avr4},
7b21ac3f 270 {"atmega88p", AVR_ISA_AVR4, bfd_mach_avr4},
e760a81b 271 {"atmega88pa", AVR_ISA_AVR4, bfd_mach_avr4},
28c9d252
NC
272 {"atmega8515", AVR_ISA_M8, bfd_mach_avr4},
273 {"atmega8535", AVR_ISA_M8, bfd_mach_avr4},
7b21ac3f
EW
274 {"atmega8hva", AVR_ISA_AVR4, bfd_mach_avr4},
275 {"at90pwm1", AVR_ISA_AVR4, bfd_mach_avr4},
276 {"at90pwm2", AVR_ISA_AVR4, bfd_mach_avr4},
277 {"at90pwm2b", AVR_ISA_AVR4, bfd_mach_avr4},
278 {"at90pwm3", AVR_ISA_AVR4, bfd_mach_avr4},
279 {"at90pwm3b", AVR_ISA_AVR4, bfd_mach_avr4},
2b02f87c 280 {"at90pwm81", AVR_ISA_AVR4, bfd_mach_avr4},
255d9eec
DC
281 {"at90pwm161", AVR_ISA_AVR5, bfd_mach_avr5},
282 {"ata5790", AVR_ISA_AVR5, bfd_mach_avr5},
283 {"ata5795", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 284 {"atmega16", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 285 {"atmega16a", AVR_ISA_AVR5, bfd_mach_avr5},
28c9d252 286 {"atmega161", AVR_ISA_M161, bfd_mach_avr5},
7b21ac3f 287 {"atmega162", AVR_ISA_AVR5, bfd_mach_avr5},
28c9d252 288 {"atmega163", AVR_ISA_M161, bfd_mach_avr5},
e760a81b 289 {"atmega164a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 290 {"atmega164p", AVR_ISA_AVR5, bfd_mach_avr5},
255d9eec 291 {"atmega164pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 292 {"atmega165", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 293 {"atmega165a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 294 {"atmega165p", AVR_ISA_AVR5, bfd_mach_avr5},
255d9eec 295 {"atmega165pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 296 {"atmega168", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 297 {"atmega168a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 298 {"atmega168p", AVR_ISA_AVR5, bfd_mach_avr5},
255d9eec 299 {"atmega168pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 300 {"atmega169", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 301 {"atmega169a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 302 {"atmega169p", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 303 {"atmega169pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 304 {"atmega32", AVR_ISA_AVR5, bfd_mach_avr5},
255d9eec 305 {"atmega32a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 306 {"atmega323", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 307 {"atmega324a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 308 {"atmega324p", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 309 {"atmega324pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 310 {"atmega325", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 311 {"atmega325a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 312 {"atmega325p", AVR_ISA_AVR5, bfd_mach_avr5},
b8c610a7 313 {"atmega325pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 314 {"atmega3250", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 315 {"atmega3250a",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 316 {"atmega3250p",AVR_ISA_AVR5, bfd_mach_avr5},
b8c610a7 317 {"atmega3250pa",AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 318 {"atmega328", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f
EW
319 {"atmega328p", AVR_ISA_AVR5, bfd_mach_avr5},
320 {"atmega329", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 321 {"atmega329a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 322 {"atmega329p", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 323 {"atmega329pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 324 {"atmega3290", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 325 {"atmega3290a",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 326 {"atmega3290p",AVR_ISA_AVR5, bfd_mach_avr5},
b8c610a7 327 {"atmega3290pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 328 {"atmega406", AVR_ISA_AVR5, bfd_mach_avr5},
255d9eec
DC
329 {"atmega64rfr2", AVR_ISA_AVR5, bfd_mach_avr5},
330 {"atmega644rfr2",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 331 {"atmega64", AVR_ISA_AVR5, bfd_mach_avr5},
255d9eec 332 {"atmega64a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f
EW
333 {"atmega640", AVR_ISA_AVR5, bfd_mach_avr5},
334 {"atmega644", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b 335 {"atmega644a", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 336 {"atmega644p", AVR_ISA_AVR5, bfd_mach_avr5},
8453da2e 337 {"atmega644pa",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 338 {"atmega645", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b
EW
339 {"atmega645a", AVR_ISA_AVR5, bfd_mach_avr5},
340 {"atmega645p", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 341 {"atmega649", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b
EW
342 {"atmega649a", AVR_ISA_AVR5, bfd_mach_avr5},
343 {"atmega649p", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 344 {"atmega6450", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b
EW
345 {"atmega6450a",AVR_ISA_AVR5, bfd_mach_avr5},
346 {"atmega6450p",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 347 {"atmega6490", AVR_ISA_AVR5, bfd_mach_avr5},
e760a81b
EW
348 {"atmega6490a",AVR_ISA_AVR5, bfd_mach_avr5},
349 {"atmega6490p",AVR_ISA_AVR5, bfd_mach_avr5},
4d13caa0
NC
350 {"atmega64rfr2",AVR_ISA_AVR5, bfd_mach_avr5},
351 {"atmega644rfr2",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 352 {"atmega16hva",AVR_ISA_AVR5, bfd_mach_avr5},
255d9eec 353 {"atmega16hva2",AVR_ISA_AVR5, bfd_mach_avr5},
2b02f87c 354 {"atmega16hvb",AVR_ISA_AVR5, bfd_mach_avr5},
b8c610a7 355 {"atmega16hvbrevb",AVR_ISA_AVR5,bfd_mach_avr5},
2b02f87c 356 {"atmega32hvb",AVR_ISA_AVR5, bfd_mach_avr5},
b8c610a7 357 {"atmega32hvbrevb",AVR_ISA_AVR5,bfd_mach_avr5},
e760a81b 358 {"atmega64hve",AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f
EW
359 {"at90can32" , AVR_ISA_AVR5, bfd_mach_avr5},
360 {"at90can64" , AVR_ISA_AVR5, bfd_mach_avr5},
b8c610a7 361 {"at90pwm161", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f
EW
362 {"at90pwm216", AVR_ISA_AVR5, bfd_mach_avr5},
363 {"at90pwm316", AVR_ISA_AVR5, bfd_mach_avr5},
364 {"atmega32c1", AVR_ISA_AVR5, bfd_mach_avr5},
2b02f87c
NC
365 {"atmega64c1", AVR_ISA_AVR5, bfd_mach_avr5},
366 {"atmega16m1", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 367 {"atmega32m1", AVR_ISA_AVR5, bfd_mach_avr5},
2b02f87c
NC
368 {"atmega64m1", AVR_ISA_AVR5, bfd_mach_avr5},
369 {"atmega16u4", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 370 {"atmega32u4", AVR_ISA_AVR5, bfd_mach_avr5},
2b02f87c 371 {"atmega32u6", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f
EW
372 {"at90usb646", AVR_ISA_AVR5, bfd_mach_avr5},
373 {"at90usb647", AVR_ISA_AVR5, bfd_mach_avr5},
2b02f87c 374 {"at90scr100", AVR_ISA_AVR5, bfd_mach_avr5},
28c9d252 375 {"at94k", AVR_ISA_94K, bfd_mach_avr5},
e760a81b 376 {"m3000", AVR_ISA_AVR5, bfd_mach_avr5},
7b21ac3f 377 {"atmega128", AVR_ISA_AVR51, bfd_mach_avr51},
255d9eec 378 {"atmega128a", AVR_ISA_AVR51, bfd_mach_avr51},
7b21ac3f
EW
379 {"atmega1280", AVR_ISA_AVR51, bfd_mach_avr51},
380 {"atmega1281", AVR_ISA_AVR51, bfd_mach_avr51},
255d9eec 381 {"atmega1284", AVR_ISA_AVR51, bfd_mach_avr51},
7b21ac3f 382 {"atmega1284p",AVR_ISA_AVR51, bfd_mach_avr51},
17f4880d 383 {"atmega128rfa1",AVR_ISA_AVR51, bfd_mach_avr51},
4d13caa0
NC
384 {"atmega128rfr2",AVR_ISA_AVR51, bfd_mach_avr51},
385 {"atmega1284rfr2",AVR_ISA_AVR51, bfd_mach_avr51},
7b21ac3f
EW
386 {"at90can128", AVR_ISA_AVR51, bfd_mach_avr51},
387 {"at90usb1286",AVR_ISA_AVR51, bfd_mach_avr51},
388 {"at90usb1287",AVR_ISA_AVR51, bfd_mach_avr51},
389 {"atmega2560", AVR_ISA_AVR6, bfd_mach_avr6},
390 {"atmega2561", AVR_ISA_AVR6, bfd_mach_avr6},
4d13caa0
NC
391 {"atmega256rfr2", AVR_ISA_AVR6, bfd_mach_avr6},
392 {"atmega2564rfr2", AVR_ISA_AVR6, bfd_mach_avr6},
8cc66334 393 {"atxmega16a4", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
255d9eec
DC
394 {"atxmega16a4u",AVR_ISA_XMEGAU, bfd_mach_avrxmega2},
395 {"atxmega16c4", AVR_ISA_XMEGAU, bfd_mach_avrxmega2},
8cc66334
EW
396 {"atxmega16d4", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
397 {"atxmega32a4", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
255d9eec
DC
398 {"atxmega32a4u",AVR_ISA_XMEGAU, bfd_mach_avrxmega2},
399 {"atxmega32c4", AVR_ISA_XMEGAU, bfd_mach_avrxmega2},
8cc66334 400 {"atxmega32d4", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
255d9eec
DC
401 {"atxmega32e5", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
402 {"atxmega16e5", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
403 {"atxmega8e5", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
6f8a4444 404 {"atxmega32x1", AVR_ISA_XMEGA, bfd_mach_avrxmega2},
f4203b2b
JL
405 {"attiny416", AVR_ISA_XMEGA, bfd_mach_avrxmega3},
406 {"attiny417", AVR_ISA_XMEGA, bfd_mach_avrxmega3},
407 {"attiny816", AVR_ISA_XMEGA, bfd_mach_avrxmega3},
408 {"attiny817", AVR_ISA_XMEGA, bfd_mach_avrxmega3},
8cc66334 409 {"atxmega64a3", AVR_ISA_XMEGA, bfd_mach_avrxmega4},
255d9eec
DC
410 {"atxmega64a3u",AVR_ISA_XMEGAU, bfd_mach_avrxmega4},
411 {"atxmega64a4u",AVR_ISA_XMEGAU, bfd_mach_avrxmega4},
412 {"atxmega64b1", AVR_ISA_XMEGAU, bfd_mach_avrxmega4},
413 {"atxmega64b3", AVR_ISA_XMEGAU, bfd_mach_avrxmega4},
414 {"atxmega64c3", AVR_ISA_XMEGAU, bfd_mach_avrxmega4},
8cc66334 415 {"atxmega64d3", AVR_ISA_XMEGA, bfd_mach_avrxmega4},
255d9eec 416 {"atxmega64d4", AVR_ISA_XMEGA, bfd_mach_avrxmega4},
8cc66334 417 {"atxmega64a1", AVR_ISA_XMEGA, bfd_mach_avrxmega5},
7bab7634 418 {"atxmega64a1u",AVR_ISA_XMEGAU, bfd_mach_avrxmega5},
8cc66334 419 {"atxmega128a3", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
255d9eec 420 {"atxmega128a3u",AVR_ISA_XMEGAU,bfd_mach_avrxmega6},
7bab7634 421 {"atxmega128b1", AVR_ISA_XMEGAU, bfd_mach_avrxmega6},
255d9eec
DC
422 {"atxmega128b3", AVR_ISA_XMEGAU,bfd_mach_avrxmega6},
423 {"atxmega128c3", AVR_ISA_XMEGAU,bfd_mach_avrxmega6},
8cc66334 424 {"atxmega128d3", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
255d9eec 425 {"atxmega128d4", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
8cc66334 426 {"atxmega192a3", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
255d9eec
DC
427 {"atxmega192a3u",AVR_ISA_XMEGAU,bfd_mach_avrxmega6},
428 {"atxmega192c3", AVR_ISA_XMEGAU, bfd_mach_avrxmega6},
8cc66334
EW
429 {"atxmega192d3", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
430 {"atxmega256a3", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
255d9eec 431 {"atxmega256a3u",AVR_ISA_XMEGAU,bfd_mach_avrxmega6},
8cc66334 432 {"atxmega256a3b",AVR_ISA_XMEGA, bfd_mach_avrxmega6},
7bab7634 433 {"atxmega256a3bu",AVR_ISA_XMEGAU, bfd_mach_avrxmega6},
255d9eec 434 {"atxmega256c3", AVR_ISA_XMEGAU,bfd_mach_avrxmega6},
8cc66334 435 {"atxmega256d3", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
255d9eec
DC
436 {"atxmega384c3", AVR_ISA_XMEGAU,bfd_mach_avrxmega6},
437 {"atxmega384d3", AVR_ISA_XMEGA, bfd_mach_avrxmega6},
8cc66334 438 {"atxmega128a1", AVR_ISA_XMEGA, bfd_mach_avrxmega7},
7bab7634 439 {"atxmega128a1u", AVR_ISA_XMEGAU, bfd_mach_avrxmega7},
255d9eec 440 {"atxmega128a4u", AVR_ISA_XMEGAU, bfd_mach_avrxmega7},
f36e8886
BS
441 {"attiny4", AVR_ISA_AVRTINY, bfd_mach_avrtiny},
442 {"attiny5", AVR_ISA_AVRTINY, bfd_mach_avrtiny},
443 {"attiny9", AVR_ISA_AVRTINY, bfd_mach_avrtiny},
444 {"attiny10", AVR_ISA_AVRTINY, bfd_mach_avrtiny},
445 {"attiny20", AVR_ISA_AVRTINY, bfd_mach_avrtiny},
446 {"attiny40", AVR_ISA_AVRTINY, bfd_mach_avrtiny},
adde6300
AM
447 {NULL, 0, 0}
448};
449
af910977 450
adde6300 451/* Current MCU type. */
7b21ac3f 452static struct mcu_type_s default_mcu = {"avr2", AVR_ISA_AVR2, bfd_mach_avr2};
af910977 453static struct mcu_type_s specified_mcu;
dc191a8f 454static struct mcu_type_s * avr_mcu = & default_mcu;
adde6300 455
00d2865b
NC
456/* AVR target-specific switches. */
457struct avr_opt_s
458{
dc191a8f
NC
459 int all_opcodes; /* -mall-opcodes: accept all known AVR opcodes. */
460 int no_skip_bug; /* -mno-skip-bug: no warnings for skipping 2-word insns. */
461 int no_wrap; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around. */
edc9e9a6
AB
462 int no_link_relax; /* -mno-link-relax / -mlink-relax: generate (or not)
463 relocations for linker relaxation. */
32f76c67 464 int have_gccisr; /* Whether "__gcc_isr" is a known (pseudo) insn. */
00d2865b
NC
465};
466
32f76c67 467static struct avr_opt_s avr_opt = { 0, 0, 0, 0, 0 };
00d2865b 468
adde6300
AM
469const char EXP_CHARS[] = "eE";
470const char FLT_CHARS[] = "dD";
dc191a8f
NC
471
472static void avr_set_arch (int);
adde6300
AM
473
474/* The target specific pseudo-ops which we support. */
475const pseudo_typeS md_pseudo_table[] =
476{
477 {"arch", avr_set_arch, 0},
478 { NULL, NULL, 0}
479};
480
481#define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
adde6300 482
dc191a8f
NC
483#define EXP_MOD_NAME(i) exp_mod[i].name
484#define EXP_MOD_RELOC(i) exp_mod[i].reloc
485#define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
486#define HAVE_PM_P(i) exp_mod[i].have_pm
adde6300
AM
487
488struct exp_mod_s
489{
e0471c16 490 const char * name;
dc191a8f
NC
491 bfd_reloc_code_real_type reloc;
492 bfd_reloc_code_real_type neg_reloc;
493 int have_pm;
adde6300
AM
494};
495
c6a7ab1f
NC
496static struct exp_mod_s exp_mod[] =
497{
adde6300
AM
498 {"hh8", BFD_RELOC_AVR_HH8_LDI, BFD_RELOC_AVR_HH8_LDI_NEG, 1},
499 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM, BFD_RELOC_AVR_HH8_LDI_PM_NEG, 0},
500 {"hi8", BFD_RELOC_AVR_HI8_LDI, BFD_RELOC_AVR_HI8_LDI_NEG, 1},
501 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM, BFD_RELOC_AVR_HI8_LDI_PM_NEG, 0},
502 {"lo8", BFD_RELOC_AVR_LO8_LDI, BFD_RELOC_AVR_LO8_LDI_NEG, 1},
503 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM, BFD_RELOC_AVR_LO8_LDI_PM_NEG, 0},
df406460 504 {"hlo8", BFD_RELOC_AVR_HH8_LDI, BFD_RELOC_AVR_HH8_LDI_NEG, 0},
e4efb665 505 {"hhi8", BFD_RELOC_AVR_MS8_LDI, BFD_RELOC_AVR_MS8_LDI_NEG, 0},
adde6300
AM
506};
507
2b0f3761 508/* A union used to store indices into the exp_mod[] array
8ad7c533
NC
509 in a hash table which expects void * data types. */
510typedef union
511{
512 void * ptr;
513 int index;
514} mod_index;
515
adde6300
AM
516/* Opcode hash table. */
517static struct hash_control *avr_hash;
518
519/* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
520static struct hash_control *avr_mod_hash;
521
32f76c67
GJL
522/* Whether some opcode does not change SREG. */
523static struct hash_control *avr_no_sreg_hash;
524
525static const char* const avr_no_sreg[] =
526 {
527 /* Arithmetic */
528 "ldi", "swap", "mov", "movw",
529 /* Special instructions. I-Flag will be restored by RETI, and we don't
530 consider I-Flag as being clobbered when changed. */
531 "sei", "cli", "reti", "brie", "brid",
532 "nop", "wdr", "sleep",
533 /* Load / Store */
534 "ld", "ldd", "lds", "pop", "in", "lpm", "elpm",
535 "st", "std", "sts", "push", "out",
536 /* Jumps and Calls. Calls might call code that changes SREG.
537 GCC has to filter out ABI calls. The non-ABI transparent calls
538 must use [R]CALL and are filtered out now by not mentioning them. */
539 "rjmp", "jmp", "ijmp", "ret",
540 /* Skipping. Branches need SREG to be set, hence we regard them
541 as if they changed SREG and don't list them here. */
542 "sbrc", "sbrs", "sbic", "sbis", "cpse",
543 /* I/O Manipulation */
544 "sbi", "cbi",
545 /* Read-Modify-Write */
546 "lac", "las", "lat", "xch"
547 };
548
00d2865b 549#define OPTION_MMCU 'm'
dc191a8f
NC
550enum options
551{
552 OPTION_ALL_OPCODES = OPTION_MD_BASE + 1,
553 OPTION_NO_SKIP_BUG,
af910977 554 OPTION_NO_WRAP,
e4ef1b6c 555 OPTION_ISA_RMW,
edc9e9a6 556 OPTION_LINK_RELAX,
32f76c67
GJL
557 OPTION_NO_LINK_RELAX,
558 OPTION_HAVE_GCCISR
dc191a8f 559};
adde6300 560
c6a7ab1f
NC
561struct option md_longopts[] =
562{
00d2865b
NC
563 { "mmcu", required_argument, NULL, OPTION_MMCU },
564 { "mall-opcodes", no_argument, NULL, OPTION_ALL_OPCODES },
565 { "mno-skip-bug", no_argument, NULL, OPTION_NO_SKIP_BUG },
566 { "mno-wrap", no_argument, NULL, OPTION_NO_WRAP },
af910977 567 { "mrmw", no_argument, NULL, OPTION_ISA_RMW },
e4ef1b6c 568 { "mlink-relax", no_argument, NULL, OPTION_LINK_RELAX },
edc9e9a6 569 { "mno-link-relax", no_argument, NULL, OPTION_NO_LINK_RELAX },
32f76c67 570 { "mgcc-isr", no_argument, NULL, OPTION_HAVE_GCCISR },
00d2865b 571 { NULL, no_argument, NULL, 0 }
adde6300 572};
adde6300 573
c6a7ab1f 574size_t md_longopts_size = sizeof (md_longopts);
00d2865b
NC
575
576/* Display nicely formatted list of known MCU names. */
c6a7ab1f 577
00d2865b 578static void
dc191a8f 579show_mcu_list (FILE *stream)
00d2865b
NC
580{
581 int i, x;
582
583 fprintf (stream, _("Known MCU names:"));
584 x = 1000;
1dab94dd 585
00d2865b
NC
586 for (i = 0; mcu_types[i].name; i++)
587 {
588 int len = strlen (mcu_types[i].name);
1dab94dd 589
00d2865b 590 x += len + 1;
1dab94dd 591
00d2865b 592 if (x < 75)
c6a7ab1f 593 fprintf (stream, " %s", mcu_types[i].name);
00d2865b
NC
594 else
595 {
596 fprintf (stream, "\n %s", mcu_types[i].name);
597 x = len + 2;
598 }
599 }
1dab94dd 600
c6a7ab1f 601 fprintf (stream, "\n");
00d2865b
NC
602}
603
adde6300 604static inline char *
dc191a8f 605skip_space (char *s)
adde6300
AM
606{
607 while (*s == ' ' || *s == '\t')
608 ++s;
609 return s;
610}
611
612/* Extract one word from FROM and copy it to TO. */
c6a7ab1f 613
adde6300
AM
614static char *
615extract_word (char *from, char *to, int limit)
616{
adde6300
AM
617 char *op_end;
618 int size = 0;
619
620 /* Drop leading whitespace. */
621 from = skip_space (from);
622 *to = 0;
c6a7ab1f 623
adde6300 624 /* Find the op code end. */
87975d2a 625 for (op_end = from; *op_end != 0 && is_part_of_name (*op_end);)
adde6300
AM
626 {
627 to[size++] = *op_end++;
628 if (size + 1 >= limit)
629 break;
630 }
1dab94dd 631
adde6300
AM
632 to[size] = 0;
633 return op_end;
634}
635
636int
dc191a8f
NC
637md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
638 asection *seg ATTRIBUTE_UNUSED)
adde6300
AM
639{
640 abort ();
641 return 0;
642}
643
644void
dc191a8f 645md_show_usage (FILE *stream)
adde6300 646{
00d2865b 647 fprintf (stream,
4fb8d1c6 648 _("AVR Assembler options:\n"
adde6300
AM
649 " -mmcu=[avr-name] select microcontroller variant\n"
650 " [avr-name] can be:\n"
7b21ac3f
EW
651 " avr1 - classic AVR core without data RAM\n"
652 " avr2 - classic AVR core with up to 8K program memory\n"
653 " avr25 - classic AVR core with up to 8K program memory\n"
654 " plus the MOVW instruction\n"
655 " avr3 - classic AVR core with up to 64K program memory\n"
656 " avr31 - classic AVR core with up to 128K program memory\n"
657 " avr35 - classic AVR core with up to 64K program memory\n"
658 " plus the MOVW instruction\n"
659 " avr4 - enhanced AVR core with up to 8K program memory\n"
660 " avr5 - enhanced AVR core with up to 64K program memory\n"
661 " avr51 - enhanced AVR core with up to 128K program memory\n"
662 " avr6 - enhanced AVR core with up to 256K program memory\n"
8c997c27 663 " avrxmega2 - XMEGA, > 8K, < 64K FLASH, < 64K RAM\n"
8cc66334
EW
664 " avrxmega3 - XMEGA, > 8K, <= 64K FLASH, > 64K RAM\n"
665 " avrxmega4 - XMEGA, > 64K, <= 128K FLASH, <= 64K RAM\n"
666 " avrxmega5 - XMEGA, > 64K, <= 128K FLASH, > 64K RAM\n"
667 " avrxmega6 - XMEGA, > 128K, <= 256K FLASH, <= 64K RAM\n"
668 " avrxmega7 - XMEGA, > 128K, <= 256K FLASH, > 64K RAM\n"
f36e8886 669 " avrtiny - AVR Tiny core with 16 gp registers\n"));
00d2865b
NC
670 fprintf (stream,
671 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
672 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
673 " (default for avr4, avr5)\n"
674 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
af910977
DC
675 " (default for avr3, avr5)\n"
676 " -mrmw accept Read-Modify-Write instructions\n"
edc9e9a6
AB
677 " -mlink-relax generate relocations for linker relaxation (default)\n"
678 " -mno-link-relax don't generate relocations for linker relaxation.\n"
32f76c67 679 " -mgcc-isr accept the __gcc_isr pseudo-instruction.\n"
edc9e9a6 680 ));
00d2865b 681 show_mcu_list (stream);
adde6300
AM
682}
683
684static void
dc191a8f 685avr_set_arch (int dummy ATTRIBUTE_UNUSED)
adde6300 686{
dc191a8f 687 char str[20];
1dab94dd 688
adde6300 689 input_line_pointer = extract_word (input_line_pointer, str, 20);
00d2865b 690 md_parse_option (OPTION_MMCU, str);
adde6300
AM
691 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
692}
693
694int
17b9d67d 695md_parse_option (int c, const char *arg)
adde6300 696{
00d2865b 697 switch (c)
adde6300 698 {
00d2865b
NC
699 case OPTION_MMCU:
700 {
701 int i;
00d2865b
NC
702
703 for (i = 0; mcu_types[i].name; ++i)
f73e41ef 704 if (strcasecmp (mcu_types[i].name, arg) == 0)
00d2865b 705 break;
adde6300 706
00d2865b
NC
707 if (!mcu_types[i].name)
708 {
709 show_mcu_list (stderr);
710 as_fatal (_("unknown MCU: %s\n"), arg);
711 }
65aa24b6 712
00d2865b
NC
713 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
714 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
715 as .arch ... in the asm output at the same time. */
00d2865b 716 if (avr_mcu == &default_mcu || avr_mcu->mach == mcu_types[i].mach)
e1fa0163
NC
717 {
718 specified_mcu.name = mcu_types[i].name;
719 specified_mcu.isa |= mcu_types[i].isa;
720 specified_mcu.mach = mcu_types[i].mach;
721 avr_mcu = &specified_mcu;
722 }
00d2865b
NC
723 else
724 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
725 avr_mcu->name, mcu_types[i].name);
726 return 1;
727 }
728 case OPTION_ALL_OPCODES:
729 avr_opt.all_opcodes = 1;
730 return 1;
731 case OPTION_NO_SKIP_BUG:
732 avr_opt.no_skip_bug = 1;
733 return 1;
734 case OPTION_NO_WRAP:
735 avr_opt.no_wrap = 1;
adde6300 736 return 1;
af910977
DC
737 case OPTION_ISA_RMW:
738 specified_mcu.isa |= AVR_ISA_RMW;
739 return 1;
e4ef1b6c 740 case OPTION_LINK_RELAX:
edc9e9a6
AB
741 avr_opt.no_link_relax = 0;
742 return 1;
743 case OPTION_NO_LINK_RELAX:
744 avr_opt.no_link_relax = 1;
e4ef1b6c 745 return 1;
32f76c67
GJL
746 case OPTION_HAVE_GCCISR:
747 avr_opt.have_gccisr = 1;
748 return 1;
adde6300 749 }
1dab94dd 750
adde6300
AM
751 return 0;
752}
753
32f76c67
GJL
754
755/* Implement `md_undefined_symbol' */
756/* If we are in `__gcc_isr' chunk, pop up `__gcc_isr.n_pushed.<NUM>'
757 instead of `__gcc_isr.n_pushed'. This will be resolved by the Done
758 chunk in `avr_patch_gccisr_frag' to the number of PUSHes produced by
759 the Prologue chunk. */
760
adde6300 761symbolS *
32f76c67 762avr_undefined_symbol (char *name)
adde6300 763{
32f76c67
GJL
764 if (ISR_CHUNK_Done != avr_isr.prev_chunk
765 && 0 == strcmp (name, "__gcc_isr.n_pushed"))
766 {
767 if (!avr_isr.sym_n_pushed)
768 {
769 static unsigned suffix;
770 char xname[30];
771 sprintf (xname, "%s.%03u", name, (++suffix) % 1000);
772 avr_isr.sym_n_pushed = symbol_new (xname, undefined_section,
773 (valueT) 0, &zero_address_frag);
774 }
775 return avr_isr.sym_n_pushed;
776 }
777
dc191a8f 778 return NULL;
adde6300
AM
779}
780
6d4af3c2 781const char *
dc191a8f 782md_atof (int type, char *litP, int *sizeP)
adde6300 783{
499ac353 784 return ieee_md_atof (type, litP, sizeP, FALSE);
adde6300
AM
785}
786
787void
dc191a8f
NC
788md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
789 asection *sec ATTRIBUTE_UNUSED,
790 fragS *fragP ATTRIBUTE_UNUSED)
adde6300
AM
791{
792 abort ();
793}
794
adde6300 795void
dc191a8f 796md_begin (void)
adde6300 797{
df136245 798 unsigned int i;
adde6300 799 struct avr_opcodes_s *opcode;
dc191a8f 800
c6a7ab1f 801 avr_hash = hash_new ();
adde6300
AM
802
803 /* Insert unique names into hash table. This hash table then provides a
804 quick index to the first opcode with a particular name in the opcode
805 table. */
adde6300
AM
806 for (opcode = avr_opcodes; opcode->name; opcode++)
807 hash_insert (avr_hash, opcode->name, (char *) opcode);
808
809 avr_mod_hash = hash_new ();
810
dc191a8f 811 for (i = 0; i < ARRAY_SIZE (exp_mod); ++i)
8ad7c533
NC
812 {
813 mod_index m;
814
815 m.index = i + 10;
816 hash_insert (avr_mod_hash, EXP_MOD_NAME (i), m.ptr);
817 }
c6a7ab1f 818
32f76c67
GJL
819 avr_no_sreg_hash = hash_new ();
820
821 for (i = 0; i < ARRAY_SIZE (avr_no_sreg); ++i)
822 {
823 gas_assert (hash_find (avr_hash, avr_no_sreg[i]));
824 hash_insert (avr_no_sreg_hash, avr_no_sreg[i], (char*) 4 /* dummy */);
825 }
826
827 avr_gccisr_opcode = (struct avr_opcodes_s*) hash_find (avr_hash, "__gcc_isr");
828 gas_assert (avr_gccisr_opcode);
829
adde6300 830 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
edc9e9a6 831 linkrelax = !avr_opt.no_link_relax;
adde6300
AM
832}
833
df136245 834/* Resolve STR as a constant expression and return the result.
c6a7ab1f 835 If result greater than MAX then error. */
df136245
DC
836
837static unsigned int
dc191a8f 838avr_get_constant (char *str, int max)
df136245
DC
839{
840 expressionS ex;
dc191a8f 841
df136245
DC
842 str = skip_space (str);
843 input_line_pointer = str;
dc191a8f 844 expression (& ex);
df136245
DC
845
846 if (ex.X_op != O_constant)
847 as_bad (_("constant value required"));
848
849 if (ex.X_add_number > max || ex.X_add_number < 0)
73f4d86e 850 as_bad (_("number must be positive and less than %d"), max + 1);
1dab94dd 851
df136245
DC
852 return ex.X_add_number;
853}
854
dc191a8f 855/* Parse for ldd/std offset. */
df136245 856
dc191a8f
NC
857static void
858avr_offset_expression (expressionS *exp)
adde6300 859{
dc191a8f
NC
860 char *str = input_line_pointer;
861 char *tmp;
862 char op[8];
adde6300 863
dc191a8f
NC
864 tmp = str;
865 str = extract_word (str, op, sizeof (op));
866
867 input_line_pointer = tmp;
868 expression (exp);
869
870 /* Warn about expressions that fail to use lo8 (). */
871 if (exp->X_op == O_constant)
adde6300 872 {
dc191a8f 873 int x = exp->X_add_number;
28c9d252 874
dc191a8f
NC
875 if (x < -255 || x > 255)
876 as_warn (_("constant out of 8-bit range: %d"), x);
877 }
878}
adde6300 879
dc191a8f 880/* Parse ordinary expression. */
adde6300 881
dc191a8f
NC
882static char *
883parse_exp (char *s, expressionS *op)
884{
885 input_line_pointer = s;
886 expression (op);
887 if (op->X_op == O_absent)
888 as_bad (_("missing operand"));
889 return input_line_pointer;
890}
1dab94dd 891
dc191a8f
NC
892/* Parse special expressions (needed for LDI command):
893 xx8 (address)
894 xx8 (-address)
895 pm_xx8 (address)
896 pm_xx8 (-address)
897 where xx is: hh, hi, lo. */
adde6300 898
dc191a8f
NC
899static bfd_reloc_code_real_type
900avr_ldi_expression (expressionS *exp)
901{
902 char *str = input_line_pointer;
903 char *tmp;
904 char op[8];
905 int mod;
28c9d252
NC
906 int linker_stubs_should_be_generated = 0;
907
dc191a8f 908 tmp = str;
adde6300 909
dc191a8f 910 str = extract_word (str, op, sizeof (op));
adde6300 911
dc191a8f
NC
912 if (op[0])
913 {
8ad7c533 914 mod_index m;
28c9d252 915
8ad7c533
NC
916 m.ptr = hash_find (avr_mod_hash, op);
917 mod = m.index;
1dab94dd 918
dc191a8f
NC
919 if (mod)
920 {
921 int closes = 0;
b170af93 922
dc191a8f
NC
923 mod -= 10;
924 str = skip_space (str);
00d2865b 925
dc191a8f
NC
926 if (*str == '(')
927 {
28c9d252 928 bfd_reloc_code_real_type reloc_to_return;
dc191a8f 929 int neg_p = 0;
00d2865b 930
dc191a8f 931 ++str;
00d2865b 932
dc191a8f 933 if (strncmp ("pm(", str, 3) == 0
28c9d252
NC
934 || strncmp ("gs(",str,3) == 0
935 || strncmp ("-(gs(",str,5) == 0
dc191a8f
NC
936 || strncmp ("-(pm(", str, 5) == 0)
937 {
938 if (HAVE_PM_P (mod))
939 {
940 ++mod;
941 ++closes;
942 }
943 else
944 as_bad (_("illegal expression"));
b170af93 945
28c9d252
NC
946 if (str[0] == 'g' || str[2] == 'g')
947 linker_stubs_should_be_generated = 1;
948
dc191a8f
NC
949 if (*str == '-')
950 {
951 neg_p = 1;
952 ++closes;
953 str += 5;
954 }
955 else
956 str += 3;
957 }
adde6300 958
dc191a8f
NC
959 if (*str == '-' && *(str + 1) == '(')
960 {
961 neg_p ^= 1;
962 ++closes;
963 str += 2;
964 }
750bce0e 965
dc191a8f
NC
966 input_line_pointer = str;
967 expression (exp);
750bce0e 968
dc191a8f
NC
969 do
970 {
971 if (*input_line_pointer != ')')
972 {
973 as_bad (_("`)' required"));
974 break;
975 }
976 input_line_pointer++;
977 }
978 while (closes--);
979
28c9d252
NC
980 reloc_to_return =
981 neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
982 if (linker_stubs_should_be_generated)
983 {
984 switch (reloc_to_return)
985 {
986 case BFD_RELOC_AVR_LO8_LDI_PM:
987 reloc_to_return = BFD_RELOC_AVR_LO8_LDI_GS;
988 break;
989 case BFD_RELOC_AVR_HI8_LDI_PM:
990 reloc_to_return = BFD_RELOC_AVR_HI8_LDI_GS;
991 break;
992
993 default:
0a903bab
NC
994 /* PR 5523: Do not generate a warning here,
995 legitimate code can trigger this case. */
996 break;
28c9d252
NC
997 }
998 }
999 return reloc_to_return;
dc191a8f
NC
1000 }
1001 }
1002 }
750bce0e
NC
1003
1004 input_line_pointer = tmp;
1005 expression (exp);
1006
1007 /* Warn about expressions that fail to use lo8 (). */
1008 if (exp->X_op == O_constant)
1009 {
1010 int x = exp->X_add_number;
dc191a8f 1011
750bce0e
NC
1012 if (x < -255 || x > 255)
1013 as_warn (_("constant out of 8-bit range: %d"), x);
1014 }
dc191a8f
NC
1015
1016 return BFD_RELOC_AVR_LDI;
750bce0e
NC
1017}
1018
df136245 1019/* Parse one instruction operand.
c6a7ab1f
NC
1020 Return operand bitmask. Also fixups can be generated. */
1021
adde6300 1022static unsigned int
dc191a8f
NC
1023avr_operand (struct avr_opcodes_s *opcode,
1024 int where,
e0471c16 1025 const char *op,
32f76c67
GJL
1026 char **line,
1027 int *pregno)
adde6300 1028{
adde6300 1029 expressionS op_expr;
df136245
DC
1030 unsigned int op_mask = 0;
1031 char *str = skip_space (*line);
adde6300 1032
adde6300
AM
1033 switch (*op)
1034 {
1035 /* Any register operand. */
1036 case 'w':
1037 case 'd':
1038 case 'r':
b170af93
DC
1039 case 'a':
1040 case 'v':
75f58085
BS
1041 {
1042 char * old_str = str;
1043 char *lower;
1044 char r_name[20];
1dab94dd 1045
75f58085
BS
1046 str = extract_word (str, r_name, sizeof (r_name));
1047 for (lower = r_name; *lower; ++lower)
1048 {
1049 if (*lower >= 'A' && *lower <= 'Z')
1050 *lower += 'a' - 'A';
1051 }
1052
1053 if (r_name[0] == 'r' && ISDIGIT (r_name[1]) && r_name[2] == 0)
1054 /* Single-digit register number, ie r0-r9. */
1055 op_mask = r_name[1] - '0';
1056 else if (r_name[0] == 'r' && ISDIGIT (r_name[1])
1057 && ISDIGIT (r_name[2]) && r_name[3] == 0)
1058 /* Double-digit register number, ie r10 - r32. */
1059 op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0';
1060 else if (r_name[0] >= 'x' && r_name[0] <= 'z'
1061 && (r_name[1] == 'l' || r_name[1] == 'h') && r_name[2] == 0)
1062 /* Registers r26-r31 referred to by name, ie xl, xh, yl, yh, zl, zh. */
1063 op_mask = (r_name[0] - 'x') * 2 + (r_name[1] == 'h') + 26;
1064 else if ((*op == 'v' || *op == 'w')
1065 && r_name[0] >= 'x' && r_name[0] <= 'z' && r_name[1] == 0)
1066 /* For the movw and addiw instructions, refer to registers x, y and z by name. */
1067 op_mask = (r_name[0] - 'x') * 2 + 26;
1068 else
1069 {
1070 /* Numeric or symbolic constant register number. */
1071 op_mask = avr_get_constant (old_str, 31);
1072 str = input_line_pointer;
1073 }
1074 }
f36e8886 1075
32f76c67
GJL
1076 if (pregno)
1077 *pregno = op_mask;
1078
f36e8886
BS
1079 if (avr_mcu->mach == bfd_mach_avrtiny)
1080 {
1081 if (op_mask < 16 || op_mask > 31)
1082 {
1083 as_bad (_("register name or number from 16 to 31 required"));
1084 break;
1085 }
1086 }
1087 else if (op_mask > 31)
1088 {
1089 as_bad (_("register name or number from 0 to 31 required"));
1090 break;
1091 }
1dab94dd 1092
c6a7ab1f
NC
1093 switch (*op)
1094 {
1095 case 'a':
1096 if (op_mask < 16 || op_mask > 23)
1097 as_bad (_("register r16-r23 required"));
1098 op_mask -= 16;
1099 break;
1dab94dd 1100
c6a7ab1f
NC
1101 case 'd':
1102 if (op_mask < 16)
1103 as_bad (_("register number above 15 required"));
1104 op_mask -= 16;
1105 break;
1dab94dd 1106
c6a7ab1f
NC
1107 case 'v':
1108 if (op_mask & 1)
1109 as_bad (_("even register number required"));
1110 op_mask >>= 1;
1111 break;
1dab94dd 1112
c6a7ab1f 1113 case 'w':
65b1d096 1114 if ((op_mask & 1) || op_mask < 24)
c6a7ab1f 1115 as_bad (_("register r24, r26, r28 or r30 required"));
65b1d096 1116 op_mask = (op_mask - 24) >> 1;
c6a7ab1f
NC
1117 break;
1118 }
1119 break;
adde6300
AM
1120
1121 case 'e':
1122 {
1123 char c;
1dab94dd 1124
adde6300
AM
1125 if (*str == '-')
1126 {
c6a7ab1f 1127 str = skip_space (str + 1);
adde6300
AM
1128 op_mask = 0x1002;
1129 }
3882b010 1130 c = TOLOWER (*str);
adde6300
AM
1131 if (c == 'x')
1132 op_mask |= 0x100c;
1133 else if (c == 'y')
1134 op_mask |= 0x8;
1135 else if (c != 'z')
00d2865b 1136 as_bad (_("pointer register (X, Y or Z) required"));
adde6300 1137
c6a7ab1f 1138 str = skip_space (str + 1);
adde6300
AM
1139 if (*str == '+')
1140 {
1141 ++str;
1142 if (op_mask & 2)
00d2865b 1143 as_bad (_("cannot both predecrement and postincrement"));
adde6300
AM
1144 op_mask |= 0x1001;
1145 }
e38c9cc2 1146
1188e082 1147 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
e38c9cc2 1148 registers, no predecrement, no postincrement. */
00d2865b
NC
1149 if (!avr_opt.all_opcodes && (op_mask & 0x100F)
1150 && !(avr_mcu->isa & AVR_ISA_SRAM))
1151 as_bad (_("addressing mode not supported"));
adde6300
AM
1152 }
1153 break;
1154
b170af93 1155 case 'z':
c6a7ab1f
NC
1156 if (*str == '-')
1157 as_bad (_("can't predecrement"));
1dab94dd 1158
c6a7ab1f
NC
1159 if (! (*str == 'z' || *str == 'Z'))
1160 as_bad (_("pointer register Z required"));
1dab94dd 1161
c6a7ab1f
NC
1162 str = skip_space (str + 1);
1163
1164 if (*str == '+')
1165 {
1166 ++str;
e0471c16 1167 const char *s;
8cc66334
EW
1168 for (s = opcode->opcode; *s; ++s)
1169 {
1170 if (*s == '+')
1171 op_mask |= (1 << (15 - (s - opcode->opcode)));
1172 }
c6a7ab1f 1173 }
d669d37f
NC
1174
1175 /* attiny26 can do "lpm" and "lpm r,Z" but not "lpm r,Z+". */
1176 if (!avr_opt.all_opcodes
1177 && (op_mask & 0x0001)
1178 && !(avr_mcu->isa & AVR_ISA_MOVW))
1179 as_bad (_("postincrement not supported"));
b170af93
DC
1180 break;
1181
adde6300
AM
1182 case 'b':
1183 {
3882b010 1184 char c = TOLOWER (*str++);
1dab94dd 1185
adde6300
AM
1186 if (c == 'y')
1187 op_mask |= 0x8;
1188 else if (c != 'z')
00d2865b 1189 as_bad (_("pointer register (Y or Z) required"));
adde6300
AM
1190 str = skip_space (str);
1191 if (*str++ == '+')
1192 {
750bce0e
NC
1193 input_line_pointer = str;
1194 avr_offset_expression (& op_expr);
adde6300 1195 str = input_line_pointer;
750bce0e
NC
1196 fix_new_exp (frag_now, where, 3,
1197 &op_expr, FALSE, BFD_RELOC_AVR_6);
adde6300
AM
1198 }
1199 }
1200 break;
1201
1202 case 'h':
c6a7ab1f
NC
1203 str = parse_exp (str, &op_expr);
1204 fix_new_exp (frag_now, where, opcode->insn_size * 2,
b34976b6 1205 &op_expr, FALSE, BFD_RELOC_AVR_CALL);
adde6300
AM
1206 break;
1207
1208 case 'L':
c6a7ab1f
NC
1209 str = parse_exp (str, &op_expr);
1210 fix_new_exp (frag_now, where, opcode->insn_size * 2,
b34976b6 1211 &op_expr, TRUE, BFD_RELOC_AVR_13_PCREL);
adde6300
AM
1212 break;
1213
1214 case 'l':
c6a7ab1f
NC
1215 str = parse_exp (str, &op_expr);
1216 fix_new_exp (frag_now, where, opcode->insn_size * 2,
b34976b6 1217 &op_expr, TRUE, BFD_RELOC_AVR_7_PCREL);
adde6300
AM
1218 break;
1219
1220 case 'i':
c6a7ab1f
NC
1221 str = parse_exp (str, &op_expr);
1222 fix_new_exp (frag_now, where + 2, opcode->insn_size * 2,
b34976b6 1223 &op_expr, FALSE, BFD_RELOC_16);
adde6300
AM
1224 break;
1225
f36e8886
BS
1226 case 'j':
1227 str = parse_exp (str, &op_expr);
1228 fix_new_exp (frag_now, where, opcode->insn_size * 2,
1229 &op_expr, FALSE, BFD_RELOC_AVR_LDS_STS_16);
1230 break;
1231
adde6300
AM
1232 case 'M':
1233 {
1234 bfd_reloc_code_real_type r_type;
1dab94dd 1235
c6a7ab1f
NC
1236 input_line_pointer = str;
1237 r_type = avr_ldi_expression (&op_expr);
1238 str = input_line_pointer;
adde6300 1239 fix_new_exp (frag_now, where, 3,
b34976b6 1240 &op_expr, FALSE, r_type);
adde6300
AM
1241 }
1242 break;
1243
1244 case 'n':
1245 {
1246 unsigned int x;
1dab94dd 1247
adde6300
AM
1248 x = ~avr_get_constant (str, 255);
1249 str = input_line_pointer;
1250 op_mask |= (x & 0xf) | ((x << 4) & 0xf00);
1251 }
1252 break;
1253
32f76c67
GJL
1254 case 'N':
1255 {
1256 unsigned int x;
1257
1258 x = avr_get_constant (str, 255);
1259 str = input_line_pointer;
1260 op_mask = x;
1261 }
1262 break;
1263
adde6300 1264 case 'K':
750bce0e
NC
1265 input_line_pointer = str;
1266 avr_offset_expression (& op_expr);
1267 str = input_line_pointer;
1268 fix_new_exp (frag_now, where, 3,
1269 & op_expr, FALSE, BFD_RELOC_AVR_6_ADIW);
adde6300
AM
1270 break;
1271
1272 case 'S':
1273 case 's':
1274 {
1275 unsigned int x;
1dab94dd 1276
adde6300
AM
1277 x = avr_get_constant (str, 7);
1278 str = input_line_pointer;
1279 if (*op == 'S')
1280 x <<= 4;
1281 op_mask |= x;
1282 }
1283 break;
1284
1285 case 'P':
75f58085
BS
1286 str = parse_exp (str, &op_expr);
1287 fix_new_exp (frag_now, where, opcode->insn_size * 2,
1288 &op_expr, FALSE, BFD_RELOC_AVR_PORT6);
adde6300
AM
1289 break;
1290
1291 case 'p':
75f58085
BS
1292 str = parse_exp (str, &op_expr);
1293 fix_new_exp (frag_now, where, opcode->insn_size * 2,
1294 &op_expr, FALSE, BFD_RELOC_AVR_PORT5);
adde6300 1295 break;
1dab94dd 1296
8cc66334
EW
1297 case 'E':
1298 {
1299 unsigned int x;
1300
1301 x = avr_get_constant (str, 15);
1302 str = input_line_pointer;
1303 op_mask |= (x << 4);
1304 }
1305 break;
99700d6f 1306
1188e082
DC
1307 case '?':
1308 break;
1dab94dd 1309
adde6300 1310 default:
00d2865b 1311 as_bad (_("unknown constraint `%c'"), *op);
adde6300 1312 }
1dab94dd 1313
adde6300
AM
1314 *line = str;
1315 return op_mask;
1316}
1317
dc191a8f
NC
1318/* Parse instruction operands.
1319 Return binary opcode. */
1320
1321static unsigned int
1322avr_operands (struct avr_opcodes_s *opcode, char **line)
1323{
e0471c16 1324 const char *op = opcode->constraints;
dc191a8f
NC
1325 unsigned int bin = opcode->bin_opcode;
1326 char *frag = frag_more (opcode->insn_size * 2);
1327 char *str = *line;
1328 int where = frag - frag_now->fr_literal;
1329 static unsigned int prev = 0; /* Previous opcode. */
32f76c67
GJL
1330 int regno1 = -2;
1331 int regno2 = -2;
dc191a8f
NC
1332
1333 /* Opcode have operands. */
1334 if (*op)
1335 {
1336 unsigned int reg1 = 0;
1337 unsigned int reg2 = 0;
1338 int reg1_present = 0;
1339 int reg2_present = 0;
1340
1341 /* Parse first operand. */
1342 if (REGISTER_P (*op))
1343 reg1_present = 1;
32f76c67 1344 reg1 = avr_operand (opcode, where, op, &str, &regno1);
dc191a8f
NC
1345 ++op;
1346
1347 /* Parse second operand. */
1348 if (*op)
1349 {
1350 if (*op == ',')
1351 ++op;
1352
1353 if (*op == '=')
1354 {
1355 reg2 = reg1;
1356 reg2_present = 1;
32f76c67 1357 regno2 = regno1;
dc191a8f
NC
1358 }
1359 else
1360 {
1361 if (REGISTER_P (*op))
1362 reg2_present = 1;
1363
1364 str = skip_space (str);
1365 if (*str++ != ',')
1366 as_bad (_("`,' required"));
1367 str = skip_space (str);
1368
32f76c67 1369 reg2 = avr_operand (opcode, where, op, &str, &regno2);
dc191a8f
NC
1370 }
1371
1372 if (reg1_present && reg2_present)
1373 reg2 = (reg2 & 0xf) | ((reg2 << 5) & 0x200);
1374 else if (reg2_present)
1375 reg2 <<= 4;
1376 }
1377 if (reg1_present)
1378 reg1 <<= 4;
1379 bin |= reg1 | reg2;
1380 }
1381
32f76c67
GJL
1382 if (avr_opt.have_gccisr)
1383 avr_update_gccisr (opcode, regno1, regno2);
1384
dc191a8f
NC
1385 /* Detect undefined combinations (like ld r31,Z+). */
1386 if (!avr_opt.all_opcodes && AVR_UNDEF_P (bin))
1387 as_warn (_("undefined combination of operands"));
1388
1389 if (opcode->insn_size == 2)
1390 {
1391 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
1392 (AVR core bug, fixed in the newer devices). */
1393 if (!(avr_opt.no_skip_bug ||
1394 (avr_mcu->isa & (AVR_ISA_MUL | AVR_ISA_MOVW)))
1395 && AVR_SKIP_P (prev))
1396 as_warn (_("skipping two-word instruction"));
1397
1398 bfd_putl32 ((bfd_vma) bin, frag);
1399 }
1400 else
1401 bfd_putl16 ((bfd_vma) bin, frag);
1402
1403 prev = bin;
1404 *line = str;
1405 return bin;
1406}
1407
adde6300
AM
1408/* GAS will call this function for each section at the end of the assembly,
1409 to permit the CPU backend to adjust the alignment of a section. */
c6a7ab1f 1410
adde6300 1411valueT
dc191a8f 1412md_section_align (asection *seg, valueT addr)
adde6300
AM
1413{
1414 int align = bfd_get_section_alignment (stdoutput, seg);
dce55a03 1415 return ((addr + (1 << align) - 1) & (-1UL << align));
adde6300
AM
1416}
1417
1418/* If you define this macro, it should return the offset between the
1419 address of a PC relative fixup and the position from which the PC
1420 relative adjustment should be made. On many processors, the base
1421 of a PC relative instruction is the next instruction, so this
1422 macro would return the length of an instruction. */
c6a7ab1f 1423
adde6300 1424long
dc191a8f 1425md_pcrel_from_section (fixS *fixp, segT sec)
adde6300 1426{
c6a7ab1f 1427 if (fixp->fx_addsy != (symbolS *) NULL
adde6300
AM
1428 && (!S_IS_DEFINED (fixp->fx_addsy)
1429 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
1430 return 0;
1dab94dd 1431
adde6300
AM
1432 return fixp->fx_frag->fr_address + fixp->fx_where;
1433}
1434
e4ef1b6c
DC
1435static bfd_boolean
1436relaxable_section (asection *sec)
1437{
edc9e9a6
AB
1438 return ((sec->flags & SEC_DEBUGGING) == 0
1439 && (sec->flags & SEC_CODE) != 0
1440 && (sec->flags & SEC_ALLOC) != 0);
e4ef1b6c
DC
1441}
1442
75f58085 1443/* Does whatever the xtensa port does. */
e4ef1b6c
DC
1444int
1445avr_validate_fix_sub (fixS *fix)
1446{
1447 segT add_symbol_segment, sub_symbol_segment;
1448
1449 /* The difference of two symbols should be resolved by the assembler when
1450 linkrelax is not set. If the linker may relax the section containing
1451 the symbols, then an Xtensa DIFF relocation must be generated so that
1452 the linker knows to adjust the difference value. */
1453 if (!linkrelax || fix->fx_addsy == NULL)
1454 return 0;
1455
1456 /* Make sure both symbols are in the same segment, and that segment is
1457 "normal" and relaxable. If the segment is not "normal", then the
1458 fix is not valid. If the segment is not "relaxable", then the fix
1459 should have been handled earlier. */
1460 add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy);
1461 if (! SEG_NORMAL (add_symbol_segment) ||
1462 ! relaxable_section (add_symbol_segment))
1463 return 0;
1464
1465 sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy);
1466 return (sub_symbol_segment == add_symbol_segment);
1467}
1468
1469/* TC_FORCE_RELOCATION hook */
1470
1471/* If linkrelax is turned on, and the symbol to relocate
1472 against is in a relaxable segment, don't compute the value -
75f58085 1473 generate a relocation instead. */
e4ef1b6c
DC
1474int
1475avr_force_relocation (fixS *fix)
1476{
1477 if (linkrelax && fix->fx_addsy
1478 && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
1479 return 1;
1480
1481 return generic_force_reloc (fix);
1482}
1483
adde6300 1484/* GAS will call this for each fixup. It should store the correct
c6a7ab1f
NC
1485 value in the object file. */
1486
94f592af 1487void
dc191a8f 1488md_apply_fix (fixS *fixP, valueT * valP, segT seg)
adde6300
AM
1489{
1490 unsigned char *where;
1491 unsigned long insn;
a161fe53 1492 long value = *valP;
adde6300 1493
94f592af
NC
1494 if (fixP->fx_addsy == (symbolS *) NULL)
1495 fixP->fx_done = 1;
1496
87733541
AM
1497 else if (fixP->fx_pcrel)
1498 {
1499 segT s = S_GET_SEGMENT (fixP->fx_addsy);
1500
1501 if (s == seg || s == absolute_section)
1502 {
1503 value += S_GET_VALUE (fixP->fx_addsy);
1504 fixP->fx_done = 1;
1505 }
1506 }
e4ef1b6c
DC
1507 else if (linkrelax && fixP->fx_subsy)
1508 {
1509 /* For a subtraction relocation expression, generate one
1510 of the DIFF relocs, with the value being the difference.
1511 Note that a sym1 - sym2 expression is adjusted into a
1512 section_start_sym + sym4_offset_from_section_start - sym1
1513 expression. fixP->fx_addsy holds the section start symbol,
1514 fixP->fx_offset holds sym2's offset, and fixP->fx_subsy
1515 holds sym1. Calculate the current difference and write value,
75f58085
BS
1516 but leave fx_offset as is - during relaxation,
1517 fx_offset - value gives sym1's value. */
e4ef1b6c
DC
1518
1519 switch (fixP->fx_r_type)
1520 {
1521 case BFD_RELOC_8:
1522 fixP->fx_r_type = BFD_RELOC_AVR_DIFF8;
1523 break;
1524 case BFD_RELOC_16:
1525 fixP->fx_r_type = BFD_RELOC_AVR_DIFF16;
1526 break;
1527 case BFD_RELOC_32:
1528 fixP->fx_r_type = BFD_RELOC_AVR_DIFF32;
1529 break;
1530 default:
1531 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1532 break;
1533 }
1534
1535 value = S_GET_VALUE (fixP->fx_addsy) +
1536 fixP->fx_offset - S_GET_VALUE (fixP->fx_subsy);
491793b5 1537 *valP = value;
e4ef1b6c
DC
1538
1539 fixP->fx_subsy = NULL;
1540 }
a161fe53
AM
1541 /* We don't actually support subtracting a symbol. */
1542 if (fixP->fx_subsy != (symbolS *) NULL)
1543 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1dab94dd 1544
e4ef1b6c
DC
1545 /* For the DIFF relocs, write the value into the object file while still
1546 keeping fx_done FALSE, as both the difference (recorded in the object file)
75f58085 1547 and the sym offset (part of fixP) are needed at link relax time. */
e4ef1b6c 1548 where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
94f592af 1549 switch (fixP->fx_r_type)
adde6300
AM
1550 {
1551 default:
94f592af 1552 fixP->fx_no_overflow = 1;
adde6300
AM
1553 break;
1554 case BFD_RELOC_AVR_7_PCREL:
1555 case BFD_RELOC_AVR_13_PCREL:
1556 case BFD_RELOC_32:
1557 case BFD_RELOC_16:
e4ef1b6c
DC
1558 break;
1559 case BFD_RELOC_AVR_DIFF8:
1560 *where = value;
1561 break;
1562 case BFD_RELOC_AVR_DIFF16:
1563 bfd_putl16 ((bfd_vma) value, where);
1564 break;
1565 case BFD_RELOC_AVR_DIFF32:
1566 bfd_putl32 ((bfd_vma) value, where);
1567 break;
adde6300
AM
1568 case BFD_RELOC_AVR_CALL:
1569 break;
1570 }
1571
94f592af 1572 if (fixP->fx_done)
adde6300
AM
1573 {
1574 /* Fetch the instruction, insert the fully resolved operand
1575 value, and stuff the instruction back again. */
2132e3a3 1576 where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
adde6300
AM
1577 insn = bfd_getl16 (where);
1578
94f592af 1579 switch (fixP->fx_r_type)
adde6300
AM
1580 {
1581 case BFD_RELOC_AVR_7_PCREL:
1582 if (value & 1)
94f592af 1583 as_bad_where (fixP->fx_file, fixP->fx_line,
adde6300 1584 _("odd address operand: %ld"), value);
1dab94dd 1585
adde6300
AM
1586 /* Instruction addresses are always right-shifted by 1. */
1587 value >>= 1;
1588 --value; /* Correct PC. */
1dab94dd 1589
adde6300 1590 if (value < -64 || value > 63)
94f592af 1591 as_bad_where (fixP->fx_file, fixP->fx_line,
adde6300
AM
1592 _("operand out of range: %ld"), value);
1593 value = (value << 3) & 0x3f8;
1594 bfd_putl16 ((bfd_vma) (value | insn), where);
1595 break;
1596
1597 case BFD_RELOC_AVR_13_PCREL:
1598 if (value & 1)
94f592af 1599 as_bad_where (fixP->fx_file, fixP->fx_line,
adde6300 1600 _("odd address operand: %ld"), value);
1dab94dd 1601
adde6300
AM
1602 /* Instruction addresses are always right-shifted by 1. */
1603 value >>= 1;
1604 --value; /* Correct PC. */
adde6300
AM
1605
1606 if (value < -2048 || value > 2047)
1607 {
65aa24b6 1608 /* No wrap for devices with >8K of program memory. */
00d2865b 1609 if ((avr_mcu->isa & AVR_ISA_MEGA) || avr_opt.no_wrap)
94f592af 1610 as_bad_where (fixP->fx_file, fixP->fx_line,
adde6300
AM
1611 _("operand out of range: %ld"), value);
1612 }
1613
1614 value &= 0xfff;
1615 bfd_putl16 ((bfd_vma) (value | insn), where);
1616 break;
1617
1618 case BFD_RELOC_32:
0b649256 1619 bfd_putl32 ((bfd_vma) value, where);
adde6300
AM
1620 break;
1621
1622 case BFD_RELOC_16:
1623 bfd_putl16 ((bfd_vma) value, where);
1624 break;
1625
17e57237
NC
1626 case BFD_RELOC_8:
1627 if (value > 255 || value < -128)
1628 as_warn_where (fixP->fx_file, fixP->fx_line,
1629 _("operand out of range: %ld"), value);
1630 *where = value;
1631 break;
1632
adde6300 1633 case BFD_RELOC_AVR_16_PM:
c6a7ab1f 1634 bfd_putl16 ((bfd_vma) (value >> 1), where);
adde6300
AM
1635 break;
1636
750bce0e
NC
1637 case BFD_RELOC_AVR_LDI:
1638 if (value > 255)
1639 as_bad_where (fixP->fx_file, fixP->fx_line,
1640 _("operand out of range: %ld"), value);
1641 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
1642 break;
1643
f36e8886
BS
1644 case BFD_RELOC_AVR_LDS_STS_16:
1645 if ((value < 0x40) || (value > 0xBF))
1646 as_warn_where (fixP->fx_file, fixP->fx_line,
1647 _("operand out of range: 0x%lx"),
1648 (unsigned long)value);
1649 insn |= ((value & 0xF) | ((value & 0x30) << 5) | ((value & 0x40) << 2));
1650 bfd_putl16 ((bfd_vma) insn, where);
1651 break;
1652
750bce0e
NC
1653 case BFD_RELOC_AVR_6:
1654 if ((value > 63) || (value < 0))
1655 as_bad_where (fixP->fx_file, fixP->fx_line,
1656 _("operand out of range: %ld"), value);
f36e8886
BS
1657 bfd_putl16 ((bfd_vma) insn | ((value & 7) | ((value & (3 << 3)) << 7)
1658 | ((value & (1 << 5)) << 8)), where);
750bce0e
NC
1659 break;
1660
1661 case BFD_RELOC_AVR_6_ADIW:
1662 if ((value > 63) || (value < 0))
1663 as_bad_where (fixP->fx_file, fixP->fx_line,
1664 _("operand out of range: %ld"), value);
1665 bfd_putl16 ((bfd_vma) insn | (value & 0xf) | ((value & 0x30) << 2), where);
1666 break;
1667
adde6300
AM
1668 case BFD_RELOC_AVR_LO8_LDI:
1669 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
1670 break;
1671
adde6300
AM
1672 case BFD_RELOC_AVR_HI8_LDI:
1673 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where);
1674 break;
1675
df406460 1676 case BFD_RELOC_AVR_MS8_LDI:
adde6300
AM
1677 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where);
1678 break;
1679
1680 case BFD_RELOC_AVR_HH8_LDI:
1681 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
1682 break;
1683
1684 case BFD_RELOC_AVR_LO8_LDI_NEG:
1685 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where);
1686 break;
1687
adde6300
AM
1688 case BFD_RELOC_AVR_HI8_LDI_NEG:
1689 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where);
1690 break;
1691
df406460 1692 case BFD_RELOC_AVR_MS8_LDI_NEG:
adde6300
AM
1693 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where);
1694 break;
1695
1696 case BFD_RELOC_AVR_HH8_LDI_NEG:
1697 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
1698 break;
1699
1700 case BFD_RELOC_AVR_LO8_LDI_PM:
1701 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where);
1702 break;
1703
1704 case BFD_RELOC_AVR_HI8_LDI_PM:
1705 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where);
1706 break;
1707
1708 case BFD_RELOC_AVR_HH8_LDI_PM:
1709 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where);
1710 break;
1711
1712 case BFD_RELOC_AVR_LO8_LDI_PM_NEG:
1713 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where);
1714 break;
1715
1716 case BFD_RELOC_AVR_HI8_LDI_PM_NEG:
1717 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where);
1718 break;
1719
1720 case BFD_RELOC_AVR_HH8_LDI_PM_NEG:
1721 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where);
1722 break;
1723
1724 case BFD_RELOC_AVR_CALL:
1725 {
1726 unsigned long x;
1dab94dd 1727
adde6300
AM
1728 x = bfd_getl16 (where);
1729 if (value & 1)
94f592af 1730 as_bad_where (fixP->fx_file, fixP->fx_line,
adde6300
AM
1731 _("odd address operand: %ld"), value);
1732 value >>= 1;
1733 x |= ((value & 0x10000) | ((value << 3) & 0x1f00000)) >> 16;
1734 bfd_putl16 ((bfd_vma) x, where);
c6a7ab1f 1735 bfd_putl16 ((bfd_vma) (value & 0xffff), where + 2);
adde6300
AM
1736 }
1737 break;
1738
99700d6f
NC
1739 case BFD_RELOC_AVR_8_LO:
1740 *where = 0xff & value;
1741 break;
1742
1743 case BFD_RELOC_AVR_8_HI:
1744 *where = 0xff & (value >> 8);
1745 break;
1746
40551fb8 1747 case BFD_RELOC_AVR_8_HLO:
99700d6f
NC
1748 *where = 0xff & (value >> 16);
1749 break;
1750
1751 default:
c6a7ab1f 1752 as_fatal (_("line %d: unknown relocation type: 0x%x"),
94f592af 1753 fixP->fx_line, fixP->fx_r_type);
adde6300 1754 break;
75f58085
BS
1755
1756 case BFD_RELOC_AVR_PORT6:
1757 if (value > 63)
1758 as_bad_where (fixP->fx_file, fixP->fx_line,
1759 _("operand out of range: %ld"), value);
1760 bfd_putl16 ((bfd_vma) insn | ((value & 0x30) << 5) | (value & 0x0f), where);
1761 break;
1762
1763 case BFD_RELOC_AVR_PORT5:
1764 if (value > 31)
1765 as_bad_where (fixP->fx_file, fixP->fx_line,
1766 _("operand out of range: %ld"), value);
1767 bfd_putl16 ((bfd_vma) insn | ((value & 0x1f) << 3), where);
1768 break;
adde6300
AM
1769 }
1770 }
1771 else
1772 {
a61a9fbc 1773 switch ((int) fixP->fx_r_type)
adde6300
AM
1774 {
1775 case -BFD_RELOC_AVR_HI8_LDI_NEG:
1776 case -BFD_RELOC_AVR_HI8_LDI:
1777 case -BFD_RELOC_AVR_LO8_LDI_NEG:
1778 case -BFD_RELOC_AVR_LO8_LDI:
94f592af 1779 as_bad_where (fixP->fx_file, fixP->fx_line,
adde6300 1780 _("only constant expression allowed"));
94f592af 1781 fixP->fx_done = 1;
adde6300
AM
1782 break;
1783 default:
1784 break;
1785 }
adde6300 1786 }
adde6300
AM
1787}
1788
7be1c489
AM
1789/* GAS will call this to generate a reloc, passing the resulting reloc
1790 to `bfd_install_relocation'. This currently works poorly, as
1791 `bfd_install_relocation' often does the wrong thing, and instances of
1792 `tc_gen_reloc' have been written to work around the problems, which
1793 in turns makes it difficult to fix `bfd_install_relocation'. */
adde6300
AM
1794
1795/* If while processing a fixup, a reloc really needs to be created
1796 then it is done here. */
1797
1798arelent *
dc191a8f
NC
1799tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED,
1800 fixS *fixp)
adde6300
AM
1801{
1802 arelent *reloc;
328e7bfd 1803 bfd_reloc_code_real_type code = fixp->fx_r_type;
adde6300 1804
94d4433a 1805 if (fixp->fx_subsy != NULL)
df406460 1806 {
94d4433a 1807 as_bad_where (fixp->fx_file, fixp->fx_line, _("expression too complex"));
df406460
NC
1808 return NULL;
1809 }
1810
325801bd 1811 reloc = XNEW (arelent);
adde6300 1812
325801bd 1813 reloc->sym_ptr_ptr = XNEW (asymbol *);
adde6300
AM
1814 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1815
1816 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
328e7bfd
DC
1817
1818 if ((fixp->fx_r_type == BFD_RELOC_32) && (fixp->fx_pcrel))
1819 {
1820 if (seg->use_rela_p)
1821 fixp->fx_offset -= md_pcrel_from_section (fixp, seg);
1822 else
1823 fixp->fx_offset = reloc->address;
1824
1825 code = BFD_RELOC_32_PCREL;
1826 }
1827
1828 reloc->addend = fixp->fx_offset;
1829
1830 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
1831
adde6300
AM
1832 if (reloc->howto == (reloc_howto_type *) NULL)
1833 {
1834 as_bad_where (fixp->fx_file, fixp->fx_line,
c6a7ab1f
NC
1835 _("reloc %d not supported by object file format"),
1836 (int) fixp->fx_r_type);
adde6300
AM
1837 return NULL;
1838 }
1839
1840 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1841 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1842 reloc->address = fixp->fx_offset;
1843
adde6300
AM
1844
1845 return reloc;
1846}
1847
adde6300 1848void
dc191a8f 1849md_assemble (char *str)
adde6300 1850{
c6a7ab1f 1851 struct avr_opcodes_s *opcode;
adde6300
AM
1852 char op[11];
1853
c6a7ab1f 1854 str = skip_space (extract_word (str, op, sizeof (op)));
adde6300
AM
1855
1856 if (!op[0])
00d2865b 1857 as_bad (_("can't find opcode "));
adde6300
AM
1858
1859 opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
1860
f36e8886
BS
1861 if (opcode && !avr_opt.all_opcodes)
1862 {
75f58085 1863 /* Check if the instruction's ISA bit is ON in the ISA bits of the part
f36e8886 1864 specified by the user. If not look for other instructions
75f58085 1865 specifications with same mnemonic who's ISA bits matches.
f36e8886
BS
1866
1867 This requires include/opcode/avr.h to have the instructions with
33eaf5de 1868 same mnemonic to be specified in sequence. */
f36e8886
BS
1869
1870 while ((opcode->isa & avr_mcu->isa) != opcode->isa)
1871 {
1872 opcode++;
75f58085 1873
f36e8886
BS
1874 if (opcode->name && strcmp(op, opcode->name))
1875 {
75f58085 1876 as_bad (_("illegal opcode %s for mcu %s"),
f36e8886
BS
1877 opcode->name, avr_mcu->name);
1878 return;
1879 }
1880 }
75f58085 1881 }
f36e8886 1882
adde6300
AM
1883 if (opcode == NULL)
1884 {
00d2865b 1885 as_bad (_("unknown opcode `%s'"), op);
adde6300
AM
1886 return;
1887 }
1888
32f76c67
GJL
1889 if (opcode == avr_gccisr_opcode
1890 && !avr_opt.have_gccisr)
1891 {
1892 as_bad (_("pseudo instruction `%s' not supported"), op);
1893 return;
1894 }
1895
b170af93 1896 /* Special case for opcodes with optional operands (lpm, elpm) -
1188e082 1897 version with operands exists in avr_opcodes[] in the next entry. */
c6a7ab1f 1898
1188e082
DC
1899 if (*str && *opcode->constraints == '?')
1900 ++opcode;
b170af93 1901
d4f4f3fb
AM
1902 dwarf2_emit_insn (0);
1903
adde6300
AM
1904 /* We used to set input_line_pointer to the result of get_operands,
1905 but that is wrong. Our caller assumes we don't change it. */
1906 {
1907 char *t = input_line_pointer;
dc191a8f 1908
32f76c67
GJL
1909 if (opcode == avr_gccisr_opcode)
1910 avr_gccisr_operands (opcode, &str);
1911 else
1912 avr_operands (opcode, &str);
b170af93 1913 if (*skip_space (str))
00d2865b 1914 as_bad (_("garbage at end of line"));
adde6300
AM
1915 input_line_pointer = t;
1916 }
1917}
1918
62ebcb5c 1919const exp_mod_data_t exp_mod_data[] =
99700d6f
NC
1920{
1921 /* Default, must be first. */
1922 { "", 0, BFD_RELOC_16, "" },
1923 /* Divides by 2 to get word address. Generate Stub. */
1924 { "gs", 2, BFD_RELOC_AVR_16_PM, "`gs' " },
1925 { "pm", 2, BFD_RELOC_AVR_16_PM, "`pm' " },
1926 /* The following are used together with avr-gcc's __memx address space
1927 in order to initialize a 24-bit pointer variable with a 24-bit address.
40551fb8
NC
1928 For address in flash, hlo8 will contain the flash segment if the
1929 symbol is located in flash. If the symbol is located in RAM; hlo8
99700d6f
NC
1930 will contain 0x80 which matches avr-gcc's notion of how 24-bit RAM/flash
1931 addresses linearize address space. */
1932 { "lo8", 1, BFD_RELOC_AVR_8_LO, "`lo8' " },
1933 { "hi8", 1, BFD_RELOC_AVR_8_HI, "`hi8' " },
40551fb8
NC
1934 { "hlo8", 1, BFD_RELOC_AVR_8_HLO, "`hlo8' " },
1935 { "hh8", 1, BFD_RELOC_AVR_8_HLO, "`hh8' " },
99700d6f
NC
1936};
1937
99700d6f
NC
1938/* Parse special CONS expression: pm (expression) or alternatively
1939 gs (expression). These are used for addressing program memory. Moreover,
40551fb8 1940 define lo8 (expression), hi8 (expression) and hlo8 (expression). */
c6a7ab1f 1941
62ebcb5c 1942const exp_mod_data_t *
dc191a8f 1943avr_parse_cons_expression (expressionS *exp, int nbytes)
adde6300 1944{
c6a7ab1f 1945 char *tmp;
814f1489 1946 unsigned int i;
adde6300 1947
adde6300
AM
1948 tmp = input_line_pointer = skip_space (input_line_pointer);
1949
99700d6f
NC
1950 /* The first entry of exp_mod_data[] contains an entry if no
1951 expression modifier is present. Skip it. */
1952
814f1489 1953 for (i = 0; i < ARRAY_SIZE (exp_mod_data); i++)
adde6300 1954 {
814f1489 1955 const exp_mod_data_t *pexp = &exp_mod_data[i];
99700d6f 1956 int len = strlen (pexp->name);
1dab94dd 1957
99700d6f
NC
1958 if (nbytes == pexp->nbytes
1959 && strncasecmp (input_line_pointer, pexp->name, len) == 0)
adde6300
AM
1960 {
1961 input_line_pointer = skip_space (input_line_pointer + len);
1dab94dd 1962
adde6300
AM
1963 if (*input_line_pointer == '(')
1964 {
1965 input_line_pointer = skip_space (input_line_pointer + 1);
adde6300 1966 expression (exp);
1dab94dd 1967
adde6300 1968 if (*input_line_pointer == ')')
62ebcb5c
AM
1969 {
1970 ++input_line_pointer;
1971 return pexp;
1972 }
adde6300
AM
1973 else
1974 {
00d2865b 1975 as_bad (_("`)' required"));
62ebcb5c 1976 return &exp_mod_data[0];
adde6300 1977 }
adde6300 1978 }
1dab94dd 1979
adde6300 1980 input_line_pointer = tmp;
99700d6f
NC
1981
1982 break;
adde6300
AM
1983 }
1984 }
1dab94dd 1985
adde6300 1986 expression (exp);
62ebcb5c 1987 return &exp_mod_data[0];
adde6300
AM
1988}
1989
1990void
dc191a8f
NC
1991avr_cons_fix_new (fragS *frag,
1992 int where,
1993 int nbytes,
62ebcb5c
AM
1994 expressionS *exp,
1995 const exp_mod_data_t *pexp_mod_data)
adde6300 1996{
99700d6f
NC
1997 int bad = 0;
1998
1999 switch (pexp_mod_data->reloc)
adde6300 2000 {
99700d6f 2001 default:
17e57237
NC
2002 if (nbytes == 1)
2003 fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_8);
2004 else if (nbytes == 2)
b34976b6 2005 fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_16);
adde6300 2006 else if (nbytes == 4)
b34976b6 2007 fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_32);
adde6300 2008 else
99700d6f
NC
2009 bad = 1;
2010 break;
2011
2012 case BFD_RELOC_AVR_16_PM:
2013 case BFD_RELOC_AVR_8_LO:
2014 case BFD_RELOC_AVR_8_HI:
40551fb8 2015 case BFD_RELOC_AVR_8_HLO:
99700d6f
NC
2016 if (nbytes == pexp_mod_data->nbytes)
2017 fix_new_exp (frag, where, nbytes, exp, FALSE, pexp_mod_data->reloc);
adde6300 2018 else
99700d6f
NC
2019 bad = 1;
2020 break;
adde6300 2021 }
99700d6f
NC
2022
2023 if (bad)
33eaf5de 2024 as_bad (_("illegal %s relocation size: %d"), pexp_mod_data->error, nbytes);
adde6300 2025}
af3ecb4a 2026
71863e73
NC
2027static bfd_boolean
2028mcu_has_3_byte_pc (void)
2029{
75f58085 2030 int mach = avr_mcu->mach;
71863e73 2031
75f58085
BS
2032 return mach == bfd_mach_avr6
2033 || mach == bfd_mach_avrxmega6
71863e73
NC
2034 || mach == bfd_mach_avrxmega7;
2035}
2036
af3ecb4a
RH
2037void
2038tc_cfi_frame_initial_instructions (void)
2039{
2040 /* AVR6 pushes 3 bytes for calls. */
71863e73 2041 int return_size = (mcu_has_3_byte_pc () ? 3 : 2);
af3ecb4a
RH
2042
2043 /* The CFA is the caller's stack location before the call insn. */
2044 /* Note that the stack pointer is dwarf register number 32. */
2045 cfi_add_CFA_def_cfa (32, return_size);
2046
2047 /* Note that AVR consistently uses post-decrement, which means that things
33eaf5de 2048 do not line up the same way as for targets that use pre-decrement. */
af3ecb4a
RH
2049 cfi_add_CFA_offset (DWARF2_DEFAULT_RETURN_COLUMN, 1-return_size);
2050}
e4ef1b6c
DC
2051
2052bfd_boolean
2053avr_allow_local_subtract (expressionS * left,
2054 expressionS * right,
2055 segT section)
2056{
75f58085 2057 /* If we are not in relaxation mode, subtraction is OK. */
e4ef1b6c
DC
2058 if (!linkrelax)
2059 return TRUE;
2060
2061 /* If the symbols are not in a code section then they are OK. */
2062 if ((section->flags & SEC_CODE) == 0)
2063 return TRUE;
2064
2065 if (left->X_add_symbol == right->X_add_symbol)
2066 return TRUE;
2067
2068 /* We have to assume that there may be instructions between the
2069 two symbols and that relaxation may increase the distance between
2070 them. */
2071 return FALSE;
2072}
eac7440d
AB
2073
2074void
2075avr_elf_final_processing (void)
2076{
2077 if (linkrelax)
2078 elf_elfheader (stdoutput)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
2079}
fdd410ac
AB
2080
2081/* Write out the header of a .avr.prop section into the area pointed to by
2082 DATA. The RECORD_COUNT will be placed in the header as the number of
2083 records that are to follow.
2084 The area DATA must be big enough the receive the header, which is
2085 AVR_PROPERTY_SECTION_HEADER_SIZE bytes long. */
2086
2087static char *
2088avr_output_property_section_header (char *data,
2089 unsigned int record_count)
2090{
2091 char *orig_data = data;
2092
2093 md_number_to_chars (data, AVR_PROPERTY_RECORDS_VERSION, 1);
2094 data++;
2095 /* There's space for a single byte flags field, but right now there's
2096 nothing to go in here, so just set the value to zero. */
2097 md_number_to_chars (data, 0, 1);
2098 data++;
2099 md_number_to_chars (data, record_count, 2);
2100 data+=2;
2101
2102 gas_assert (data - orig_data == AVR_PROPERTY_SECTION_HEADER_SIZE);
2103
2104 return data;
2105}
2106
2107/* Return the number of bytes required to store RECORD into the .avr.prop
2108 section. The size returned is the compressed size that corresponds to
2109 how the record will be written out in AVR_OUTPUT_PROPERTY_RECORD. */
2110
2111static int
2112avr_record_size (const struct avr_property_record *record)
2113{
2114 /* The first 5 bytes are a 4-byte address, followed by a 1-byte type
2115 identifier. */
2116 int size = 5;
2117
2118 switch (record->type)
2119 {
2120 case RECORD_ORG:
2121 size += 0; /* No extra information. */
2122 break;
2123
2124 case RECORD_ORG_AND_FILL:
2125 size += 4; /* A 4-byte fill value. */
2126 break;
2127
2128 case RECORD_ALIGN:
2129 size += 4; /* A 4-byte alignment value. */
2130 break;
2131
2132 case RECORD_ALIGN_AND_FILL:
2133 size += 8; /* A 4-byte alignment, and 4-byte fill value. */
2134 break;
2135
2136 default:
2137 as_fatal (_("unknown record type %d (in %s)"),
2138 record->type, __PRETTY_FUNCTION__);
2139 }
2140
2141 return size;
2142}
2143
2144/* Write out RECORD. FRAG_BASE points to the start of the data area setup
2145 to hold all of the .avr.prop content, FRAG_PTR points to the next
2146 writable location. The data area must be big enough to hold all of the
2147 records. The size of the data written out for this RECORD must match
2148 the size from AVR_RECORD_SIZE. */
2149
2150static char *
2151avr_output_property_record (char * const frag_base, char *frag_ptr,
2152 const struct avr_property_record *record)
2153{
2154 fixS *fix;
2155 int where;
2156 char *init_frag_ptr = frag_ptr;
2157
2158 where = frag_ptr - frag_base;
2159 fix = fix_new (frag_now, where, 4,
2160 section_symbol (record->section),
2161 record->offset, FALSE, BFD_RELOC_32);
2162 fix->fx_file = "<internal>";
2163 fix->fx_line = 0;
2164 frag_ptr += 4;
2165
2166 md_number_to_chars (frag_ptr, (bfd_byte) record->type, 1);
2167 frag_ptr += 1;
2168
2169 /* Write out the rest of the data. */
2170 switch (record->type)
2171 {
2172 case RECORD_ORG:
2173 break;
2174
2175 case RECORD_ORG_AND_FILL:
2176 md_number_to_chars (frag_ptr, record->data.org.fill, 4);
2177 frag_ptr += 4;
2178 break;
2179
2180 case RECORD_ALIGN:
2181 md_number_to_chars (frag_ptr, record->data.align.bytes, 4);
2182 frag_ptr += 4;
2183 break;
2184
2185 case RECORD_ALIGN_AND_FILL:
2186 md_number_to_chars (frag_ptr, record->data.align.bytes, 4);
431ff075 2187 md_number_to_chars (frag_ptr + 4, record->data.align.fill, 4);
fdd410ac
AB
2188 frag_ptr += 8;
2189 break;
2190
2191 default:
2192 as_fatal (_("unknown record type %d (in %s)"),
2193 record->type, __PRETTY_FUNCTION__);
2194 }
2195
2196 gas_assert (frag_ptr - init_frag_ptr == avr_record_size (record));
2197
2198 return frag_ptr;
2199}
2200
2201/* Create the section to hold the AVR property information. Return the
2202 section. */
2203
2204static asection *
2205avr_create_property_section (void)
2206{
2207 asection *sec;
2208 flagword flags = (SEC_RELOC | SEC_HAS_CONTENTS | SEC_READONLY);
2209 const char *section_name = AVR_PROPERTY_RECORD_SECTION_NAME;
2210
2211 sec = bfd_make_section (stdoutput, section_name);
2212 if (sec == NULL)
2213 as_fatal (_("Failed to create property section `%s'\n"), section_name);
2214 bfd_set_section_flags (stdoutput, sec, flags);
2215 sec->output_section = sec;
2216 return sec;
2217}
2218
2219/* This hook is called when alignment is performed, and allows us to
2220 capture the details of both .org and .align directives. */
2221
2222void
2223avr_handle_align (fragS *fragP)
2224{
2225 if (linkrelax)
2226 {
2227 /* Ignore alignment requests at FR_ADDRESS 0, these are at the very
2228 start of a section, and will be handled by the standard section
2229 alignment mechanism. */
2230 if ((fragP->fr_type == rs_align
2231 || fragP->fr_type == rs_align_code)
fdd410ac
AB
2232 && fragP->fr_offset > 0)
2233 {
431ff075
AB
2234 char *p = fragP->fr_literal + fragP->fr_fix;
2235
fdd410ac
AB
2236 fragP->tc_frag_data.is_align = TRUE;
2237 fragP->tc_frag_data.alignment = fragP->fr_offset;
431ff075
AB
2238 fragP->tc_frag_data.fill = *p;
2239 fragP->tc_frag_data.has_fill = (fragP->tc_frag_data.fill != 0);
fdd410ac
AB
2240 }
2241
2242 if (fragP->fr_type == rs_org && fragP->fr_offset > 0)
2243 {
2244 char *p = fragP->fr_literal + fragP->fr_fix;
2245
2246 fragP->tc_frag_data.is_org = TRUE;
2247 fragP->tc_frag_data.fill = *p;
2248 fragP->tc_frag_data.has_fill = (fragP->tc_frag_data.fill != 0);
2249 }
2250 }
2251}
2252
2253/* Return TRUE if this section is not one for which we need to record
2254 information in the avr property section. */
2255
2256static bfd_boolean
2257exclude_section_from_property_tables (segT sec)
2258{
2259 /* Only generate property information for sections on which linker
2260 relaxation could be performed. */
2261 return !relaxable_section (sec);
2262}
2263
2264/* Create a property record for fragment FRAGP from section SEC and place
2265 it into an AVR_PROPERTY_RECORD_LINK structure, which can then formed
2266 into a linked list by the caller. */
2267
2268static struct avr_property_record_link *
2269create_record_for_frag (segT sec, fragS *fragP)
2270{
ef7a9369 2271 struct avr_property_record_link *prop_rec_link;
fdd410ac 2272
325801bd 2273 prop_rec_link = XCNEW (struct avr_property_record_link);
431ff075 2274 gas_assert (fragP->fr_next != NULL);
fdd410ac
AB
2275
2276 if (fragP->tc_frag_data.is_org)
2277 {
ef7a9369
SKS
2278 prop_rec_link->record.offset = fragP->fr_next->fr_address;
2279 prop_rec_link->record.section = sec;
fdd410ac
AB
2280
2281 if (fragP->tc_frag_data.has_fill)
2282 {
ef7a9369
SKS
2283 prop_rec_link->record.data.org.fill = fragP->tc_frag_data.fill;
2284 prop_rec_link->record.type = RECORD_ORG_AND_FILL;
fdd410ac
AB
2285 }
2286 else
ef7a9369 2287 prop_rec_link->record.type = RECORD_ORG;
fdd410ac
AB
2288 }
2289 else
2290 {
431ff075 2291 prop_rec_link->record.offset = fragP->fr_next->fr_address;
ef7a9369 2292 prop_rec_link->record.section = sec;
fdd410ac
AB
2293
2294 gas_assert (fragP->tc_frag_data.is_align);
2295 if (fragP->tc_frag_data.has_fill)
2296 {
ef7a9369
SKS
2297 prop_rec_link->record.data.align.fill = fragP->tc_frag_data.fill;
2298 prop_rec_link->record.type = RECORD_ALIGN_AND_FILL;
fdd410ac
AB
2299 }
2300 else
ef7a9369
SKS
2301 prop_rec_link->record.type = RECORD_ALIGN;
2302 prop_rec_link->record.data.align.bytes = fragP->tc_frag_data.alignment;
fdd410ac
AB
2303 }
2304
ef7a9369 2305 return prop_rec_link;
fdd410ac
AB
2306}
2307
2308/* Build a list of AVR_PROPERTY_RECORD_LINK structures for section SEC, and
2309 merged them onto the list pointed to by NEXT_PTR. Return a pointer to
2310 the last list item created. */
2311
2312static struct avr_property_record_link **
2313append_records_for_section (segT sec,
2314 struct avr_property_record_link **next_ptr)
2315{
2316 segment_info_type *seginfo = seg_info (sec);
2317 fragS *fragP;
2318
2319 if (seginfo && seginfo->frchainP)
2320 {
2321 for (fragP = seginfo->frchainP->frch_root;
2322 fragP;
2323 fragP = fragP->fr_next)
2324 {
2325 if (fragP->tc_frag_data.is_align
2326 || fragP->tc_frag_data.is_org)
2327 {
2328 /* Create a single new entry. */
2329 struct avr_property_record_link *new_link
2330 = create_record_for_frag (sec, fragP);
2331
2332 *next_ptr = new_link;
2333 next_ptr = &new_link->next;
2334 }
2335 }
2336 }
2337
2338 return next_ptr;
2339}
2340
2341/* Create the AVR property section and fill it with records of .org and
2342 .align directives that were used. The section is only created if it
2343 will actually have any content. */
2344
2345static void
2346avr_create_and_fill_property_section (void)
2347{
2348 segT *seclist;
2349 asection *prop_sec;
2350 struct avr_property_record_link *r_list, **next_ptr;
2351 char *frag_ptr, *frag_base;
2352 bfd_size_type sec_size;
2353 struct avr_property_record_link *rec;
2354 unsigned int record_count;
2355
2356 /* First walk over all sections. For sections on which linker
2357 relaxation could be applied, extend the record list. The record list
2358 holds information that the linker will need to know. */
2359
2360 prop_sec = NULL;
2361 r_list = NULL;
2362 next_ptr = &r_list;
2363 for (seclist = &stdoutput->sections;
2364 seclist && *seclist;
2365 seclist = &(*seclist)->next)
2366 {
2367 segT sec = *seclist;
2368
2369 if (exclude_section_from_property_tables (sec))
2370 continue;
2371
2372 next_ptr = append_records_for_section (sec, next_ptr);
2373 }
2374
2375 /* Create property section and ensure the size is correct. We've already
2376 passed the point where gas could size this for us. */
2377 sec_size = AVR_PROPERTY_SECTION_HEADER_SIZE;
2378 record_count = 0;
2379 for (rec = r_list; rec != NULL; rec = rec->next)
2380 {
2381 record_count++;
2382 sec_size += avr_record_size (&rec->record);
2383 }
2384
2385 if (record_count == 0)
2386 return;
2387
2388 prop_sec = avr_create_property_section ();
2389 bfd_set_section_size (stdoutput, prop_sec, sec_size);
2390
2391 subseg_set (prop_sec, 0);
2392 frag_base = frag_more (sec_size);
2393
2394 frag_ptr =
2395 avr_output_property_section_header (frag_base, record_count);
2396
2397 for (rec = r_list; rec != NULL; rec = rec->next)
2398 frag_ptr = avr_output_property_record (frag_base, frag_ptr, &rec->record);
2399
2400 frag_wane (frag_now);
2401 frag_new (0);
2402 frag_wane (frag_now);
2403}
2404
2405/* We're using this hook to build up the AVR property section. It's called
2406 late in the assembly process which suits our needs. */
2407void
2408avr_post_relax_hook (void)
2409{
2410 avr_create_and_fill_property_section ();
2411}
32f76c67
GJL
2412
2413
2414/* Accumulate information about instruction sequence to `avr_isr':
2415 wheter TMP_REG, ZERO_REG and SREG might be touched. Used during parse.
2416 REG1 is either -1 or a register number used by the instruction as input
2417 or output operand. Similar for REG2. */
2418
2419static void
2420avr_update_gccisr (struct avr_opcodes_s *opcode, int reg1, int reg2)
2421{
2422 const int tiny_p = avr_mcu->mach == bfd_mach_avrtiny;
2423 const int reg_tmp = tiny_p ? 16 : 0;
2424 const int reg_zero = 1 + reg_tmp;
2425
2426 if (ISR_CHUNK_Done == avr_isr.prev_chunk
2427 || (avr_isr.need_sreg
2428 && avr_isr.need_reg_tmp
2429 && avr_isr.need_reg_zero))
2430 {
2431 /* Nothing (more) to do */
2432 return;
2433 }
2434
2435 /* SREG: Look up instructions that don't clobber SREG. */
2436
2437 if (!avr_isr.need_sreg
2438 && !hash_find (avr_no_sreg_hash, opcode->name))
2439 {
2440 avr_isr.need_sreg = 1;
2441 }
2442
2443 /* Handle explicit register operands. Record *any* use as clobber.
2444 This is because TMP_REG and ZERO_REG are not global and using
2445 them makes no sense without a previous set. */
2446
2447 avr_isr.need_reg_tmp |= reg1 == reg_tmp || reg2 == reg_tmp;
2448 avr_isr.need_reg_zero |= reg1 == reg_zero || reg2 == reg_zero;
2449
2450 /* Handle implicit register operands and some opaque stuff. */
2451
2452 if (strstr (opcode->name, "lpm")
2453 && '?' == *opcode->constraints)
2454 {
2455 avr_isr.need_reg_tmp = 1;
2456 }
2457
2458 if (strstr (opcode->name, "call")
2459 || strstr (opcode->name, "mul")
2460 || 0 == strcmp (opcode->name, "des")
2461 || (0 == strcmp (opcode->name, "movw")
2462 && (reg1 == reg_tmp || reg2 == reg_tmp)))
2463 {
2464 avr_isr.need_reg_tmp = 1;
2465 avr_isr.need_reg_zero = 1;
2466 }
2467}
2468
2469
2470/* Emit some 1-word instruction to **PWHERE and advance *PWHERE by the number
2471 of octets written. INSN specifies the desired instruction and REG is the
2472 register used by it. This function is only used with restricted subset of
2473 instructions as might be emit by `__gcc_isr'. IN / OUT will use SREG
2474 and LDI loads 0. */
2475
2476static void
2477avr_emit_insn (const char *insn, int reg, char **pwhere)
2478{
2479 const int sreg = 0x3f;
2480 unsigned bin = 0;
2481 const struct avr_opcodes_s *op
2482 = (struct avr_opcodes_s*) hash_find (avr_hash, insn);
2483
2484 /* We only have to deal with: IN, OUT, PUSH, POP, CLR, LDI 0. All of
2485 these deal with at least one Reg and are 1-word instructions. */
2486
2487 gas_assert (op && 1 == op->insn_size);
2488 gas_assert (reg >= 0 && reg <= 31);
2489
2490 if (strchr (op->constraints, 'r'))
2491 {
2492 bin = op->bin_opcode | (reg << 4);
2493 }
2494 else if (strchr (op->constraints, 'd'))
2495 {
2496 gas_assert (reg >= 16);
2497 bin = op->bin_opcode | ((reg & 0xf) << 4);
2498 }
2499 else
2500 abort();
2501
2502 if (strchr (op->constraints, 'P'))
2503 {
2504 bin |= ((sreg & 0x30) << 5) | (sreg & 0x0f);
2505 }
2506 else if (0 == strcmp ("r=r", op->constraints))
2507 {
2508 bin |= ((reg & 0x10) << 5) | (reg & 0x0f);
2509 }
2510 else
2511 gas_assert (0 == strcmp ("r", op->constraints)
2512 || 0 == strcmp ("ldi", op->name));
2513
2514 bfd_putl16 ((bfd_vma) bin, *pwhere);
2515 (*pwhere) += 2 * op->insn_size;
2516}
2517
2518
2519/* Turn rs_machine_dependent frag *FR into an ordinary rs_fill code frag,
2520 using information gathered in `avr_isr'. REG is the register number as
2521 supplied by Done chunk "__gcc_isr 0,REG". */
2522
2523static void
2524avr_patch_gccisr_frag (fragS *fr, int reg)
2525{
2526 int treg;
2527 int n_pushed = 0;
2528 char *where = fr->fr_literal;
2529 const int tiny_p = avr_mcu->mach == bfd_mach_avrtiny;
2530 const int reg_tmp = tiny_p ? 16 : 0;
2531 const int reg_zero = 1 + reg_tmp;
2532
2533 /* Clearing ZERO_REG on non-Tiny needs CLR which clobbers SREG. */
2534
2535 avr_isr.need_sreg |= !tiny_p && avr_isr.need_reg_zero;
2536
2537 /* A working register to PUSH / POP the SREG. We might use the register
2538 as supplied by ISR_CHUNK_Done for that purpose as GCC wants to push
2539 it anyways. If GCC passes ZERO_REG or TMP_REG, it has no clue (and
2540 no additional regs to safe) and we use that reg. */
2541
2542 treg
2543 = avr_isr.need_reg_tmp ? reg_tmp
2544 : avr_isr.need_reg_zero ? reg_zero
2545 : avr_isr.need_sreg ? reg
2546 : reg > reg_zero ? reg
2547 : -1;
2548
2549 if (treg >= 0)
2550 {
2551 /* Non-empty prologue / epilogue */
2552
2553 if (ISR_CHUNK_Prologue == fr->fr_subtype)
2554 {
2555 avr_emit_insn ("push", treg, &where);
2556 n_pushed++;
2557
2558 if (avr_isr.need_sreg)
2559 {
2560 avr_emit_insn ("in", treg, &where);
2561 avr_emit_insn ("push", treg, &where);
2562 n_pushed++;
2563 }
2564
2565 if (avr_isr.need_reg_zero)
2566 {
2567 if (reg_zero != treg)
2568 {
2569 avr_emit_insn ("push", reg_zero, &where);
2570 n_pushed++;
2571 }
2572 avr_emit_insn (tiny_p ? "ldi" : "clr", reg_zero, &where);
2573 }
2574
2575 if (reg > reg_zero && reg != treg)
2576 {
2577 avr_emit_insn ("push", reg, &where);
2578 n_pushed++;
2579 }
2580 }
2581 else if (ISR_CHUNK_Epilogue == fr->fr_subtype)
2582 {
2583 /* Same logic as in Prologue but in reverse order and with counter
2584 parts of either instruction: POP instead of PUSH and OUT instead
2585 of IN. Clearing ZERO_REG has no couter part. */
2586
2587 if (reg > reg_zero && reg != treg)
2588 avr_emit_insn ("pop", reg, &where);
2589
2590 if (avr_isr.need_reg_zero
2591 && reg_zero != treg)
2592 avr_emit_insn ("pop", reg_zero, &where);
2593
2594 if (avr_isr.need_sreg)
2595 {
2596 avr_emit_insn ("pop", treg, &where);
2597 avr_emit_insn ("out", treg, &where);
2598 }
2599
2600 avr_emit_insn ("pop", treg, &where);
2601 }
2602 else
2603 abort();
2604 } /* treg >= 0 */
2605
2606 if (ISR_CHUNK_Prologue == fr->fr_subtype
2607 && avr_isr.sym_n_pushed)
2608 {
2609 symbolS *sy = avr_isr.sym_n_pushed;
2610 /* Turn magic `__gcc_isr.n_pushed' into its now known value. */
2611
2612 sy->sy_value.X_op = O_constant;
2613 sy->sy_value.X_add_number = n_pushed;
2614 S_SET_SEGMENT (sy, expr_section);
2615 avr_isr.sym_n_pushed = NULL;
2616 }
2617
2618 /* Turn frag into ordinary code frag of now known size. */
2619
2620 fr->fr_var = 0;
2621 fr->fr_fix = (offsetT) (where - fr->fr_literal);
2622 gas_assert (fr->fr_fix <= fr->fr_offset);
2623 fr->fr_offset = 0;
2624 fr->fr_type = rs_fill;
2625 fr->fr_subtype = 0;
2626}
2627
2628
2629/* Implements `__gcc_isr' pseudo-instruction. For Prologue and Epilogue
2630 chunks, emit a new rs_machine_dependent frag. For Done chunks, traverse
2631 the current segment and patch all rs_machine_dependent frags to become
2632 appropriate rs_fill code frags. If chunks are seen in an odd ordering,
2633 throw an error instead. */
2634
2635static void
2636avr_gccisr_operands (struct avr_opcodes_s *opcode, char **line)
2637{
2638 int bad = 0;
2639 int chunk, reg = 0;
2640 char *str = *line;
2641
2642 gas_assert (avr_opt.have_gccisr);
2643
2644 /* We only use operands "N" and "r" which don't pop new fix-ups. */
2645
2646 /* 1st operand: Which chunk of __gcc_isr: 0...2. */
2647
2648 chunk = avr_operand (opcode, -1, "N", &str, NULL);
2649 if (chunk < 0 || chunk > 2)
2650 as_bad (_("%s requires value 0-2 as operand 1"), opcode->name);
2651
2652 if (ISR_CHUNK_Done == chunk)
2653 {
2654 /* 2nd operand: A register to push / pop. */
2655
2656 str = skip_space (str);
2657 if (*str == '\0' || *str++ != ',')
2658 as_bad (_("`,' required"));
2659 else
2660 avr_operand (opcode, -1, "r", &str, &reg);
2661 }
2662
2663 *line = str;
2664
2665 /* Chunks must follow in a specific order:
2666 - Prologue: Exactly one
2667 - Epilogue: Any number
2668 - Done: Exactly one. */
2669 bad |= ISR_CHUNK_Prologue == chunk && avr_isr.prev_chunk != ISR_CHUNK_Done;
2670 bad |= ISR_CHUNK_Epilogue == chunk && avr_isr.prev_chunk == ISR_CHUNK_Done;
2671 bad |= ISR_CHUNK_Done == chunk && avr_isr.prev_chunk == ISR_CHUNK_Done;
2672 if (bad)
2673 {
2674 if (avr_isr.file)
2675 as_bad (_("`%s %d' after `%s %d' from %s:%u"), opcode->name, chunk,
2676 opcode->name, avr_isr.prev_chunk, avr_isr.file, avr_isr.line);
2677 else
2678 as_bad (_("`%s %d' but no chunk open yet"), opcode->name, chunk);
2679 }
2680
2681 if (!had_errors())
2682 {
2683 /* The longest sequence (prologue) might have up to 6 insns (words):
2684
2685 push R0
2686 in R0, SREG
2687 push R0
2688 push R1
2689 clr R1
2690 push Rx
2691 */
2692 unsigned int size = 2 * 6;
2693 fragS *fr;
2694
2695 switch (chunk)
2696 {
2697 case ISR_CHUNK_Prologue:
2698 avr_isr.need_reg_tmp = 0;
2699 avr_isr.need_reg_zero = 0;
2700 avr_isr.need_sreg = 0;
2701 avr_isr.sym_n_pushed = NULL;
2702 /* FALLTHRU */
2703
2704 case ISR_CHUNK_Epilogue:
2705 /* Emit a new rs_machine_dependent fragment into the fragment chain.
2706 It will be patched and cleaned up once we see the matching
2707 ISR_CHUNK_Done. */
2708 frag_wane (frag_now);
2709 frag_new (0);
2710 frag_more (size);
2711
2712 frag_now->fr_var = 1;
2713 frag_now->fr_offset = size;
2714 frag_now->fr_fix = 0;
2715 frag_now->fr_type = rs_machine_dependent;
2716 frag_now->fr_subtype = chunk;
2717 frag_new (size);
2718 break;
2719
2720 case ISR_CHUNK_Done:
2721 /* Traverse all frags of the current subseg and turn ones of type
2722 rs_machine_dependent into ordinary code as expected by GCC. */
2723
2724 for (fr = frchain_now->frch_root; fr; fr = fr->fr_next)
2725 if (fr->fr_type == rs_machine_dependent)
2726 avr_patch_gccisr_frag (fr, reg);
2727 break;
2728
2729 default:
2730 abort();
2731 break;
2732 }
2733 } /* !had_errors */
2734
2735 avr_isr.prev_chunk = chunk;
2736 avr_isr.file = as_where (&avr_isr.line);
2737}
2738
2739
2740/* Callback used by the function below. Diagnose any dangling stuff from
2741 `__gcc_isr', i.e. frags of type rs_machine_dependent. Such frags should
2742 have been resolved during parse by ISR_CHUNK_Done. If such a frag is
2743 seen, report an error and turn it into something harmless. */
2744
2745static void
2746avr_check_gccisr_done (bfd *abfd ATTRIBUTE_UNUSED,
2747 segT section,
2748 void *xxx ATTRIBUTE_UNUSED)
2749{
2750 segment_info_type *info = seg_info (section);
2751
2752 if (SEG_NORMAL (section)
2753 /* BFD may have introduced its own sections without using
2754 subseg_new, so it is possible that seg_info is NULL. */
2755 && info)
2756 {
2757 fragS *fr;
2758 frchainS *frch;
2759
2760 for (frch = info->frchainP; frch; frch = frch->frch_next)
2761 for (fr = frch->frch_root; fr; fr = fr->fr_next)
2762 if (fr->fr_type == rs_machine_dependent)
2763 {
2764 if (avr_isr.file)
2765 as_bad_where (avr_isr.file, avr_isr.line,
2766 _("dangling `__gcc_isr %d'"), avr_isr.prev_chunk);
2767 else if (!had_errors())
2768 as_bad (_("dangling `__gcc_isr'"));
2769
2770 avr_isr.file = NULL;
2771
2772 /* Avoid Internal errors due to rs_machine_dependent in the
2773 remainder: Turn frag into something harmless. */
2774 fr->fr_var = 0;
2775 fr->fr_fix = 0;
2776 fr->fr_offset = 0;
2777 fr->fr_type = rs_fill;
2778 fr->fr_subtype = 0;
2779 }
2780 }
2781}
2782
2783
2784/* Implement `md_pre_output_hook' */
2785/* Run over all relevant sections and diagnose any dangling `__gcc_isr'.
2786 This runs after parsing all inputs but before relaxing and writing. */
2787
2788void
2789avr_pre_output_hook (void)
2790{
2791 if (avr_opt.have_gccisr)
2792 bfd_map_over_sections (stdoutput, avr_check_gccisr_done, NULL);
2793}
This page took 0.885235 seconds and 4 git commands to generate.