Debian Codegraph @

gcc/config/avr/gen-avr-mmcu-specs.cc

raw
1 /* Build device-specs for AVR 8-bit microcontrollers.
2 Copyright (C) 1998-2025 Free Software Foundation, Inc.
3 Contributed by Joern Rennecke
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #define IN_TARGET_CODE 1
26
27 #include "config.h"
28
29 #define IN_GEN_AVR_MMCU_TEXI
30
31 #include "avr-devices.cc"
32
33 // Get rid of "defaults.h". We just need tm.h for `WITH_AVRLIBC' and
34 // and `WITH_RTEMS'.
35 #define GCC_DEFAULTS_H
36
37 #include "tm.h"
38
39 // Mimic the include order as specified in config.gcc::tm_file.
40
41 #include "specs.h"
42
43 #if defined (WITH_AVRLIBC)
44 #include "avrlibc.h"
45 #endif
46
47
48 #define SPECFILE_DOC_URL \
49 "https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html"
50
51 #define SPECFILE_USAGE_URL \
52 "https://gcc.gnu.org/gcc-5/changes.html"
53
54 #define WIKI_URL \
55 "https://gcc.gnu.org/wiki/avr-gcc#spec-files"
56
57 static const char header[] =
58 "#\n"
59 "# Generated by : ./gcc/config/avr/gen-avr-mmcu-specs.cc\n"
60 "# Generated from : ./gcc/config/gcc.cc\n"
61 "# ./gcc/config/avr/specs.h\n"
62 #if defined (WITH_AVRLIBC)
63 "# ./gcc/config/avr/avrlibc.h\n"
64 #endif
65 "# Used by : avr-gcc compiler driver\n"
66 "# Used for : building command options for sub-processes\n"
67 "#\n"
68 "# See <" SPECFILE_DOC_URL ">\n"
69 "# for a documentation of spec files.\n"
70 "\n";
71
72 static const char help_copy_paste[] =
73 "# If you intend to use an existing device specs file as a starting point\n"
74 "# for a new device spec file, make sure you are copying from a specs file\n"
75 "# for a device from the same or compatible:\n"
76 "# compiler version, compiler vendor, core architecture, SP width,\n"
77 "# short-calls, features like CVT and FLMAP.\n"
78 "# Otherwise, errors and wrong or sub-optimal code may likely occur.\n"
79 "# See <" WIKI_URL ">\n"
80 "# and <" SPECFILE_USAGE_URL "> for a description\n"
81 "# of how to use such own spec files.\n";
82
83 #if defined (WITH_AVRLIBC)
84 static const char help_dev_lib_name[] =
85 "# AVR-LibC's avr/io.h uses the device specifying macro to determine\n"
86 "# the name of the device header. For example, -mmcu=atmega8a triggers\n"
87 "# the definition of __AVR_ATmega8A__ and avr/io.h includes the device\n"
88 "# header 'iom8a.h' by means of:\n"
89 "#\n"
90 "# ...\n"
91 "# #elif defined (__AVR_ATmega8A__)\n"
92 "# # include <avr/iom8a.h>\n"
93 "# #elif ...\n"
94 "# \n"
95 "# If no device macro is defined, AVR-LibC uses __AVR_DEV_LIB_NAME__\n"
96 "# as fallback to determine the name of the device header as\n"
97 "#\n"
98 "# \"avr/io\" + __AVR_DEV_LIB_NAME__ + \".h\"\n"
99 "#\n"
100 "# If you provide your own specs file for a device not yet known to\n"
101 "# AVR-LibC, you can now define the hook macro __AVR_DEV_LIB_NAME__\n"
102 "# as needed so that\n"
103 "#\n"
104 "# #include <avr/io.h>\n"
105 "#\n"
106 "# will include the desired device header. For ATmega8A the supplement\n"
107 "# to *cpp_avrlibc would read\n"
108 "#\n"
109 "# -D__AVR_DEV_LIB_NAME__=m8a\n"
110 "\n";
111 #endif // WITH_AVRLIBC
112
113
114 #ifdef HAVE_LD_AVR_AVRXMEGA2_FLMAP
115 static const bool have_avrxmega2_flmap = true;
116 #else
117 static const bool have_avrxmega2_flmap = false;
118 #endif
119
120 #ifdef HAVE_LD_AVR_AVRXMEGA4_FLMAP
121 static const bool have_avrxmega4_flmap = true;
122 #else
123 static const bool have_avrxmega4_flmap = false;
124 #endif
125
126 #ifdef HAVE_LD_AVR_AVRXMEGA3_RODATA_IN_FLASH
127 static const bool have_avrxmega3_rodata_in_flash = true;
128 #else
129 static const bool have_avrxmega3_rodata_in_flash = false;
130 #endif
131
132
133 struct McuInfo
134 {
135 enum avr_arch_id arch_id;
136 const avr_arch_t *arch;
137 bool is_arch, is_device;
138 bool flmap, have_flmap2, have_flmap4, have_flmap;
139 bool rodata_in_flash, have_cvt;
140 // Device name as used by the vendor, extracted from "__AVR_<Name>__".
141 char mcu_Name[50] = { 0 };
142
143 McuInfo (const avr_mcu_t *mcu)
144 : arch_id (mcu->arch_id), arch (& avr_arch_types[arch_id]),
145 is_arch (mcu->macro == NULL), is_device (! is_arch),
146 flmap (mcu->dev_attribute & AVR_ISA_FLMAP),
147 have_flmap2 (have_avrxmega2_flmap && arch_id == ARCH_AVRXMEGA2),
148 have_flmap4 (have_avrxmega4_flmap && arch_id == ARCH_AVRXMEGA4),
149 have_flmap (flmap && (have_flmap2 || have_flmap4)),
150 rodata_in_flash (arch_id == ARCH_AVRTINY
151 || (arch_id == ARCH_AVRXMEGA3
152 && have_avrxmega3_rodata_in_flash)),
153 have_cvt (mcu->dev_attribute & AVR_CVT)
154 {
155 if (is_device)
156 snprintf (mcu_Name, 1 + strlen (mcu->macro) - strlen ("__AVR_" "__"),
157 "%s", mcu->macro + strlen ("__AVR_"));
158 }
159 };
160
161
162 static void
163 diagnose_mrodata_in_ram (FILE *f, const char *spec, const avr_mcu_t *mcu,
164 const McuInfo &mi)
165 {
166 fprintf (f, "%s:\n", spec);
167 if (mi.rodata_in_flash && mi.is_arch)
168 fprintf (f, "\t%%{mrodata-in-ram: %%e-mrodata-in-ram is not supported"
169 " for %s}", mcu->name);
170 else if (mi.rodata_in_flash)
171 fprintf (f, "\t%%{mrodata-in-ram: %%e-mrodata-in-ram is not supported"
172 " for %s (arch=%s)}", mi.mcu_Name, mi.arch->name);
173 else if (mi.is_arch)
174 {
175 if (! mi.have_flmap2 && ! mi.have_flmap4)
176 fprintf (f, "\t%%{mno-rodata-in-ram: %%e-mno-rodata-in-ram is not"
177 " supported for %s}", mcu->name);
178 }
179 else if (! mi.have_flmap)
180 fprintf (f, "\t%%{mno-rodata-in-ram: %%e-mno-rodata-in-ram is not supported"
181 " for %s (arch=%s)}", mi.mcu_Name, mi.arch->name);
182 fprintf (f, "\n\n");
183 }
184
185
186 static void
187 print_mcu (const avr_mcu_t *mcu, const McuInfo &mi)
188 {
189 const char *sp8_spec;
190 const char *rcall_spec;
191 const avr_mcu_t *arch_mcu;
192
193 for (arch_mcu = mcu; arch_mcu->macro; )
194 arch_mcu--;
195 if (arch_mcu->arch_id != mi.arch_id)
196 exit (EXIT_FAILURE);
197
198 char name[100];
199 if (snprintf (name, sizeof name, "specs-%s", mcu->name) >= (int) sizeof name)
200 exit (EXIT_FAILURE);
201
202 FILE *f = fopen (name ,"w");
203
204 bool absdata = (mcu->dev_attribute & AVR_ISA_LDS) != 0;
205 bool errata_skip = (mcu->dev_attribute & AVR_ERRATA_SKIP) != 0;
206 bool rmw = (mcu->dev_attribute & AVR_ISA_RMW) != 0;
207 bool sp8 = (mcu->dev_attribute & AVR_SHORT_SP) != 0;
208 bool rcall = (mcu->dev_attribute & AVR_ISA_RCALL);
209 int rodata_pm_offset = 0;
210 int pm_base_address = 0;
211
212 if (mi.arch->flash_pm_offset
213 && mcu->flash_pm_offset
214 && mcu->flash_pm_offset != mi.arch->flash_pm_offset)
215 {
216 rodata_pm_offset = mcu->flash_pm_offset;
217 }
218
219 if (mi.arch->flash_pm_offset)
220 {
221 pm_base_address = mcu->flash_pm_offset
222 ? mcu->flash_pm_offset
223 : mi.arch->flash_pm_offset;
224 }
225
226 if (mi.is_arch
227 && (ARCH_AVR2 == mi.arch_id
228 || ARCH_AVR25 == mi.arch_id))
229 {
230 // Leave "avr2" and "avr25" alone. These two architectures are
231 // the only ones that mix devices with 8-bit SP and 16-bit SP.
232 sp8_spec = "";
233 }
234 else
235 {
236 sp8_spec = sp8 ? "-msp8" :"%<msp8";
237 }
238
239 if (mi.is_arch
240 && ARCH_AVRXMEGA3 == mi.arch_id)
241 {
242 // Leave "avrxmega3" alone. This architectures is the only one
243 // that mixes devices with and without JMP / CALL.
244 rcall_spec = "";
245 }
246 else
247 {
248 rcall_spec = rcall ? "-mshort-calls" : "%<mshort-calls";
249 }
250
251 const char *flmap_spec = mi.flmap ? "-mflmap" : "%<mflmap";
252 const char *link_arch_spec = "%{mmcu=*:-m%*}";
253 const char *link_arch_flmap_spec = "%{mmcu=*:-m%*%{!mrodata-in-ram:_flmap}}";
254
255 if (mi.have_flmap)
256 link_arch_spec = link_arch_flmap_spec;
257
258 fprintf (f, "#\n"
259 "# Auto-generated specs for AVR ");
260 if (mi.is_arch)
261 fprintf (f, "core architecture %s\n", mi.arch->name);
262 else
263 fprintf (f, "device %s (core %s, %d-bit SP%s%s%s)\n", mi.mcu_Name,
264 mi.arch->name, sp8 ? 8 : 16, rcall ? ", short-calls" : "",
265 mi.have_cvt ? ", CVT" : "",
266 mi.have_flmap ? ", FLMAP" : "");
267 fprintf (f, "%s\n", header);
268
269 if (mi.is_device)
270 fprintf (f, "%s\n", help_copy_paste);
271
272 #if defined (WITH_AVRLIBC)
273 // AVR-LibC specific. See avrlibc.h for the specs using them as subspecs.
274
275 if (mi.is_device)
276 {
277 fprintf (f, "*avrlibc_startfile:\n");
278 fprintf (f, "\t%%{!mcvt:crt%s.o%%s}", mcu->name);
279 if (mi.have_cvt)
280 fprintf (f, " %%{mcvt:crt%s-cvt.o%%s}", mcu->name);
281 else
282 fprintf (f, " %%{mcvt:%%e%s does not support a compact vector"
283 " table (-mcvt)}", mi.mcu_Name);
284 fprintf (f, "\n\n");
285
286 fprintf (f, "*avrlibc_devicelib:\n");
287 fprintf (f, "\t%%{!nodevicelib:-l%s}", mcu->name);
288 fprintf (f, "\n\n");
289 }
290 #endif // WITH_AVRLIBC
291
292 // Diagnose usage of -m[no-]rodata-in-ram.
293 diagnose_mrodata_in_ram (f, "*check_rodata_in_ram", mcu, mi);
294
295 // avr-gcc specific specs for the compilation / the compiler proper.
296
297 int n_flash = 1 + (mcu->flash_size - 1) / 0x10000;
298
299 fprintf (f, "*cc1_n_flash:\n"
300 "\t%%{!mn-flash=*:-mn-flash=%d}\n\n", n_flash);
301
302 fprintf (f, "*cc1_rmw:\n%s\n\n", rmw
303 ? "\t%{!mno-rmw: -mrmw}"
304 : "\t%{mrmw}");
305
306 fprintf (f, "*cc1_errata_skip:\n%s\n\n", errata_skip
307 ? "\t%{!mno-skip-bug: -mskip-bug}"
308 : "\t%{!mskip-bug: -mno-skip-bug}");
309
310 fprintf (f, "*cc1_absdata:\n%s\n\n", absdata
311 ? "\t%{!mno-absdata: -mabsdata}"
312 : "\t%{mabsdata}");
313
314 // -m[no-]rodata-in-ram basically affects linking, but sanity-check early.
315 fprintf (f, "*cc1_rodata_in_ram:\n\t%%(check_rodata_in_ram)\n\n");
316
317 // avr-gcc specific specs for assembling / the assembler.
318
319 fprintf (f, "*asm_arch:\n\t-mmcu=%s\n\n", mi.arch->name);
320
321 #ifdef HAVE_AS_AVR_MLINK_RELAX_OPTION
322 fprintf (f, "*asm_relax:\n\t%s\n\n", ASM_RELAX_SPEC);
323 #endif // have avr-as --mlink-relax
324
325 #ifdef HAVE_AS_AVR_MRMW_OPTION
326 fprintf (f, "*asm_rmw:\n%s\n\n", rmw
327 ? "\t%{!mno-rmw: -mrmw}"
328 : "\t%{mrmw}");
329 #endif // have avr-as -mrmw
330
331 #ifdef HAVE_AS_AVR_MGCCISR_OPTION
332 fprintf (f, "*asm_gccisr:\n%s\n\n",
333 "\t%{!mno-gas-isr-prologues: -mgcc-isr}");
334 #endif // have avr-as -mgcc-isr
335
336 fprintf (f, "*asm_errata_skip:\n%s\n\n", errata_skip
337 ? "\t%{mno-skip-bug}"
338 : "\t%{!mskip-bug: -mno-skip-bug}");
339
340 // avr-specific specs for linking / the linker.
341
342 int wrap_k =
343 mcu->flash_size == 0x2000 ? 8
344 : mcu->flash_size == 0x4000 ? 16
345 : mcu->flash_size == 0x8000 ? 32
346 : mcu->flash_size == 0x10000 ? 64
347 : 0;
348
349 fprintf (f, "*link_pmem_wrap:\n");
350 if (wrap_k == 8)
351 fprintf (f, "\t%%{!mno-pmem-wrap-around: --pmem-wrap-around=8k}");
352 else if (wrap_k > 8)
353 fprintf (f, "\t%%{mpmem-wrap-around: --pmem-wrap-around=%dk}", wrap_k);
354 fprintf (f, "\n\n");
355
356 fprintf (f, "*link_relax:\n\t%s\n\n", LINK_RELAX_SPEC);
357
358 fprintf (f, "*link_no_call_main:\n\t%s\n\n", "%{mno-call-main: --defsym __call_main=0}");
359
360 fprintf (f, "*link_arch:\n\t%s", link_arch_spec);
361 if (mi.is_device
362 && rodata_pm_offset)
363 fprintf (f, " --defsym=__RODATA_PM_OFFSET__=0x%x", rodata_pm_offset);
364 fprintf (f, "\n\n");
365
366 if (mi.is_device)
367 {
368 fprintf (f, "*link_data_start:\n");
369 if (mcu->data_section_start
370 != mi.arch->default_data_section_start)
371 fprintf (f, "\t%%{!Tdata:-Tdata 0x%lX}",
372 0x800000UL + mcu->data_section_start);
373 fprintf (f, "\n\n");
374
375 fprintf (f, "*link_text_start:\n");
376 if (mcu->text_section_start != 0x0)
377 fprintf (f, "\t%%{!Ttext:-Ttext 0x%lX}", 0UL + mcu->text_section_start);
378 fprintf (f, "\n\n");
379 }
380
381 // -m[no-]rodata-in-ram affects linking. Sanity check its usage.
382 fprintf (f, "*link_rodata_in_ram:\n\t%%(check_rodata_in_ram)");
383 if (mi.is_device && mi.have_flmap)
384 fprintf (f, " %%{!mrodata-in-ram:-u __do_flmap_init}");
385 fprintf (f, "\n\n");
386
387 // Specs known to GCC.
388
389 if (mi.is_device)
390 {
391 fprintf (f, "*self_spec:\n");
392 fprintf (f, "\t%%<mmcu=* -mmcu=%s ", mi.arch->name);
393 fprintf (f, "%s ", flmap_spec);
394 fprintf (f, "%s ", rcall_spec);
395 fprintf (f, "%s\n\n", sp8_spec);
396
397 #if defined (WITH_AVRLIBC)
398 fprintf (f, "%s\n", help_dev_lib_name);
399
400 fprintf (f, "*cpp_avrlibc:\n");
401 fprintf (f, "\t-D__AVR_DEVICE_NAME__=%s", mcu->name);
402 fprintf (f, "\n\n");
403 #endif // WITH_AVRLIBC
404
405 fprintf (f, "*cpp_mcu:\n");
406 fprintf (f, "\t-D%s", mcu->macro);
407 if (pm_base_address)
408 {
409 fprintf (f, " -U__AVR_PM_BASE_ADDRESS__");
410 fprintf (f, " -D__AVR_PM_BASE_ADDRESS__=0x%x", pm_base_address);
411 }
412 if (mi.have_flmap)
413 fprintf (f, " -D__AVR_HAVE_FLMAP__");
414
415 fprintf (f, "\n\n"); // *cpp_mcu
416
417 fprintf (f, "*cpp_rodata_in_ram:\n\t-D__AVR_RODATA_IN_RAM__=");
418 if (mi.rodata_in_flash)
419 fprintf (f, "0");
420 else if (! mi.have_flmap)
421 fprintf (f, "1");
422 else
423 fprintf (f, "%%{!mrodata-in-ram:%%{!mno-rodata-in-ram:0}}"
424 "%%{mrodata-in-ram:1}" "%%{mno-rodata-in-ram:0}");
425 fprintf (f, "\n\n");
426
427 fprintf (f, "*cpp:\n");
428 fprintf (f, "\t%%(cpp_mcu) %%(cpp_rodata_in_ram)");
429 #if defined (WITH_AVRLIBC)
430 fprintf (f, " %%(cpp_avrlibc)");
431 #endif // WITH_AVRLIBC
432 fprintf (f, "\n\n");
433 }
434
435 fprintf (f, "# End of file\n");
436
437 fclose (f);
438 }
439
440
441 int main (void)
442 {
443 for (const avr_mcu_t *mcu = avr_mcu_types; mcu->name; mcu++)
444 print_mcu (mcu, McuInfo (mcu));
445
446 return EXIT_SUCCESS;
447 }