xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/cpu-avr.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* BFD library support routines for the AVR architecture.
2*e992f068Schristos    Copyright (C) 1999-2022 Free Software Foundation, Inc.
375fd0b74Schristos    Contributed by Denis Chertykov <denisc@overta.ru>
475fd0b74Schristos 
575fd0b74Schristos    This file is part of BFD, the Binary File Descriptor library.
675fd0b74Schristos 
775fd0b74Schristos    This program is free software; you can redistribute it and/or modify
875fd0b74Schristos    it under the terms of the GNU General Public License as published by
975fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
1075fd0b74Schristos    (at your option) any later version.
1175fd0b74Schristos 
1275fd0b74Schristos    This program is distributed in the hope that it will be useful,
1375fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1475fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1575fd0b74Schristos    GNU General Public License for more details.
1675fd0b74Schristos 
1775fd0b74Schristos    You should have received a copy of the GNU General Public License
1875fd0b74Schristos    along with this program; if not, write to the Free Software
1975fd0b74Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2075fd0b74Schristos    MA 02110-1301, USA.  */
2175fd0b74Schristos 
2275fd0b74Schristos #include "sysdep.h"
2375fd0b74Schristos #include "bfd.h"
2475fd0b74Schristos #include "libbfd.h"
2575fd0b74Schristos 
2675fd0b74Schristos /* This routine is provided two arch_infos and works out which AVR
2775fd0b74Schristos    machine which would be compatible with both and returns a pointer
2875fd0b74Schristos    to its info structure.  */
2975fd0b74Schristos 
3075fd0b74Schristos static const bfd_arch_info_type *
compatible(const bfd_arch_info_type * a,const bfd_arch_info_type * b)3175fd0b74Schristos compatible (const bfd_arch_info_type * a,
3275fd0b74Schristos 	    const bfd_arch_info_type * b)
3375fd0b74Schristos {
3475fd0b74Schristos   /* If a & b are for different architectures we can do nothing.  */
3575fd0b74Schristos   if (a->arch != b->arch)
3675fd0b74Schristos     return NULL;
3775fd0b74Schristos 
3875fd0b74Schristos   if (a->mach == b->mach)
3975fd0b74Schristos     return a;
4075fd0b74Schristos 
4175fd0b74Schristos   /* avr-6 is compatible only with itself as its call convention is not
4275fd0b74Schristos      compatible with other avr (the mcu saves the return address on 3 bytes
4375fd0b74Schristos      instead of 2).  */
4475fd0b74Schristos   if (a->mach == bfd_mach_avr6 || b->mach == bfd_mach_avr6)
4575fd0b74Schristos     return NULL;
4675fd0b74Schristos 
4775fd0b74Schristos   if (a->mach < bfd_mach_avr6 && b->mach < bfd_mach_avr6)
4875fd0b74Schristos     {
4975fd0b74Schristos       /* Special case for ATmega[16]03 (avr:3) and ATmega83 (avr:4).  */
5075fd0b74Schristos       if ((a->mach == bfd_mach_avr3 && b->mach == bfd_mach_avr4)
5175fd0b74Schristos 	 || (a->mach == bfd_mach_avr4 && b->mach == bfd_mach_avr3))
5275fd0b74Schristos        return NULL;
5375fd0b74Schristos 
5475fd0b74Schristos       if (a->mach <= b->mach)
5575fd0b74Schristos        return b;
5675fd0b74Schristos 
5775fd0b74Schristos       if (a->mach >= b->mach)
5875fd0b74Schristos        return a;
5975fd0b74Schristos     }
6075fd0b74Schristos 
6175fd0b74Schristos   if (a->mach == bfd_mach_avr2 && b->mach == bfd_mach_avr25)
6275fd0b74Schristos     return a;
6375fd0b74Schristos   if (a->mach == bfd_mach_avr25 && b->mach == bfd_mach_avr2)
6475fd0b74Schristos     return b;
6575fd0b74Schristos 
6675fd0b74Schristos   if (a->mach == bfd_mach_avr3 && b->mach == bfd_mach_avr31)
6775fd0b74Schristos     return a;
6875fd0b74Schristos   if (a->mach == bfd_mach_avr31 && b->mach == bfd_mach_avr3)
6975fd0b74Schristos     return b;
7075fd0b74Schristos   if (a->mach == bfd_mach_avr3 && b->mach == bfd_mach_avr35)
7175fd0b74Schristos     return a;
7275fd0b74Schristos   if (a->mach == bfd_mach_avr35 && b->mach == bfd_mach_avr3)
7375fd0b74Schristos     return b;
7475fd0b74Schristos 
7575fd0b74Schristos   if (a->mach == bfd_mach_avr5 && b->mach == bfd_mach_avr51)
7675fd0b74Schristos     return a;
7775fd0b74Schristos   if (a->mach == bfd_mach_avr51 && b->mach == bfd_mach_avr5)
7875fd0b74Schristos     return b;
7975fd0b74Schristos 
8075fd0b74Schristos   return NULL;
8175fd0b74Schristos }
8275fd0b74Schristos 
8375fd0b74Schristos #define N(addr_bits, machine, print, default, next)		\
8475fd0b74Schristos {								\
85012573ebSchristos   8,				/* Bits in a word.  */		\
86012573ebSchristos   addr_bits,			/* Bits in an address.  */	\
87012573ebSchristos   8,				/* Bits in a byte.  */		\
8875fd0b74Schristos   bfd_arch_avr,							\
8975fd0b74Schristos   machine,			/* Machine number.  */		\
9075fd0b74Schristos   "avr",			/* Architecture name.   */	\
9175fd0b74Schristos   print,			/* Printable name.  */		\
9275fd0b74Schristos   1,				/* Section align power.  */	\
9375fd0b74Schristos   default,			/* Is this the default ?  */	\
9475fd0b74Schristos   compatible,							\
9575fd0b74Schristos   bfd_default_scan,						\
9675fd0b74Schristos   bfd_arch_default_fill,					\
97012573ebSchristos   next,								\
98012573ebSchristos   0 /* Maximum offset of a reloc from the start of an insn.  */	\
9975fd0b74Schristos }
10075fd0b74Schristos 
10175fd0b74Schristos static const bfd_arch_info_type arch_info_struct[] =
10275fd0b74Schristos {
10375fd0b74Schristos   /* Assembler only.  */
104*e992f068Schristos   N (16, bfd_mach_avr1, "avr:1", false, & arch_info_struct[1]),
10575fd0b74Schristos 
10675fd0b74Schristos   /* Classic, <= 8K.  */
107*e992f068Schristos   N (16, bfd_mach_avr2, "avr:2", false, & arch_info_struct[2]),
10875fd0b74Schristos 
10975fd0b74Schristos   /* Classic + MOVW, <= 8K.  */
110*e992f068Schristos   N (16, bfd_mach_avr25, "avr:25", false, & arch_info_struct[3]),
11175fd0b74Schristos 
11275fd0b74Schristos   /* Classic, > 8K, <= 64K.  */
11375fd0b74Schristos   /* TODO:  addr_bits should be 16, but set to 22 for some following
11475fd0b74Schristos      version of GCC (from 4.3) for backward compatibility.  */
115*e992f068Schristos   N (22, bfd_mach_avr3, "avr:3", false, & arch_info_struct[4]),
11675fd0b74Schristos 
11775fd0b74Schristos   /* Classic, == 128K.  */
118*e992f068Schristos   N (22, bfd_mach_avr31, "avr:31", false, & arch_info_struct[5]),
11975fd0b74Schristos 
12075fd0b74Schristos   /* Classic + MOVW + JMP/CALL, > 8K, <= 64K. */
121*e992f068Schristos   N (16, bfd_mach_avr35, "avr:35", false, & arch_info_struct[6]),
12275fd0b74Schristos 
12375fd0b74Schristos   /* Enhanced, <= 8K.  */
124*e992f068Schristos   N (16, bfd_mach_avr4, "avr:4", false, & arch_info_struct[7]),
12575fd0b74Schristos 
12675fd0b74Schristos   /* Enhanced, > 8K, <= 64K.  */
12775fd0b74Schristos   /* TODO:  addr_bits should be 16, but set to 22 for some following
12875fd0b74Schristos      version of GCC (from 4.3) for backward compatibility.  */
129*e992f068Schristos   N (22, bfd_mach_avr5, "avr:5", false, & arch_info_struct[8]),
13075fd0b74Schristos 
13175fd0b74Schristos   /* Enhanced, == 128K.  */
132*e992f068Schristos   N (22, bfd_mach_avr51, "avr:51", false, & arch_info_struct[9]),
13375fd0b74Schristos 
13475fd0b74Schristos   /* 3-Byte PC.  */
135*e992f068Schristos   N (22, bfd_mach_avr6, "avr:6", false, & arch_info_struct[10]),
13675fd0b74Schristos 
13775fd0b74Schristos   /* Tiny core (AVR Tiny).  */
138*e992f068Schristos   N (16, bfd_mach_avrtiny, "avr:100", false, & arch_info_struct[11]),
13975fd0b74Schristos 
14075fd0b74Schristos   /* Xmega 1.  */
141*e992f068Schristos   N (24, bfd_mach_avrxmega1, "avr:101", false, & arch_info_struct[12]),
14275fd0b74Schristos 
14375fd0b74Schristos   /* Xmega 2.  */
144*e992f068Schristos   N (24, bfd_mach_avrxmega2, "avr:102", false, & arch_info_struct[13]),
14575fd0b74Schristos 
14675fd0b74Schristos   /* Xmega 3.  */
147*e992f068Schristos   N (24, bfd_mach_avrxmega3, "avr:103", false, & arch_info_struct[14]),
14875fd0b74Schristos 
14975fd0b74Schristos   /* Xmega 4.  */
150*e992f068Schristos   N (24, bfd_mach_avrxmega4, "avr:104", false, & arch_info_struct[15]),
15175fd0b74Schristos 
15275fd0b74Schristos   /* Xmega 5.  */
153*e992f068Schristos   N (24, bfd_mach_avrxmega5, "avr:105", false, & arch_info_struct[16]),
15475fd0b74Schristos 
15575fd0b74Schristos   /* Xmega 6.  */
156*e992f068Schristos   N (24, bfd_mach_avrxmega6, "avr:106", false, & arch_info_struct[17]),
15775fd0b74Schristos 
15875fd0b74Schristos   /* Xmega 7.  */
159*e992f068Schristos   N (24, bfd_mach_avrxmega7, "avr:107", false, NULL)
16075fd0b74Schristos 
16175fd0b74Schristos };
16275fd0b74Schristos 
16375fd0b74Schristos const bfd_arch_info_type bfd_avr_arch =
164*e992f068Schristos   N (16, bfd_mach_avr2, "avr", true, & arch_info_struct[0]);
165