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)
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, 51 Franklin Street - Fifth Floor,
22 Boston, MA 02110-1301, USA. */
26 #include "safe-ctype.h"
33 int insn_size
; /* In words. */
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"
47 const char comment_chars
[] = ";";
48 const char line_comment_chars
[] = "#";
49 const char line_separator_chars
[] = "$";
51 const char *md_shortopts
= "m:";
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
},
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. */
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},
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
168 bfd_reloc_code_real_type reloc
;
169 bfd_reloc_code_real_type neg_reloc
;
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. */
210 show_mcu_list (stream
)
215 fprintf (stream
, _("Known MCU names:"));
218 for (i
= 0; mcu_types
[i
].name
; i
++)
220 int len
= strlen (mcu_types
[i
].name
);
225 fprintf (stream
, " %s", mcu_types
[i
].name
);
228 fprintf (stream
, "\n %s", mcu_types
[i
].name
);
233 fprintf (stream
, "\n");
240 while (*s
== ' ' || *s
== '\t')
245 /* Extract one word from FROM and copy it to TO. */
248 extract_word (char *from
, char *to
, int limit
)
254 /* Drop leading whitespace. */
255 from
= skip_space (from
);
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
)
271 md_estimate_size_before_relax (fragp
, seg
)
272 fragS
*fragp ATTRIBUTE_UNUSED
;
273 asection
*seg ATTRIBUTE_UNUSED
;
280 md_show_usage (stream
)
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"));
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
);
304 int dummy ATTRIBUTE_UNUSED
;
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
)
324 char *s
= alloca (strlen (arg
) + 1);
331 *t
= TOLOWER (*arg1
++);
335 for (i
= 0; mcu_types
[i
].name
; ++i
)
336 if (strcmp (mcu_types
[i
].name
, s
) == 0)
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
];
351 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
352 avr_mcu
->name
, mcu_types
[i
].name
);
355 case OPTION_ALL_OPCODES
:
356 avr_opt
.all_opcodes
= 1;
358 case OPTION_NO_SKIP_BUG
:
359 avr_opt
.no_skip_bug
= 1;
370 md_undefined_symbol (name
)
371 char *name ATTRIBUTE_UNUSED
;
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. */
382 md_atof (type
, litP
, sizeP
)
388 LITTLENUM_TYPE words
[4];
389 LITTLENUM_TYPE
*wordP
;
402 return _("bad call to md_atof");
405 t
= atof_ieee (input_line_pointer
, type
, words
);
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
);
422 md_convert_frag (abfd
, sec
, fragP
)
423 bfd
*abfd ATTRIBUTE_UNUSED
;
424 asection
*sec ATTRIBUTE_UNUSED
;
425 fragS
*fragP ATTRIBUTE_UNUSED
;
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
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. */
455 avr_get_constant (str
, max
)
460 str
= skip_space (str
);
461 input_line_pointer
= str
;
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. */
477 avr_operands (opcode
, line
)
478 struct avr_opcodes_s
*opcode
;
481 char *op
= opcode
->constraints
;
482 unsigned int bin
= opcode
->bin_opcode
;
483 char *frag
= frag_more (opcode
->insn_size
* 2);
485 int where
= frag
- frag_now
->fr_literal
;
486 static unsigned int prev
= 0; /* Previous opcode. */
488 /* Opcode have operands. */
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
))
499 reg1
= avr_operand (opcode
, where
, op
, &str
);
502 /* Parse second operand. */
515 if (REGISTER_P (*op
))
518 str
= skip_space (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
)
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
);
554 bfd_putl16 ((bfd_vma
) bin
, frag
);
561 /* Parse for ldd/std offset */
564 avr_offset_expression (expressionS
*exp
)
566 char *str
= input_line_pointer
;
571 str
= extract_word (str
, op
, sizeof (op
));
573 input_line_pointer
= tmp
;
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. */
590 avr_operand (opcode
, where
, op
, line
)
591 struct avr_opcodes_s
*opcode
;
597 unsigned int op_mask
= 0;
598 char *str
= skip_space (*line
);
602 /* Any register operand. */
608 if (*str
== 'r' || *str
== 'R')
612 str
= extract_word (str
, r_name
, sizeof (r_name
));
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';
626 op_mask
= avr_get_constant (str
, 31);
627 str
= input_line_pointer
;
635 if (op_mask
< 16 || op_mask
> 23)
636 as_bad (_("register r16-r23 required"));
642 as_bad (_("register number above 15 required"));
648 as_bad (_("even register number required"));
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;
660 as_bad (_("register name or number from 0 to 31 required"));
669 str
= skip_space (str
+ 1);
678 as_bad (_("pointer register (X, Y or Z) required"));
680 str
= skip_space (str
+ 1);
685 as_bad (_("cannot both predecrement and postincrement"));
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"));
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);
715 char c
= TOLOWER (*str
++);
720 as_bad (_("pointer register (Y or Z) required"));
721 str
= skip_space (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
);
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
);
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
);
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
);
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
);
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
);
773 x
= ~avr_get_constant (str
, 255);
774 str
= input_line_pointer
;
775 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
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
);
792 x
= avr_get_constant (str
, 7);
793 str
= input_line_pointer
;
804 x
= avr_get_constant (str
, 63);
805 str
= input_line_pointer
;
806 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
814 x
= avr_get_constant (str
, 31);
815 str
= input_line_pointer
;
824 as_bad (_("unknown constraint `%c'"), *op
);
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. */
835 md_section_align (seg
, 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. */
850 md_pcrel_from_section (fixp
, sec
)
854 if (fixp
->fx_addsy
!= (symbolS
*) NULL
855 && (!S_IS_DEFINED (fixp
->fx_addsy
)
856 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
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. */
866 md_apply_fix3 (fixP
, valP
, seg
)
871 unsigned char *where
;
875 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
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
);
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
)
896 fixP
->fx_no_overflow
= 1;
898 case BFD_RELOC_AVR_7_PCREL
:
899 case BFD_RELOC_AVR_13_PCREL
:
902 case BFD_RELOC_AVR_CALL
:
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
:
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. */
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
);
931 case BFD_RELOC_AVR_13_PCREL
:
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. */
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
);
949 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
953 bfd_putl16 ((bfd_vma
) value
, where
);
957 bfd_putl16 ((bfd_vma
) value
, where
);
960 case BFD_RELOC_AVR_16_PM
:
961 bfd_putl16 ((bfd_vma
) (value
>> 1), where
);
964 case BFD_RELOC_AVR_LDI
:
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
);
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
);
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
);
985 case BFD_RELOC_AVR_LO8_LDI
:
986 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
989 case -BFD_RELOC_AVR_LO8_LDI
:
990 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
993 case BFD_RELOC_AVR_HI8_LDI
:
994 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
997 case -BFD_RELOC_AVR_HI8_LDI
:
998 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
1001 case BFD_RELOC_AVR_HH8_LDI
:
1002 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
1005 case BFD_RELOC_AVR_LO8_LDI_NEG
:
1006 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
1009 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
1010 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
1013 case BFD_RELOC_AVR_HI8_LDI_NEG
:
1014 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
1017 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
1018 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
1021 case BFD_RELOC_AVR_HH8_LDI_NEG
:
1022 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
1025 case BFD_RELOC_AVR_LO8_LDI_PM
:
1026 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
1029 case BFD_RELOC_AVR_HI8_LDI_PM
:
1030 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
1033 case BFD_RELOC_AVR_HH8_LDI_PM
:
1034 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
1037 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
1038 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
1041 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
1042 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
1045 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
1046 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
1049 case BFD_RELOC_AVR_CALL
:
1053 x
= bfd_getl16 (where
);
1055 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1056 _("odd address operand: %ld"), value
);
1058 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
1059 bfd_putl16 ((bfd_vma
) x
, where
);
1060 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+ 2);
1065 as_fatal (_("line %d: unknown relocation type: 0x%x"),
1066 fixP
->fx_line
, fixP
->fx_r_type
);
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"));
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. */
1099 tc_gen_reloc (seg
, fixp
)
1100 asection
*seg ATTRIBUTE_UNUSED
;
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
);
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
;
1133 struct avr_opcodes_s
*opcode
;
1136 str
= skip_space (extract_word (str
, op
, sizeof (op
)));
1139 as_bad (_("can't find opcode "));
1141 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1145 as_bad (_("unknown opcode `%s'"), op
);
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
== '?')
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. */
1176 input_line_pointer
= s
;
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):
1188 where xx is: hh, hi, lo. */
1190 static bfd_reloc_code_real_type
1191 avr_ldi_expression (exp
)
1194 char *str
= input_line_pointer
;
1200 str
= extract_word (str
, op
, sizeof (op
));
1204 mod
= (int) hash_find (avr_mod_hash
, op
);
1211 str
= skip_space (str
);
1219 if (strncmp ("pm(", str
, 3) == 0
1220 || strncmp ("-(pm(", str
, 5) == 0)
1222 if (HAVE_PM_P (mod
))
1228 as_bad (_("illegal expression"));
1240 if (*str
== '-' && *(str
+ 1) == '(')
1247 input_line_pointer
= str
;
1252 if (*input_line_pointer
!= ')')
1254 as_bad (_("`)' required"));
1257 input_line_pointer
++;
1261 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1266 input_line_pointer
= tmp
;
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. */
1289 avr_parse_cons_expression (exp
, nbytes
)
1297 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
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);
1314 if (*input_line_pointer
== ')')
1315 ++input_line_pointer
;
1318 as_bad (_("`)' required"));
1325 input_line_pointer
= tmp
;
1333 avr_cons_fix_new (frag
, where
, nbytes
, exp
)
1339 if (exp_mod_pm
== 0)
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
);
1346 as_bad (_("illegal %srelocation size: %d"), "", nbytes
);
1351 fix_new_exp (frag
, where
, nbytes
, exp
, FALSE
, BFD_RELOC_AVR_16_PM
);
1353 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes
);