1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
32 int insn_size
; /* in words */
34 unsigned int bin_opcode
;
37 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
38 {#NAME, CONSTR, SIZE, ISA, BIN},
40 struct avr_opcodes_s avr_opcodes
[] =
42 #include "opcode/avr.h"
47 const char comment_chars
[] = ";";
48 const char line_comment_chars
[] = "#";
49 const char line_separator_chars
[] = "$";
51 const char *md_shortopts
= "m:";
59 static struct mcu_type_s mcu_types
[] =
61 {"avr1", AVR_ISA_TINY1
, bfd_mach_avr1
},
62 {"avr2", AVR_ISA_2xxx
, bfd_mach_avr2
},
63 {"avr3", AVR_ISA_M103
, bfd_mach_avr3
},
64 {"avr4", AVR_ISA_M83
, bfd_mach_avr4
},
65 {"avr5", AVR_ISA_ALL
, bfd_mach_avr5
},
66 {"at90s1200", AVR_ISA_1200
, bfd_mach_avr1
},
67 {"attiny10", AVR_ISA_TINY1
, bfd_mach_avr1
},
68 {"attiny11", AVR_ISA_TINY1
, bfd_mach_avr1
},
69 {"attiny12", AVR_ISA_TINY1
, bfd_mach_avr1
},
70 {"attiny15", AVR_ISA_TINY1
, bfd_mach_avr1
},
71 {"attiny28", AVR_ISA_TINY1
, bfd_mach_avr1
},
72 {"at90s2313", AVR_ISA_2xxx
, bfd_mach_avr2
},
73 {"at90s2323", AVR_ISA_2xxx
, bfd_mach_avr2
},
74 {"at90s2333", AVR_ISA_2xxx
, bfd_mach_avr2
},
75 {"attiny22" , AVR_ISA_2xxx
, bfd_mach_avr2
},
76 {"at90s2343", AVR_ISA_2xxx
, bfd_mach_avr2
},
77 {"at90s4433", AVR_ISA_2xxx
, bfd_mach_avr2
},
78 {"at90s4414", AVR_ISA_2xxx
, bfd_mach_avr2
},
79 {"at90s4434", AVR_ISA_2xxx
, bfd_mach_avr2
},
80 {"at90s8515", AVR_ISA_2xxx
, bfd_mach_avr2
},
81 {"at90s8535", AVR_ISA_2xxx
, bfd_mach_avr2
},
82 {"at90c8534", AVR_ISA_2xxx
, bfd_mach_avr2
},
83 {"atmega603", AVR_ISA_M603
, bfd_mach_avr3
},
84 {"atmega103", AVR_ISA_M103
, bfd_mach_avr3
},
85 {"atmega83", AVR_ISA_M83
, bfd_mach_avr4
},
86 {"atmega85", AVR_ISA_M83
, bfd_mach_avr4
},
87 {"atmega161", AVR_ISA_M161
, bfd_mach_avr5
},
88 {"atmega163", AVR_ISA_M161
, bfd_mach_avr5
},
89 {"atmega32", AVR_ISA_M161
, bfd_mach_avr5
},
90 {"at94k", AVR_ISA_94K
, bfd_mach_avr5
},
95 /* Current MCU type. */
96 static struct mcu_type_s default_mcu
= {"avr2", AVR_ISA_2xxx
,bfd_mach_avr2
};
97 static struct mcu_type_s
*avr_mcu
= &default_mcu
;
99 const char EXP_CHARS
[] = "eE";
100 const char FLT_CHARS
[] = "dD";
101 static void avr_set_arch (int dummy
);
103 /* The target specific pseudo-ops which we support. */
104 const pseudo_typeS md_pseudo_table
[] =
106 {"arch", avr_set_arch
, 0},
110 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
112 static char * skip_space (char * s
);
113 static char * extract_word (char *from
, char *to
, int limit
);
114 static unsigned int avr_operand (struct avr_opcodes_s
*opcode
,
115 int where
, char *op
, char **line
);
116 static unsigned int avr_operands (struct avr_opcodes_s
*opcode
, char **line
);
117 static unsigned int avr_get_constant (char * str
, int max
);
118 static char *parse_exp (char *s
, expressionS
* op
);
119 static bfd_reloc_code_real_type
avr_ldi_expression (expressionS
*exp
);
120 long md_pcrel_from_section
PARAMS ((fixS
*, segT
));
123 #define EXP_MOD_NAME(i) exp_mod[i].name
124 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
125 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
126 #define HAVE_PM_P(i) exp_mod[i].have_pm
131 bfd_reloc_code_real_type reloc
;
132 bfd_reloc_code_real_type neg_reloc
;
136 static struct exp_mod_s exp_mod
[] = {
137 {"hh8", BFD_RELOC_AVR_HH8_LDI
, BFD_RELOC_AVR_HH8_LDI_NEG
, 1},
138 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM
, BFD_RELOC_AVR_HH8_LDI_PM_NEG
, 0},
139 {"hi8", BFD_RELOC_AVR_HI8_LDI
, BFD_RELOC_AVR_HI8_LDI_NEG
, 1},
140 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM
, BFD_RELOC_AVR_HI8_LDI_PM_NEG
, 0},
141 {"lo8", BFD_RELOC_AVR_LO8_LDI
, BFD_RELOC_AVR_LO8_LDI_NEG
, 1},
142 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM
, BFD_RELOC_AVR_LO8_LDI_PM_NEG
, 0},
143 {"hlo8", -BFD_RELOC_AVR_LO8_LDI
, -BFD_RELOC_AVR_LO8_LDI_NEG
, 0},
144 {"hhi8", -BFD_RELOC_AVR_HI8_LDI
, -BFD_RELOC_AVR_HI8_LDI_NEG
, 0},
147 /* Opcode hash table. */
148 static struct hash_control
*avr_hash
;
150 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
151 static struct hash_control
*avr_mod_hash
;
153 #define OPTION_MMCU (OPTION_MD_BASE + 1)
155 struct option md_longopts
[] = {
156 {"mmcu", required_argument
, NULL
, 'm'},
157 {NULL
, no_argument
, NULL
, 0}
159 size_t md_longopts_size
= sizeof(md_longopts
);
165 while (*s
== ' ' || *s
== '\t')
170 /* Extract one word from FROM and copy it to TO. */
172 extract_word (char *from
, char *to
, int limit
)
178 /* Drop leading whitespace. */
179 from
= skip_space (from
);
181 /* Find the op code end. */
182 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name(*op_end
); )
184 to
[size
++] = *op_end
++;
185 if (size
+ 1 >= limit
)
193 md_estimate_size_before_relax (fragp
, seg
)
194 fragS
*fragp ATTRIBUTE_UNUSED
;
195 asection
*seg ATTRIBUTE_UNUSED
;
202 md_show_usage (stream
)
208 " -mmcu=[avr-name] select microcontroller variant\n"
209 " [avr-name] can be:\n"
210 " avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
211 " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
212 " avr3 - ATmega103, ATmega603\n"
213 " avr4 - ATmega83, ATmega85\n"
214 " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
215 " or immediate microcontroller name.\n"));
220 int dummy ATTRIBUTE_UNUSED
;
223 str
= (char *)alloca (20);
224 input_line_pointer
= extract_word (input_line_pointer
, str
, 20);
225 md_parse_option ('m', str
);
226 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
230 md_parse_option (c
, arg
)
234 char *t
= alloca (strlen (arg
) + 1);
238 *t
= tolower (*arg1
++);
245 for (i
= 0; mcu_types
[i
].name
; ++i
)
246 if (strcmp (mcu_types
[i
].name
, s
) == 0)
249 if (!mcu_types
[i
].name
)
250 as_fatal (_ ("unknown MCU: %s\n"), arg
);
252 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
253 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
254 as .arch ... in the asm output at the same time. */
256 if (avr_mcu
== &default_mcu
|| avr_mcu
->mach
== mcu_types
[i
].mach
)
257 avr_mcu
= &mcu_types
[i
];
259 as_fatal (_ ("redefinition of mcu type `%s' to `%s'"),
260 avr_mcu
->name
, mcu_types
[i
].name
);
267 md_undefined_symbol(name
)
268 char *name ATTRIBUTE_UNUSED
;
273 /* Convert a string pointed to by input_line_pointer into a floating point
274 constant of type `type', and store the appropriate bytes to `*litP'.
275 The number of LITTLENUMS emitted is stored in `*sizeP'. Returns NULL if
276 OK, or an error message otherwise. */
278 md_atof (type
, litP
, sizeP
)
284 LITTLENUM_TYPE words
[4];
285 LITTLENUM_TYPE
*wordP
;
298 return _("bad call to md_atof");
301 t
= atof_ieee (input_line_pointer
, type
, words
);
303 input_line_pointer
= t
;
305 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
306 /* This loop outputs the LITTLENUMs in REVERSE order. */
307 for (wordP
= words
+ prec
- 1; prec
--;)
309 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
310 litP
+= sizeof (LITTLENUM_TYPE
);
316 md_convert_frag (abfd
, sec
, fragP
)
317 bfd
*abfd ATTRIBUTE_UNUSED
;
318 asection
*sec ATTRIBUTE_UNUSED
;
319 fragS
*fragP ATTRIBUTE_UNUSED
;
329 struct avr_opcodes_s
*opcode
;
330 avr_hash
= hash_new();
332 /* Insert unique names into hash table. This hash table then provides a
333 quick index to the first opcode with a particular name in the opcode
336 for (opcode
= avr_opcodes
; opcode
->name
; opcode
++)
337 hash_insert (avr_hash
, opcode
->name
, (char *) opcode
);
339 avr_mod_hash
= hash_new ();
341 for (i
= 0; i
< sizeof (exp_mod
) / sizeof (exp_mod
[0]); ++i
)
342 hash_insert (avr_mod_hash
, EXP_MOD_NAME(i
), (void*)(i
+10));
344 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, avr_mcu
->mach
);
348 /* Resolve STR as a constant expression and return the result.
349 If result greater than MAX then error. */
352 avr_get_constant (str
, max
)
357 str
= skip_space (str
);
358 input_line_pointer
= str
;
361 if (ex
.X_op
!= O_constant
)
362 as_bad (_("constant value required"));
364 if (ex
.X_add_number
> max
|| ex
.X_add_number
< 0)
365 as_bad (_("number must be less than %d"), max
+1);
366 return ex
.X_add_number
;
370 /* Parse instruction operands.
371 Returns binary opcode. */
374 avr_operands (opcode
, line
)
375 struct avr_opcodes_s
*opcode
;
378 char *op
= opcode
->constraints
;
379 unsigned int bin
= opcode
->bin_opcode
;
380 char *frag
= frag_more (opcode
->insn_size
* 2);
382 int where
= frag
- frag_now
->fr_literal
;
383 static unsigned int prev
= 0; /* previous opcode */
385 /* Opcode have operands. */
388 unsigned int reg1
= 0;
389 unsigned int reg2
= 0;
390 int reg1_present
= 0;
391 int reg2_present
= 0;
393 /* Parse first operand. */
394 if (REGISTER_P (*op
))
396 reg1
= avr_operand (opcode
, where
, op
, &str
);
399 /* Parse second operand. */
411 if (REGISTER_P (*op
))
414 str
= skip_space (str
);
416 as_bad (_ ("`,' required"));
417 str
= skip_space (str
);
419 reg2
= avr_operand (opcode
, where
, op
, &str
);
422 if (reg1_present
&& reg2_present
)
423 reg2
= (reg2
& 0xf) | ((reg2
<< 5) & 0x200);
424 else if (reg2_present
)
432 /* detect undefined combinations (like lpm r31,Z+) */
433 if (((bin
& 0xFDEF) == 0x91AD) || ((bin
& 0xFDEF) == 0x91AE) ||
434 ((bin
& 0xFDEF) == 0x91C9) || ((bin
& 0xFDEF) == 0x91CA) ||
435 ((bin
& 0xFDEF) == 0x91E1) || ((bin
& 0xFDEF) == 0x91E2) ||
436 ((bin
& 0xFFED) == 0x91E5))
437 as_warn( _("undefined combination of operands"));
439 if (opcode
->insn_size
== 2)
441 /* warn if previous opcode was cpse/sbic/sbis/sbrc/sbrs
443 if ((prev
& 0xFC00) == 0x1000
444 || (prev
& 0xFD00) == 0x9900
445 || (prev
& 0xFC08) == 0xFC00)
446 as_warn (_("skipping two-word instruction"));
448 bfd_putl32 ((bfd_vma
)bin
, frag
);
451 bfd_putl16 ((bfd_vma
)bin
, frag
);
459 /* Parse one instruction operand.
460 Returns operand bitmask. Also fixups can be generated. */
463 avr_operand (opcode
, where
, op
, line
)
464 struct avr_opcodes_s
*opcode
;
470 unsigned int op_mask
= 0;
471 char *str
= skip_space (*line
);
475 /* Any register operand. */
484 if (*str
== 'r' || *str
== 'R')
488 str
= extract_word (str
, r_name
, sizeof (r_name
));
489 if (isdigit(r_name
[1]))
491 if (r_name
[2] == '\0')
492 op_mask
= r_name
[1] - '0';
493 else if (r_name
[1] != '0'
494 && isdigit(r_name
[2])
495 && r_name
[3] == '\0')
496 op_mask
= (r_name
[1] - '0') * 10 + r_name
[2] - '0';
501 op_mask
= avr_get_constant (str
, 31);
502 str
= input_line_pointer
;
510 if (op_mask
< 16 || op_mask
> 23)
511 as_bad (_ ("register r16-r23 required"));
517 as_bad (_ ("register number above 15 required"));
523 as_bad (_ ("even register number required"));
529 if (op_mask
& 1 || op_mask
> 6)
530 as_bad (_ ("register r24,r26,r28 or r30 required"));
536 as_bad (_ ("register name or number from 0 to 31 required"));
545 str
= skip_space (str
+1);
554 as_bad (_ ("pointer register (X,Y or Z) required"));
556 str
= skip_space (str
+1);
561 as_bad (_ ("cannot both predecrement and postincrement"));
565 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
566 registers, no predecrement, no postincrement. */
568 if ((op_mask
& 0x100F) && !(avr_mcu
->isa
& AVR_ISA_SRAM
))
569 as_bad (_ ("addressing mode not supported"));
576 as_bad (_ ("can't predecrement"));
578 if (! (*str
== 'z' || *str
== 'Z'))
579 as_bad (_ ("pointer register Z required"));
581 str
= skip_space (str
+ 1);
592 char c
= tolower (*str
++);
596 as_bad (_ ("pointer register (Y or Z) required"));
597 str
= skip_space (str
);
601 x
= avr_get_constant (str
, 63);
602 str
= input_line_pointer
;
603 op_mask
|= (x
& 7) | ((x
& (3 << 3)) << 7) | ((x
& (1 << 5)) << 8);
610 str
= parse_exp (str
, &op_expr
);
611 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
612 &op_expr
, false, BFD_RELOC_AVR_CALL
);
619 str
= parse_exp (str
, &op_expr
);
620 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
621 &op_expr
, true, BFD_RELOC_AVR_13_PCREL
);
628 str
= parse_exp (str
, &op_expr
);
629 fix_new_exp (frag_now
, where
, opcode
->insn_size
* 2,
630 &op_expr
, true, BFD_RELOC_AVR_7_PCREL
);
637 str
= parse_exp (str
, &op_expr
);
638 fix_new_exp (frag_now
, where
+2, opcode
->insn_size
* 2,
639 &op_expr
, false, BFD_RELOC_16
);
646 bfd_reloc_code_real_type r_type
;
647 input_line_pointer
= str
;
648 r_type
= avr_ldi_expression (&op_expr
);
649 str
= input_line_pointer
;
650 fix_new_exp (frag_now
, where
, 3,
651 &op_expr
, false, r_type
);
658 x
= ~avr_get_constant (str
, 255);
659 str
= input_line_pointer
;
660 op_mask
|= (x
& 0xf) | ((x
<< 4) & 0xf00);
667 x
= avr_get_constant (str
, 63);
668 str
= input_line_pointer
;
669 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 2);
677 x
= avr_get_constant (str
, 7);
678 str
= input_line_pointer
;
688 x
= avr_get_constant (str
, 63);
689 str
= input_line_pointer
;
690 op_mask
|= (x
& 0xf) | ((x
& 0x30) << 5);
697 x
= avr_get_constant (str
, 31);
698 str
= input_line_pointer
;
705 as_bad (_ ("unknown constraint `%c'"), *op
);
711 /* GAS will call this function for each section at the end of the assembly,
712 to permit the CPU backend to adjust the alignment of a section. */
714 md_section_align (seg
, addr
)
718 int align
= bfd_get_section_alignment (stdoutput
, seg
);
719 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
722 /* If you define this macro, it should return the offset between the
723 address of a PC relative fixup and the position from which the PC
724 relative adjustment should be made. On many processors, the base
725 of a PC relative instruction is the next instruction, so this
726 macro would return the length of an instruction. */
728 md_pcrel_from_section (fixp
, sec
)
732 if (fixp
->fx_addsy
!= (symbolS
*)NULL
733 && (!S_IS_DEFINED (fixp
->fx_addsy
)
734 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
736 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
739 /* GAS will call this for each fixup. It should store the correct
740 value in the object file. */
742 md_apply_fix3 (fixp
, valuep
, seg
)
747 unsigned char *where
;
751 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
756 else if (fixp
->fx_pcrel
)
758 segT s
= S_GET_SEGMENT (fixp
->fx_addsy
);
759 if (fixp
->fx_addsy
&& (s
== seg
|| s
== absolute_section
))
761 value
= S_GET_VALUE (fixp
->fx_addsy
) + *valuep
;
769 value
= fixp
->fx_offset
;
770 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
772 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
774 value
-= S_GET_VALUE (fixp
->fx_subsy
);
779 /* We don't actually support subtracting a symbol. */
780 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
781 _ ("expression too complex"));
785 switch (fixp
->fx_r_type
)
788 fixp
->fx_no_overflow
= 1;
790 case BFD_RELOC_AVR_7_PCREL
:
791 case BFD_RELOC_AVR_13_PCREL
:
794 case BFD_RELOC_AVR_CALL
:
800 /* Fetch the instruction, insert the fully resolved operand
801 value, and stuff the instruction back again. */
802 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
803 insn
= bfd_getl16 (where
);
805 switch (fixp
->fx_r_type
)
807 case BFD_RELOC_AVR_7_PCREL
:
809 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
810 _("odd address operand: %ld"), value
);
811 /* Instruction addresses are always right-shifted by 1. */
813 --value
; /* Correct PC. */
814 if (value
< -64 || value
> 63)
815 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
816 _("operand out of range: %ld"), value
);
817 value
= (value
<< 3) & 0x3f8;
818 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
821 case BFD_RELOC_AVR_13_PCREL
:
823 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
824 _("odd address operand: %ld"), value
);
825 /* Instruction addresses are always right-shifted by 1. */
827 --value
; /* Correct PC. */
829 if (value
< -2048 || value
> 2047)
831 /* No wrap for devices with >8K of program memory. */
832 if (avr_mcu
->isa
& AVR_ISA_MEGA
)
833 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
834 _("operand out of range: %ld"), value
);
838 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
842 bfd_putl16 ((bfd_vma
) value
, where
);
846 bfd_putl16 ((bfd_vma
) value
, where
);
849 case BFD_RELOC_AVR_16_PM
:
850 bfd_putl16 ((bfd_vma
) (value
>>1), where
);
853 case BFD_RELOC_AVR_LO8_LDI
:
854 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
), where
);
857 case -BFD_RELOC_AVR_LO8_LDI
:
858 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
861 case BFD_RELOC_AVR_HI8_LDI
:
862 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 8), where
);
865 case -BFD_RELOC_AVR_HI8_LDI
:
866 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 24), where
);
869 case BFD_RELOC_AVR_HH8_LDI
:
870 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 16), where
);
873 case BFD_RELOC_AVR_LO8_LDI_NEG
:
874 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
), where
);
877 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
878 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
881 case BFD_RELOC_AVR_HI8_LDI_NEG
:
882 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 8), where
);
885 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
886 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 24), where
);
889 case BFD_RELOC_AVR_HH8_LDI_NEG
:
890 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 16), where
);
893 case BFD_RELOC_AVR_LO8_LDI_PM
:
894 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 1), where
);
897 case BFD_RELOC_AVR_HI8_LDI_PM
:
898 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 9), where
);
901 case BFD_RELOC_AVR_HH8_LDI_PM
:
902 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (value
>> 17), where
);
905 case BFD_RELOC_AVR_LO8_LDI_PM_NEG
:
906 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 1), where
);
909 case BFD_RELOC_AVR_HI8_LDI_PM_NEG
:
910 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 9), where
);
913 case BFD_RELOC_AVR_HH8_LDI_PM_NEG
:
914 bfd_putl16 ((bfd_vma
) insn
| LDI_IMMEDIATE (-value
>> 17), where
);
917 case BFD_RELOC_AVR_CALL
:
920 x
= bfd_getl16 (where
);
922 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
923 _("odd address operand: %ld"), value
);
925 x
|= ((value
& 0x10000) | ((value
<< 3) & 0x1f00000)) >> 16;
926 bfd_putl16 ((bfd_vma
) x
, where
);
927 bfd_putl16 ((bfd_vma
) (value
& 0xffff), where
+2);
932 as_fatal ( _("line %d: unknown relocation type: 0x%x"),
933 fixp
->fx_line
, fixp
->fx_r_type
);
939 switch (fixp
->fx_r_type
)
941 case -BFD_RELOC_AVR_HI8_LDI_NEG
:
942 case -BFD_RELOC_AVR_HI8_LDI
:
943 case -BFD_RELOC_AVR_LO8_LDI_NEG
:
944 case -BFD_RELOC_AVR_LO8_LDI
:
945 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
946 _("only constant expression allowed"));
952 fixp
->fx_addnumber
= value
;
958 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
959 will pass the resulting reloc to `bfd_install_relocation'. This
960 currently works poorly, as `bfd_install_relocation' often does the
961 wrong thing, and instances of `tc_gen_reloc' have been written to
962 work around the problems, which in turns makes it difficult to fix
963 `bfd_install_relocation'. */
965 /* If while processing a fixup, a reloc really needs to be created
966 then it is done here. */
969 tc_gen_reloc (seg
, fixp
)
970 asection
*seg ATTRIBUTE_UNUSED
;
975 reloc
= (arelent
*) xmalloc (sizeof (arelent
));
977 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
978 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
980 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
981 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
982 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
984 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
985 _("reloc %d not supported by object file format"),
986 (int)fixp
->fx_r_type
);
990 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
991 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
992 reloc
->address
= fixp
->fx_offset
;
994 reloc
->addend
= fixp
->fx_offset
;
1004 struct avr_opcodes_s
* opcode
;
1007 str
= skip_space (extract_word (str
, op
, sizeof(op
)));
1010 as_bad (_ ("can't find opcode "));
1012 opcode
= (struct avr_opcodes_s
*) hash_find (avr_hash
, op
);
1016 as_bad (_ ("unknown opcode `%s'"), op
);
1020 /* Special case for opcodes with optional operands (lpm, elpm) -
1021 version with operands exists in avr_opcodes[] in the next entry. */
1023 if (*str
&& *opcode
->constraints
== '?')
1026 if ((opcode
->isa
& avr_mcu
->isa
) != opcode
->isa
)
1027 as_bad (_ ("illegal opcode %s for mcu %s"), opcode
->name
, avr_mcu
->name
);
1029 /* We used to set input_line_pointer to the result of get_operands,
1030 but that is wrong. Our caller assumes we don't change it. */
1032 char *t
= input_line_pointer
;
1033 avr_operands (opcode
, &str
);
1034 if (*skip_space (str
))
1035 as_bad (_ ("garbage at end of line"));
1036 input_line_pointer
= t
;
1040 /* Parse ordinary expression. */
1046 input_line_pointer
= s
;
1048 if (op
->X_op
== O_absent
)
1049 as_bad (_("missing operand"));
1050 return input_line_pointer
;
1054 /* Parse special expressions (needed for LDI command):
1059 where xx is: hh, hi, lo
1061 static bfd_reloc_code_real_type
1062 avr_ldi_expression (exp
)
1065 char *str
= input_line_pointer
;
1071 str
= extract_word (str
, op
, sizeof (op
));
1074 mod
= (int) hash_find (avr_mod_hash
, op
);
1079 str
= skip_space (str
);
1084 if (strncmp ("pm(", str
, 3) == 0
1085 || strncmp ("-(pm(", str
, 5) == 0)
1093 as_bad (_ ("illegal expression"));
1103 if (*str
== '-' && *(str
+ 1) == '(')
1109 input_line_pointer
= str
;
1113 if (*input_line_pointer
!= ')')
1115 as_bad (_ ("`)' required"));
1118 input_line_pointer
++;
1121 return neg_p
? EXP_MOD_NEG_RELOC (mod
) : EXP_MOD_RELOC (mod
);
1125 input_line_pointer
= tmp
;
1127 return BFD_RELOC_AVR_LO8_LDI
;
1130 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1131 `avr_cons_fix_new' */
1132 static int exp_mod_pm
= 0;
1134 /* Parse special CONS expression: pm (expression)
1135 which is used for addressing to a program memory.
1136 Relocation: BFD_RELOC_AVR_16_PM */
1138 avr_parse_cons_expression (exp
, nbytes
)
1146 tmp
= input_line_pointer
= skip_space (input_line_pointer
);
1150 char * pm_name
= "pm";
1151 int len
= strlen (pm_name
);
1152 if (strncasecmp (input_line_pointer
, pm_name
, len
) == 0)
1154 input_line_pointer
= skip_space (input_line_pointer
+ len
);
1155 if (*input_line_pointer
== '(')
1157 input_line_pointer
= skip_space (input_line_pointer
+ 1);
1160 if (*input_line_pointer
== ')')
1161 ++input_line_pointer
;
1164 as_bad (_ ("`)' required"));
1169 input_line_pointer
= tmp
;
1176 avr_cons_fix_new(frag
, where
, nbytes
, exp
)
1182 if (exp_mod_pm
== 0)
1185 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_16
);
1186 else if (nbytes
== 4)
1187 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_32
);
1189 as_bad (_ ("illegal %srelocation size: %d"), "", nbytes
);
1194 fix_new_exp (frag
, where
, nbytes
, exp
, false, BFD_RELOC_AVR_16_PM
);
1196 as_bad (_ ("illegal %srelocation size: %d"), "`pm' ", nbytes
);