Debian Codegraph @

gcc/config/avr/avr-c.cc

raw
1 /* Code for the C/C++ front end for AVR 8-bit microcontrollers.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
3 Contributed by Anatoly Sokolov ([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 /* Not included in avr.cc since this requires C front end. */
22
23 #define IN_TARGET_CODE 1
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "target.h"
29 #include "c-family/c-common.h"
30 #include "stor-layout.h"
31 #include "langhooks.h"
32 #include "memmodel.h"
33 #include "tm_p.h"
34
35 /* IDs for all the AVR builtins. */
36
37 enum avr_builtin_id
38 {
39 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME, ATTRS) \
40 AVR_BUILTIN_ ## NAME,
41 #include "builtins.def"
42 #undef DEF_BUILTIN
43
44 AVR_BUILTIN_COUNT
45 };
46
47
48 /* Some of our built-in functions are available for GNU-C only:
49 - Built-ins that use named address-spaces.
50 - Built-ins that use fixed-point types. */
51
52 static bool
53 avr_builtin_supported_p (location_t loc, avr_builtin_id bid)
54 {
55 if (! lang_GNU_C () // Means "C" actually, not "GNU-C".
56 && bid >= AVR_FIRST_C_ONLY_BUILTIN_ID)
57 {
58 if (loc != UNKNOWN_LOCATION)
59 error_at (loc, "built-in function is only supported for GNU-C");
60 return false;
61 }
62
63 const bool uses_as = (bid == AVR_BUILTIN_FLASH_SEGMENT
64 || bid == AVR_BUILTIN_STRLEN_FLASH
65 || bid == AVR_BUILTIN_STRLEN_FLASHX
66 || bid == AVR_BUILTIN_STRLEN_MEMX);
67 if (AVR_TINY && uses_as)
68 {
69 if (loc != UNKNOWN_LOCATION)
70 error_at (loc, "built-in function for named address-space is not"
71 " supported for reduced Tiny devices");
72 return false;
73 }
74
75 return true;
76 }
77
78
79 /* Implement `TARGET_RESOLVE_OVERLOADED_BUILTIN'. */
80
81 static tree
82 avr_resolve_overloaded_builtin (location_t loc, tree fndecl, void *vargs, bool)
83 {
84 const avr_builtin_id bid = (avr_builtin_id) DECL_MD_FUNCTION_CODE (fndecl);
85
86 if (! avr_builtin_supported_p (loc, bid))
87 return error_mark_node;
88
89 tree type0, type1, fold = NULL_TREE;
90 avr_builtin_id id = AVR_BUILTIN_COUNT;
91 vec<tree, va_gc> &args = * (vec<tree, va_gc> *) vargs;
92
93 switch (bid)
94 {
95 default:
96 break;
97
98 case AVR_BUILTIN_ABSFX:
99 if (args.length () != 1)
100 {
101 error_at (loc, "%qs expects 1 argument but %d given",
102 "absfx", (int) args.length ());
103
104 fold = error_mark_node;
105 break;
106 }
107
108 type0 = TREE_TYPE (args[0]);
109
110 if (!FIXED_POINT_TYPE_P (type0))
111 {
112 error_at (loc, "%qs expects a fixed-point value as argument",
113 "absfx");
114
115 fold = error_mark_node;
116 }
117
118 switch (TYPE_MODE (type0))
119 {
120 case E_QQmode: id = AVR_BUILTIN_ABSHR; break;
121 case E_HQmode: id = AVR_BUILTIN_ABSR; break;
122 case E_SQmode: id = AVR_BUILTIN_ABSLR; break;
123 case E_DQmode: id = AVR_BUILTIN_ABSLLR; break;
124
125 case E_HAmode: id = AVR_BUILTIN_ABSHK; break;
126 case E_SAmode: id = AVR_BUILTIN_ABSK; break;
127 case E_DAmode: id = AVR_BUILTIN_ABSLK; break;
128 case E_TAmode: id = AVR_BUILTIN_ABSLLK; break;
129
130 case E_UQQmode:
131 case E_UHQmode:
132 case E_USQmode:
133 case E_UDQmode:
134 case E_UHAmode:
135 case E_USAmode:
136 case E_UDAmode:
137 case E_UTAmode:
138 warning_at (loc, 0, "using %qs with unsigned type has no effect",
139 "absfx");
140 return args[0];
141
142 default:
143 error_at (loc, "no matching fixed-point overload found for %qs",
144 "absfx");
145
146 fold = error_mark_node;
147 break;
148 }
149
150 fold = targetm.builtin_decl (id, true);
151
152 if (fold != error_mark_node)
153 fold = build_function_call_vec (loc, vNULL, fold, &args, NULL);
154
155 break; // absfx
156
157 case AVR_BUILTIN_ROUNDFX:
158 if (args.length () != 2)
159 {
160 error_at (loc, "%qs expects 2 arguments but %d given",
161 "roundfx", (int) args.length ());
162
163 fold = error_mark_node;
164 break;
165 }
166
167 type0 = TREE_TYPE (args[0]);
168 type1 = TREE_TYPE (args[1]);
169
170 if (!FIXED_POINT_TYPE_P (type0))
171 {
172 error_at (loc, "%qs expects a fixed-point value as first argument",
173 "roundfx");
174
175 fold = error_mark_node;
176 }
177
178 if (!INTEGRAL_TYPE_P (type1))
179 {
180 error_at (loc, "%qs expects an integer value as second argument",
181 "roundfx");
182
183 fold = error_mark_node;
184 }
185
186 switch (TYPE_MODE (type0))
187 {
188 case E_QQmode: id = AVR_BUILTIN_ROUNDHR; break;
189 case E_HQmode: id = AVR_BUILTIN_ROUNDR; break;
190 case E_SQmode: id = AVR_BUILTIN_ROUNDLR; break;
191 case E_DQmode: id = AVR_BUILTIN_ROUNDLLR; break;
192
193 case E_UQQmode: id = AVR_BUILTIN_ROUNDUHR; break;
194 case E_UHQmode: id = AVR_BUILTIN_ROUNDUR; break;
195 case E_USQmode: id = AVR_BUILTIN_ROUNDULR; break;
196 case E_UDQmode: id = AVR_BUILTIN_ROUNDULLR; break;
197
198 case E_HAmode: id = AVR_BUILTIN_ROUNDHK; break;
199 case E_SAmode: id = AVR_BUILTIN_ROUNDK; break;
200 case E_DAmode: id = AVR_BUILTIN_ROUNDLK; break;
201 case E_TAmode: id = AVR_BUILTIN_ROUNDLLK; break;
202
203 case E_UHAmode: id = AVR_BUILTIN_ROUNDUHK; break;
204 case E_USAmode: id = AVR_BUILTIN_ROUNDUK; break;
205 case E_UDAmode: id = AVR_BUILTIN_ROUNDULK; break;
206 case E_UTAmode: id = AVR_BUILTIN_ROUNDULLK; break;
207
208 default:
209 error_at (loc, "no matching fixed-point overload found for %qs",
210 "roundfx");
211
212 fold = error_mark_node;
213 break;
214 }
215
216 fold = targetm.builtin_decl (id, true);
217
218 if (fold != error_mark_node)
219 fold = build_function_call_vec (loc, vNULL, fold, &args, NULL);
220
221 break; // roundfx
222
223 case AVR_BUILTIN_COUNTLSFX:
224 if (args.length () != 1)
225 {
226 error_at (loc, "%qs expects 1 argument but %d given",
227 "countlsfx", (int) args.length ());
228
229 fold = error_mark_node;
230 break;
231 }
232
233 type0 = TREE_TYPE (args[0]);
234
235 if (!FIXED_POINT_TYPE_P (type0))
236 {
237 error_at (loc, "%qs expects a fixed-point value as first argument",
238 "countlsfx");
239
240 fold = error_mark_node;
241 }
242
243 switch (TYPE_MODE (type0))
244 {
245 case E_QQmode: id = AVR_BUILTIN_COUNTLSHR; break;
246 case E_HQmode: id = AVR_BUILTIN_COUNTLSR; break;
247 case E_SQmode: id = AVR_BUILTIN_COUNTLSLR; break;
248 case E_DQmode: id = AVR_BUILTIN_COUNTLSLLR; break;
249
250 case E_UQQmode: id = AVR_BUILTIN_COUNTLSUHR; break;
251 case E_UHQmode: id = AVR_BUILTIN_COUNTLSUR; break;
252 case E_USQmode: id = AVR_BUILTIN_COUNTLSULR; break;
253 case E_UDQmode: id = AVR_BUILTIN_COUNTLSULLR; break;
254
255 case E_HAmode: id = AVR_BUILTIN_COUNTLSHK; break;
256 case E_SAmode: id = AVR_BUILTIN_COUNTLSK; break;
257 case E_DAmode: id = AVR_BUILTIN_COUNTLSLK; break;
258 case E_TAmode: id = AVR_BUILTIN_COUNTLSLLK; break;
259
260 case E_UHAmode: id = AVR_BUILTIN_COUNTLSUHK; break;
261 case E_USAmode: id = AVR_BUILTIN_COUNTLSUK; break;
262 case E_UDAmode: id = AVR_BUILTIN_COUNTLSULK; break;
263 case E_UTAmode: id = AVR_BUILTIN_COUNTLSULLK; break;
264
265 default:
266 error_at (loc, "no matching fixed-point overload found for %qs",
267 "countlsfx");
268
269 fold = error_mark_node;
270 break;
271 }
272
273 fold = targetm.builtin_decl (id, true);
274
275 if (fold != error_mark_node)
276 fold = build_function_call_vec (loc, vNULL, fold, &args, NULL);
277
278 break; // countlsfx
279 }
280
281 return fold;
282 }
283
284
285 /* Implement `REGISTER_TARGET_PRAGMAS'. */
286
287 void
288 avr_register_target_pragmas (void)
289 {
290 gcc_assert (ADDR_SPACE_GENERIC == ADDR_SPACE_RAM);
291
292 /* Register address spaces. The order must be the same as in the respective
293 enum from avr.h (or designated initializers must be used in avr.cc).
294 We always register all address spaces even if some of them make no
295 sense for some targets. Diagnose for non-supported spaces will be
296 emit by TARGET_ADDR_SPACE_DIAGNOSE_USAGE. */
297
298 for (int i = 0; i < ADDR_SPACE_COUNT; i++)
299 {
300 gcc_assert (i == avr_addrspace[i].id);
301
302 if (!ADDR_SPACE_GENERIC_P (i))
303 c_register_addr_space (avr_addrspace[i].name, avr_addrspace[i].id);
304 }
305
306 targetm.resolve_overloaded_builtin = avr_resolve_overloaded_builtin;
307 }
308
309
310 /* Transform LO into uppercase and write the result to UP.
311 You must provide enough space for UP. Return UP. */
312
313 static char*
314 avr_toupper (char *up, const char *lo)
315 {
316 char *up0 = up;
317
318 for (; *lo; lo++, up++)
319 *up = TOUPPER (*lo);
320
321 *up = '\0';
322
323 return up0;
324 }
325
326 /* Worker function for TARGET_CPU_CPP_BUILTINS. */
327
328 void
329 avr_cpu_cpp_builtins (cpp_reader *pfile)
330 {
331 builtin_define_std ("AVR");
332
333 /* __AVR_DEVICE_NAME__ and avr_mcu_types[].macro like __AVR_ATmega8__
334 are defined by -D command option, see device-specs file. */
335
336 if (avr_arch->macro)
337 cpp_define_formatted (pfile, "__AVR_ARCH__=%s", avr_arch->macro);
338 if (AVR_HAVE_RAMPD) cpp_define (pfile, "__AVR_HAVE_RAMPD__");
339 if (AVR_HAVE_RAMPX) cpp_define (pfile, "__AVR_HAVE_RAMPX__");
340 if (AVR_HAVE_RAMPY) cpp_define (pfile, "__AVR_HAVE_RAMPY__");
341 if (AVR_HAVE_RAMPZ) cpp_define (pfile, "__AVR_HAVE_RAMPZ__");
342 if (AVR_HAVE_ELPM) cpp_define (pfile, "__AVR_HAVE_ELPM__");
343 if (AVR_HAVE_ELPMX) cpp_define (pfile, "__AVR_HAVE_ELPMX__");
344 if (AVR_HAVE_MOVW) cpp_define (pfile, "__AVR_HAVE_MOVW__");
345 if (AVR_HAVE_LPMX) cpp_define (pfile, "__AVR_HAVE_LPMX__");
346 if (AVR_HAVE_ADIW) cpp_define (pfile, "__AVR_HAVE_ADIW__");
347
348 if (avr_arch->asm_only)
349 cpp_define (pfile, "__AVR_ASM_ONLY__");
350 if (AVR_HAVE_MUL)
351 {
352 cpp_define (pfile, "__AVR_ENHANCED__");
353 cpp_define (pfile, "__AVR_HAVE_MUL__");
354 }
355
356 if (AVR_HAVE_JMP_CALL)
357 cpp_define (pfile, "__AVR_HAVE_JMP_CALL__");
358
359 if (avr_arch->have_jmp_call)
360 cpp_define (pfile, "__AVR_MEGA__");
361
362 if (AVR_SHORT_CALLS)
363 cpp_define (pfile, "__AVR_SHORT_CALLS__");
364
365 if (AVR_XMEGA)
366 cpp_define (pfile, "__AVR_XMEGA__");
367
368 if (AVR_TINY)
369 {
370 cpp_define (pfile, "__AVR_TINY__");
371
372 /* Define macro "__AVR_TINY_PM_BASE_ADDRESS__" with mapped program memory
373 start address. This macro shall be used where mapped program
374 memory is accessed, eg. copying data section (__do_copy_data)
375 contents to data memory region.
376 NOTE:
377 Program memory of AVR_TINY devices cannot be accessed directly,
378 it has been mapped to the data memory. For AVR_TINY devices
379 (ATtiny4/5/9/10/20 and 40) mapped program memory starts at 0x4000. */
380
381 cpp_define_formatted (pfile, "__AVR_TINY_PM_BASE_ADDRESS__=0x%x",
382 avr_arch->flash_pm_offset);
383 }
384
385 if (avr_arch->flash_pm_offset)
386 cpp_define_formatted (pfile, "__AVR_PM_BASE_ADDRESS__=0x%x",
387 avr_arch->flash_pm_offset);
388
389 if (AVR_HAVE_EIJMP_EICALL)
390 {
391 cpp_define (pfile, "__AVR_HAVE_EIJMP_EICALL__");
392 cpp_define (pfile, "__AVR_3_BYTE_PC__");
393 }
394 else
395 {
396 cpp_define (pfile, "__AVR_2_BYTE_PC__");
397 }
398
399 if (AVR_HAVE_8BIT_SP)
400 cpp_define (pfile, "__AVR_HAVE_8BIT_SP__");
401 else
402 cpp_define (pfile, "__AVR_HAVE_16BIT_SP__");
403
404 if (AVR_HAVE_SPH)
405 cpp_define (pfile, "__AVR_HAVE_SPH__");
406 else
407 cpp_define (pfile, "__AVR_SP8__");
408
409 if (TARGET_NO_INTERRUPTS)
410 cpp_define (pfile, "__NO_INTERRUPTS__");
411
412 if (TARGET_SKIP_BUG)
413 {
414 cpp_define (pfile, "__AVR_ERRATA_SKIP__");
415
416 if (AVR_HAVE_JMP_CALL)
417 cpp_define (pfile, "__AVR_ERRATA_SKIP_JMP_CALL__");
418 }
419
420 if (TARGET_RMW)
421 cpp_define (pfile, "__AVR_ISA_RMW__");
422
423 if (TARGET_CVT)
424 cpp_define (pfile, "__AVR_CVT__");
425
426 cpp_define_formatted (pfile, "__AVR_SFR_OFFSET__=0x%x",
427 avr_arch->sfr_offset);
428
429 #ifdef WITH_AVRLIBC
430 cpp_define (pfile, "__WITH_AVRLIBC__");
431 #endif /* WITH_AVRLIBC */
432
433 // We support __attribute__((signal/interrupt (n1, n2, ...)[, noblock]))
434 cpp_define (pfile, "__HAVE_SIGNAL_N__");
435
436
437 // From configure --with-libf7={|libgcc|math|math-symbols|yes|no}
438
439 #ifdef WITH_LIBF7_LIBGCC
440 cpp_define (pfile, "__WITH_LIBF7_LIBGCC__");
441 #endif /* WITH_LIBF7_LIBGCC */
442
443 #ifdef WITH_LIBF7_MATH
444 cpp_define (pfile, "__WITH_LIBF7_MATH__");
445 #endif /* WITH_LIBF7_MATH */
446
447 #ifdef WITH_LIBF7_MATH_SYMBOLS
448 cpp_define (pfile, "__WITH_LIBF7_MATH_SYMBOLS__");
449 #endif /* WITH_LIBF7_MATH_SYMBOLS */
450
451 // From configure --with-double={|32|32,64|64,32|64}
452
453 #ifdef HAVE_DOUBLE_MULTILIB
454 cpp_define (pfile, "__HAVE_DOUBLE_MULTILIB__");
455 #endif
456
457 #ifdef HAVE_DOUBLE64
458 cpp_define (pfile, "__HAVE_DOUBLE64__");
459 #endif
460
461 #ifdef HAVE_DOUBLE32
462 cpp_define (pfile, "__HAVE_DOUBLE32__");
463 #endif
464
465 #if defined (WITH_DOUBLE64)
466 cpp_define (pfile, "__DEFAULT_DOUBLE__=64");
467 #elif defined (WITH_DOUBLE32)
468 cpp_define (pfile, "__DEFAULT_DOUBLE__=32");
469 #else
470 #error "align this with config.gcc"
471 #endif
472
473 // From configure --with-long-double={|32|32,64|64,32|64|double}
474
475 #ifdef HAVE_LONG_DOUBLE_MULTILIB
476 cpp_define (pfile, "__HAVE_LONG_DOUBLE_MULTILIB__");
477 #endif
478
479 #ifdef HAVE_LONG_DOUBLE64
480 cpp_define (pfile, "__HAVE_LONG_DOUBLE64__");
481 #endif
482
483 #ifdef HAVE_LONG_DOUBLE32
484 cpp_define (pfile, "__HAVE_LONG_DOUBLE32__");
485 #endif
486
487 #ifdef HAVE_LONG_DOUBLE_IS_DOUBLE
488 cpp_define (pfile, "__HAVE_LONG_DOUBLE_IS_DOUBLE__");
489 #endif
490
491 #if defined (WITH_LONG_DOUBLE64)
492 cpp_define (pfile, "__DEFAULT_LONG_DOUBLE__=64");
493 #elif defined (WITH_LONG_DOUBLE32)
494 cpp_define (pfile, "__DEFAULT_LONG_DOUBLE__=32");
495 #else
496 #error "align this with config.gcc"
497 #endif
498
499 // From configure --with-double-comparison={2|3} --with-libf7.
500
501 #if defined (WITH_DOUBLE_COMPARISON)
502 #if WITH_DOUBLE_COMPARISON == 2 || WITH_DOUBLE_COMPARISON == 3
503 /* The number of states a DFmode comparison libcall might take and
504 reflects what avr.cc:FLOAT_LIB_COMPARE_RETURNS_BOOL returns for
505 DFmode. GCC's default is 3-state, but some libraries like LibF7
506 implement true / false (2-state). */
507 cpp_define_formatted (pfile, "__WITH_DOUBLE_COMPARISON__=%d",
508 WITH_DOUBLE_COMPARISON);
509 #else
510 #error "align this with config.gcc"
511 #endif
512 #else
513 #error "align this with config.gcc"
514 #endif
515
516 /* Define builtin macros so that the user can easily query whether
517 non-generic address spaces (and which) are supported or not.
518 This is only supported for C. For C++, a language extension is needed
519 (as mentioned in ISO/IEC DTR 18037; Annex F.2) which is not
520 implemented in GCC up to now. */
521
522 if (lang_GNU_C ())
523 {
524 for (int i = 0; i < ADDR_SPACE_COUNT; i++)
525 if (!ADDR_SPACE_GENERIC_P (i)
526 /* Only supply __FLASH<n> macro if the address space is reasonable
527 for this target. The address space qualifier itself is still
528 supported, but using it will throw an error. */
529 && avr_addr_space_supported_p ((addr_space_t) i))
530 {
531 const char *name = avr_addrspace[i].name;
532 char *Name = (char*) alloca (1 + strlen (name));
533
534 cpp_define (pfile, avr_toupper (Name, name));
535 }
536 }
537
538 /* Define builtin macros so that the user can easily query whether or
539 not a specific builtin is available. */
540
541 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME, ATTRS) \
542 if (avr_builtin_supported_p (UNKNOWN_LOCATION, AVR_BUILTIN_ ## NAME)) \
543 cpp_define (pfile, "__BUILTIN_AVR_" #NAME);
544 #include "builtins.def"
545 #undef DEF_BUILTIN
546
547 /* Builtin macros for the __int24 and __uint24 type. */
548
549 cpp_define_formatted (pfile, "__INT24_MAX__=8388607%s",
550 INT_TYPE_SIZE == 8 ? "LL" : "L");
551 cpp_define (pfile, "__INT24_MIN__=(-__INT24_MAX__-1)");
552 cpp_define_formatted (pfile, "__UINT24_MAX__=16777215%s",
553 INT_TYPE_SIZE == 8 ? "ULL" : "UL");
554 }