2005-04-05 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / gas / config / tc-avr.c
blob6bce110b4deacae3688e31be7073cd0f9c95967a
1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright 1999, 2000, 2001, 2002, 2004, 2005
4 Free Software Foundation, Inc.
5 Contributed by Denis Chertykov <denisc@overta.ru>
7 This file is part of GAS, the GNU Assembler.
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.
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.
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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 #include <stdio.h>
25 #include "as.h"
26 #include "safe-ctype.h"
27 #include "subsegs.h"
29 struct avr_opcodes_s
31 char *name;
32 char *constraints;
33 int insn_size; /* In words. */
34 int isa;
35 unsigned int bin_opcode;
38 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
39 {#NAME, CONSTR, SIZE, ISA, BIN},
41 struct avr_opcodes_s avr_opcodes[] =
43 #include "opcode/avr.h"
44 {NULL, NULL, 0, 0, 0}
47 const char comment_chars[] = ";";
48 const char line_comment_chars[] = "#";
49 const char line_separator_chars[] = "$";
51 const char *md_shortopts = "m:";
52 struct mcu_type_s
54 char *name;
55 int isa;
56 int mach;
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? */
63 static struct mcu_type_s mcu_types[] =
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}
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;
127 /* AVR target-specific switches. */
128 struct avr_opt_s
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 */
135 static struct avr_opt_s avr_opt = { 0, 0, 0 };
137 const char EXP_CHARS[] = "eE";
138 const char FLT_CHARS[] = "dD";
139 static void avr_set_arch (int dummy);
141 /* The target specific pseudo-ops which we support. */
142 const pseudo_typeS md_pseudo_table[] =
144 {"arch", avr_set_arch, 0},
145 { NULL, NULL, 0}
148 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
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 *));
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
165 struct exp_mod_s
167 char *name;
168 bfd_reloc_code_real_type reloc;
169 bfd_reloc_code_real_type neg_reloc;
170 int have_pm;
173 static struct exp_mod_s exp_mod[] =
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},
185 /* Opcode hash table. */
186 static struct hash_control *avr_hash;
188 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
189 static struct hash_control *avr_mod_hash;
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)
196 struct option md_longopts[] =
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 }
205 size_t md_longopts_size = sizeof (md_longopts);
207 /* Display nicely formatted list of known MCU names. */
209 static void
210 show_mcu_list (stream)
211 FILE *stream;
213 int i, x;
215 fprintf (stream, _("Known MCU names:"));
216 x = 1000;
218 for (i = 0; mcu_types[i].name; i++)
220 int len = strlen (mcu_types[i].name);
222 x += len + 1;
224 if (x < 75)
225 fprintf (stream, " %s", mcu_types[i].name);
226 else
228 fprintf (stream, "\n %s", mcu_types[i].name);
229 x = len + 2;
233 fprintf (stream, "\n");
236 static inline char *
237 skip_space (s)
238 char *s;
240 while (*s == ' ' || *s == '\t')
241 ++s;
242 return s;
245 /* Extract one word from FROM and copy it to TO. */
247 static char *
248 extract_word (char *from, char *to, int limit)
250 char *op_start;
251 char *op_end;
252 int size = 0;
254 /* Drop leading whitespace. */
255 from = skip_space (from);
256 *to = 0;
258 /* Find the op code end. */
259 for (op_start = op_end = from; *op_end != 0 && is_part_of_name (*op_end);)
261 to[size++] = *op_end++;
262 if (size + 1 >= limit)
263 break;
266 to[size] = 0;
267 return op_end;
271 md_estimate_size_before_relax (fragp, seg)
272 fragS *fragp ATTRIBUTE_UNUSED;
273 asection *seg ATTRIBUTE_UNUSED;
275 abort ();
276 return 0;
279 void
280 md_show_usage (stream)
281 FILE *stream;
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);
302 static void
303 avr_set_arch (dummy)
304 int dummy ATTRIBUTE_UNUSED;
306 char *str;
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);
315 md_parse_option (c, arg)
316 int c;
317 char *arg;
319 switch (c)
321 case OPTION_MMCU:
323 int i;
324 char *s = alloca (strlen (arg) + 1);
327 char *t = s;
328 char *arg1 = arg;
331 *t = TOLOWER (*arg1++);
332 while (*t++);
335 for (i = 0; mcu_types[i].name; ++i)
336 if (strcmp (mcu_types[i].name, s) == 0)
337 break;
339 if (!mcu_types[i].name)
341 show_mcu_list (stderr);
342 as_fatal (_("unknown MCU: %s\n"), arg);
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;
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;
366 return 0;
369 symbolS *
370 md_undefined_symbol (name)
371 char *name ATTRIBUTE_UNUSED;
373 return 0;
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. */
381 char *
382 md_atof (type, litP, sizeP)
383 int type;
384 char *litP;
385 int *sizeP;
387 int prec;
388 LITTLENUM_TYPE words[4];
389 LITTLENUM_TYPE *wordP;
390 char *t;
392 switch (type)
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");
405 t = atof_ieee (input_line_pointer, type, words);
406 if (t)
407 input_line_pointer = t;
409 *sizeP = prec * sizeof (LITTLENUM_TYPE);
411 /* This loop outputs the LITTLENUMs in REVERSE order. */
412 for (wordP = words + prec - 1; prec--;)
414 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
415 litP += sizeof (LITTLENUM_TYPE);
418 return NULL;
421 void
422 md_convert_frag (abfd, sec, fragP)
423 bfd *abfd ATTRIBUTE_UNUSED;
424 asection *sec ATTRIBUTE_UNUSED;
425 fragS *fragP ATTRIBUTE_UNUSED;
427 abort ();
430 void
431 md_begin ()
433 unsigned int i;
434 struct avr_opcodes_s *opcode;
435 avr_hash = hash_new ();
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);
443 avr_mod_hash = hash_new ();
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));
448 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
451 /* Resolve STR as a constant expression and return the result.
452 If result greater than MAX then error. */
454 static unsigned int
455 avr_get_constant (str, max)
456 char *str;
457 int max;
459 expressionS ex;
460 str = skip_space (str);
461 input_line_pointer = str;
462 expression (&ex);
464 if (ex.X_op != O_constant)
465 as_bad (_("constant value required"));
467 if (ex.X_add_number > max || ex.X_add_number < 0)
468 as_bad (_("number must be less than %d"), max + 1);
470 return ex.X_add_number;
473 /* Parse instruction operands.
474 Return binary opcode. */
476 static unsigned int
477 avr_operands (opcode, line)
478 struct avr_opcodes_s *opcode;
479 char **line;
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. */
488 /* Opcode have operands. */
489 if (*op)
491 unsigned int reg1 = 0;
492 unsigned int reg2 = 0;
493 int reg1_present = 0;
494 int reg2_present = 0;
496 /* Parse first operand. */
497 if (REGISTER_P (*op))
498 reg1_present = 1;
499 reg1 = avr_operand (opcode, where, op, &str);
500 ++op;
502 /* Parse second operand. */
503 if (*op)
505 if (*op == ',')
506 ++op;
508 if (*op == '=')
510 reg2 = reg1;
511 reg2_present = 1;
513 else
515 if (REGISTER_P (*op))
516 reg2_present = 1;
518 str = skip_space (str);
519 if (*str++ != ',')
520 as_bad (_("`,' required"));
521 str = skip_space (str);
523 reg2 = avr_operand (opcode, where, op, &str);
527 if (reg1_present && reg2_present)
528 reg2 = (reg2 & 0xf) | ((reg2 << 5) & 0x200);
529 else if (reg2_present)
530 reg2 <<= 4;
532 if (reg1_present)
533 reg1 <<= 4;
534 bin |= reg1 | reg2;
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"));
541 if (opcode->insn_size == 2)
543 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
544 (AVR core bug, fixed in the newer devices). */
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"));
551 bfd_putl32 ((bfd_vma) bin, frag);
553 else
554 bfd_putl16 ((bfd_vma) bin, frag);
556 prev = bin;
557 *line = str;
558 return bin;
561 /* Parse for ldd/std offset */
563 static void
564 avr_offset_expression (expressionS *exp)
566 char *str = input_line_pointer;
567 char *tmp;
568 char op[8];
570 tmp = str;
571 str = extract_word (str, op, sizeof (op));
573 input_line_pointer = tmp;
574 expression (exp);
576 /* Warn about expressions that fail to use lo8 (). */
577 if (exp->X_op == O_constant)
579 int x = exp->X_add_number;
581 if (x < -255 || x > 255)
582 as_warn (_("constant out of 8-bit range: %d"), x);
586 /* Parse one instruction operand.
587 Return operand bitmask. Also fixups can be generated. */
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;
596 expressionS op_expr;
597 unsigned int op_mask = 0;
598 char *str = skip_space (*line);
600 switch (*op)
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')
610 char r_name[20];
612 str = extract_word (str, r_name, sizeof (r_name));
613 op_mask = 0xff;
614 if (ISDIGIT (r_name[1]))
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';
624 else
626 op_mask = avr_get_constant (str, 31);
627 str = input_line_pointer;
630 if (op_mask <= 31)
632 switch (*op)
634 case 'a':
635 if (op_mask < 16 || op_mask > 23)
636 as_bad (_("register r16-r23 required"));
637 op_mask -= 16;
638 break;
640 case 'd':
641 if (op_mask < 16)
642 as_bad (_("register number above 15 required"));
643 op_mask -= 16;
644 break;
646 case 'v':
647 if (op_mask & 1)
648 as_bad (_("even register number required"));
649 op_mask >>= 1;
650 break;
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;
658 break;
660 as_bad (_("register name or number from 0 to 31 required"));
661 break;
663 case 'e':
665 char c;
667 if (*str == '-')
669 str = skip_space (str + 1);
670 op_mask = 0x1002;
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"));
680 str = skip_space (str + 1);
681 if (*str == '+')
683 ++str;
684 if (op_mask & 2)
685 as_bad (_("cannot both predecrement and postincrement"));
686 op_mask |= 0x1001;
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"));
695 break;
697 case 'z':
698 if (*str == '-')
699 as_bad (_("can't predecrement"));
701 if (! (*str == 'z' || *str == 'Z'))
702 as_bad (_("pointer register Z required"));
704 str = skip_space (str + 1);
706 if (*str == '+')
708 ++str;
709 op_mask |= 1;
711 break;
713 case 'b':
715 char c = TOLOWER (*str++);
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++ == '+')
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);
731 break;
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;
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;
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;
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;
757 case 'M':
759 bfd_reloc_code_real_type r_type;
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);
767 break;
769 case 'n':
771 unsigned int x;
773 x = ~avr_get_constant (str, 255);
774 str = input_line_pointer;
775 op_mask |= (x & 0xf) | ((x << 4) & 0xf00);
777 break;
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;
787 case 'S':
788 case 's':
790 unsigned int x;
792 x = avr_get_constant (str, 7);
793 str = input_line_pointer;
794 if (*op == 'S')
795 x <<= 4;
796 op_mask |= x;
798 break;
800 case 'P':
802 unsigned int x;
804 x = avr_get_constant (str, 63);
805 str = input_line_pointer;
806 op_mask |= (x & 0xf) | ((x & 0x30) << 5);
808 break;
810 case 'p':
812 unsigned int x;
814 x = avr_get_constant (str, 31);
815 str = input_line_pointer;
816 op_mask |= x << 3;
818 break;
820 case '?':
821 break;
823 default:
824 as_bad (_("unknown constraint `%c'"), *op);
827 *line = str;
828 return op_mask;
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. */
834 valueT
835 md_section_align (seg, addr)
836 asection *seg;
837 valueT addr;
839 int align = bfd_get_section_alignment (stdoutput, seg);
840 return ((addr + (1 << align) - 1) & (-1 << align));
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. */
849 long
850 md_pcrel_from_section (fixp, sec)
851 fixS *fixp;
852 segT sec;
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;
859 return fixp->fx_frag->fr_address + fixp->fx_where;
862 /* GAS will call this for each fixup. It should store the correct
863 value in the object file. */
865 void
866 md_apply_fix3 (fixP, valP, seg)
867 fixS *fixP;
868 valueT * valP;
869 segT seg;
871 unsigned char *where;
872 unsigned long insn;
873 long value = *valP;
875 if (fixP->fx_addsy == (symbolS *) NULL)
876 fixP->fx_done = 1;
878 else if (fixP->fx_pcrel)
880 segT s = S_GET_SEGMENT (fixP->fx_addsy);
882 if (s == seg || s == absolute_section)
884 value += S_GET_VALUE (fixP->fx_addsy);
885 fixP->fx_done = 1;
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"));
893 switch (fixP->fx_r_type)
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;
906 if (fixP->fx_done)
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);
913 switch (fixP->fx_r_type)
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);
920 /* Instruction addresses are always right-shifted by 1. */
921 value >>= 1;
922 --value; /* Correct PC. */
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;
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);
936 /* Instruction addresses are always right-shifted by 1. */
937 value >>= 1;
938 --value; /* Correct PC. */
940 if (value < -2048 || value > 2047)
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);
948 value &= 0xfff;
949 bfd_putl16 ((bfd_vma) (value | insn), where);
950 break;
952 case BFD_RELOC_32:
953 bfd_putl16 ((bfd_vma) value, where);
954 break;
956 case BFD_RELOC_16:
957 bfd_putl16 ((bfd_vma) value, where);
958 break;
960 case BFD_RELOC_AVR_16_PM:
961 bfd_putl16 ((bfd_vma) (value >> 1), where);
962 break;
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;
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;
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;
985 case BFD_RELOC_AVR_LO8_LDI:
986 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
987 break;
989 case -BFD_RELOC_AVR_LO8_LDI:
990 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
991 break;
993 case BFD_RELOC_AVR_HI8_LDI:
994 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where);
995 break;
997 case -BFD_RELOC_AVR_HI8_LDI:
998 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where);
999 break;
1001 case BFD_RELOC_AVR_HH8_LDI:
1002 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
1003 break;
1005 case BFD_RELOC_AVR_LO8_LDI_NEG:
1006 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where);
1007 break;
1009 case -BFD_RELOC_AVR_LO8_LDI_NEG:
1010 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
1011 break;
1013 case BFD_RELOC_AVR_HI8_LDI_NEG:
1014 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where);
1015 break;
1017 case -BFD_RELOC_AVR_HI8_LDI_NEG:
1018 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where);
1019 break;
1021 case BFD_RELOC_AVR_HH8_LDI_NEG:
1022 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
1023 break;
1025 case BFD_RELOC_AVR_LO8_LDI_PM:
1026 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where);
1027 break;
1029 case BFD_RELOC_AVR_HI8_LDI_PM:
1030 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where);
1031 break;
1033 case BFD_RELOC_AVR_HH8_LDI_PM:
1034 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where);
1035 break;
1037 case BFD_RELOC_AVR_LO8_LDI_PM_NEG:
1038 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where);
1039 break;
1041 case BFD_RELOC_AVR_HI8_LDI_PM_NEG:
1042 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where);
1043 break;
1045 case BFD_RELOC_AVR_HH8_LDI_PM_NEG:
1046 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where);
1047 break;
1049 case BFD_RELOC_AVR_CALL:
1051 unsigned long x;
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);
1062 break;
1064 default:
1065 as_fatal (_("line %d: unknown relocation type: 0x%x"),
1066 fixP->fx_line, fixP->fx_r_type);
1067 break;
1070 else
1072 switch (fixP->fx_r_type)
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;
1088 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1089 will pass the resulting reloc to `bfd_install_relocation'. This
1090 currently works poorly, as `bfd_install_relocation' often does the
1091 wrong thing, and instances of `tc_gen_reloc' have been written to
1092 work around the problems, which in turns makes it difficult to fix
1093 `bfd_install_relocation'. */
1095 /* If while processing a fixup, a reloc really needs to be created
1096 then it is done here. */
1098 arelent *
1099 tc_gen_reloc (seg, fixp)
1100 asection *seg ATTRIBUTE_UNUSED;
1101 fixS *fixp;
1103 arelent *reloc;
1105 reloc = (arelent *) xmalloc (sizeof (arelent));
1107 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1108 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1110 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1111 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1112 if (reloc->howto == (reloc_howto_type *) NULL)
1114 as_bad_where (fixp->fx_file, fixp->fx_line,
1115 _("reloc %d not supported by object file format"),
1116 (int) fixp->fx_r_type);
1117 return NULL;
1120 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1121 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1122 reloc->address = fixp->fx_offset;
1124 reloc->addend = fixp->fx_offset;
1126 return reloc;
1129 void
1130 md_assemble (str)
1131 char *str;
1133 struct avr_opcodes_s *opcode;
1134 char op[11];
1136 str = skip_space (extract_word (str, op, sizeof (op)));
1138 if (!op[0])
1139 as_bad (_("can't find opcode "));
1141 opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
1143 if (opcode == NULL)
1145 as_bad (_("unknown opcode `%s'"), op);
1146 return;
1149 /* Special case for opcodes with optional operands (lpm, elpm) -
1150 version with operands exists in avr_opcodes[] in the next entry. */
1152 if (*str && *opcode->constraints == '?')
1153 ++opcode;
1155 if (!avr_opt.all_opcodes && (opcode->isa & avr_mcu->isa) != opcode->isa)
1156 as_bad (_("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name);
1158 /* We used to set input_line_pointer to the result of get_operands,
1159 but that is wrong. Our caller assumes we don't change it. */
1161 char *t = input_line_pointer;
1162 avr_operands (opcode, &str);
1163 if (*skip_space (str))
1164 as_bad (_("garbage at end of line"));
1165 input_line_pointer = t;
1169 /* Parse ordinary expression. */
1171 static char *
1172 parse_exp (s, op)
1173 char *s;
1174 expressionS *op;
1176 input_line_pointer = s;
1177 expression (op);
1178 if (op->X_op == O_absent)
1179 as_bad (_("missing operand"));
1180 return input_line_pointer;
1183 /* Parse special expressions (needed for LDI command):
1184 xx8 (address)
1185 xx8 (-address)
1186 pm_xx8 (address)
1187 pm_xx8 (-address)
1188 where xx is: hh, hi, lo. */
1190 static bfd_reloc_code_real_type
1191 avr_ldi_expression (exp)
1192 expressionS *exp;
1194 char *str = input_line_pointer;
1195 char *tmp;
1196 char op[8];
1197 int mod;
1198 tmp = str;
1200 str = extract_word (str, op, sizeof (op));
1202 if (op[0])
1204 mod = (int) hash_find (avr_mod_hash, op);
1206 if (mod)
1208 int closes = 0;
1210 mod -= 10;
1211 str = skip_space (str);
1213 if (*str == '(')
1215 int neg_p = 0;
1217 ++str;
1219 if (strncmp ("pm(", str, 3) == 0
1220 || strncmp ("-(pm(", str, 5) == 0)
1222 if (HAVE_PM_P (mod))
1224 ++mod;
1225 ++closes;
1227 else
1228 as_bad (_("illegal expression"));
1230 if (*str == '-')
1232 neg_p = 1;
1233 ++closes;
1234 str += 5;
1236 else
1237 str += 3;
1240 if (*str == '-' && *(str + 1) == '(')
1242 neg_p ^= 1;
1243 ++closes;
1244 str += 2;
1247 input_line_pointer = str;
1248 expression (exp);
1252 if (*input_line_pointer != ')')
1254 as_bad (_("`)' required"));
1255 break;
1257 input_line_pointer++;
1259 while (closes--);
1261 return neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
1266 input_line_pointer = tmp;
1267 expression (exp);
1269 /* Warn about expressions that fail to use lo8 (). */
1270 if (exp->X_op == O_constant)
1272 int x = exp->X_add_number;
1273 if (x < -255 || x > 255)
1274 as_warn (_("constant out of 8-bit range: %d"), x);
1277 return BFD_RELOC_AVR_LDI;
1280 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1281 `avr_cons_fix_new'. */
1282 static int exp_mod_pm = 0;
1284 /* Parse special CONS expression: pm (expression)
1285 which is used for addressing to a program memory.
1286 Relocation: BFD_RELOC_AVR_16_PM. */
1288 void
1289 avr_parse_cons_expression (exp, nbytes)
1290 expressionS *exp;
1291 int nbytes;
1293 char *tmp;
1295 exp_mod_pm = 0;
1297 tmp = input_line_pointer = skip_space (input_line_pointer);
1299 if (nbytes == 2)
1301 char *pm_name = "pm";
1302 int len = strlen (pm_name);
1304 if (strncasecmp (input_line_pointer, pm_name, len) == 0)
1306 input_line_pointer = skip_space (input_line_pointer + len);
1308 if (*input_line_pointer == '(')
1310 input_line_pointer = skip_space (input_line_pointer + 1);
1311 exp_mod_pm = 1;
1312 expression (exp);
1314 if (*input_line_pointer == ')')
1315 ++input_line_pointer;
1316 else
1318 as_bad (_("`)' required"));
1319 exp_mod_pm = 0;
1322 return;
1325 input_line_pointer = tmp;
1329 expression (exp);
1332 void
1333 avr_cons_fix_new (frag, where, nbytes, exp)
1334 fragS *frag;
1335 int where;
1336 int nbytes;
1337 expressionS *exp;
1339 if (exp_mod_pm == 0)
1341 if (nbytes == 2)
1342 fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_16);
1343 else if (nbytes == 4)
1344 fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_32);
1345 else
1346 as_bad (_("illegal %srelocation size: %d"), "", nbytes);
1348 else
1350 if (nbytes == 2)
1351 fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_AVR_16_PM);
1352 else
1353 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes);
1354 exp_mod_pm = 0;