1 /* Copyright (C) 1998-2019 Free Software Foundation, Inc.
2    Contributed by Joern Rennecke
3 
4    This file is part of GCC.
5 
6    GCC is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    GCC is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #define IN_TARGET_CODE 1
25 
26 #include "config.h"
27 
28 #define IN_GEN_AVR_MMCU_TEXI
29 
30 #include "avr-devices.c"
31 
32 // Get rid of "defaults.h".  We just need tm.h for `WITH_AVRLIBC' and
33 // and `WITH_RTEMS'.  */
34 #define GCC_DEFAULTS_H
35 
36 #include "tm.h"
37 
38 // Mimic the include order as specified in config.gcc::tm_file.
39 
40 #include "specs.h"
41 
42 #if defined (WITH_AVRLIBC)
43 #include "avrlibc.h"
44 #endif
45 
46 
47 #define SPECFILE_DOC_URL                                \
48   "https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html"
49 
50 #define SPECFILE_USAGE_URL                              \
51   "https://gcc.gnu.org/gcc-5/changes.html"
52 
53 
54 static const char header[] =
55   "#\n"
56   "# Generated by   : ./gcc/config/avr/gen-avr-mmcu-specs.c\n"
57   "# Generated from : ./gcc/config/gcc.c\n"
58   "#                  ./gcc/config/avr/specs.h\n"
59 #if defined (WITH_AVRLIBC)
60   "#                  ./gcc/config/avr/avrlibc.h\n"
61 #endif
62   "# Used by        : avr-gcc compiler driver\n"
63   "# Used for       : building command options for sub-processes\n"
64   "#\n"
65   "# See <" SPECFILE_DOC_URL ">\n"
66   "# for a documentation of spec files.\n"
67   "\n";
68 
69 static const char help_copy_paste[] =
70   "# If you intend to use an existing device specs file as a starting point\n"
71   "# for a new device spec file, make sure you are copying from a specs\n"
72   "# file for a device from the same core architecture and SP width.\n"
73   "# See <" SPECFILE_USAGE_URL "> for a description\n"
74   "# of how to use such own spec files.\n";
75 
76 #if defined (WITH_AVRLIBC)
77 static const char help_dev_lib_name[] =
78   "# AVR-LibC's avr/io.h uses the device specifying macro to determine\n"
79   "# the name of the device header.  For example, -mmcu=atmega8a triggers\n"
80   "# the definition of __AVR_ATmega8A__ and avr/io.h includes the device\n"
81   "# header 'iom8a.h' by means of:\n"
82   "#\n"
83   "#     ...\n"
84   "#     #elif defined (__AVR_ATmega8A__)\n"
85   "#     #  include <avr/iom8a.h>\n"
86   "#     #elif ...\n"
87   "# \n"
88   "# If no device macro is defined, AVR-LibC uses __AVR_DEV_LIB_NAME__\n"
89   "# as fallback to determine the name of the device header as\n"
90   "#\n"
91   "#     \"avr/io\" + __AVR_DEV_LIB_NAME__ + \".h\"\n"
92   "#\n"
93   "# If you provide your own specs file for a device not yet known to\n"
94   "# AVR-LibC, you can now define the hook macro __AVR_DEV_LIB_NAME__\n"
95   "# as needed so that\n"
96   "#\n"
97   "#     #include <avr/io.h>\n"
98   "#\n"
99   "# will include the desired device header.  For ATmega8A the supplement\n"
100   "# to *cpp_avrlibc would read\n"
101   "#\n"
102   "#     -D__AVR_DEV_LIB_NAME__=m8a\n"
103   "\n";
104 #endif // WITH_AVRLIBC
105 
106 static void
print_mcu(const avr_mcu_t * mcu)107 print_mcu (const avr_mcu_t *mcu)
108 {
109   const char *sp8_spec;
110   const char *rcall_spec;
111   const avr_mcu_t *arch_mcu;
112   const avr_arch_t *arch;
113   enum avr_arch_id arch_id = mcu->arch_id;
114 
115   for (arch_mcu = mcu; arch_mcu->macro; )
116     arch_mcu--;
117   if (arch_mcu->arch_id != arch_id)
118     exit (EXIT_FAILURE);
119 
120   arch = &avr_arch_types[arch_id];
121 
122   char name[100];
123   if (snprintf (name, sizeof name, "specs-%s", mcu->name) >= (int) sizeof name)
124    exit (EXIT_FAILURE);
125 
126   FILE *f = fopen (name ,"w");
127 
128   bool absdata = (mcu->dev_attribute & AVR_ISA_LDS) != 0;
129   bool errata_skip = (mcu->dev_attribute & AVR_ERRATA_SKIP) != 0;
130   bool rmw = (mcu->dev_attribute & AVR_ISA_RMW) != 0;
131   bool sp8 = (mcu->dev_attribute & AVR_SHORT_SP) != 0;
132   bool rcall = (mcu->dev_attribute & AVR_ISA_RCALL);
133   bool is_arch = mcu->macro == NULL;
134   bool is_device = ! is_arch;
135   int flash_pm_offset = 0;
136 
137   if (arch->flash_pm_offset
138       && mcu->flash_pm_offset
139       && mcu->flash_pm_offset != arch->flash_pm_offset)
140     {
141       flash_pm_offset = mcu->flash_pm_offset;
142     }
143 
144   if (is_arch
145       && (ARCH_AVR2 == arch_id
146           || ARCH_AVR25 == arch_id))
147     {
148       // Leave "avr2" and "avr25" alone.  These two architectures are
149       // the only ones that mix devices with 8-bit SP and 16-bit SP.
150       sp8_spec = "";
151     }
152   else
153     {
154       sp8_spec = sp8 ? "-msp8" :"%<msp8";
155     }
156 
157   if (is_arch
158       && ARCH_AVRXMEGA3 == arch_id)
159     {
160       // Leave "avrxmega3" alone.  This architectures is the only one
161       // that mixes devices with and without JMP / CALL.
162       rcall_spec = "";
163     }
164   else
165     {
166       rcall_spec = rcall ? "-mshort-calls" : "%<mshort-calls";
167     }
168 
169   fprintf (f, "#\n"
170            "# Auto-generated specs for AVR ");
171   if (is_arch)
172     fprintf (f, "core architecture %s\n", arch->name);
173   else
174     fprintf (f, "device %s (core %s, %d-bit SP%s)\n", mcu->name,
175              arch->name, sp8 ? 8 : 16, rcall ? ", short-calls" : "");
176   fprintf (f, "%s\n", header);
177 
178   if (is_device)
179     fprintf (f, "%s\n", help_copy_paste);
180 
181 #if defined (WITH_AVRLIBC)
182   // AVR-LibC specific.  See avrlibc.h for the specs using them as subspecs.
183 
184   if (is_device)
185     {
186       fprintf (f, "*avrlibc_startfile:\n");
187       fprintf (f, "\tcrt%s.o%%s", mcu->name);
188       fprintf (f, "\n\n");
189 
190       fprintf (f, "*avrlibc_devicelib:\n");
191       fprintf (f, "\t%%{!nodevicelib:-l%s}", mcu->name);
192       fprintf (f, "\n\n");
193     }
194 #endif  // WITH_AVRLIBC
195 
196   // avr-gcc specific specs for the compilation / the compiler proper.
197 
198   int n_flash = 1 + (mcu->flash_size - 1) / 0x10000;
199 
200   fprintf (f, "*cc1_n_flash:\n"
201            "\t%%{!mn-flash=*:-mn-flash=%d}\n\n", n_flash);
202 
203   fprintf (f, "*cc1_rmw:\n%s\n\n", rmw
204            ? "\t%{!mno-rmw: -mrmw}"
205            : "\t%{mrmw}");
206 
207   fprintf (f, "*cc1_errata_skip:\n%s\n\n", errata_skip
208            ? "\t%{!mno-skip-bug: -mskip-bug}"
209            : "\t%{!mskip-bug: -mno-skip-bug}");
210 
211   fprintf (f, "*cc1_absdata:\n%s\n\n", absdata
212            ? "\t%{!mno-absdata: -mabsdata}"
213            : "\t%{mabsdata}");
214 
215   // avr-gcc specific specs for assembling / the assembler.
216 
217   fprintf (f, "*asm_arch:\n\t-mmcu=%s\n\n", arch->name);
218 
219 #ifdef HAVE_AS_AVR_MLINK_RELAX_OPTION
220   fprintf (f, "*asm_relax:\n\t%s\n\n", ASM_RELAX_SPEC);
221 #endif // have avr-as --mlink-relax
222 
223 #ifdef HAVE_AS_AVR_MRMW_OPTION
224   fprintf (f, "*asm_rmw:\n%s\n\n", rmw
225            ? "\t%{!mno-rmw: -mrmw}"
226            : "\t%{mrmw}");
227 #endif // have avr-as -mrmw
228 
229 #ifdef HAVE_AS_AVR_MGCCISR_OPTION
230   fprintf (f, "*asm_gccisr:\n%s\n\n",
231            "\t%{!mno-gas-isr-prologues: -mgcc-isr}");
232 #endif // have avr-as -mgcc-isr
233 
234   fprintf (f, "*asm_errata_skip:\n%s\n\n", errata_skip
235            ? "\t%{mno-skip-bug}"
236            : "\t%{!mskip-bug: -mno-skip-bug}");
237 
238   // avr-specific specs for linking / the linker.
239 
240   int wrap_k =
241     mcu->flash_size == 0x2000 ? 8
242     : mcu->flash_size == 0x4000 ? 16
243     : mcu->flash_size == 0x8000 ? 32
244     : mcu->flash_size == 0x10000 ? 64
245     : 0;
246 
247   fprintf (f, "*link_pmem_wrap:\n");
248   if (wrap_k == 8)
249     fprintf (f, "\t%%{!mno-pmem-wrap-around: --pmem-wrap-around=8k}");
250   else if (wrap_k > 8)
251     fprintf (f, "\t%%{mpmem-wrap-around: --pmem-wrap-around=%dk}", wrap_k);
252   fprintf (f, "\n\n");
253 
254   fprintf (f, "*link_relax:\n\t%s\n\n", LINK_RELAX_SPEC);
255 
256   fprintf (f, "*link_arch:\n\t%s", LINK_ARCH_SPEC);
257   if (is_device
258       && flash_pm_offset)
259     fprintf (f, " --defsym=__RODATA_PM_OFFSET__=0x%x", flash_pm_offset);
260   fprintf (f, "\n\n");
261 
262   if (is_device)
263     {
264       fprintf (f, "*link_data_start:\n");
265       if (mcu->data_section_start
266           != arch->default_data_section_start)
267         fprintf (f, "\t%%{!Tdata:-Tdata 0x%lX}",
268                  0x800000UL + mcu->data_section_start);
269       fprintf (f, "\n\n");
270 
271       fprintf (f, "*link_text_start:\n");
272       if (mcu->text_section_start != 0x0)
273         fprintf (f, "\t%%{!Ttext:-Ttext 0x%lX}", 0UL + mcu->text_section_start);
274       fprintf (f, "\n\n");
275     }
276 
277   // Specs known to GCC.
278 
279   if (is_device)
280     {
281       fprintf (f, "*self_spec:\n");
282       fprintf (f, "\t%%{!mmcu=avr*: %%<mmcu=* -mmcu=%s} ", arch->name);
283       fprintf (f, "%s ", rcall_spec);
284       fprintf (f, "%s\n\n", sp8_spec);
285 
286 #if defined (WITH_AVRLIBC)
287       fprintf (f, "%s\n", help_dev_lib_name);
288 
289       fprintf (f, "*cpp_avrlibc:\n");
290       fprintf (f, "\t-D__AVR_DEVICE_NAME__=%s", mcu->name);
291       fprintf (f, "\n\n");
292 #endif // WITH_AVRLIBC
293 
294       fprintf (f, "*cpp_mcu:\n");
295       fprintf (f, "\t-D%s", mcu->macro);
296       if (flash_pm_offset)
297 	{
298 	  fprintf (f, " -U__AVR_PM_BASE_ADDRESS__");
299 	  fprintf (f, " -D__AVR_PM_BASE_ADDRESS__=0x%x", flash_pm_offset);
300 	}
301       fprintf (f, "\n\n");
302 
303       fprintf (f, "*cpp:\n");
304       fprintf (f, "\t%%(cpp_mcu)");
305 #if defined (WITH_AVRLIBC)
306       fprintf (f, " %%(cpp_avrlibc)");
307 #endif // WITH_AVRLIBC
308       fprintf (f, "\n\n");
309     }
310 
311   fprintf (f, "# End of file\n");
312 
313   fclose (f);
314 }
315 
316 
main(void)317 int main (void)
318 {
319   for (const avr_mcu_t *mcu = avr_mcu_types; mcu->name; mcu++)
320     print_mcu (mcu);
321 
322   return EXIT_SUCCESS;
323 }
324