1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
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.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
28 const char comment_chars
[] = ";";
29 const char line_comment_chars
[] = "#";
30 const char line_separator_chars
[] = "$";
32 #define AVR_ISA_1200 0x0001 /* in the beginning there was ... */
33 #define AVR_ISA_LPM 0x0002 /* device has LPM */
34 #define AVR_ISA_LPMX 0x0004 /* device has LPM Rd,Z[+] */
35 #define AVR_ISA_SRAM 0x0008 /* device has SRAM (LD, ST, PUSH, POP, ...) */
36 #define AVR_ISA_WRAP 0x0010 /* device has exactly 8K program memory */
37 #define AVR_ISA_MEGA 0x0020 /* device has >8K program memory (JMP, CALL) */
38 #define AVR_ISA_MUL 0x0040 /* device has new core (MUL, MOVW, ...) */
39 #define AVR_ISA_ELPM 0x0080 /* device has >64K program memory (ELPM) */
40 #define AVR_ISA_ELPMX 0x0100 /* device has ELPM Rd,Z[+] (none yet) */
41 #define AVR_ISA_SPM 0x0200 /* device can program itself (<=64K) */
42 #define AVR_ISA_ESPM 0x0400 /* device can program itself (>64K, none yet) */
43 #define AVR_ISA_EIND 0x0800 /* device has >128K program memory (none yet) */
45 #define AVR_ISA_TINY1 (AVR_ISA_1200 | AVR_ISA_LPM)
46 #define AVR_ISA_2xxx (AVR_ISA_TINY1 | AVR_ISA_SRAM)
47 #define AVR_ISA_85xx (AVR_ISA_2xxx | AVR_ISA_WRAP)
48 #define AVR_ISA_M603 (AVR_ISA_2xxx | AVR_ISA_MEGA)
49 #define AVR_ISA_M103 (AVR_ISA_M603 | AVR_ISA_ELPM)
50 #define AVR_ISA_M161 (AVR_ISA_M603 | AVR_ISA_MUL | AVR_ISA_LPMX | AVR_ISA_SPM)
51 #define AVR_ISA_94K (AVR_ISA_M603 | AVR_ISA_MUL | AVR_ISA_LPMX)
53 #define AVR_ISA_ALL 0xFFFF
55 const char *md_shortopts
= "m:";
63 static struct mcu_type_s mcu_types
[] =
65 {"avr1", AVR_ISA_TINY1
, bfd_mach_avr1
},
66 {"avr2", AVR_ISA_85xx
, bfd_mach_avr2
},
67 {"avr3", AVR_ISA_M103
, bfd_mach_avr3
},
68 {"avr4", AVR_ISA_ALL
, bfd_mach_avr4
},
69 {"at90s1200", AVR_ISA_1200
, bfd_mach_avr1
},
70 {"attiny10", AVR_ISA_TINY1
, bfd_mach_avr1
},
71 {"attiny11", AVR_ISA_TINY1
, bfd_mach_avr1
},
72 {"attiny12", AVR_ISA_TINY1
, bfd_mach_avr1
},
73 {"attiny15", AVR_ISA_TINY1
, bfd_mach_avr1
},
74 {"attiny28", AVR_ISA_TINY1
, bfd_mach_avr1
},
75 {"at90s2313", AVR_ISA_2xxx
, bfd_mach_avr2
},
76 {"at90s2323", AVR_ISA_2xxx
, bfd_mach_avr2
},
77 {"at90s2333", AVR_ISA_2xxx
, bfd_mach_avr2
},
78 {"attiny22" , AVR_ISA_2xxx
, bfd_mach_avr2
},
79 {"at90s2343", AVR_ISA_2xxx
, bfd_mach_avr2
},
80 {"at90s4433", AVR_ISA_2xxx
, bfd_mach_avr2
},
81 {"at90s4414", AVR_ISA_2xxx
, bfd_mach_avr2
},
82 {"at90s4434", AVR_ISA_2xxx
, bfd_mach_avr2
},
83 {"at90s8515", AVR_ISA_85xx
, bfd_mach_avr2
},
84 {"at90s8535", AVR_ISA_85xx
, bfd_mach_avr2
},
85 {"at90c8534", AVR_ISA_85xx
, bfd_mach_avr2
},
86 {"atmega603", AVR_ISA_M603
, bfd_mach_avr3
},
87 {"atmega103", AVR_ISA_M103
, bfd_mach_avr3
},
88 {"atmega161", AVR_ISA_M161
, bfd_mach_avr4
},
89 {"at94k10", AVR_ISA_94K
, bfd_mach_avr4
},
90 {"at94k20", AVR_ISA_94K
, bfd_mach_avr4
},
91 {"at94k40", AVR_ISA_94K
, bfd_mach_avr4
},
96 /* Current MCU type. */
97 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_2xxx
,bfd_mach_avr2
};
98 static struct mcu_type_s
*avr_mcu
= &default_mcu
;
100 const char EXP_CHARS
[] = "eE";
101 const char FLT_CHARS
[] = "dD";
102 static void avr_set_arch (int dummy
);
104 /* The target specific pseudo-ops which we support. */
105 const pseudo_typeS md_pseudo_table
[] =
107 {"arch", avr_set_arch
, 0},
111 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
112 #define REGISTER_P(x) ((x) == 'r' \
123 int insn_size
; /* in words */
125 unsigned int bin_opcode
;
128 static char * skip_space (char * s
);
129 static char * extract_word (char *from
, char *to
, int limit
);
130 static unsigned int avr_operand (struct avr_opcodes_s
*opcode
,
131 int where
, char *op
, char **line
);
132 static unsigned int avr_operands (struct avr_opcodes_s
*opcode
, char **line
);
133 static unsigned int avr_get_constant (char * str
, int max
);
134 static char *parse_exp (char *s
, expressionS
* op
);
135 static bfd_reloc_code_real_type
avr_ldi_expression (expressionS
*exp
);
136 long md_pcrel_from_section
PARAMS ((fixS
*, segT
));
139 /* constraint letters
141 d - `ldi' register (r16-r31)
142 v - `movw' even register (r0, r2, ..., r28, r30)
143 a - `fmul' register (r16-r23)
144 w - `adiw' register (r24,r26,r28,r30)
145 e - pointer registers (X,Y,Z)
146 b - base pointer register and displacement ([YZ]+disp)
147 z - Z pointer register (for [e]lpm Rd,Z[+])
148 M - immediate value from 0 to 255
149 n - immediate value from 0 to 255 ( n = ~M ). Relocation impossible
150 s - immediate value from 0 to 7
151 P - Port address value from 0 to 64. (in, out)
152 p - Port address value from 0 to 32. (cbi, sbi, sbic, sbis)
153 K - immediate value from 0 to 64 (used in `adiw', `sbiw')
155 l - signed pc relative offset from -64 to 63
156 L - signed pc relative offset from -2048 to 2047
157 h - absolut code address (call, jmp)
158 S - immediate value from 0 to 7 (S = s << 4)
161 struct avr_opcodes_s avr_opcodes
[] =
163 {"adc", "r,r", "000111rdddddrrrr", 1, AVR_ISA_1200
, 0x1c00},
164 {"add", "r,r", "000011rdddddrrrr", 1, AVR_ISA_1200
, 0x0c00},
165 {"and", "r,r", "001000rdddddrrrr", 1, AVR_ISA_1200
, 0x2000},
166 {"cp", "r,r", "000101rdddddrrrr", 1, AVR_ISA_1200
, 0x1400},
167 {"cpc", "r,r", "000001rdddddrrrr", 1, AVR_ISA_1200
, 0x0400},
168 {"cpse", "r,r", "000100rdddddrrrr", 1, AVR_ISA_1200
, 0x1000},
169 {"eor", "r,r", "001001rdddddrrrr", 1, AVR_ISA_1200
, 0x2400},
170 {"mov", "r,r", "001011rdddddrrrr", 1, AVR_ISA_1200
, 0x2c00},
171 {"mul", "r,r", "100111rdddddrrrr", 1, AVR_ISA_MUL
, 0x9c00},
172 {"or", "r,r", "001010rdddddrrrr", 1, AVR_ISA_1200
, 0x2800},
173 {"sbc", "r,r", "000010rdddddrrrr", 1, AVR_ISA_1200
, 0x0800},
174 {"sub", "r,r", "000110rdddddrrrr", 1, AVR_ISA_1200
, 0x1800},
176 {"clr", "r=r", "001001rdddddrrrr", 1, AVR_ISA_1200
, 0x2400},
177 {"lsl", "r=r", "000011rdddddrrrr", 1, AVR_ISA_1200
, 0x0c00},
178 {"rol", "r=r", "000111rdddddrrrr", 1, AVR_ISA_1200
, 0x1c00},
179 {"tst", "r=r", "001000rdddddrrrr", 1, AVR_ISA_1200
, 0x2000},
181 {"andi", "d,M", "0111KKKKddddKKKK", 1, AVR_ISA_1200
, 0x7000},
183 {"cbr", "d,n", "0111KKKKddddKKKK", 1, AVR_ISA_1200
, 0x7000},
184 {"cpi", "d,M", "0011KKKKddddKKKK", 1, AVR_ISA_1200
, 0x3000},
185 {"ldi", "d,M", "1110KKKKddddKKKK", 1, AVR_ISA_1200
, 0xe000},
186 {"ori", "d,M", "0110KKKKddddKKKK", 1, AVR_ISA_1200
, 0x6000},
187 {"sbci", "d,M", "0100KKKKddddKKKK", 1, AVR_ISA_1200
, 0x4000},
188 {"sbr", "d,M", "0110KKKKddddKKKK", 1, AVR_ISA_1200
, 0x6000},
189 {"subi", "d,M", "0101KKKKddddKKKK", 1, AVR_ISA_1200
, 0x5000},
191 {"sbrc", "r,s", "1111110rrrrr0sss", 1, AVR_ISA_1200
, 0xfc00},
192 {"sbrs", "r,s", "1111111rrrrr0sss", 1, AVR_ISA_1200
, 0xfe00},
193 {"bld", "r,s", "1111100ddddd0sss", 1, AVR_ISA_1200
, 0xf800},
194 {"bst", "r,s", "1111101ddddd0sss", 1, AVR_ISA_1200
, 0xfa00},
196 {"in", "r,P", "10110PPdddddPPPP", 1, AVR_ISA_1200
, 0xb000},
197 {"out", "P,r", "10111PPrrrrrPPPP", 1, AVR_ISA_1200
, 0xb800},
199 {"adiw", "w,K", "10010110KKddKKKK", 1, AVR_ISA_2xxx
, 0x9600},
200 {"sbiw", "w,K", "10010111KKddKKKK", 1, AVR_ISA_2xxx
, 0x9700},
202 {"cbi", "p,s", "10011000pppppsss", 1, AVR_ISA_1200
, 0x9800},
203 {"sbi", "p,s", "10011010pppppsss", 1, AVR_ISA_1200
, 0x9a00},
204 {"sbic", "p,s", "10011001pppppsss", 1, AVR_ISA_1200
, 0x9900},
205 {"sbis", "p,s", "10011011pppppsss", 1, AVR_ISA_1200
, 0x9b00},
207 /* ee = {X=11,Y=10,Z=00, 0} */
208 {"ld", "r,e", "100!000dddddee-+", 1, AVR_ISA_2xxx
, 0x8000},
209 {"st", "e,r", "100!001rrrrree-+", 1, AVR_ISA_2xxx
, 0x8200},
210 {"ldd", "r,b", "10o0oo0dddddbooo", 1, AVR_ISA_2xxx
, 0x8000},
211 {"std", "b,r", "10o0oo1rrrrrbooo", 1, AVR_ISA_2xxx
, 0x8200},
212 {"sts", "i,r", "1001001ddddd0000", 2, AVR_ISA_2xxx
, 0x9200},
213 {"lds", "r,i", "1001000ddddd0000", 2, AVR_ISA_2xxx
, 0x9000},
215 {"brbc", "s,l", "111101lllllllsss", 1, AVR_ISA_1200
, 0xf400},
216 {"brbs", "s,l", "111100lllllllsss", 1, AVR_ISA_1200
, 0xf000},
218 {"brcc", "l", "111101lllllll000", 1, AVR_ISA_1200
, 0xf400},
219 {"brcs", "l", "111100lllllll000", 1, AVR_ISA_1200
, 0xf000},
220 {"breq", "l", "111100lllllll001", 1, AVR_ISA_1200
, 0xf001},
221 {"brge", "l", "111101lllllll100", 1, AVR_ISA_1200
, 0xf404},
222 {"brhc", "l", "111101lllllll101", 1, AVR_ISA_1200
, 0xf405},
223 {"brhs", "l", "111100lllllll101", 1, AVR_ISA_1200
, 0xf005},
224 {"brid", "l", "111101lllllll111", 1, AVR_ISA_1200
, 0xf407},
225 {"brie", "l", "111100lllllll111", 1, AVR_ISA_1200
, 0xf007},
226 {"brlo", "l", "111100lllllll000", 1, AVR_ISA_1200
, 0xf000},
227 {"brlt", "l", "111100lllllll100", 1, AVR_ISA_1200
, 0xf004},
228 {"brmi", "l", "111100lllllll010", 1, AVR_ISA_1200
, 0xf002},
229 {"brne", "l", "111101lllllll001", 1, AVR_ISA_1200
, 0xf401},
230 {"brpl", "l", "111101lllllll010", 1, AVR_ISA_1200
, 0xf402},
231 {"brsh", "l", "111101lllllll000", 1, AVR_ISA_1200
, 0xf400},
232 {"brtc", "l", "111101lllllll110", 1, AVR_ISA_1200
, 0xf406},
233 {"brts", "l", "111100lllllll110", 1, AVR_ISA_1200
, 0xf006},
234 {"brvc", "l", "111101lllllll011", 1, AVR_ISA_1200
, 0xf403},
235 {"brvs", "l", "111100lllllll011", 1, AVR_ISA_1200
, 0xf003},
237 {"rcall", "L", "1101LLLLLLLLLLLL", 1, AVR_ISA_1200
, 0xd000},
238 {"rjmp", "L", "1100LLLLLLLLLLLL", 1, AVR_ISA_1200
, 0xc000},
240 {"call", "h", "1001010hhhhh111h", 2, AVR_ISA_MEGA
, 0x940e},
241 {"jmp", "h", "1001010hhhhh110h", 2, AVR_ISA_MEGA
, 0x940c},
243 {"asr", "r", "1001010rrrrr0101", 1, AVR_ISA_1200
, 0x9405},
244 {"com", "r", "1001010rrrrr0000", 1, AVR_ISA_1200
, 0x9400},
245 {"dec", "r", "1001010rrrrr1010", 1, AVR_ISA_1200
, 0x940a},
246 {"inc", "r", "1001010rrrrr0011", 1, AVR_ISA_1200
, 0x9403},
247 {"lsr", "r", "1001010rrrrr0110", 1, AVR_ISA_1200
, 0x9406},
248 {"neg", "r", "1001010rrrrr0001", 1, AVR_ISA_1200
, 0x9401},
249 {"pop", "r", "1001000rrrrr1111", 1, AVR_ISA_2xxx
, 0x900f},
250 {"push", "r", "1001001rrrrr1111", 1, AVR_ISA_2xxx
, 0x920f},
251 {"ror", "r", "1001010rrrrr0111", 1, AVR_ISA_1200
, 0x9407},
252 {"ser", "d", "11101111dddd1111", 1, AVR_ISA_1200
, 0xef0f},
253 {"swap", "r", "1001010rrrrr0010", 1, AVR_ISA_1200
, 0x9402},
255 {"bclr", "S", "100101001SSS1000", 1, AVR_ISA_1200
, 0x9488},
256 {"bset", "S", "100101000SSS1000", 1, AVR_ISA_1200
, 0x9408},
258 {"clc", "", "1001010010001000", 1, AVR_ISA_1200
, 0x9488},
259 {"clh", "", "1001010011011000", 1, AVR_ISA_1200
, 0x94d8},
260 {"cli", "", "1001010011111000", 1, AVR_ISA_1200
, 0x94f8},
261 {"cln", "", "1001010010101000", 1, AVR_ISA_1200
, 0x94a8},
262 {"cls", "", "1001010011001000", 1, AVR_ISA_1200
, 0x94c8},
263 {"clt", "", "1001010011101000", 1, AVR_ISA_1200
, 0x94e8},
264 {"clv", "", "1001010010111000", 1, AVR_ISA_1200
, 0x94b8},
265 {"clz", "", "1001010010011000", 1, AVR_ISA_1200
, 0x9498},
266 {"icall","", "1001010100001001", 1, AVR_ISA_2xxx
, 0x9509},
267 {"ijmp", "", "1001010000001001", 1, AVR_ISA_2xxx
, 0x9409},
268 {"lpm", "", "1001010111001000", 1, AVR_ISA_TINY1
,0x95c8},
269 {"nop", "", "0000000000000000", 1, AVR_ISA_1200
, 0x0000},
270 {"ret", "", "1001010100001000", 1, AVR_ISA_1200
, 0x9508},
271 {"reti", "", "1001010100011000", 1, AVR_ISA_1200
, 0x9518},
272 {"sec", "", "1001010000001000", 1, AVR_ISA_1200
, 0x9408},
273 {"seh", "", "1001010001011000", 1, AVR_ISA_1200
, 0x9458},
274 {"sei", "", "1001010001111000", 1, AVR_ISA_1200
, 0x9478},
275 {"sen", "", "1001010000101000", 1, AVR_ISA_1200
, 0x9428},
276 {"ses", "", "1001010001001000", 1, AVR_ISA_1200
, 0x9448},
277 {"set", "", "1001010001101000", 1, AVR_ISA_1200
, 0x9468},
278 {"sev", "", "1001010000111000", 1, AVR_ISA_1200
, 0x9438},
279 {"sez", "", "1001010000011000", 1, AVR_ISA_1200
, 0x9418},
280 {"sleep","", "1001010110001000", 1, AVR_ISA_1200
, 0x9588},
281 {"wdr", "", "1001010110101000", 1, AVR_ISA_1200
, 0x95a8},
282 {"elpm", "", "1001010111011000", 1, AVR_ISA_ELPM
, 0x95d8},
283 {"spm", "", "1001010111101000", 1, AVR_ISA_SPM
, 0x95e8},
284 {"movw", "v,v", "00000001ddddrrrr", 1, AVR_ISA_MUL
, 0x0100},
285 {"muls", "d,d", "00000010ddddrrrr", 1, AVR_ISA_MUL
, 0x0200},
286 {"mulsu","a,a", "000000110ddd0rrr", 1, AVR_ISA_MUL
, 0x0300},
287 {"fmul", "a,a", "000000110ddd1rrr", 1, AVR_ISA_MUL
, 0x0308},
288 {"fmuls","a,a", "000000111ddd0rrr", 1, AVR_ISA_MUL
, 0x0380},
289 {"fmulsu","a,a","000000111ddd1rrr", 1, AVR_ISA_MUL
, 0x0388},
290 {"lpmx", "r,z", "1001000ddddd010+", 1, AVR_ISA_LPMX
, 0x9004},
291 /* these are for devices that don't exists yet */
292 /* >64K program memory, new core */
293 {"elpmx","r,z", "1001000ddddd011+", 1, AVR_ISA_ELPMX
,0x9006},
294 {"espm", "", "1001010111111000", 1, AVR_ISA_ESPM
, 0x95f8},
295 /* >128K program memory (PC = EIND:Z) */
296 {"eicall", "", "1001010100011001", 1, AVR_ISA_EIND
, 0x9519},
297 {"eijmp", "", "1001010000011001", 1, AVR_ISA_EIND
, 0x9419},
298 {NULL
, NULL
, NULL
, 0, 0, 0}
303 #define EXP_MOD_NAME(i) exp_mod[i].name
304 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
305 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
306 #define HAVE_PM_P(i) exp_mod[i].have_pm
311 bfd_reloc_code_real_type reloc
;
312 bfd_reloc_code_real_type neg_reloc
;
316 static struct exp_mod_s exp_mod
[] = {
317 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
318 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
319 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
320 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
321 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
322 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
323 {"hlo8", -BFD_RELOC_AVR_LO8_LDI
, -BFD_RELOC_AVR_LO8_LDI_NEG
, 0},
324 {"hhi8", -BFD_RELOC_AVR_HI8_LDI
, -BFD_RELOC_AVR_HI8_LDI_NEG
, 0},
327 /* Opcode hash table. */
328 static struct hash_control
*avr_hash
;
330 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
331 static struct hash_control
*avr_mod_hash
;
333 #define OPTION_MMCU (OPTION_MD_BASE + 1)
335 struct option md_longopts
[] = {
336 {"mmcu", required_argument
, NULL
, 'm'},
337 {NULL
, no_argument
, NULL
, 0}
339 size_t md_longopts_size
= sizeof(md_longopts
);
345 while (*s
== ' ' || *s
== '\t')
350 /* Extract one word from FROM and copy it to TO. */
352 extract_word (char *from
, char *to
, int limit
)
358 /* Drop leading whitespace. */
359 from
= skip_space (from
);
361 /* Find the op code end. */
362 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name(*op_end
); )
364 to
[size
++] = *op_end
++;
365 if (size
+ 1 >= limit
)
373 md_estimate_size_before_relax (fragp
, seg
)
374 fragS
*fragp ATTRIBUTE_UNUSED
;
375 asection
*seg ATTRIBUTE_UNUSED
;
382 md_show_usage (stream
)
388 " -mmcu=[avr-name] select microcontroller variant\n"
389 " [avr-name] can be:\n"
390 " avr1 - AT90S1200\n"
391 " avr2 - AT90S2xxx, AT90S4xxx, AT90S85xx, ATtiny22\n"
392 " avr3 - ATmega103 or ATmega603\n"
393 " avr4 - ATmega161\n"
394 " or immediate microcontroller name.\n"));
399 int dummy ATTRIBUTE_UNUSED
;
402 str
= (char *)alloca (20);
403 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
404 md_parse_option ('m', str
);
405 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
409 md_parse_option (c
, arg
)
413 char *t
= alloca (strlen (arg
) + 1);
417 *t
= tolower (*arg1
++);
424 for (i
= 0; mcu_types
[i
].name
; ++i
)
425 if (strcmp (mcu_types
[i
].name
, s
) == 0)
428 if (!mcu_types
[i
].name
)
429 as_fatal (_ ("unknown MCU: %s\n"), arg
);
430 if (avr_mcu
== &default_mcu
)
431 avr_mcu
= &mcu_types
[i
];
433 as_fatal (_ ("redefinition of mcu type `%s'"), mcu_types
[i
].name
);
440 md_undefined_symbol(name
)
441 char *name ATTRIBUTE_UNUSED
;
446 /* Convert a string pointed to by input_line_pointer into a floating point
447 constant of type `type', and store the appropriate bytes to `*litP'.
448 The number of LITTLENUMS emitted is stored in `*sizeP'. Returns NULL if
449 OK, or an error message otherwise. */
451 md_atof (type
, litP
, sizeP
)
457 LITTLENUM_TYPE words
[4];
458 LITTLENUM_TYPE
*wordP
;
471 return _("bad call to md_atof");
474 t
= atof_ieee (input_line_pointer
, type
, words
);
476 input_line_pointer
= t
;
478 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
479 /* This loop outputs the LITTLENUMs in REVERSE order. */
480 for (wordP
= words
+ prec
- 1; prec
--;)
482 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
483 litP
+= sizeof (LITTLENUM_TYPE
);
489 md_convert_frag (abfd
, sec
, fragP
)
490 bfd
*abfd ATTRIBUTE_UNUSED
;
491 asection
*sec ATTRIBUTE_UNUSED
;
492 fragS
*fragP ATTRIBUTE_UNUSED
;
502 struct avr_opcodes_s
*opcode
;
503 avr_hash
= hash_new();
505 /* Insert unique names into hash table. This hash table then provides a
506 quick index to the first opcode with a particular name in the opcode
509 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
510 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
512 avr_mod_hash
= hash_new ();
514 for (i
= 0; i
< sizeof (exp_mod
) / sizeof (exp_mod
[0]); ++i
)
515 hash_insert (avr_mod_hash
, EXP_MOD_NAME(i
), (void*)(i
+10));
517 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
521 /* Resolve STR as a constant expression and return the result.
522 If result greater than MAX then error. */
525 avr_get_constant (str
, max
)
530 str
= skip_space (str
);
531 input_line_pointer
= str
;
534 if (ex
.X_op
!= O_constant
)
535 as_bad (_("constant value required"));
537 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
538 as_bad (_("number must be less than %d"), max
+1);
539 return ex
.X_add_number
;
543 /* Parse instruction operands.
544 Returns binary opcode. */
547 avr_operands (opcode
, line
)
548 struct avr_opcodes_s
*opcode
;
551 char *op
= opcode
->constraints
;
552 unsigned int bin
= opcode
->bin_opcode
;
553 char *frag
= frag_more (opcode
->insn_size
* 2);
555 int where
= frag
- frag_now
->fr_literal
;
556 static unsigned int prev
= 0; /* previous opcode */
558 /* Opcode have operands. */
561 unsigned int reg1
= 0;
562 unsigned int reg2
= 0;
563 int reg1_present
= 0;
564 int reg2_present
= 0;
566 /* Parse first operand. */
567 if (REGISTER_P (*op
))
569 reg1
= avr_operand (opcode
, where
, op
, &str
);
572 /* Parse second operand. */
584 if (REGISTER_P (*op
))
587 str
= skip_space (str
);
589 as_bad (_ ("`,' required"));
590 str
= skip_space (str
);
592 reg2
= avr_operand (opcode
, where
, op
, &str
);
595 if (reg1_present
&& reg2_present
)
596 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
597 else if (reg2_present
)
605 /* detect undefined combinations (like lpm r31,Z+) */
606 if (((bin
& 0xFDEF) == 0x91AD) || ((bin
& 0xFDEF) == 0x91AE) ||
607 ((bin
& 0xFDEF) == 0x91C9) || ((bin
& 0xFDEF) == 0x91CA) ||
608 ((bin
& 0xFDEF) == 0x91E1) || ((bin
& 0xFDEF) == 0x91E2) ||
609 ((bin
& 0xFFED) == 0x91E5))
610 as_warn( _("undefined combination of operands"));
612 if (opcode
->insn_size
== 2)
614 /* warn if previous opcode was cpse/sbic/sbis/sbrc/sbrs
616 if ((prev
& 0xFC00) == 0x1000
617 || (prev
& 0xFD00) == 0x9900
618 || (prev
& 0xFC08) == 0xFC00)
619 as_warn (_("skipping two-word instruction"));
621 bfd_putl32 ((bfd_vma
)bin
, frag
);
624 bfd_putl16 ((bfd_vma
)bin
, frag
);
632 /* Parse one instruction operand.
633 Returns operand bitmask. Also fixups can be generated. */
636 avr_operand (opcode
, where
, op
, line
)
637 struct avr_opcodes_s
*opcode
;
643 unsigned int op_mask
= 0;
644 char *str
= skip_space (*line
);
648 /* Any register operand. */
657 if (*str
== 'r' || *str
== 'R')
661 str
= extract_word (str
, r_name
, sizeof (r_name
));
662 if (isdigit(r_name
[1]))
664 if (r_name
[2] == '\0')
665 op_mask
= r_name
[1] - '0';
666 else if (r_name
[1] != '0'
667 && isdigit(r_name
[2])
668 && r_name
[3] == '\0')
669 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
674 op_mask
= avr_get_constant (str
, 31);
675 str
= input_line_pointer
;
683 if (op_mask
< 16 || op_mask
> 23)
684 as_bad (_ ("register r16-r23 required"));
690 as_bad (_ ("register number above 15 required"));
696 as_bad (_ ("even register number required"));
702 if (op_mask
& 1 || op_mask
> 6)
703 as_bad (_ ("register r24,r26,r28 or r30 required"));
709 as_bad (_ ("register name or number from 0 to 31 required"));
718 str
= skip_space (str
+1);
727 as_bad (_ ("pointer register (X,Y or Z) required"));
729 str
= skip_space (str
+1);
734 as_bad (_ ("cannot both predecrement and postincrement"));
743 as_bad (_ ("can't predecrement"));
745 if (! (*str
== 'z' || *str
== 'Z'))
746 as_bad (_ ("pointer register Z required"));
748 str
= skip_space (str
+ 1);
759 char c
= tolower (*str
++);
763 as_bad (_ ("pointer register (Y or Z) required"));
764 str
= skip_space (str
);
768 x
= avr_get_constant (str
, 63);
769 str
= input_line_pointer
;
770 op_mask
|= (x
& 7) | ((x
& (3 << 3)) << 7) | ((x
& (1 << 5)) << 8);
777 str
= parse_exp (str
, &op_expr
);
778 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
779 &op_expr
, false, BFD_RELOC_AVR_CALL
);
786 str
= parse_exp (str
, &op_expr
);
787 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
788 &op_expr
, true, BFD_RELOC_AVR_13_PCREL
);
795 str
= parse_exp (str
, &op_expr
);
796 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
797 &op_expr
, true, BFD_RELOC_AVR_7_PCREL
);
804 str
= parse_exp (str
, &op_expr
);
805 fix_new_exp (frag_now
, where
+2, opcode
->insn_size
* 2,
806 &op_expr
, false, BFD_RELOC_16
);
813 bfd_reloc_code_real_type r_type
;
814 input_line_pointer
= str
;
815 r_type
= avr_ldi_expression (&op_expr
);
816 str
= input_line_pointer
;
817 fix_new_exp (frag_now
, where
, 3,
818 &op_expr
, false, r_type
);
825 x
= ~avr_get_constant (str
, 255);
826 str
= input_line_pointer
;
827 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
834 x
= avr_get_constant (str
, 63);
835 str
= input_line_pointer
;
836 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 2);
844 x
= avr_get_constant (str
, 7);
845 str
= input_line_pointer
;
855 x
= avr_get_constant (str
, 63);
856 str
= input_line_pointer
;
857 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
864 x
= avr_get_constant (str
, 31);
865 str
= input_line_pointer
;
870 as_bad (_ ("unknown constraint `%c'"), *op
);
876 /* GAS will call this function for each section at the end of the assembly,
877 to permit the CPU backend to adjust the alignment of a section. */
879 md_section_align (seg
, addr
)
883 int align
= bfd_get_section_alignment (stdoutput
, seg
);
884 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
887 /* If you define this macro, it should return the offset between the
888 address of a PC relative fixup and the position from which the PC
889 relative adjustment should be made. On many processors, the base
890 of a PC relative instruction is the next instruction, so this
891 macro would return the length of an instruction. */
893 md_pcrel_from_section (fixp
, sec
)
897 if (fixp
->fx_addsy
!= (symbolS
*)NULL
898 && (!S_IS_DEFINED (fixp
->fx_addsy
)
899 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
901 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
904 /* GAS will call this for each fixup. It should store the correct
905 value in the object file. */
907 md_apply_fix3 (fixp
, valuep
, seg
)
912 unsigned char *where
;
916 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
921 else if (fixp
->fx_pcrel
)
923 segT s
= S_GET_SEGMENT (fixp
->fx_addsy
);
924 if (fixp
->fx_addsy
&& (s
== seg
|| s
== absolute_section
))
926 value
= S_GET_VALUE (fixp
->fx_addsy
) + *valuep
;
934 value
= fixp
->fx_offset
;
935 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
937 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
939 value
-= S_GET_VALUE (fixp
->fx_subsy
);
944 /* We don't actually support subtracting a symbol. */
945 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
946 _ ("expression too complex"));
950 switch (fixp
->fx_r_type
)
953 fixp
->fx_no_overflow
= 1;
955 case BFD_RELOC_AVR_7_PCREL
:
956 case BFD_RELOC_AVR_13_PCREL
:
959 case BFD_RELOC_AVR_CALL
:
965 /* Fetch the instruction, insert the fully resolved operand
966 value, and stuff the instruction back again. */
967 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
968 insn
= bfd_getl16 (where
);
970 switch (fixp
->fx_r_type
)
972 case BFD_RELOC_AVR_7_PCREL
:
974 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
975 _("odd address operand: %ld"), value
);
976 /* Instruction addresses are always right-shifted by 1. */
978 --value
; /* Correct PC. */
979 if (value
< -64 || value
> 63)
980 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
981 _("operand out of range: %ld"), value
);
982 value
= (value
<< 3) & 0x3f8;
983 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
986 case BFD_RELOC_AVR_13_PCREL
:
988 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
989 _("odd address operand: %ld"), value
);
990 /* Instruction addresses are always right-shifted by 1. */
992 --value
; /* Correct PC. */
994 if (value
< -2048 || value
> 2047)
996 if (avr_mcu
->isa
& AVR_ISA_WRAP
)
1004 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1005 _("operand out of range: %ld"), value
);
1009 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
1013 bfd_putl16 ((bfd_vma
) value
, where
);
1017 bfd_putl16 ((bfd_vma
) value
, where
);
1020 case BFD_RELOC_AVR_16_PM
:
1021 bfd_putl16 ((bfd_vma
) (value
>>1), where
);
1024 case BFD_RELOC_AVR_LO8_LDI
:
1025 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
1028 case -BFD_RELOC_AVR_LO8_LDI
:
1029 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
1032 case BFD_RELOC_AVR_HI8_LDI
:
1033 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
1036 case -BFD_RELOC_AVR_HI8_LDI
:
1037 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
1040 case BFD_RELOC_AVR_HH8_LDI
:
1041 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
1044 case BFD_RELOC_AVR_LO8_LDI_NEG
:
1045 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
1048 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
1049 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
1052 case BFD_RELOC_AVR_HI8_LDI_NEG
:
1053 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
1056 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
1057 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
1060 case BFD_RELOC_AVR_HH8_LDI_NEG
:
1061 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
1064 case BFD_RELOC_AVR_LO8_LDI_PM
:
1065 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
1068 case BFD_RELOC_AVR_HI8_LDI_PM
:
1069 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
1072 case BFD_RELOC_AVR_HH8_LDI_PM
:
1073 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
1076 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
1077 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
1080 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
1081 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
1084 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
1085 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
1088 case BFD_RELOC_AVR_CALL
:
1091 x
= bfd_getl16 (where
);
1093 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1094 _("odd address operand: %ld"), value
);
1096 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
1097 bfd_putl16 ((bfd_vma
) x
, where
);
1098 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+2);
1103 as_fatal ( _("line %d: unknown relocation type: 0x%x"),
1104 fixp
->fx_line
, fixp
->fx_r_type
);
1110 switch (fixp
->fx_r_type
)
1112 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
1113 case -BFD_RELOC_AVR_HI8_LDI
:
1114 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
1115 case -BFD_RELOC_AVR_LO8_LDI
:
1116 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1117 _("only constant expression allowed"));
1123 fixp
->fx_addnumber
= value
;
1129 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1130 will pass the resulting reloc to `bfd_install_relocation'. This
1131 currently works poorly, as `bfd_install_relocation' often does the
1132 wrong thing, and instances of `tc_gen_reloc' have been written to
1133 work around the problems, which in turns makes it difficult to fix
1134 `bfd_install_relocation'. */
1136 /* If while processing a fixup, a reloc really needs to be created
1137 then it is done here. */
1140 tc_gen_reloc (seg
, fixp
)
1141 asection
*seg ATTRIBUTE_UNUSED
;
1146 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1148 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1149 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1151 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1152 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1153 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
1155 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1156 _("reloc %d not supported by object file format"),
1157 (int)fixp
->fx_r_type
);
1161 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1162 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1163 reloc
->address
= fixp
->fx_offset
;
1165 reloc
->addend
= fixp
->fx_offset
;
1175 struct avr_opcodes_s
* opcode
;
1178 str
= skip_space (extract_word (str
, op
, sizeof(op
)));
1181 as_bad (_ ("can't find opcode "));
1183 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1187 as_bad (_ ("unknown opcode `%s'"), op
);
1191 /* Special case for opcodes with optional operands (lpm, elpm) -
1192 version with operands is listed in avr_opcodes[] with "x" suffix. */
1194 if (*str
&& !(*opcode
->constraints
))
1196 struct avr_opcodes_s
*opc1
;
1198 /* known opcode, so strlen(op) <= 6 and strcat() should be safe */
1200 opc1
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1202 /* if unknown, just forget it and use the original opcode */
1207 if ((opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1208 as_bad (_ ("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1210 /* We used to set input_line_pointer to the result of get_operands,
1211 but that is wrong. Our caller assumes we don't change it. */
1213 char *t
= input_line_pointer
;
1214 avr_operands (opcode
, &str
);
1215 if (*skip_space (str
))
1216 as_bad (_ ("garbage at end of line"));
1217 input_line_pointer
= t
;
1221 /* Parse ordinary expression. */
1227 input_line_pointer
= s
;
1229 if (op
->X_op
== O_absent
)
1230 as_bad (_("missing operand"));
1231 return input_line_pointer
;
1235 /* Parse special expressions (needed for LDI command):
1240 where xx is: hh, hi, lo
1242 static bfd_reloc_code_real_type
1243 avr_ldi_expression (exp
)
1246 char *str
= input_line_pointer
;
1252 str
= extract_word (str
, op
, sizeof (op
));
1255 mod
= (int) hash_find (avr_mod_hash
, op
);
1260 str
= skip_space (str
);
1265 if (strncmp ("pm(", str
, 3) == 0
1266 || strncmp ("-(pm(", str
, 5) == 0)
1274 as_bad (_ ("illegal expression"));
1284 if (*str
== '-' && *(str
+ 1) == '(')
1290 input_line_pointer
= str
;
1294 if (*input_line_pointer
!= ')')
1296 as_bad (_ ("`)' required"));
1299 input_line_pointer
++;
1302 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1306 input_line_pointer
= tmp
;
1308 return BFD_RELOC_AVR_LO8_LDI
;
1311 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1312 `avr_cons_fix_new' */
1313 static int exp_mod_pm
= 0;
1315 /* Parse special CONS expression: pm (expression)
1316 which is used for addressing to a program memory.
1317 Relocation: BFD_RELOC_AVR_16_PM */
1319 avr_parse_cons_expression (exp
, nbytes
)
1327 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1331 char * pm_name
= "pm";
1332 int len
= strlen (pm_name
);
1333 if (strncasecmp (input_line_pointer
, pm_name
, len
) == 0)
1335 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1336 if (*input_line_pointer
== '(')
1338 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1341 if (*input_line_pointer
== ')')
1342 ++input_line_pointer
;
1345 as_bad (_ ("`)' required"));
1350 input_line_pointer
= tmp
;
1357 avr_cons_fix_new(frag
, where
, nbytes
, exp
)
1363 if (exp_mod_pm
== 0)
1366 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_16
);
1367 else if (nbytes
== 4)
1368 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_32
);
1370 as_bad (_ ("illegal %srelocation size: %d"), "", nbytes
);
1375 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_AVR_16_PM
);
1377 as_bad (_ ("illegal %srelocation size: %d"), "`pm' ", nbytes
);