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