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_85xx
, bfd_mach_avr2
},
63 {"avr3", AVR_ISA_M103
, bfd_mach_avr3
},
64 {"avr4", AVR_ISA_ALL
, bfd_mach_avr4
},
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_85xx
, bfd_mach_avr2
},
80 {"at90s8535", AVR_ISA_85xx
, bfd_mach_avr2
},
81 {"at90c8534", AVR_ISA_85xx
, bfd_mach_avr2
},
82 {"atmega603", AVR_ISA_M603
, bfd_mach_avr3
},
83 {"atmega103", AVR_ISA_M103
, bfd_mach_avr3
},
84 {"atmega161", AVR_ISA_M161
, bfd_mach_avr4
},
85 {"at94k10", AVR_ISA_94K
, bfd_mach_avr4
},
86 {"at94k20", AVR_ISA_94K
, bfd_mach_avr4
},
87 {"at94k40", AVR_ISA_94K
, bfd_mach_avr4
},
92 /* Current MCU type. */
93 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_2xxx
,bfd_mach_avr2
};
94 static struct mcu_type_s
*avr_mcu
= &default_mcu
;
96 const char EXP_CHARS
[] = "eE";
97 const char FLT_CHARS
[] = "dD";
98 static void avr_set_arch (int dummy
);
100 /* The target specific pseudo-ops which we support. */
101 const pseudo_typeS md_pseudo_table
[] =
103 {"arch", avr_set_arch
, 0},
107 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
109 static char * skip_space (char * s
);
110 static char * extract_word (char *from
, char *to
, int limit
);
111 static unsigned int avr_operand (struct avr_opcodes_s
*opcode
,
112 int where
, char *op
, char **line
);
113 static unsigned int avr_operands (struct avr_opcodes_s
*opcode
, char **line
);
114 static unsigned int avr_get_constant (char * str
, int max
);
115 static char *parse_exp (char *s
, expressionS
* op
);
116 static bfd_reloc_code_real_type
avr_ldi_expression (expressionS
*exp
);
117 long md_pcrel_from_section
PARAMS ((fixS
*, segT
));
120 #define EXP_MOD_NAME(i) exp_mod[i].name
121 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
122 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
123 #define HAVE_PM_P(i) exp_mod[i].have_pm
128 bfd_reloc_code_real_type reloc
;
129 bfd_reloc_code_real_type neg_reloc
;
133 static struct exp_mod_s exp_mod
[] = {
134 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
135 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
136 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
137 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
138 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
139 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
140 {"hlo8", -BFD_RELOC_AVR_LO8_LDI
, -BFD_RELOC_AVR_LO8_LDI_NEG
, 0},
141 {"hhi8", -BFD_RELOC_AVR_HI8_LDI
, -BFD_RELOC_AVR_HI8_LDI_NEG
, 0},
144 /* Opcode hash table. */
145 static struct hash_control
*avr_hash
;
147 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
148 static struct hash_control
*avr_mod_hash
;
150 #define OPTION_MMCU (OPTION_MD_BASE + 1)
152 struct option md_longopts
[] = {
153 {"mmcu", required_argument
, NULL
, 'm'},
154 {NULL
, no_argument
, NULL
, 0}
156 size_t md_longopts_size
= sizeof(md_longopts
);
162 while (*s
== ' ' || *s
== '\t')
167 /* Extract one word from FROM and copy it to TO. */
169 extract_word (char *from
, char *to
, int limit
)
175 /* Drop leading whitespace. */
176 from
= skip_space (from
);
178 /* Find the op code end. */
179 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name(*op_end
); )
181 to
[size
++] = *op_end
++;
182 if (size
+ 1 >= limit
)
190 md_estimate_size_before_relax (fragp
, seg
)
191 fragS
*fragp ATTRIBUTE_UNUSED
;
192 asection
*seg ATTRIBUTE_UNUSED
;
199 md_show_usage (stream
)
205 " -mmcu=[avr-name] select microcontroller variant\n"
206 " [avr-name] can be:\n"
207 " avr1 - AT90S1200\n"
208 " avr2 - AT90S2xxx, AT90S4xxx, AT90S85xx, ATtiny22\n"
209 " avr3 - ATmega103 or ATmega603\n"
210 " avr4 - ATmega161\n"
211 " or immediate microcontroller name.\n"));
216 int dummy ATTRIBUTE_UNUSED
;
219 str
= (char *)alloca (20);
220 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
221 md_parse_option ('m', str
);
222 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
226 md_parse_option (c
, arg
)
230 char *t
= alloca (strlen (arg
) + 1);
234 *t
= tolower (*arg1
++);
241 for (i
= 0; mcu_types
[i
].name
; ++i
)
242 if (strcmp (mcu_types
[i
].name
, s
) == 0)
245 if (!mcu_types
[i
].name
)
246 as_fatal (_ ("unknown MCU: %s\n"), arg
);
247 if (avr_mcu
== &default_mcu
)
248 avr_mcu
= &mcu_types
[i
];
250 as_fatal (_ ("redefinition of mcu type `%s'"), mcu_types
[i
].name
);
257 md_undefined_symbol(name
)
258 char *name ATTRIBUTE_UNUSED
;
263 /* Convert a string pointed to by input_line_pointer into a floating point
264 constant of type `type', and store the appropriate bytes to `*litP'.
265 The number of LITTLENUMS emitted is stored in `*sizeP'. Returns NULL if
266 OK, or an error message otherwise. */
268 md_atof (type
, litP
, sizeP
)
274 LITTLENUM_TYPE words
[4];
275 LITTLENUM_TYPE
*wordP
;
288 return _("bad call to md_atof");
291 t
= atof_ieee (input_line_pointer
, type
, words
);
293 input_line_pointer
= t
;
295 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
296 /* This loop outputs the LITTLENUMs in REVERSE order. */
297 for (wordP
= words
+ prec
- 1; prec
--;)
299 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
300 litP
+= sizeof (LITTLENUM_TYPE
);
306 md_convert_frag (abfd
, sec
, fragP
)
307 bfd
*abfd ATTRIBUTE_UNUSED
;
308 asection
*sec ATTRIBUTE_UNUSED
;
309 fragS
*fragP ATTRIBUTE_UNUSED
;
319 struct avr_opcodes_s
*opcode
;
320 avr_hash
= hash_new();
322 /* Insert unique names into hash table. This hash table then provides a
323 quick index to the first opcode with a particular name in the opcode
326 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
327 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
329 avr_mod_hash
= hash_new ();
331 for (i
= 0; i
< sizeof (exp_mod
) / sizeof (exp_mod
[0]); ++i
)
332 hash_insert (avr_mod_hash
, EXP_MOD_NAME(i
), (void*)(i
+10));
334 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
338 /* Resolve STR as a constant expression and return the result.
339 If result greater than MAX then error. */
342 avr_get_constant (str
, max
)
347 str
= skip_space (str
);
348 input_line_pointer
= str
;
351 if (ex
.X_op
!= O_constant
)
352 as_bad (_("constant value required"));
354 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
355 as_bad (_("number must be less than %d"), max
+1);
356 return ex
.X_add_number
;
360 /* Parse instruction operands.
361 Returns binary opcode. */
364 avr_operands (opcode
, line
)
365 struct avr_opcodes_s
*opcode
;
368 char *op
= opcode
->constraints
;
369 unsigned int bin
= opcode
->bin_opcode
;
370 char *frag
= frag_more (opcode
->insn_size
* 2);
372 int where
= frag
- frag_now
->fr_literal
;
373 static unsigned int prev
= 0; /* previous opcode */
375 /* Opcode have operands. */
378 unsigned int reg1
= 0;
379 unsigned int reg2
= 0;
380 int reg1_present
= 0;
381 int reg2_present
= 0;
383 /* Parse first operand. */
384 if (REGISTER_P (*op
))
386 reg1
= avr_operand (opcode
, where
, op
, &str
);
389 /* Parse second operand. */
401 if (REGISTER_P (*op
))
404 str
= skip_space (str
);
406 as_bad (_ ("`,' required"));
407 str
= skip_space (str
);
409 reg2
= avr_operand (opcode
, where
, op
, &str
);
412 if (reg1_present
&& reg2_present
)
413 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
414 else if (reg2_present
)
422 /* detect undefined combinations (like lpm r31,Z+) */
423 if (((bin
& 0xFDEF) == 0x91AD) || ((bin
& 0xFDEF) == 0x91AE) ||
424 ((bin
& 0xFDEF) == 0x91C9) || ((bin
& 0xFDEF) == 0x91CA) ||
425 ((bin
& 0xFDEF) == 0x91E1) || ((bin
& 0xFDEF) == 0x91E2) ||
426 ((bin
& 0xFFED) == 0x91E5))
427 as_warn( _("undefined combination of operands"));
429 if (opcode
->insn_size
== 2)
431 /* warn if previous opcode was cpse/sbic/sbis/sbrc/sbrs
433 if ((prev
& 0xFC00) == 0x1000
434 || (prev
& 0xFD00) == 0x9900
435 || (prev
& 0xFC08) == 0xFC00)
436 as_warn (_("skipping two-word instruction"));
438 bfd_putl32 ((bfd_vma
)bin
, frag
);
441 bfd_putl16 ((bfd_vma
)bin
, frag
);
449 /* Parse one instruction operand.
450 Returns operand bitmask. Also fixups can be generated. */
453 avr_operand (opcode
, where
, op
, line
)
454 struct avr_opcodes_s
*opcode
;
460 unsigned int op_mask
= 0;
461 char *str
= skip_space (*line
);
465 /* Any register operand. */
474 if (*str
== 'r' || *str
== 'R')
478 str
= extract_word (str
, r_name
, sizeof (r_name
));
479 if (isdigit(r_name
[1]))
481 if (r_name
[2] == '\0')
482 op_mask
= r_name
[1] - '0';
483 else if (r_name
[1] != '0'
484 && isdigit(r_name
[2])
485 && r_name
[3] == '\0')
486 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
491 op_mask
= avr_get_constant (str
, 31);
492 str
= input_line_pointer
;
500 if (op_mask
< 16 || op_mask
> 23)
501 as_bad (_ ("register r16-r23 required"));
507 as_bad (_ ("register number above 15 required"));
513 as_bad (_ ("even register number required"));
519 if (op_mask
& 1 || op_mask
> 6)
520 as_bad (_ ("register r24,r26,r28 or r30 required"));
526 as_bad (_ ("register name or number from 0 to 31 required"));
535 str
= skip_space (str
+1);
544 as_bad (_ ("pointer register (X,Y or Z) required"));
546 str
= skip_space (str
+1);
551 as_bad (_ ("cannot both predecrement and postincrement"));
554 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
555 registers, no predecrement, no postincrement */
556 if ((op_mask
& 0x100F) && !(avr_mcu
->isa
& AVR_ISA_SRAM
))
557 as_bad (_ ("addressing mode not supported"));
564 as_bad (_ ("can't predecrement"));
566 if (! (*str
== 'z' || *str
== 'Z'))
567 as_bad (_ ("pointer register Z required"));
569 str
= skip_space (str
+ 1);
580 char c
= tolower (*str
++);
584 as_bad (_ ("pointer register (Y or Z) required"));
585 str
= skip_space (str
);
589 x
= avr_get_constant (str
, 63);
590 str
= input_line_pointer
;
591 op_mask
|= (x
& 7) | ((x
& (3 << 3)) << 7) | ((x
& (1 << 5)) << 8);
598 str
= parse_exp (str
, &op_expr
);
599 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
600 &op_expr
, false, BFD_RELOC_AVR_CALL
);
607 str
= parse_exp (str
, &op_expr
);
608 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
609 &op_expr
, true, BFD_RELOC_AVR_13_PCREL
);
616 str
= parse_exp (str
, &op_expr
);
617 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
618 &op_expr
, true, BFD_RELOC_AVR_7_PCREL
);
625 str
= parse_exp (str
, &op_expr
);
626 fix_new_exp (frag_now
, where
+2, opcode
->insn_size
* 2,
627 &op_expr
, false, BFD_RELOC_16
);
634 bfd_reloc_code_real_type r_type
;
635 input_line_pointer
= str
;
636 r_type
= avr_ldi_expression (&op_expr
);
637 str
= input_line_pointer
;
638 fix_new_exp (frag_now
, where
, 3,
639 &op_expr
, false, r_type
);
646 x
= ~avr_get_constant (str
, 255);
647 str
= input_line_pointer
;
648 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
655 x
= avr_get_constant (str
, 63);
656 str
= input_line_pointer
;
657 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 2);
665 x
= avr_get_constant (str
, 7);
666 str
= input_line_pointer
;
676 x
= avr_get_constant (str
, 63);
677 str
= input_line_pointer
;
678 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
685 x
= avr_get_constant (str
, 31);
686 str
= input_line_pointer
;
693 as_bad (_ ("unknown constraint `%c'"), *op
);
699 /* GAS will call this function for each section at the end of the assembly,
700 to permit the CPU backend to adjust the alignment of a section. */
702 md_section_align (seg
, addr
)
706 int align
= bfd_get_section_alignment (stdoutput
, seg
);
707 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
710 /* If you define this macro, it should return the offset between the
711 address of a PC relative fixup and the position from which the PC
712 relative adjustment should be made. On many processors, the base
713 of a PC relative instruction is the next instruction, so this
714 macro would return the length of an instruction. */
716 md_pcrel_from_section (fixp
, sec
)
720 if (fixp
->fx_addsy
!= (symbolS
*)NULL
721 && (!S_IS_DEFINED (fixp
->fx_addsy
)
722 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
724 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
727 /* GAS will call this for each fixup. It should store the correct
728 value in the object file. */
730 md_apply_fix3 (fixp
, valuep
, seg
)
735 unsigned char *where
;
739 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
744 else if (fixp
->fx_pcrel
)
746 segT s
= S_GET_SEGMENT (fixp
->fx_addsy
);
747 if (fixp
->fx_addsy
&& (s
== seg
|| s
== absolute_section
))
749 value
= S_GET_VALUE (fixp
->fx_addsy
) + *valuep
;
757 value
= fixp
->fx_offset
;
758 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
760 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
762 value
-= S_GET_VALUE (fixp
->fx_subsy
);
767 /* We don't actually support subtracting a symbol. */
768 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
769 _ ("expression too complex"));
773 switch (fixp
->fx_r_type
)
776 fixp
->fx_no_overflow
= 1;
778 case BFD_RELOC_AVR_7_PCREL
:
779 case BFD_RELOC_AVR_13_PCREL
:
782 case BFD_RELOC_AVR_CALL
:
788 /* Fetch the instruction, insert the fully resolved operand
789 value, and stuff the instruction back again. */
790 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
791 insn
= bfd_getl16 (where
);
793 switch (fixp
->fx_r_type
)
795 case BFD_RELOC_AVR_7_PCREL
:
797 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
798 _("odd address operand: %ld"), value
);
799 /* Instruction addresses are always right-shifted by 1. */
801 --value
; /* Correct PC. */
802 if (value
< -64 || value
> 63)
803 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
804 _("operand out of range: %ld"), value
);
805 value
= (value
<< 3) & 0x3f8;
806 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
809 case BFD_RELOC_AVR_13_PCREL
:
811 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
812 _("odd address operand: %ld"), value
);
813 /* Instruction addresses are always right-shifted by 1. */
815 --value
; /* Correct PC. */
817 if (value
< -2048 || value
> 2047)
819 if (avr_mcu
->isa
& AVR_ISA_WRAP
)
827 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
828 _("operand out of range: %ld"), value
);
832 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
836 bfd_putl16 ((bfd_vma
) value
, where
);
840 bfd_putl16 ((bfd_vma
) value
, where
);
843 case BFD_RELOC_AVR_16_PM
:
844 bfd_putl16 ((bfd_vma
) (value
>>1), where
);
847 case BFD_RELOC_AVR_LO8_LDI
:
848 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
851 case -BFD_RELOC_AVR_LO8_LDI
:
852 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
855 case BFD_RELOC_AVR_HI8_LDI
:
856 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
859 case -BFD_RELOC_AVR_HI8_LDI
:
860 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
863 case BFD_RELOC_AVR_HH8_LDI
:
864 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
867 case BFD_RELOC_AVR_LO8_LDI_NEG
:
868 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
871 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
872 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
875 case BFD_RELOC_AVR_HI8_LDI_NEG
:
876 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
879 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
880 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
883 case BFD_RELOC_AVR_HH8_LDI_NEG
:
884 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
887 case BFD_RELOC_AVR_LO8_LDI_PM
:
888 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
891 case BFD_RELOC_AVR_HI8_LDI_PM
:
892 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
895 case BFD_RELOC_AVR_HH8_LDI_PM
:
896 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
899 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
900 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
903 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
904 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
907 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
908 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
911 case BFD_RELOC_AVR_CALL
:
914 x
= bfd_getl16 (where
);
916 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
917 _("odd address operand: %ld"), value
);
919 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
920 bfd_putl16 ((bfd_vma
) x
, where
);
921 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+2);
926 as_fatal ( _("line %d: unknown relocation type: 0x%x"),
927 fixp
->fx_line
, fixp
->fx_r_type
);
933 switch (fixp
->fx_r_type
)
935 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
936 case -BFD_RELOC_AVR_HI8_LDI
:
937 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
938 case -BFD_RELOC_AVR_LO8_LDI
:
939 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
940 _("only constant expression allowed"));
946 fixp
->fx_addnumber
= value
;
952 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
953 will pass the resulting reloc to `bfd_install_relocation'. This
954 currently works poorly, as `bfd_install_relocation' often does the
955 wrong thing, and instances of `tc_gen_reloc' have been written to
956 work around the problems, which in turns makes it difficult to fix
957 `bfd_install_relocation'. */
959 /* If while processing a fixup, a reloc really needs to be created
960 then it is done here. */
963 tc_gen_reloc (seg
, fixp
)
964 asection
*seg ATTRIBUTE_UNUSED
;
969 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
971 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
972 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
974 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
975 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
976 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
978 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
979 _("reloc %d not supported by object file format"),
980 (int)fixp
->fx_r_type
);
984 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
985 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
986 reloc
->address
= fixp
->fx_offset
;
988 reloc
->addend
= fixp
->fx_offset
;
998 struct avr_opcodes_s
* opcode
;
1001 str
= skip_space (extract_word (str
, op
, sizeof(op
)));
1004 as_bad (_ ("can't find opcode "));
1006 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1010 as_bad (_ ("unknown opcode `%s'"), op
);
1014 /* Special case for opcodes with optional operands (lpm, elpm) -
1015 version with operands exists in avr_opcodes[] in the next entry. */
1017 if (*str
&& *opcode
->constraints
== '?')
1020 if ((opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1021 as_bad (_ ("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1023 /* We used to set input_line_pointer to the result of get_operands,
1024 but that is wrong. Our caller assumes we don't change it. */
1026 char *t
= input_line_pointer
;
1027 avr_operands (opcode
, &str
);
1028 if (*skip_space (str
))
1029 as_bad (_ ("garbage at end of line"));
1030 input_line_pointer
= t
;
1034 /* Parse ordinary expression. */
1040 input_line_pointer
= s
;
1042 if (op
->X_op
== O_absent
)
1043 as_bad (_("missing operand"));
1044 return input_line_pointer
;
1048 /* Parse special expressions (needed for LDI command):
1053 where xx is: hh, hi, lo
1055 static bfd_reloc_code_real_type
1056 avr_ldi_expression (exp
)
1059 char *str
= input_line_pointer
;
1065 str
= extract_word (str
, op
, sizeof (op
));
1068 mod
= (int) hash_find (avr_mod_hash
, op
);
1073 str
= skip_space (str
);
1078 if (strncmp ("pm(", str
, 3) == 0
1079 || strncmp ("-(pm(", str
, 5) == 0)
1087 as_bad (_ ("illegal expression"));
1097 if (*str
== '-' && *(str
+ 1) == '(')
1103 input_line_pointer
= str
;
1107 if (*input_line_pointer
!= ')')
1109 as_bad (_ ("`)' required"));
1112 input_line_pointer
++;
1115 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1119 input_line_pointer
= tmp
;
1121 return BFD_RELOC_AVR_LO8_LDI
;
1124 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1125 `avr_cons_fix_new' */
1126 static int exp_mod_pm
= 0;
1128 /* Parse special CONS expression: pm (expression)
1129 which is used for addressing to a program memory.
1130 Relocation: BFD_RELOC_AVR_16_PM */
1132 avr_parse_cons_expression (exp
, nbytes
)
1140 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1144 char * pm_name
= "pm";
1145 int len
= strlen (pm_name
);
1146 if (strncasecmp (input_line_pointer
, pm_name
, len
) == 0)
1148 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1149 if (*input_line_pointer
== '(')
1151 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1154 if (*input_line_pointer
== ')')
1155 ++input_line_pointer
;
1158 as_bad (_ ("`)' required"));
1163 input_line_pointer
= tmp
;
1170 avr_cons_fix_new(frag
, where
, nbytes
, exp
)
1176 if (exp_mod_pm
== 0)
1179 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_16
);
1180 else if (nbytes
== 4)
1181 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_32
);
1183 as_bad (_ ("illegal %srelocation size: %d"), "", nbytes
);
1188 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_AVR_16_PM
);
1190 as_bad (_ ("illegal %srelocation size: %d"), "`pm' ", nbytes
);