1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright 1999, 2000, 2001, 2002 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. */
25 #include "safe-ctype.h"
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"
46 const char comment_chars
[] = ";";
47 const char line_comment_chars
[] = "#";
48 const char line_separator_chars
[] = "$";
50 const char *md_shortopts
= "m:";
58 /* XXX - devices that don't seem to exist (renamed, replaced with larger
59 ones, or planned but never produced), left here for compatibility.
60 TODO: hide them in show_mcu_list output? */
62 static struct mcu_type_s mcu_types
[] =
64 {"avr1", AVR_ISA_TINY1
, bfd_mach_avr1
},
65 {"avr2", AVR_ISA_2xxx
, bfd_mach_avr2
},
66 {"avr3", AVR_ISA_M103
, bfd_mach_avr3
},
67 {"avr4", AVR_ISA_M8
, bfd_mach_avr4
},
68 {"avr5", AVR_ISA_ALL
, bfd_mach_avr5
},
69 {"at90s1200", AVR_ISA_1200
, bfd_mach_avr1
},
70 {"attiny10", AVR_ISA_TINY1
, bfd_mach_avr1
}, /* XXX -> tn11 */
71 {"attiny11", AVR_ISA_TINY1
, bfd_mach_avr1
},
72 {"attiny12", AVR_ISA_TINY1
, bfd_mach_avr1
},
73 {"attiny15", AVR_ISA_TINY1
, bfd_mach_avr1
},
74 {"attiny28", AVR_ISA_TINY1
, bfd_mach_avr1
},
75 {"at90s2313", AVR_ISA_2xxx
, bfd_mach_avr2
},
76 {"at90s2323", AVR_ISA_2xxx
, bfd_mach_avr2
},
77 {"at90s2333", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 4433 */
78 {"at90s2343", AVR_ISA_2xxx
, bfd_mach_avr2
},
79 {"attiny22", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 2343 */
80 {"attiny26", AVR_ISA_2xxx
, bfd_mach_avr2
},
81 {"at90s4433", AVR_ISA_2xxx
, bfd_mach_avr2
},
82 {"at90s4414", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 8515 */
83 {"at90s4434", AVR_ISA_2xxx
, bfd_mach_avr2
}, /* XXX -> 8535 */
84 {"at90s8515", AVR_ISA_2xxx
, bfd_mach_avr2
},
85 {"at90s8535", AVR_ISA_2xxx
, bfd_mach_avr2
},
86 {"at90c8534", AVR_ISA_2xxx
, bfd_mach_avr2
},
87 {"at86rf401", AVR_ISA_2xxx
, bfd_mach_avr2
},
88 {"atmega603", AVR_ISA_M603
, bfd_mach_avr3
}, /* XXX -> m103 */
89 {"atmega103", AVR_ISA_M103
, bfd_mach_avr3
},
90 {"at43usb320",AVR_ISA_M103
, bfd_mach_avr3
},
91 {"at43usb355",AVR_ISA_M603
, bfd_mach_avr3
},
92 {"at76c711", AVR_ISA_M603
, bfd_mach_avr3
},
93 {"atmega48", AVR_ISA_M8
, bfd_mach_avr4
},
94 {"atmega8", AVR_ISA_M8
, bfd_mach_avr4
},
95 {"atmega83", AVR_ISA_M8
, bfd_mach_avr4
}, /* XXX -> m8535 */
96 {"atmega85", AVR_ISA_M8
, bfd_mach_avr4
}, /* XXX -> m8 */
97 {"atmega88", AVR_ISA_M8
, bfd_mach_avr4
},
98 {"atmega8515",AVR_ISA_M8
, bfd_mach_avr4
},
99 {"atmega8535",AVR_ISA_M8
, bfd_mach_avr4
},
100 {"attiny13", AVR_ISA_TINY2
, bfd_mach_avr4
},
101 {"attiny2313",AVR_ISA_TINY2
, bfd_mach_avr4
},
102 {"atmega16", AVR_ISA_M323
, bfd_mach_avr5
},
103 {"atmega161", AVR_ISA_M161
, bfd_mach_avr5
},
104 {"atmega162", AVR_ISA_M323
, bfd_mach_avr5
},
105 {"atmega163", AVR_ISA_M161
, bfd_mach_avr5
},
106 {"atmega165", AVR_ISA_M323
, bfd_mach_avr5
},
107 {"atmega168", AVR_ISA_M323
, bfd_mach_avr5
},
108 {"atmega169", AVR_ISA_M323
, bfd_mach_avr5
},
109 {"atmega32", AVR_ISA_M323
, bfd_mach_avr5
},
110 {"atmega323", AVR_ISA_M323
, bfd_mach_avr5
},
111 {"atmega325", AVR_ISA_M323
, bfd_mach_avr5
},
112 {"atmega3250",AVR_ISA_M323
, bfd_mach_avr5
},
113 {"atmega64", AVR_ISA_M323
, bfd_mach_avr5
},
114 {"atmega128", AVR_ISA_M128
, bfd_mach_avr5
},
115 {"atmega645", AVR_ISA_M323
, bfd_mach_avr5
},
116 {"atmega6450",AVR_ISA_M323
, bfd_mach_avr5
},
117 {"at90can128",AVR_ISA_M128
, bfd_mach_avr5
},
118 {"at94k", AVR_ISA_94K
, bfd_mach_avr5
},
122 /* Current MCU type. */
123 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_2xxx
,bfd_mach_avr2
};
124 static struct mcu_type_s
*avr_mcu
= &default_mcu
;
126 /* AVR target-specific switches. */
129 int all_opcodes
; /* -mall-opcodes: accept all known AVR opcodes */
130 int no_skip_bug
; /* -mno-skip-bug: no warnings for skipping 2-word insns */
131 int no_wrap
; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around */
134 static struct avr_opt_s avr_opt
= { 0, 0, 0 };
136 const char EXP_CHARS
[] = "eE";
137 const char FLT_CHARS
[] = "dD";
138 static void avr_set_arch (int dummy
);
140 /* The target specific pseudo-ops which we support. */
141 const pseudo_typeS md_pseudo_table
[] =
143 {"arch", avr_set_arch
, 0},
147 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
149 static void show_mcu_list
PARAMS ((FILE *));
150 static char *skip_space
PARAMS ((char *));
151 static char *extract_word
PARAMS ((char *, char *, int));
152 static unsigned int avr_operand
PARAMS ((struct avr_opcodes_s
*,
153 int, char *, char **));
154 static unsigned int avr_operands
PARAMS ((struct avr_opcodes_s
*, char **));
155 static unsigned int avr_get_constant
PARAMS ((char *, int));
156 static char *parse_exp
PARAMS ((char *, expressionS
*));
157 static bfd_reloc_code_real_type avr_ldi_expression
PARAMS ((expressionS
*));
159 #define EXP_MOD_NAME(i) exp_mod[i].name
160 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
161 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
162 #define HAVE_PM_P(i) exp_mod[i].have_pm
167 bfd_reloc_code_real_type reloc
;
168 bfd_reloc_code_real_type neg_reloc
;
172 static struct exp_mod_s exp_mod
[] =
174 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
175 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
176 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
177 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
178 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
179 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
180 {"hlo8", -BFD_RELOC_AVR_LO8_LDI
, -BFD_RELOC_AVR_LO8_LDI_NEG
, 0},
181 {"hhi8", -BFD_RELOC_AVR_HI8_LDI
, -BFD_RELOC_AVR_HI8_LDI_NEG
, 0},
184 /* Opcode hash table. */
185 static struct hash_control
*avr_hash
;
187 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
188 static struct hash_control
*avr_mod_hash
;
190 #define OPTION_MMCU 'm'
191 #define OPTION_ALL_OPCODES (OPTION_MD_BASE + 1)
192 #define OPTION_NO_SKIP_BUG (OPTION_MD_BASE + 2)
193 #define OPTION_NO_WRAP (OPTION_MD_BASE + 3)
195 struct option md_longopts
[] =
197 { "mmcu", required_argument
, NULL
, OPTION_MMCU
},
198 { "mall-opcodes", no_argument
, NULL
, OPTION_ALL_OPCODES
},
199 { "mno-skip-bug", no_argument
, NULL
, OPTION_NO_SKIP_BUG
},
200 { "mno-wrap", no_argument
, NULL
, OPTION_NO_WRAP
},
201 { NULL
, no_argument
, NULL
, 0 }
204 size_t md_longopts_size
= sizeof (md_longopts
);
206 /* Display nicely formatted list of known MCU names. */
209 show_mcu_list (stream
)
214 fprintf (stream
, _("Known MCU names:"));
217 for (i
= 0; mcu_types
[i
].name
; i
++)
219 int len
= strlen (mcu_types
[i
].name
);
224 fprintf (stream
, " %s", mcu_types
[i
].name
);
227 fprintf (stream
, "\n %s", mcu_types
[i
].name
);
232 fprintf (stream
, "\n");
239 while (*s
== ' ' || *s
== '\t')
244 /* Extract one word from FROM and copy it to TO. */
247 extract_word (char *from
, char *to
, int limit
)
253 /* Drop leading whitespace. */
254 from
= skip_space (from
);
257 /* Find the op code end. */
258 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name (*op_end
);)
260 to
[size
++] = *op_end
++;
261 if (size
+ 1 >= limit
)
270 md_estimate_size_before_relax (fragp
, seg
)
271 fragS
*fragp ATTRIBUTE_UNUSED
;
272 asection
*seg ATTRIBUTE_UNUSED
;
279 md_show_usage (stream
)
284 " -mmcu=[avr-name] select microcontroller variant\n"
285 " [avr-name] can be:\n"
286 " avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
287 " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
288 " avr3 - ATmega103, ATmega603\n"
289 " avr4 - ATmega83, ATmega85\n"
290 " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
291 " or immediate microcontroller name.\n"));
293 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
294 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
295 " (default for avr4, avr5)\n"
296 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
297 " (default for avr3, avr5)\n"));
298 show_mcu_list (stream
);
303 int dummy ATTRIBUTE_UNUSED
;
307 str
= (char *) alloca (20);
308 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
309 md_parse_option (OPTION_MMCU
, str
);
310 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
314 md_parse_option (c
, arg
)
323 char *s
= alloca (strlen (arg
) + 1);
330 *t
= TOLOWER (*arg1
++);
334 for (i
= 0; mcu_types
[i
].name
; ++i
)
335 if (strcmp (mcu_types
[i
].name
, s
) == 0)
338 if (!mcu_types
[i
].name
)
340 show_mcu_list (stderr
);
341 as_fatal (_("unknown MCU: %s\n"), arg
);
344 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
345 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
346 as .arch ... in the asm output at the same time. */
347 if (avr_mcu
== &default_mcu
|| avr_mcu
->mach
== mcu_types
[i
].mach
)
348 avr_mcu
= &mcu_types
[i
];
350 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
351 avr_mcu
->name
, mcu_types
[i
].name
);
354 case OPTION_ALL_OPCODES
:
355 avr_opt
.all_opcodes
= 1;
357 case OPTION_NO_SKIP_BUG
:
358 avr_opt
.no_skip_bug
= 1;
369 md_undefined_symbol (name
)
370 char *name ATTRIBUTE_UNUSED
;
375 /* Turn a string in input_line_pointer into a floating point constant
376 of type TYPE, and store the appropriate bytes in *LITP. The number
377 of LITTLENUMS emitted is stored in *SIZEP. An error message is
378 returned, or NULL on OK. */
381 md_atof (type
, litP
, sizeP
)
387 LITTLENUM_TYPE words
[4];
388 LITTLENUM_TYPE
*wordP
;
401 return _("bad call to md_atof");
404 t
= atof_ieee (input_line_pointer
, type
, words
);
406 input_line_pointer
= t
;
408 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
410 /* This loop outputs the LITTLENUMs in REVERSE order. */
411 for (wordP
= words
+ prec
- 1; prec
--;)
413 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
414 litP
+= sizeof (LITTLENUM_TYPE
);
421 md_convert_frag (abfd
, sec
, fragP
)
422 bfd
*abfd ATTRIBUTE_UNUSED
;
423 asection
*sec ATTRIBUTE_UNUSED
;
424 fragS
*fragP ATTRIBUTE_UNUSED
;
433 struct avr_opcodes_s
*opcode
;
434 avr_hash
= hash_new ();
436 /* Insert unique names into hash table. This hash table then provides a
437 quick index to the first opcode with a particular name in the opcode
439 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
440 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
442 avr_mod_hash
= hash_new ();
444 for (i
= 0; i
< sizeof (exp_mod
) / sizeof (exp_mod
[0]); ++i
)
445 hash_insert (avr_mod_hash
, EXP_MOD_NAME (i
), (void *) (i
+ 10));
447 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
450 /* Resolve STR as a constant expression and return the result.
451 If result greater than MAX then error. */
454 avr_get_constant (str
, max
)
459 str
= skip_space (str
);
460 input_line_pointer
= str
;
463 if (ex
.X_op
!= O_constant
)
464 as_bad (_("constant value required"));
466 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
467 as_bad (_("number must be less than %d"), max
+ 1);
469 return ex
.X_add_number
;
472 /* Parse instruction operands.
473 Return binary opcode. */
476 avr_operands (opcode
, line
)
477 struct avr_opcodes_s
*opcode
;
480 char *op
= opcode
->constraints
;
481 unsigned int bin
= opcode
->bin_opcode
;
482 char *frag
= frag_more (opcode
->insn_size
* 2);
484 int where
= frag
- frag_now
->fr_literal
;
485 static unsigned int prev
= 0; /* Previous opcode. */
487 /* Opcode have operands. */
490 unsigned int reg1
= 0;
491 unsigned int reg2
= 0;
492 int reg1_present
= 0;
493 int reg2_present
= 0;
495 /* Parse first operand. */
496 if (REGISTER_P (*op
))
498 reg1
= avr_operand (opcode
, where
, op
, &str
);
501 /* Parse second operand. */
514 if (REGISTER_P (*op
))
517 str
= skip_space (str
);
519 as_bad (_("`,' required"));
520 str
= skip_space (str
);
522 reg2
= avr_operand (opcode
, where
, op
, &str
);
526 if (reg1_present
&& reg2_present
)
527 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
528 else if (reg2_present
)
536 /* Detect undefined combinations (like ld r31,Z+). */
537 if (!avr_opt
.all_opcodes
&& AVR_UNDEF_P (bin
))
538 as_warn (_("undefined combination of operands"));
540 if (opcode
->insn_size
== 2)
542 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
543 (AVR core bug, fixed in the newer devices). */
545 if (!(avr_opt
.no_skip_bug
||
546 (avr_mcu
->isa
& (AVR_ISA_MUL
| AVR_ISA_MOVW
)))
547 && AVR_SKIP_P (prev
))
548 as_warn (_("skipping two-word instruction"));
550 bfd_putl32 ((bfd_vma
) bin
, frag
);
553 bfd_putl16 ((bfd_vma
) bin
, frag
);
560 /* Parse one instruction operand.
561 Return operand bitmask. Also fixups can be generated. */
564 avr_operand (opcode
, where
, op
, line
)
565 struct avr_opcodes_s
*opcode
;
571 unsigned int op_mask
= 0;
572 char *str
= skip_space (*line
);
576 /* Any register operand. */
582 if (*str
== 'r' || *str
== 'R')
586 str
= extract_word (str
, r_name
, sizeof (r_name
));
588 if (ISDIGIT (r_name
[1]))
590 if (r_name
[2] == '\0')
591 op_mask
= r_name
[1] - '0';
592 else if (r_name
[1] != '0'
593 && ISDIGIT (r_name
[2])
594 && r_name
[3] == '\0')
595 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
600 op_mask
= avr_get_constant (str
, 31);
601 str
= input_line_pointer
;
609 if (op_mask
< 16 || op_mask
> 23)
610 as_bad (_("register r16-r23 required"));
616 as_bad (_("register number above 15 required"));
622 as_bad (_("even register number required"));
627 if ((op_mask
& 1) || op_mask
< 24)
628 as_bad (_("register r24, r26, r28 or r30 required"));
629 op_mask
= (op_mask
- 24) >> 1;
634 as_bad (_("register name or number from 0 to 31 required"));
643 str
= skip_space (str
+ 1);
652 as_bad (_("pointer register (X, Y or Z) required"));
654 str
= skip_space (str
+ 1);
659 as_bad (_("cannot both predecrement and postincrement"));
663 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
664 registers, no predecrement, no postincrement. */
665 if (!avr_opt
.all_opcodes
&& (op_mask
& 0x100F)
666 && !(avr_mcu
->isa
& AVR_ISA_SRAM
))
667 as_bad (_("addressing mode not supported"));
673 as_bad (_("can't predecrement"));
675 if (! (*str
== 'z' || *str
== 'Z'))
676 as_bad (_("pointer register Z required"));
678 str
= skip_space (str
+ 1);
689 char c
= TOLOWER (*str
++);
694 as_bad (_("pointer register (Y or Z) required"));
695 str
= skip_space (str
);
699 x
= avr_get_constant (str
, 63);
700 str
= input_line_pointer
;
701 op_mask
|= (x
& 7) | ((x
& (3 << 3)) << 7) | ((x
& (1 << 5)) << 8);
707 str
= parse_exp (str
, &op_expr
);
708 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
709 &op_expr
, FALSE
, BFD_RELOC_AVR_CALL
);
713 str
= parse_exp (str
, &op_expr
);
714 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
715 &op_expr
, TRUE
, BFD_RELOC_AVR_13_PCREL
);
719 str
= parse_exp (str
, &op_expr
);
720 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
721 &op_expr
, TRUE
, BFD_RELOC_AVR_7_PCREL
);
725 str
= parse_exp (str
, &op_expr
);
726 fix_new_exp (frag_now
, where
+ 2, opcode
->insn_size
* 2,
727 &op_expr
, FALSE
, BFD_RELOC_16
);
732 bfd_reloc_code_real_type r_type
;
734 input_line_pointer
= str
;
735 r_type
= avr_ldi_expression (&op_expr
);
736 str
= input_line_pointer
;
737 fix_new_exp (frag_now
, where
, 3,
738 &op_expr
, FALSE
, r_type
);
746 x
= ~avr_get_constant (str
, 255);
747 str
= input_line_pointer
;
748 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
756 x
= avr_get_constant (str
, 63);
757 str
= input_line_pointer
;
758 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 2);
767 x
= avr_get_constant (str
, 7);
768 str
= input_line_pointer
;
779 x
= avr_get_constant (str
, 63);
780 str
= input_line_pointer
;
781 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
789 x
= avr_get_constant (str
, 31);
790 str
= input_line_pointer
;
799 as_bad (_("unknown constraint `%c'"), *op
);
806 /* GAS will call this function for each section at the end of the assembly,
807 to permit the CPU backend to adjust the alignment of a section. */
810 md_section_align (seg
, addr
)
814 int align
= bfd_get_section_alignment (stdoutput
, seg
);
815 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
818 /* If you define this macro, it should return the offset between the
819 address of a PC relative fixup and the position from which the PC
820 relative adjustment should be made. On many processors, the base
821 of a PC relative instruction is the next instruction, so this
822 macro would return the length of an instruction. */
825 md_pcrel_from_section (fixp
, sec
)
829 if (fixp
->fx_addsy
!= (symbolS
*) NULL
830 && (!S_IS_DEFINED (fixp
->fx_addsy
)
831 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
834 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
837 /* GAS will call this for each fixup. It should store the correct
838 value in the object file. */
841 md_apply_fix3 (fixP
, valP
, seg
)
846 unsigned char *where
;
850 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
853 else if (fixP
->fx_pcrel
)
855 segT s
= S_GET_SEGMENT (fixP
->fx_addsy
);
857 if (s
== seg
|| s
== absolute_section
)
859 value
+= S_GET_VALUE (fixP
->fx_addsy
);
864 /* We don't actually support subtracting a symbol. */
865 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
866 as_bad_where (fixP
->fx_file
, fixP
->fx_line
, _("expression too complex"));
868 switch (fixP
->fx_r_type
)
871 fixP
->fx_no_overflow
= 1;
873 case BFD_RELOC_AVR_7_PCREL
:
874 case BFD_RELOC_AVR_13_PCREL
:
877 case BFD_RELOC_AVR_CALL
:
883 /* Fetch the instruction, insert the fully resolved operand
884 value, and stuff the instruction back again. */
885 where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
886 insn
= bfd_getl16 (where
);
888 switch (fixP
->fx_r_type
)
890 case BFD_RELOC_AVR_7_PCREL
:
892 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
893 _("odd address operand: %ld"), value
);
895 /* Instruction addresses are always right-shifted by 1. */
897 --value
; /* Correct PC. */
899 if (value
< -64 || value
> 63)
900 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
901 _("operand out of range: %ld"), value
);
902 value
= (value
<< 3) & 0x3f8;
903 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
906 case BFD_RELOC_AVR_13_PCREL
:
908 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
909 _("odd address operand: %ld"), value
);
911 /* Instruction addresses are always right-shifted by 1. */
913 --value
; /* Correct PC. */
915 if (value
< -2048 || value
> 2047)
917 /* No wrap for devices with >8K of program memory. */
918 if ((avr_mcu
->isa
& AVR_ISA_MEGA
) || avr_opt
.no_wrap
)
919 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
920 _("operand out of range: %ld"), value
);
924 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
928 bfd_putl16 ((bfd_vma
) value
, where
);
932 bfd_putl16 ((bfd_vma
) value
, where
);
935 case BFD_RELOC_AVR_16_PM
:
936 bfd_putl16 ((bfd_vma
) (value
>> 1), where
);
939 case BFD_RELOC_AVR_LO8_LDI
:
940 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
943 case -BFD_RELOC_AVR_LO8_LDI
:
944 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
947 case BFD_RELOC_AVR_HI8_LDI
:
948 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
951 case -BFD_RELOC_AVR_HI8_LDI
:
952 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
955 case BFD_RELOC_AVR_HH8_LDI
:
956 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
959 case BFD_RELOC_AVR_LO8_LDI_NEG
:
960 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
963 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
964 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
967 case BFD_RELOC_AVR_HI8_LDI_NEG
:
968 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
971 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
972 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
975 case BFD_RELOC_AVR_HH8_LDI_NEG
:
976 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
979 case BFD_RELOC_AVR_LO8_LDI_PM
:
980 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
983 case BFD_RELOC_AVR_HI8_LDI_PM
:
984 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
987 case BFD_RELOC_AVR_HH8_LDI_PM
:
988 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
991 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
992 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
995 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
996 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
999 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
1000 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
1003 case BFD_RELOC_AVR_CALL
:
1007 x
= bfd_getl16 (where
);
1009 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1010 _("odd address operand: %ld"), value
);
1012 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
1013 bfd_putl16 ((bfd_vma
) x
, where
);
1014 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+ 2);
1019 as_fatal (_("line %d: unknown relocation type: 0x%x"),
1020 fixP
->fx_line
, fixP
->fx_r_type
);
1026 switch (fixP
->fx_r_type
)
1028 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
1029 case -BFD_RELOC_AVR_HI8_LDI
:
1030 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
1031 case -BFD_RELOC_AVR_LO8_LDI
:
1032 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1033 _("only constant expression allowed"));
1042 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1043 will pass the resulting reloc to `bfd_install_relocation'. This
1044 currently works poorly, as `bfd_install_relocation' often does the
1045 wrong thing, and instances of `tc_gen_reloc' have been written to
1046 work around the problems, which in turns makes it difficult to fix
1047 `bfd_install_relocation'. */
1049 /* If while processing a fixup, a reloc really needs to be created
1050 then it is done here. */
1053 tc_gen_reloc (seg
, fixp
)
1054 asection
*seg ATTRIBUTE_UNUSED
;
1059 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1061 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1062 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1064 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1065 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1066 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
1068 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1069 _("reloc %d not supported by object file format"),
1070 (int) fixp
->fx_r_type
);
1074 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1075 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1076 reloc
->address
= fixp
->fx_offset
;
1078 reloc
->addend
= fixp
->fx_offset
;
1087 struct avr_opcodes_s
*opcode
;
1090 str
= skip_space (extract_word (str
, op
, sizeof (op
)));
1093 as_bad (_("can't find opcode "));
1095 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1099 as_bad (_("unknown opcode `%s'"), op
);
1103 /* Special case for opcodes with optional operands (lpm, elpm) -
1104 version with operands exists in avr_opcodes[] in the next entry. */
1106 if (*str
&& *opcode
->constraints
== '?')
1109 if (!avr_opt
.all_opcodes
&& (opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1110 as_bad (_("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1112 /* We used to set input_line_pointer to the result of get_operands,
1113 but that is wrong. Our caller assumes we don't change it. */
1115 char *t
= input_line_pointer
;
1116 avr_operands (opcode
, &str
);
1117 if (*skip_space (str
))
1118 as_bad (_("garbage at end of line"));
1119 input_line_pointer
= t
;
1123 /* Parse ordinary expression. */
1130 input_line_pointer
= s
;
1132 if (op
->X_op
== O_absent
)
1133 as_bad (_("missing operand"));
1134 return input_line_pointer
;
1137 /* Parse special expressions (needed for LDI command):
1142 where xx is: hh, hi, lo. */
1144 static bfd_reloc_code_real_type
1145 avr_ldi_expression (exp
)
1148 char *str
= input_line_pointer
;
1154 str
= extract_word (str
, op
, sizeof (op
));
1158 mod
= (int) hash_find (avr_mod_hash
, op
);
1165 str
= skip_space (str
);
1173 if (strncmp ("pm(", str
, 3) == 0
1174 || strncmp ("-(pm(", str
, 5) == 0)
1176 if (HAVE_PM_P (mod
))
1182 as_bad (_("illegal expression"));
1194 if (*str
== '-' && *(str
+ 1) == '(')
1201 input_line_pointer
= str
;
1206 if (*input_line_pointer
!= ')')
1208 as_bad (_("`)' required"));
1211 input_line_pointer
++;
1215 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1220 input_line_pointer
= tmp
;
1223 /* Warn about expressions that fail to use lo8 (). */
1224 if (exp
->X_op
== O_constant
)
1226 int x
= exp
->X_add_number
;
1227 if (x
< -255 || x
> 255)
1228 as_warn (_("constant out of 8-bit range: %d"), x
);
1231 as_warn (_("expression possibly out of 8-bit range"));
1233 return BFD_RELOC_AVR_LO8_LDI
;
1236 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1237 `avr_cons_fix_new'. */
1238 static int exp_mod_pm
= 0;
1240 /* Parse special CONS expression: pm (expression)
1241 which is used for addressing to a program memory.
1242 Relocation: BFD_RELOC_AVR_16_PM. */
1245 avr_parse_cons_expression (exp
, nbytes
)
1253 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1257 char *pm_name
= "pm";
1258 int len
= strlen (pm_name
);
1260 if (strncasecmp (input_line_pointer
, pm_name
, len
) == 0)
1262 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1264 if (*input_line_pointer
== '(')
1266 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1270 if (*input_line_pointer
== ')')
1271 ++input_line_pointer
;
1274 as_bad (_("`)' required"));
1281 input_line_pointer
= tmp
;
1289 avr_cons_fix_new (frag
, where
, nbytes
, exp
)
1295 if (exp_mod_pm
== 0)
1298 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_16
);
1299 else if (nbytes
== 4)
1300 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_32
);
1302 as_bad (_("illegal %srelocation size: %d"), "", nbytes
);
1307 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_AVR_16_PM
);
1309 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes
);