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. */
32 int insn_size; /* in words */
34 unsigned int bin_opcode;
37 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
38 {#NAME, CONSTR, SIZE, ISA, BIN},
40 struct avr_opcodes_s avr_opcodes[] =
42 #include "opcode/avr.h"
47 const char comment_chars[] = ";";
48 const char line_comment_chars[] = "#";
49 const char line_separator_chars[] = "$";
51 const char *md_shortopts = "m:";
59 static struct mcu_type_s mcu_types[] =
61 {"avr1", AVR_ISA_TINY1, bfd_mach_avr1},
62 {"avr2", AVR_ISA_2xxx, bfd_mach_avr2},
63 {"avr3", AVR_ISA_M103, bfd_mach_avr3},
64 {"avr4", AVR_ISA_M83, bfd_mach_avr4},
65 {"avr5", AVR_ISA_ALL, bfd_mach_avr5},
66 {"at90s1200", AVR_ISA_1200, bfd_mach_avr1},
67 {"attiny10", AVR_ISA_TINY1, bfd_mach_avr1},
68 {"attiny11", AVR_ISA_TINY1, bfd_mach_avr1},
69 {"attiny12", AVR_ISA_TINY1, bfd_mach_avr1},
70 {"attiny15", AVR_ISA_TINY1, bfd_mach_avr1},
71 {"attiny28", AVR_ISA_TINY1, bfd_mach_avr1},
72 {"at90s2313", AVR_ISA_2xxx, bfd_mach_avr2},
73 {"at90s2323", AVR_ISA_2xxx, bfd_mach_avr2},
74 {"at90s2333", AVR_ISA_2xxx, bfd_mach_avr2},
75 {"attiny22" , AVR_ISA_2xxx, bfd_mach_avr2},
76 {"at90s2343", AVR_ISA_2xxx, bfd_mach_avr2},
77 {"at90s4433", AVR_ISA_2xxx, bfd_mach_avr2},
78 {"at90s4414", AVR_ISA_2xxx, bfd_mach_avr2},
79 {"at90s4434", AVR_ISA_2xxx, bfd_mach_avr2},
80 {"at90s8515", AVR_ISA_2xxx, bfd_mach_avr2},
81 {"at90s8535", AVR_ISA_2xxx, bfd_mach_avr2},
82 {"at90c8534", AVR_ISA_2xxx, bfd_mach_avr2},
83 {"atmega603", AVR_ISA_M603, bfd_mach_avr3},
84 {"atmega103", AVR_ISA_M103, bfd_mach_avr3},
85 {"atmega83", AVR_ISA_M83, bfd_mach_avr4},
86 {"atmega85", AVR_ISA_M83, bfd_mach_avr4},
87 {"atmega161", AVR_ISA_M161, bfd_mach_avr5},
88 {"atmega163", AVR_ISA_M161, bfd_mach_avr5},
89 {"atmega32", AVR_ISA_M161, bfd_mach_avr5},
90 {"at94k", AVR_ISA_94K, bfd_mach_avr5},
95 /* Current MCU type. */
96 static struct mcu_type_s default_mcu = {"avr2", AVR_ISA_2xxx,bfd_mach_avr2};
97 static struct mcu_type_s *avr_mcu = &default_mcu;
99 /* AVR target-specific switches. */
102 int all_opcodes; /* -mall-opcodes: accept all known AVR opcodes */
103 int no_skip_bug; /* -mno-skip-bug: no warnings for skipping 2-word insns */
104 int no_wrap; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around */
107 static struct avr_opt_s avr_opt = { 0, 0, 0 };
109 const char EXP_CHARS[] = "eE";
110 const char FLT_CHARS[] = "dD";
111 static void avr_set_arch (int dummy);
113 /* The target specific pseudo-ops which we support. */
114 const pseudo_typeS md_pseudo_table[] =
116 {"arch", avr_set_arch, 0},
120 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
122 static void show_mcu_list (FILE *stream);
123 static char * skip_space (char * s);
124 static char * extract_word (char *from, char *to, int limit);
125 static unsigned int avr_operand (struct avr_opcodes_s *opcode,
126 int where, char *op, char **line);
127 static unsigned int avr_operands (struct avr_opcodes_s *opcode, char **line);
128 static unsigned int avr_get_constant (char * str, int max);
129 static char *parse_exp (char *s, expressionS * op);
130 static bfd_reloc_code_real_type avr_ldi_expression (expressionS *exp);
131 long md_pcrel_from_section PARAMS ((fixS *, segT));
134 #define EXP_MOD_NAME(i) exp_mod[i].name
135 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
136 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
137 #define HAVE_PM_P(i) exp_mod[i].have_pm
142 bfd_reloc_code_real_type reloc;
143 bfd_reloc_code_real_type neg_reloc;
147 static struct exp_mod_s exp_mod[] = {
148 {"hh8", BFD_RELOC_AVR_HH8_LDI, BFD_RELOC_AVR_HH8_LDI_NEG, 1},
149 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM, BFD_RELOC_AVR_HH8_LDI_PM_NEG, 0},
150 {"hi8", BFD_RELOC_AVR_HI8_LDI, BFD_RELOC_AVR_HI8_LDI_NEG, 1},
151 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM, BFD_RELOC_AVR_HI8_LDI_PM_NEG, 0},
152 {"lo8", BFD_RELOC_AVR_LO8_LDI, BFD_RELOC_AVR_LO8_LDI_NEG, 1},
153 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM, BFD_RELOC_AVR_LO8_LDI_PM_NEG, 0},
154 {"hlo8", -BFD_RELOC_AVR_LO8_LDI, -BFD_RELOC_AVR_LO8_LDI_NEG, 0},
155 {"hhi8", -BFD_RELOC_AVR_HI8_LDI, -BFD_RELOC_AVR_HI8_LDI_NEG, 0},
158 /* Opcode hash table. */
159 static struct hash_control *avr_hash;
161 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
162 static struct hash_control *avr_mod_hash;
164 #define OPTION_MMCU 'm'
165 #define OPTION_ALL_OPCODES (OPTION_MD_BASE + 1)
166 #define OPTION_NO_SKIP_BUG (OPTION_MD_BASE + 2)
167 #define OPTION_NO_WRAP (OPTION_MD_BASE + 3)
169 struct option md_longopts[] = {
170 { "mmcu", required_argument, NULL, OPTION_MMCU },
171 { "mall-opcodes", no_argument, NULL, OPTION_ALL_OPCODES },
172 { "mno-skip-bug", no_argument, NULL, OPTION_NO_SKIP_BUG },
173 { "mno-wrap", no_argument, NULL, OPTION_NO_WRAP },
174 { NULL, no_argument, NULL, 0 }
176 size_t md_longopts_size = sizeof(md_longopts);
179 /* Display nicely formatted list of known MCU names. */
181 show_mcu_list (FILE *stream)
185 fprintf (stream, _("Known MCU names:"));
187 for (i = 0; mcu_types[i].name; i++)
189 int len = strlen (mcu_types[i].name);
193 fprintf (stream, " %s", mcu_types[i].name);
197 fprintf (stream, "\n %s", mcu_types[i].name);
201 fprintf (stream, "\n");
209 while (*s == ' ' || *s == '\t')
214 /* Extract one word from FROM and copy it to TO. */
216 extract_word (char *from, char *to, int limit)
222 /* Drop leading whitespace. */
223 from = skip_space (from);
225 /* Find the op code end. */
226 for (op_start = op_end = from; *op_end != 0 && is_part_of_name(*op_end); )
228 to[size++] = *op_end++;
229 if (size + 1 >= limit)
237 md_estimate_size_before_relax (fragp, seg)
238 fragS *fragp ATTRIBUTE_UNUSED;
239 asection *seg ATTRIBUTE_UNUSED;
246 md_show_usage (stream)
251 " -mmcu=[avr-name] select microcontroller variant\n"
252 " [avr-name] can be:\n"
253 " avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
254 " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
255 " avr3 - ATmega103, ATmega603\n"
256 " avr4 - ATmega83, ATmega85\n"
257 " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
258 " or immediate microcontroller name.\n"));
260 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
261 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
262 " (default for avr4, avr5)\n"
263 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
264 " (default for avr3, avr5)\n"));
265 show_mcu_list (stream);
270 int dummy ATTRIBUTE_UNUSED;
273 str = (char *)alloca (20);
274 input_line_pointer = extract_word (input_line_pointer, str, 20);
275 md_parse_option (OPTION_MMCU, str);
276 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
280 md_parse_option (c, arg)
289 char *s = alloca (strlen (arg) + 1);
296 *t = tolower(*arg1++);
300 for (i = 0; mcu_types[i].name; ++i)
301 if (strcmp (mcu_types[i].name, s) == 0)
304 if (!mcu_types[i].name)
306 show_mcu_list (stderr);
307 as_fatal (_("unknown MCU: %s\n"), arg);
310 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
311 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
312 as .arch ... in the asm output at the same time. */
314 if (avr_mcu == &default_mcu || avr_mcu->mach == mcu_types[i].mach)
315 avr_mcu = &mcu_types[i];
317 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
318 avr_mcu->name, mcu_types[i].name);
321 case OPTION_ALL_OPCODES:
322 avr_opt.all_opcodes = 1;
324 case OPTION_NO_SKIP_BUG:
325 avr_opt.no_skip_bug = 1;
335 md_undefined_symbol(name)
336 char *name ATTRIBUTE_UNUSED;
341 /* Convert a string pointed to by input_line_pointer into a floating point
342 constant of type `type', and store the appropriate bytes to `*litP'.
343 The number of LITTLENUMS emitted is stored in `*sizeP'. Returns NULL if
344 OK, or an error message otherwise. */
346 md_atof (type, litP, sizeP)
352 LITTLENUM_TYPE words[4];
353 LITTLENUM_TYPE *wordP;
366 return _("bad call to md_atof");
369 t = atof_ieee (input_line_pointer, type, words);
371 input_line_pointer = t;
373 *sizeP = prec * sizeof (LITTLENUM_TYPE);
374 /* This loop outputs the LITTLENUMs in REVERSE order. */
375 for (wordP = words + prec - 1; prec--;)
377 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
378 litP += sizeof (LITTLENUM_TYPE);
384 md_convert_frag (abfd, sec, fragP)
385 bfd *abfd ATTRIBUTE_UNUSED;
386 asection *sec ATTRIBUTE_UNUSED;
387 fragS *fragP ATTRIBUTE_UNUSED;
397 struct avr_opcodes_s *opcode;
398 avr_hash = hash_new();
400 /* Insert unique names into hash table. This hash table then provides a
401 quick index to the first opcode with a particular name in the opcode
404 for (opcode = avr_opcodes; opcode->name; opcode++)
405 hash_insert (avr_hash, opcode->name, (char *) opcode);
407 avr_mod_hash = hash_new ();
409 for (i = 0; i < sizeof (exp_mod) / sizeof (exp_mod[0]); ++i)
410 hash_insert (avr_mod_hash, EXP_MOD_NAME(i), (void*)(i+10));
412 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
416 /* Resolve STR as a constant expression and return the result.
417 If result greater than MAX then error. */
420 avr_get_constant (str, max)
425 str = skip_space (str);
426 input_line_pointer = str;
429 if (ex.X_op != O_constant)
430 as_bad (_("constant value required"));
432 if (ex.X_add_number > max || ex.X_add_number < 0)
433 as_bad (_("number must be less than %d"), max+1);
434 return ex.X_add_number;
438 /* Parse instruction operands.
439 Returns binary opcode. */
442 avr_operands (opcode, line)
443 struct avr_opcodes_s *opcode;
446 char *op = opcode->constraints;
447 unsigned int bin = opcode->bin_opcode;
448 char *frag = frag_more (opcode->insn_size * 2);
450 int where = frag - frag_now->fr_literal;
451 static unsigned int prev = 0; /* previous opcode */
453 /* Opcode have operands. */
456 unsigned int reg1 = 0;
457 unsigned int reg2 = 0;
458 int reg1_present = 0;
459 int reg2_present = 0;
461 /* Parse first operand. */
462 if (REGISTER_P (*op))
464 reg1 = avr_operand (opcode, where, op, &str);
467 /* Parse second operand. */
479 if (REGISTER_P (*op))
482 str = skip_space (str);
484 as_bad (_("`,' required"));
485 str = skip_space (str);
487 reg2 = avr_operand (opcode, where, op, &str);
490 if (reg1_present && reg2_present)
491 reg2 = (reg2 & 0xf) | ((reg2 << 5) & 0x200);
492 else if (reg2_present)
500 if (!avr_opt.all_opcodes)
502 /* Detect undefined combinations (like ld r31,Z+). */
503 if (((bin & 0xFDEF) == 0x91AD) || ((bin & 0xFDEF) == 0x91AE) ||
504 ((bin & 0xFDEF) == 0x91C9) || ((bin & 0xFDEF) == 0x91CA) ||
505 ((bin & 0xFDEF) == 0x91E1) || ((bin & 0xFDEF) == 0x91E2) ||
506 ((bin & 0xFFED) == 0x91E5))
507 as_warn (_("undefined combination of operands"));
510 if (opcode->insn_size == 2)
512 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
513 (AVR core bug, fixed in the newer devices). */
515 if (!((avr_mcu->isa & AVR_ISA_MUL) || avr_opt.no_skip_bug))
517 if ((prev & 0xFC00) == 0x1000
518 || (prev & 0xFD00) == 0x9900
519 || (prev & 0xFC08) == 0xFC00)
520 as_warn (_("skipping two-word instruction"));
523 bfd_putl32 ((bfd_vma)bin, frag);
526 bfd_putl16 ((bfd_vma)bin, frag);
534 /* Parse one instruction operand.
535 Returns operand bitmask. Also fixups can be generated. */
538 avr_operand (opcode, where, op, line)
539 struct avr_opcodes_s *opcode;
545 unsigned int op_mask = 0;
546 char *str = skip_space (*line);
550 /* Any register operand. */
559 if (*str == 'r' || *str == 'R')
563 str = extract_word (str, r_name, sizeof (r_name));
564 if (isdigit(r_name[1]))
566 if (r_name[2] == '\0')
567 op_mask = r_name[1] - '0';
568 else if (r_name[1] != '0'
569 && isdigit(r_name[2])
570 && r_name[3] == '\0')
571 op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0';
576 op_mask = avr_get_constant (str, 31);
577 str = input_line_pointer;
585 if (op_mask < 16 || op_mask > 23)
586 as_bad (_("register r16-r23 required"));
592 as_bad (_("register number above 15 required"));
598 as_bad (_("even register number required"));
604 if (op_mask & 1 || op_mask > 6)
605 as_bad (_("register r24, r26, r28 or r30 required"));
611 as_bad (_("register name or number from 0 to 31 required"));
620 str = skip_space (str+1);
629 as_bad (_("pointer register (X, Y or Z) required"));
631 str = skip_space (str+1);
636 as_bad (_("cannot both predecrement and postincrement"));
640 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
641 registers, no predecrement, no postincrement. */
643 if (!avr_opt.all_opcodes && (op_mask & 0x100F)
644 && !(avr_mcu->isa & AVR_ISA_SRAM))
645 as_bad (_("addressing mode not supported"));
652 as_bad (_("can't predecrement"));
654 if (! (*str == 'z' || *str == 'Z'))
655 as_bad (_("pointer register Z required"));
657 str = skip_space (str + 1);
668 char c = tolower (*str++);
672 as_bad (_("pointer register (Y or Z) required"));
673 str = skip_space (str);
677 x = avr_get_constant (str, 63);
678 str = input_line_pointer;
679 op_mask |= (x & 7) | ((x & (3 << 3)) << 7) | ((x & (1 << 5)) << 8);
686 str = parse_exp (str, &op_expr);
687 fix_new_exp (frag_now, where, opcode->insn_size * 2,
688 &op_expr, false, BFD_RELOC_AVR_CALL);
695 str = parse_exp (str, &op_expr);
696 fix_new_exp (frag_now, where, opcode->insn_size * 2,
697 &op_expr, true, BFD_RELOC_AVR_13_PCREL);
704 str = parse_exp (str, &op_expr);
705 fix_new_exp (frag_now, where, opcode->insn_size * 2,
706 &op_expr, true, BFD_RELOC_AVR_7_PCREL);
713 str = parse_exp (str, &op_expr);
714 fix_new_exp (frag_now, where+2, opcode->insn_size * 2,
715 &op_expr, false, BFD_RELOC_16);
722 bfd_reloc_code_real_type r_type;
723 input_line_pointer = str;
724 r_type = avr_ldi_expression (&op_expr);
725 str = input_line_pointer;
726 fix_new_exp (frag_now, where, 3,
727 &op_expr, false, r_type);
734 x = ~avr_get_constant (str, 255);
735 str = input_line_pointer;
736 op_mask |= (x & 0xf) | ((x << 4) & 0xf00);
743 x = avr_get_constant (str, 63);
744 str = input_line_pointer;
745 op_mask |= (x & 0xf) | ((x & 0x30) << 2);
753 x = avr_get_constant (str, 7);
754 str = input_line_pointer;
764 x = avr_get_constant (str, 63);
765 str = input_line_pointer;
766 op_mask |= (x & 0xf) | ((x & 0x30) << 5);
773 x = avr_get_constant (str, 31);
774 str = input_line_pointer;
781 as_bad (_("unknown constraint `%c'"), *op);
787 /* GAS will call this function for each section at the end of the assembly,
788 to permit the CPU backend to adjust the alignment of a section. */
790 md_section_align (seg, addr)
794 int align = bfd_get_section_alignment (stdoutput, seg);
795 return ((addr + (1 << align) - 1) & (-1 << align));
798 /* If you define this macro, it should return the offset between the
799 address of a PC relative fixup and the position from which the PC
800 relative adjustment should be made. On many processors, the base
801 of a PC relative instruction is the next instruction, so this
802 macro would return the length of an instruction. */
804 md_pcrel_from_section (fixp, sec)
808 if (fixp->fx_addsy != (symbolS *)NULL
809 && (!S_IS_DEFINED (fixp->fx_addsy)
810 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
812 return fixp->fx_frag->fr_address + fixp->fx_where;
815 /* GAS will call this for each fixup. It should store the correct
816 value in the object file. */
818 md_apply_fix3 (fixp, valuep, seg)
823 unsigned char *where;
827 if (fixp->fx_addsy == (symbolS *) NULL)
832 else if (fixp->fx_pcrel)
834 segT s = S_GET_SEGMENT (fixp->fx_addsy);
835 if (fixp->fx_addsy && (s == seg || s == absolute_section))
837 value = S_GET_VALUE (fixp->fx_addsy) + *valuep;
845 value = fixp->fx_offset;
846 if (fixp->fx_subsy != (symbolS *) NULL)
848 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
850 value -= S_GET_VALUE (fixp->fx_subsy);
855 /* We don't actually support subtracting a symbol. */
856 as_bad_where (fixp->fx_file, fixp->fx_line,
857 _("expression too complex"));
861 switch (fixp->fx_r_type)
864 fixp->fx_no_overflow = 1;
866 case BFD_RELOC_AVR_7_PCREL:
867 case BFD_RELOC_AVR_13_PCREL:
870 case BFD_RELOC_AVR_CALL:
876 /* Fetch the instruction, insert the fully resolved operand
877 value, and stuff the instruction back again. */
878 where = fixp->fx_frag->fr_literal + fixp->fx_where;
879 insn = bfd_getl16 (where);
881 switch (fixp->fx_r_type)
883 case BFD_RELOC_AVR_7_PCREL:
885 as_bad_where (fixp->fx_file, fixp->fx_line,
886 _("odd address operand: %ld"), value);
887 /* Instruction addresses are always right-shifted by 1. */
889 --value; /* Correct PC. */
890 if (value < -64 || value > 63)
891 as_bad_where (fixp->fx_file, fixp->fx_line,
892 _("operand out of range: %ld"), value);
893 value = (value << 3) & 0x3f8;
894 bfd_putl16 ((bfd_vma) (value | insn), where);
897 case BFD_RELOC_AVR_13_PCREL:
899 as_bad_where (fixp->fx_file, fixp->fx_line,
900 _("odd address operand: %ld"), value);
901 /* Instruction addresses are always right-shifted by 1. */
903 --value; /* Correct PC. */
905 if (value < -2048 || value > 2047)
907 /* No wrap for devices with >8K of program memory. */
908 if ((avr_mcu->isa & AVR_ISA_MEGA) || avr_opt.no_wrap)
909 as_bad_where (fixp->fx_file, fixp->fx_line,
910 _("operand out of range: %ld"), value);
914 bfd_putl16 ((bfd_vma) (value | insn), where);
918 bfd_putl16 ((bfd_vma) value, where);
922 bfd_putl16 ((bfd_vma) value, where);
925 case BFD_RELOC_AVR_16_PM:
926 bfd_putl16 ((bfd_vma) (value>>1), where);
929 case BFD_RELOC_AVR_LO8_LDI:
930 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
933 case -BFD_RELOC_AVR_LO8_LDI:
934 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
937 case BFD_RELOC_AVR_HI8_LDI:
938 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where);
941 case -BFD_RELOC_AVR_HI8_LDI:
942 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where);
945 case BFD_RELOC_AVR_HH8_LDI:
946 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
949 case BFD_RELOC_AVR_LO8_LDI_NEG:
950 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where);
953 case -BFD_RELOC_AVR_LO8_LDI_NEG:
954 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
957 case BFD_RELOC_AVR_HI8_LDI_NEG:
958 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where);
961 case -BFD_RELOC_AVR_HI8_LDI_NEG:
962 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where);
965 case BFD_RELOC_AVR_HH8_LDI_NEG:
966 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
969 case BFD_RELOC_AVR_LO8_LDI_PM:
970 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where);
973 case BFD_RELOC_AVR_HI8_LDI_PM:
974 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where);
977 case BFD_RELOC_AVR_HH8_LDI_PM:
978 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where);
981 case BFD_RELOC_AVR_LO8_LDI_PM_NEG:
982 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where);
985 case BFD_RELOC_AVR_HI8_LDI_PM_NEG:
986 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where);
989 case BFD_RELOC_AVR_HH8_LDI_PM_NEG:
990 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where);
993 case BFD_RELOC_AVR_CALL:
996 x = bfd_getl16 (where);
998 as_bad_where (fixp->fx_file, fixp->fx_line,
999 _("odd address operand: %ld"), value);
1001 x |= ((value & 0x10000) | ((value << 3) & 0x1f00000)) >> 16;
1002 bfd_putl16 ((bfd_vma) x, where);
1003 bfd_putl16 ((bfd_vma) (value & 0xffff), where+2);
1008 as_fatal ( _("line %d: unknown relocation type: 0x%x"),
1009 fixp->fx_line, fixp->fx_r_type);
1015 switch (fixp->fx_r_type)
1017 case -BFD_RELOC_AVR_HI8_LDI_NEG:
1018 case -BFD_RELOC_AVR_HI8_LDI:
1019 case -BFD_RELOC_AVR_LO8_LDI_NEG:
1020 case -BFD_RELOC_AVR_LO8_LDI:
1021 as_bad_where (fixp->fx_file, fixp->fx_line,
1022 _("only constant expression allowed"));
1028 fixp->fx_addnumber = value;
1034 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1035 will pass the resulting reloc to `bfd_install_relocation'. This
1036 currently works poorly, as `bfd_install_relocation' often does the
1037 wrong thing, and instances of `tc_gen_reloc' have been written to
1038 work around the problems, which in turns makes it difficult to fix
1039 `bfd_install_relocation'. */
1041 /* If while processing a fixup, a reloc really needs to be created
1042 then it is done here. */
1045 tc_gen_reloc (seg, fixp)
1046 asection *seg ATTRIBUTE_UNUSED;
1051 reloc = (arelent *) xmalloc (sizeof (arelent));
1053 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1054 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1056 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1057 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1058 if (reloc->howto == (reloc_howto_type *) NULL)
1060 as_bad_where (fixp->fx_file, fixp->fx_line,
1061 _("reloc %d not supported by object file format"),
1062 (int)fixp->fx_r_type);
1066 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1067 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1068 reloc->address = fixp->fx_offset;
1070 reloc->addend = fixp->fx_offset;
1080 struct avr_opcodes_s * opcode;
1083 str = skip_space (extract_word (str, op, sizeof(op)));
1086 as_bad (_("can't find opcode "));
1088 opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
1092 as_bad (_("unknown opcode `%s'"), op);
1096 /* Special case for opcodes with optional operands (lpm, elpm) -
1097 version with operands exists in avr_opcodes[] in the next entry. */
1099 if (*str && *opcode->constraints == '?')
1102 if (!avr_opt.all_opcodes && (opcode->isa & avr_mcu->isa) != opcode->isa)
1103 as_bad (_("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name);
1105 /* We used to set input_line_pointer to the result of get_operands,
1106 but that is wrong. Our caller assumes we don't change it. */
1108 char *t = input_line_pointer;
1109 avr_operands (opcode, &str);
1110 if (*skip_space (str))
1111 as_bad (_("garbage at end of line"));
1112 input_line_pointer = t;
1116 /* Parse ordinary expression. */
1122 input_line_pointer = s;
1124 if (op->X_op == O_absent)
1125 as_bad (_("missing operand"));
1126 return input_line_pointer;
1130 /* Parse special expressions (needed for LDI command):
1135 where xx is: hh, hi, lo
1137 static bfd_reloc_code_real_type
1138 avr_ldi_expression (exp)
1141 char *str = input_line_pointer;
1147 str = extract_word (str, op, sizeof (op));
1150 mod = (int) hash_find (avr_mod_hash, op);
1155 str = skip_space (str);
1160 if (strncmp ("pm(", str, 3) == 0
1161 || strncmp ("-(pm(", str, 5) == 0)
1169 as_bad (_("illegal expression"));
1179 if (*str == '-' && *(str + 1) == '(')
1185 input_line_pointer = str;
1189 if (*input_line_pointer != ')')
1191 as_bad (_("`)' required"));
1194 input_line_pointer++;
1197 return neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
1201 input_line_pointer = tmp;
1204 /* Warn about expressions that fail to use lo8(). */
1205 if (exp->X_op == O_constant)
1207 int x = exp->X_add_number;
1208 if (x < -255 || x > 255)
1209 as_warn (_("constant out of 8-bit range: %d"), x);
1212 as_warn (_("expression possibly out of 8-bit range"));
1214 return BFD_RELOC_AVR_LO8_LDI;
1217 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1218 `avr_cons_fix_new' */
1219 static int exp_mod_pm = 0;
1221 /* Parse special CONS expression: pm (expression)
1222 which is used for addressing to a program memory.
1223 Relocation: BFD_RELOC_AVR_16_PM */
1225 avr_parse_cons_expression (exp, nbytes)
1233 tmp = input_line_pointer = skip_space (input_line_pointer);
1237 char * pm_name = "pm";
1238 int len = strlen (pm_name);
1239 if (strncasecmp (input_line_pointer, pm_name, len) == 0)
1241 input_line_pointer = skip_space (input_line_pointer + len);
1242 if (*input_line_pointer == '(')
1244 input_line_pointer = skip_space (input_line_pointer + 1);
1247 if (*input_line_pointer == ')')
1248 ++input_line_pointer;
1251 as_bad (_("`)' required"));
1256 input_line_pointer = tmp;
1263 avr_cons_fix_new(frag, where, nbytes, exp)
1269 if (exp_mod_pm == 0)
1272 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_16);
1273 else if (nbytes == 4)
1274 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_32);
1276 as_bad (_("illegal %srelocation size: %d"), "", nbytes);
1281 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_AVR_16_PM);
1283 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes);