| 1 |
/* Build texi documentation for option -mmcu for AVR 8-bit microcontrollers. |
| 2 |
Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Georg-Johann Lay ([email protected]) |
| 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_GEN_AVR_MMCU_TEXI |
| 26 |
|
| 27 |
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) |
| 28 |
|
| 29 |
#include "avr-devices.cc" |
| 30 |
|
| 31 |
static const avr_mcu_t* |
| 32 |
mcus[ARRAY_SIZE (avr_mcu_types)]; |
| 33 |
|
| 34 |
static int letter (char c) |
| 35 |
{ |
| 36 |
return c >= 'a' && c <= 'z'; |
| 37 |
} |
| 38 |
|
| 39 |
static int digit (char c) |
| 40 |
{ |
| 41 |
return c >= '0' && c <= '9'; |
| 42 |
} |
| 43 |
|
| 44 |
static int |
| 45 |
str_prefix_p (const char *str, const char *prefix) |
| 46 |
{ |
| 47 |
return strncmp (str, prefix, strlen (prefix)) == 0; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/* Used by string comparator to group MCUs by their |
| 52 |
name prefix like "attiny" or "atmega". */ |
| 53 |
|
| 54 |
static int |
| 55 |
c_prefix (const char *str) |
| 56 |
{ |
| 57 |
static const char *const prefixes[] = |
| 58 |
{ |
| 59 |
"attiny", "atmega", "atxmega", "ata", "at90", "avr" |
| 60 |
}; |
| 61 |
|
| 62 |
int i, n = (int) (ARRAY_SIZE (prefixes)); |
| 63 |
|
| 64 |
for (i = 0; i < n; i++) |
| 65 |
if (str_prefix_p (str, prefixes[i])) |
| 66 |
return i; |
| 67 |
|
| 68 |
return n; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/* If A starts a group of digits, return their value as a number. */ |
| 73 |
|
| 74 |
static int |
| 75 |
c_number (const char *a) |
| 76 |
{ |
| 77 |
int val = 0; |
| 78 |
|
| 79 |
if (digit (*a) && ! digit (*(a-1))) |
| 80 |
{ |
| 81 |
while (digit (*a)) |
| 82 |
val = 10 * val + (*a++) - '0'; |
| 83 |
} |
| 84 |
|
| 85 |
return val; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/* Compare two MCUs and order them for easy lookup. */ |
| 90 |
|
| 91 |
static int |
| 92 |
comparator (const void *va, const void *vb) |
| 93 |
{ |
| 94 |
const avr_mcu_t *mcu_a = *(const avr_mcu_t* const*) va; |
| 95 |
const avr_mcu_t *mcu_b = *(const avr_mcu_t* const*) vb; |
| 96 |
const char *a = mcu_a->name; |
| 97 |
const char *b = mcu_b->name; |
| 98 |
|
| 99 |
// First, group MCUs according to their pure-letter prefix. |
| 100 |
|
| 101 |
int c = c_prefix (a) - c_prefix (b); |
| 102 |
if (c) |
| 103 |
return c; |
| 104 |
|
| 105 |
// Second, if their prefixes are the same, group according to |
| 106 |
// their flash size. |
| 107 |
|
| 108 |
c = (int) mcu_a->flash_size - (int) mcu_b->flash_size; |
| 109 |
if (c) |
| 110 |
return c; |
| 111 |
|
| 112 |
// Third, group according to aligned groups of digits. |
| 113 |
|
| 114 |
while (*a && *b) |
| 115 |
{ |
| 116 |
c = c_number (a) - c_number (b); |
| 117 |
if (c) |
| 118 |
return c; |
| 119 |
|
| 120 |
if (*a != *b) |
| 121 |
return *a - *b; |
| 122 |
|
| 123 |
a++; |
| 124 |
b++; |
| 125 |
} |
| 126 |
|
| 127 |
return *a - *b; |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
print_mcus (size_t n_mcus) |
| 132 |
{ |
| 133 |
int duplicate = 0; |
| 134 |
size_t i; |
| 135 |
|
| 136 |
if (!n_mcus) |
| 137 |
return; |
| 138 |
|
| 139 |
qsort (mcus, n_mcus, sizeof (avr_mcu_t*), comparator); |
| 140 |
|
| 141 |
printf ("@*@var{mcu}@tie{}="); |
| 142 |
|
| 143 |
for (i = 0; i < n_mcus; i++) |
| 144 |
{ |
| 145 |
printf (" @code{%s}%s", mcus[i]->name, i == n_mcus-1 ? ".\n\n" : ","); |
| 146 |
|
| 147 |
if (i && !strcmp (mcus[i]->name, mcus[i-1]->name)) |
| 148 |
{ |
| 149 |
// Sanity-check: Fail on devices that are present more than once. |
| 150 |
|
| 151 |
duplicate = 1; |
| 152 |
fprintf (stderr, "error: duplicate device: %s\n", mcus[i]->name); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if (duplicate) |
| 157 |
exit (1); |
| 158 |
} |
| 159 |
|
| 160 |
int main (void) |
| 161 |
{ |
| 162 |
enum avr_arch_id arch_id = ARCH_UNKNOWN; |
| 163 |
size_t i, n_mcus = 0; |
| 164 |
const avr_mcu_t *mcu; |
| 165 |
|
| 166 |
printf ("@c Copyright (C) 2012-2025 Free Software Foundation, Inc.\n"); |
| 167 |
printf ("@c This is part of the GCC manual.\n"); |
| 168 |
printf ("@c For copying conditions, see the file " |
| 169 |
"gcc/doc/include/fdl.texi.\n\n"); |
| 170 |
|
| 171 |
printf ("@c This file is generated automatically using\n"); |
| 172 |
printf ("@c gcc/config/avr/gen-avr-mmcu-texi.cc from:\n"); |
| 173 |
printf ("@c gcc/config/avr/avr-arch.h\n"); |
| 174 |
printf ("@c gcc/config/avr/avr-devices.cc\n"); |
| 175 |
printf ("@c gcc/config/avr/avr-mcus.def\n\n"); |
| 176 |
|
| 177 |
printf ("@c Please do not edit manually.\n\n"); |
| 178 |
|
| 179 |
printf ("@table @code\n\n"); |
| 180 |
|
| 181 |
for (mcu = avr_mcu_types; mcu->name; mcu++) |
| 182 |
{ |
| 183 |
if (mcu->macro == NULL) |
| 184 |
{ |
| 185 |
arch_id = mcu->arch_id; |
| 186 |
|
| 187 |
// Start a new architecture: Flush the MCUs collected so far. |
| 188 |
print_mcus (n_mcus); |
| 189 |
n_mcus = 0; |
| 190 |
|
| 191 |
for (i = 0; i < ARRAY_SIZE (avr_texinfo); i++) |
| 192 |
if (arch_id == avr_texinfo[i].arch_id) |
| 193 |
printf ("@item @anchor{%s}%s\n%s\n", mcu->name, mcu->name, |
| 194 |
avr_texinfo[i].texinfo); |
| 195 |
} |
| 196 |
else if (arch_id == (enum avr_arch_id) mcu->arch_id) |
| 197 |
{ |
| 198 |
mcus[n_mcus++] = mcu; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
print_mcus (n_mcus); |
| 203 |
printf ("@end table\n"); |
| 204 |
|
| 205 |
return EXIT_SUCCESS; |
| 206 |
} |