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"
46 const char comment_chars
[] = ";";
47 const char line_comment_chars
[] = "#";
48 const char line_separator_chars
[] = "$";
50 const char *md_shortopts
= "m:";
58 static struct mcu_type_s mcu_types
[] =
60 {"avr1", AVR_ISA_TINY1
, bfd_mach_avr1
},
61 {"avr2", AVR_ISA_2xxx
, bfd_mach_avr2
},
62 {"avr3", AVR_ISA_M103
, bfd_mach_avr3
},
63 {"avr4", AVR_ISA_M83
, bfd_mach_avr4
},
64 {"avr5", AVR_ISA_ALL
, bfd_mach_avr5
},
65 {"at90s1200", AVR_ISA_1200
, bfd_mach_avr1
},
66 {"attiny10", AVR_ISA_TINY1
, bfd_mach_avr1
},
67 {"attiny11", AVR_ISA_TINY1
, bfd_mach_avr1
},
68 {"attiny12", AVR_ISA_TINY1
, bfd_mach_avr1
},
69 {"attiny15", AVR_ISA_TINY1
, bfd_mach_avr1
},
70 {"attiny28", AVR_ISA_TINY1
, bfd_mach_avr1
},
71 {"at90s2313", AVR_ISA_2xxx
, bfd_mach_avr2
},
72 {"at90s2323", AVR_ISA_2xxx
, bfd_mach_avr2
},
73 {"at90s2333", AVR_ISA_2xxx
, bfd_mach_avr2
},
74 {"attiny22" , AVR_ISA_2xxx
, bfd_mach_avr2
},
75 {"at90s2343", AVR_ISA_2xxx
, bfd_mach_avr2
},
76 {"at90s4433", AVR_ISA_2xxx
, bfd_mach_avr2
},
77 {"at90s4414", AVR_ISA_2xxx
, bfd_mach_avr2
},
78 {"at90s4434", AVR_ISA_2xxx
, bfd_mach_avr2
},
79 {"at90s8515", AVR_ISA_2xxx
, bfd_mach_avr2
},
80 {"at90s8535", AVR_ISA_2xxx
, bfd_mach_avr2
},
81 {"at90c8534", AVR_ISA_2xxx
, bfd_mach_avr2
},
82 {"atmega603", AVR_ISA_M603
, bfd_mach_avr3
},
83 {"atmega103", AVR_ISA_M103
, bfd_mach_avr3
},
84 {"atmega83", AVR_ISA_M83
, bfd_mach_avr4
},
85 {"atmega85", AVR_ISA_M83
, bfd_mach_avr4
},
86 {"atmega161", AVR_ISA_M161
, bfd_mach_avr5
},
87 {"atmega163", AVR_ISA_M161
, bfd_mach_avr5
},
88 {"atmega32", AVR_ISA_M161
, bfd_mach_avr5
},
89 {"at94k", AVR_ISA_94K
, bfd_mach_avr5
},
93 /* Current MCU type. */
94 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_2xxx
,bfd_mach_avr2
};
95 static struct mcu_type_s
*avr_mcu
= &default_mcu
;
97 /* AVR target-specific switches. */
100 int all_opcodes
; /* -mall-opcodes: accept all known AVR opcodes */
101 int no_skip_bug
; /* -mno-skip-bug: no warnings for skipping 2-word insns */
102 int no_wrap
; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around */
105 static struct avr_opt_s avr_opt
= { 0, 0, 0 };
107 const char EXP_CHARS
[] = "eE";
108 const char FLT_CHARS
[] = "dD";
109 static void avr_set_arch (int dummy
);
111 /* The target specific pseudo-ops which we support. */
112 const pseudo_typeS md_pseudo_table
[] =
114 {"arch", avr_set_arch
, 0},
118 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
120 static void show_mcu_list
PARAMS ((FILE *));
121 static char *skip_space
PARAMS ((char *));
122 static char *extract_word
PARAMS ((char *, char *, int));
123 static unsigned int avr_operand
PARAMS ((struct avr_opcodes_s
*,
124 int, char *, char **));
125 static unsigned int avr_operands
PARAMS ((struct avr_opcodes_s
*, char **));
126 static unsigned int avr_get_constant
PARAMS ((char *, int));
127 static char *parse_exp
PARAMS ((char *, expressionS
*));
128 static bfd_reloc_code_real_type avr_ldi_expression
PARAMS ((expressionS
*));
130 #define EXP_MOD_NAME(i) exp_mod[i].name
131 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
132 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
133 #define HAVE_PM_P(i) exp_mod[i].have_pm
138 bfd_reloc_code_real_type reloc
;
139 bfd_reloc_code_real_type neg_reloc
;
143 static struct exp_mod_s exp_mod
[] =
145 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
146 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
147 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
148 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
149 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
150 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
151 {"hlo8", -BFD_RELOC_AVR_LO8_LDI
, -BFD_RELOC_AVR_LO8_LDI_NEG
, 0},
152 {"hhi8", -BFD_RELOC_AVR_HI8_LDI
, -BFD_RELOC_AVR_HI8_LDI_NEG
, 0},
155 /* Opcode hash table. */
156 static struct hash_control
*avr_hash
;
158 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
159 static struct hash_control
*avr_mod_hash
;
161 #define OPTION_MMCU 'm'
162 #define OPTION_ALL_OPCODES (OPTION_MD_BASE + 1)
163 #define OPTION_NO_SKIP_BUG (OPTION_MD_BASE + 2)
164 #define OPTION_NO_WRAP (OPTION_MD_BASE + 3)
166 struct option md_longopts
[] =
168 { "mmcu", required_argument
, NULL
, OPTION_MMCU
},
169 { "mall-opcodes", no_argument
, NULL
, OPTION_ALL_OPCODES
},
170 { "mno-skip-bug", no_argument
, NULL
, OPTION_NO_SKIP_BUG
},
171 { "mno-wrap", no_argument
, NULL
, OPTION_NO_WRAP
},
172 { NULL
, no_argument
, NULL
, 0 }
175 size_t md_longopts_size
= sizeof (md_longopts
);
177 /* Display nicely formatted list of known MCU names. */
180 show_mcu_list (stream
)
185 fprintf (stream
, _("Known MCU names:"));
188 for (i
= 0; mcu_types
[i
].name
; i
++)
190 int len
= strlen (mcu_types
[i
].name
);
195 fprintf (stream
, " %s", mcu_types
[i
].name
);
198 fprintf (stream
, "\n %s", mcu_types
[i
].name
);
203 fprintf (stream
, "\n");
210 while (*s
== ' ' || *s
== '\t')
215 /* Extract one word from FROM and copy it to TO. */
218 extract_word (char *from
, char *to
, int limit
)
224 /* Drop leading whitespace. */
225 from
= skip_space (from
);
228 /* Find the op code end. */
229 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name (*op_end
);)
231 to
[size
++] = *op_end
++;
232 if (size
+ 1 >= limit
)
241 md_estimate_size_before_relax (fragp
, seg
)
242 fragS
*fragp ATTRIBUTE_UNUSED
;
243 asection
*seg ATTRIBUTE_UNUSED
;
250 md_show_usage (stream
)
255 " -mmcu=[avr-name] select microcontroller variant\n"
256 " [avr-name] can be:\n"
257 " avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
258 " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
259 " avr3 - ATmega103, ATmega603\n"
260 " avr4 - ATmega83, ATmega85\n"
261 " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
262 " or immediate microcontroller name.\n"));
264 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
265 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
266 " (default for avr4, avr5)\n"
267 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
268 " (default for avr3, avr5)\n"));
269 show_mcu_list (stream
);
274 int dummy ATTRIBUTE_UNUSED
;
278 str
= (char *) alloca (20);
279 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
280 md_parse_option (OPTION_MMCU
, str
);
281 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
285 md_parse_option (c
, arg
)
294 char *s
= alloca (strlen (arg
) + 1);
301 *t
= tolower (*arg1
++);
305 for (i
= 0; mcu_types
[i
].name
; ++i
)
306 if (strcmp (mcu_types
[i
].name
, s
) == 0)
309 if (!mcu_types
[i
].name
)
311 show_mcu_list (stderr
);
312 as_fatal (_("unknown MCU: %s\n"), arg
);
315 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
316 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
317 as .arch ... in the asm output at the same time. */
318 if (avr_mcu
== &default_mcu
|| avr_mcu
->mach
== mcu_types
[i
].mach
)
319 avr_mcu
= &mcu_types
[i
];
321 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
322 avr_mcu
->name
, mcu_types
[i
].name
);
325 case OPTION_ALL_OPCODES
:
326 avr_opt
.all_opcodes
= 1;
328 case OPTION_NO_SKIP_BUG
:
329 avr_opt
.no_skip_bug
= 1;
340 md_undefined_symbol (name
)
341 char *name ATTRIBUTE_UNUSED
;
346 /* Turn a string in input_line_pointer into a floating point constant
347 of type TYPE, and store the appropriate bytes in *LITP. The number
348 of LITTLENUMS emitted is stored in *SIZEP. An error message is
349 returned, or NULL on OK. */
352 md_atof (type
, litP
, sizeP
)
358 LITTLENUM_TYPE words
[4];
359 LITTLENUM_TYPE
*wordP
;
372 return _("bad call to md_atof");
375 t
= atof_ieee (input_line_pointer
, type
, words
);
377 input_line_pointer
= t
;
379 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
381 /* This loop outputs the LITTLENUMs in REVERSE order. */
382 for (wordP
= words
+ prec
- 1; prec
--;)
384 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
385 litP
+= sizeof (LITTLENUM_TYPE
);
392 md_convert_frag (abfd
, sec
, fragP
)
393 bfd
*abfd ATTRIBUTE_UNUSED
;
394 asection
*sec ATTRIBUTE_UNUSED
;
395 fragS
*fragP ATTRIBUTE_UNUSED
;
404 struct avr_opcodes_s
*opcode
;
405 avr_hash
= hash_new ();
407 /* Insert unique names into hash table. This hash table then provides a
408 quick index to the first opcode with a particular name in the opcode
410 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
411 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
413 avr_mod_hash
= hash_new ();
415 for (i
= 0; i
< sizeof (exp_mod
) / sizeof (exp_mod
[0]); ++i
)
416 hash_insert (avr_mod_hash
, EXP_MOD_NAME (i
), (void *) (i
+ 10));
418 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
421 /* Resolve STR as a constant expression and return the result.
422 If result greater than MAX then error. */
425 avr_get_constant (str
, max
)
430 str
= skip_space (str
);
431 input_line_pointer
= str
;
434 if (ex
.X_op
!= O_constant
)
435 as_bad (_("constant value required"));
437 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
438 as_bad (_("number must be less than %d"), max
+ 1);
440 return ex
.X_add_number
;
443 /* Parse instruction operands.
444 Return binary opcode. */
447 avr_operands (opcode
, line
)
448 struct avr_opcodes_s
*opcode
;
451 char *op
= opcode
->constraints
;
452 unsigned int bin
= opcode
->bin_opcode
;
453 char *frag
= frag_more (opcode
->insn_size
* 2);
455 int where
= frag
- frag_now
->fr_literal
;
456 static unsigned int prev
= 0; /* Previous opcode. */
458 /* Opcode have operands. */
461 unsigned int reg1
= 0;
462 unsigned int reg2
= 0;
463 int reg1_present
= 0;
464 int reg2_present
= 0;
466 /* Parse first operand. */
467 if (REGISTER_P (*op
))
469 reg1
= avr_operand (opcode
, where
, op
, &str
);
472 /* Parse second operand. */
485 if (REGISTER_P (*op
))
488 str
= skip_space (str
);
490 as_bad (_("`,' required"));
491 str
= skip_space (str
);
493 reg2
= avr_operand (opcode
, where
, op
, &str
);
497 if (reg1_present
&& reg2_present
)
498 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
499 else if (reg2_present
)
507 /* Detect undefined combinations (like ld r31,Z+). */
508 if (!avr_opt
.all_opcodes
&& AVR_UNDEF_P (bin
))
509 as_warn (_("undefined combination of operands"));
511 if (opcode
->insn_size
== 2)
513 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
514 (AVR core bug, fixed in the newer devices). */
516 if (!(avr_opt
.no_skip_bug
|| (avr_mcu
->isa
& AVR_ISA_MUL
))
517 && AVR_SKIP_P (prev
))
518 as_warn (_("skipping two-word instruction"));
520 bfd_putl32 ((bfd_vma
) bin
, frag
);
523 bfd_putl16 ((bfd_vma
) bin
, frag
);
530 /* Parse one instruction operand.
531 Return operand bitmask. Also fixups can be generated. */
534 avr_operand (opcode
, where
, op
, line
)
535 struct avr_opcodes_s
*opcode
;
541 unsigned int op_mask
= 0;
542 char *str
= skip_space (*line
);
546 /* Any register operand. */
552 if (*str
== 'r' || *str
== 'R')
556 str
= extract_word (str
, r_name
, sizeof (r_name
));
558 if (isdigit (r_name
[1]))
560 if (r_name
[2] == '\0')
561 op_mask
= r_name
[1] - '0';
562 else if (r_name
[1] != '0'
563 && isdigit (r_name
[2])
564 && r_name
[3] == '\0')
565 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
570 op_mask
= avr_get_constant (str
, 31);
571 str
= input_line_pointer
;
579 if (op_mask
< 16 || op_mask
> 23)
580 as_bad (_("register r16-r23 required"));
586 as_bad (_("register number above 15 required"));
592 as_bad (_("even register number required"));
597 if ((op_mask
& 1) || op_mask
< 24)
598 as_bad (_("register r24, r26, r28 or r30 required"));
599 op_mask
= (op_mask
- 24) >> 1;
604 as_bad (_("register name or number from 0 to 31 required"));
613 str
= skip_space (str
+ 1);
622 as_bad (_("pointer register (X, Y or Z) required"));
624 str
= skip_space (str
+ 1);
629 as_bad (_("cannot both predecrement and postincrement"));
633 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
634 registers, no predecrement, no postincrement. */
635 if (!avr_opt
.all_opcodes
&& (op_mask
& 0x100F)
636 && !(avr_mcu
->isa
& AVR_ISA_SRAM
))
637 as_bad (_("addressing mode not supported"));
643 as_bad (_("can't predecrement"));
645 if (! (*str
== 'z' || *str
== 'Z'))
646 as_bad (_("pointer register Z required"));
648 str
= skip_space (str
+ 1);
659 char c
= tolower (*str
++);
664 as_bad (_("pointer register (Y or Z) required"));
665 str
= skip_space (str
);
669 x
= avr_get_constant (str
, 63);
670 str
= input_line_pointer
;
671 op_mask
|= (x
& 7) | ((x
& (3 << 3)) << 7) | ((x
& (1 << 5)) << 8);
677 str
= parse_exp (str
, &op_expr
);
678 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
679 &op_expr
, false, BFD_RELOC_AVR_CALL
);
683 str
= parse_exp (str
, &op_expr
);
684 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
685 &op_expr
, true, BFD_RELOC_AVR_13_PCREL
);
689 str
= parse_exp (str
, &op_expr
);
690 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
691 &op_expr
, true, BFD_RELOC_AVR_7_PCREL
);
695 str
= parse_exp (str
, &op_expr
);
696 fix_new_exp (frag_now
, where
+ 2, opcode
->insn_size
* 2,
697 &op_expr
, false, BFD_RELOC_16
);
702 bfd_reloc_code_real_type r_type
;
704 input_line_pointer
= str
;
705 r_type
= avr_ldi_expression (&op_expr
);
706 str
= input_line_pointer
;
707 fix_new_exp (frag_now
, where
, 3,
708 &op_expr
, false, r_type
);
716 x
= ~avr_get_constant (str
, 255);
717 str
= input_line_pointer
;
718 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
726 x
= avr_get_constant (str
, 63);
727 str
= input_line_pointer
;
728 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 2);
737 x
= avr_get_constant (str
, 7);
738 str
= input_line_pointer
;
749 x
= avr_get_constant (str
, 63);
750 str
= input_line_pointer
;
751 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
759 x
= avr_get_constant (str
, 31);
760 str
= input_line_pointer
;
769 as_bad (_("unknown constraint `%c'"), *op
);
776 /* GAS will call this function for each section at the end of the assembly,
777 to permit the CPU backend to adjust the alignment of a section. */
780 md_section_align (seg
, addr
)
784 int align
= bfd_get_section_alignment (stdoutput
, seg
);
785 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
788 /* If you define this macro, it should return the offset between the
789 address of a PC relative fixup and the position from which the PC
790 relative adjustment should be made. On many processors, the base
791 of a PC relative instruction is the next instruction, so this
792 macro would return the length of an instruction. */
795 md_pcrel_from_section (fixp
, sec
)
799 if (fixp
->fx_addsy
!= (symbolS
*) NULL
800 && (!S_IS_DEFINED (fixp
->fx_addsy
)
801 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
804 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
807 /* GAS will call this for each fixup. It should store the correct
808 value in the object file. */
811 md_apply_fix3 (fixp
, valuep
, seg
)
816 unsigned char *where
;
820 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
825 else if (fixp
->fx_pcrel
)
827 segT s
= S_GET_SEGMENT (fixp
->fx_addsy
);
829 if (fixp
->fx_addsy
&& (s
== seg
|| s
== absolute_section
))
831 value
= S_GET_VALUE (fixp
->fx_addsy
) + *valuep
;
839 value
= fixp
->fx_offset
;
841 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
843 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
845 value
-= S_GET_VALUE (fixp
->fx_subsy
);
850 /* We don't actually support subtracting a symbol. */
851 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
852 _("expression too complex"));
857 switch (fixp
->fx_r_type
)
860 fixp
->fx_no_overflow
= 1;
862 case BFD_RELOC_AVR_7_PCREL
:
863 case BFD_RELOC_AVR_13_PCREL
:
866 case BFD_RELOC_AVR_CALL
:
872 /* Fetch the instruction, insert the fully resolved operand
873 value, and stuff the instruction back again. */
874 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
875 insn
= bfd_getl16 (where
);
877 switch (fixp
->fx_r_type
)
879 case BFD_RELOC_AVR_7_PCREL
:
881 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
882 _("odd address operand: %ld"), value
);
884 /* Instruction addresses are always right-shifted by 1. */
886 --value
; /* Correct PC. */
888 if (value
< -64 || value
> 63)
889 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
890 _("operand out of range: %ld"), value
);
891 value
= (value
<< 3) & 0x3f8;
892 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
895 case BFD_RELOC_AVR_13_PCREL
:
897 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
898 _("odd address operand: %ld"), value
);
900 /* Instruction addresses are always right-shifted by 1. */
902 --value
; /* Correct PC. */
904 if (value
< -2048 || value
> 2047)
906 /* No wrap for devices with >8K of program memory. */
907 if ((avr_mcu
->isa
& AVR_ISA_MEGA
) || avr_opt
.no_wrap
)
908 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
909 _("operand out of range: %ld"), value
);
913 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
917 bfd_putl16 ((bfd_vma
) value
, where
);
921 bfd_putl16 ((bfd_vma
) value
, where
);
924 case BFD_RELOC_AVR_16_PM
:
925 bfd_putl16 ((bfd_vma
) (value
>> 1), where
);
928 case BFD_RELOC_AVR_LO8_LDI
:
929 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
932 case -BFD_RELOC_AVR_LO8_LDI
:
933 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
936 case BFD_RELOC_AVR_HI8_LDI
:
937 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
940 case -BFD_RELOC_AVR_HI8_LDI
:
941 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
944 case BFD_RELOC_AVR_HH8_LDI
:
945 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
948 case BFD_RELOC_AVR_LO8_LDI_NEG
:
949 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
952 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
953 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
956 case BFD_RELOC_AVR_HI8_LDI_NEG
:
957 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
960 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
961 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
964 case BFD_RELOC_AVR_HH8_LDI_NEG
:
965 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
968 case BFD_RELOC_AVR_LO8_LDI_PM
:
969 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
972 case BFD_RELOC_AVR_HI8_LDI_PM
:
973 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
976 case BFD_RELOC_AVR_HH8_LDI_PM
:
977 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
980 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
981 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
984 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
985 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
988 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
989 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
992 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
;
1033 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1034 will pass the resulting reloc to `bfd_install_relocation'. This
1035 currently works poorly, as `bfd_install_relocation' often does the
1036 wrong thing, and instances of `tc_gen_reloc' have been written to
1037 work around the problems, which in turns makes it difficult to fix
1038 `bfd_install_relocation'. */
1040 /* If while processing a fixup, a reloc really needs to be created
1041 then it is done here. */
1044 tc_gen_reloc (seg
, fixp
)
1045 asection
*seg ATTRIBUTE_UNUSED
;
1050 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
1052 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1053 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
1055 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1056 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
1057 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
1059 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1060 _("reloc %d not supported by object file format"),
1061 (int) fixp
->fx_r_type
);
1065 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
1066 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
1067 reloc
->address
= fixp
->fx_offset
;
1069 reloc
->addend
= fixp
->fx_offset
;
1078 struct avr_opcodes_s
*opcode
;
1081 str
= skip_space (extract_word (str
, op
, sizeof (op
)));
1084 as_bad (_("can't find opcode "));
1086 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1090 as_bad (_("unknown opcode `%s'"), op
);
1094 /* Special case for opcodes with optional operands (lpm, elpm) -
1095 version with operands exists in avr_opcodes[] in the next entry. */
1097 if (*str
&& *opcode
->constraints
== '?')
1100 if (!avr_opt
.all_opcodes
&& (opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1101 as_bad (_("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1103 /* We used to set input_line_pointer to the result of get_operands,
1104 but that is wrong. Our caller assumes we don't change it. */
1106 char *t
= input_line_pointer
;
1107 avr_operands (opcode
, &str
);
1108 if (*skip_space (str
))
1109 as_bad (_("garbage at end of line"));
1110 input_line_pointer
= t
;
1114 /* Parse ordinary expression. */
1121 input_line_pointer
= s
;
1123 if (op
->X_op
== O_absent
)
1124 as_bad (_("missing operand"));
1125 return input_line_pointer
;
1128 /* Parse special expressions (needed for LDI command):
1133 where xx is: hh, hi, lo. */
1135 static bfd_reloc_code_real_type
1136 avr_ldi_expression (exp
)
1139 char *str
= input_line_pointer
;
1145 str
= extract_word (str
, op
, sizeof (op
));
1149 mod
= (int) hash_find (avr_mod_hash
, op
);
1156 str
= skip_space (str
);
1164 if (strncmp ("pm(", str
, 3) == 0
1165 || strncmp ("-(pm(", str
, 5) == 0)
1167 if (HAVE_PM_P (mod
))
1173 as_bad (_("illegal expression"));
1185 if (*str
== '-' && *(str
+ 1) == '(')
1192 input_line_pointer
= str
;
1197 if (*input_line_pointer
!= ')')
1199 as_bad (_("`)' required"));
1202 input_line_pointer
++;
1206 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1211 input_line_pointer
= tmp
;
1214 /* Warn about expressions that fail to use lo8 (). */
1215 if (exp
->X_op
== O_constant
)
1217 int x
= exp
->X_add_number
;
1218 if (x
< -255 || x
> 255)
1219 as_warn (_("constant out of 8-bit range: %d"), x
);
1222 as_warn (_("expression possibly out of 8-bit range"));
1224 return BFD_RELOC_AVR_LO8_LDI
;
1227 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1228 `avr_cons_fix_new'. */
1229 static int exp_mod_pm
= 0;
1231 /* Parse special CONS expression: pm (expression)
1232 which is used for addressing to a program memory.
1233 Relocation: BFD_RELOC_AVR_16_PM. */
1236 avr_parse_cons_expression (exp
, nbytes
)
1244 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1248 char *pm_name
= "pm";
1249 int len
= strlen (pm_name
);
1251 if (strncasecmp (input_line_pointer
, pm_name
, len
) == 0)
1253 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1255 if (*input_line_pointer
== '(')
1257 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1261 if (*input_line_pointer
== ')')
1262 ++input_line_pointer
;
1265 as_bad (_("`)' required"));
1272 input_line_pointer
= tmp
;
1280 avr_cons_fix_new (frag
, where
, nbytes
, exp
)
1286 if (exp_mod_pm
== 0)
1289 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_16
);
1290 else if (nbytes
== 4)
1291 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_32
);
1293 as_bad (_("illegal %srelocation size: %d"), "", nbytes
);
1298 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_AVR_16_PM
);
1300 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes
);