1 /* Copyright (C) 2009-2017 Free Software Foundation, Inc.
2 Contributed by Anatoly Sokolov (aesok@post.ru)
4 This file is part of GCC.
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)
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.
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/>. */
20 /* Not included in avr.c since this requires C front end. */
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
27 #include "stor-layout.h"
28 #include "langhooks.h"
32 /* IDs for all the AVR builtins. */
36 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME) \
38 #include "builtins.def"
45 /* Implement `TARGET_RESOLVE_OVERLOADED_PLUGIN'. */
48 avr_resolve_overloaded_builtin (unsigned int iloc
, tree fndecl
, void *vargs
)
50 tree type0
, type1
, fold
= NULL_TREE
;
51 enum avr_builtin_id id
= AVR_BUILTIN_COUNT
;
52 location_t loc
= (location_t
) iloc
;
53 vec
<tree
, va_gc
> &args
= * (vec
<tree
, va_gc
>*) vargs
;
55 switch (DECL_FUNCTION_CODE (fndecl
))
60 case AVR_BUILTIN_ABSFX
:
61 if (args
.length() != 1)
63 error_at (loc
, "%qs expects 1 argument but %d given",
64 "absfx", (int) args
.length());
66 fold
= error_mark_node
;
70 type0
= TREE_TYPE (args
[0]);
72 if (!FIXED_POINT_TYPE_P (type0
))
74 error_at (loc
, "%qs expects a fixed-point value as argument",
77 fold
= error_mark_node
;
80 switch (TYPE_MODE (type0
))
82 case E_QQmode
: id
= AVR_BUILTIN_ABSHR
; break;
83 case E_HQmode
: id
= AVR_BUILTIN_ABSR
; break;
84 case E_SQmode
: id
= AVR_BUILTIN_ABSLR
; break;
85 case E_DQmode
: id
= AVR_BUILTIN_ABSLLR
; break;
87 case E_HAmode
: id
= AVR_BUILTIN_ABSHK
; break;
88 case E_SAmode
: id
= AVR_BUILTIN_ABSK
; break;
89 case E_DAmode
: id
= AVR_BUILTIN_ABSLK
; break;
90 case E_TAmode
: id
= AVR_BUILTIN_ABSLLK
; break;
100 warning_at (loc
, 0, "using %qs with unsigned type has no effect",
105 error_at (loc
, "no matching fixed-point overload found for %qs",
108 fold
= error_mark_node
;
112 fold
= targetm
.builtin_decl (id
, true);
114 if (fold
!= error_mark_node
)
115 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
119 case AVR_BUILTIN_ROUNDFX
:
120 if (args
.length() != 2)
122 error_at (loc
, "%qs expects 2 arguments but %d given",
123 "roundfx", (int) args
.length());
125 fold
= error_mark_node
;
129 type0
= TREE_TYPE (args
[0]);
130 type1
= TREE_TYPE (args
[1]);
132 if (!FIXED_POINT_TYPE_P (type0
))
134 error_at (loc
, "%qs expects a fixed-point value as first argument",
137 fold
= error_mark_node
;
140 if (!INTEGRAL_TYPE_P (type1
))
142 error_at (loc
, "%qs expects an integer value as second argument",
145 fold
= error_mark_node
;
148 switch (TYPE_MODE (type0
))
150 case E_QQmode
: id
= AVR_BUILTIN_ROUNDHR
; break;
151 case E_HQmode
: id
= AVR_BUILTIN_ROUNDR
; break;
152 case E_SQmode
: id
= AVR_BUILTIN_ROUNDLR
; break;
153 case E_DQmode
: id
= AVR_BUILTIN_ROUNDLLR
; break;
155 case E_UQQmode
: id
= AVR_BUILTIN_ROUNDUHR
; break;
156 case E_UHQmode
: id
= AVR_BUILTIN_ROUNDUR
; break;
157 case E_USQmode
: id
= AVR_BUILTIN_ROUNDULR
; break;
158 case E_UDQmode
: id
= AVR_BUILTIN_ROUNDULLR
; break;
160 case E_HAmode
: id
= AVR_BUILTIN_ROUNDHK
; break;
161 case E_SAmode
: id
= AVR_BUILTIN_ROUNDK
; break;
162 case E_DAmode
: id
= AVR_BUILTIN_ROUNDLK
; break;
163 case E_TAmode
: id
= AVR_BUILTIN_ROUNDLLK
; break;
165 case E_UHAmode
: id
= AVR_BUILTIN_ROUNDUHK
; break;
166 case E_USAmode
: id
= AVR_BUILTIN_ROUNDUK
; break;
167 case E_UDAmode
: id
= AVR_BUILTIN_ROUNDULK
; break;
168 case E_UTAmode
: id
= AVR_BUILTIN_ROUNDULLK
; break;
171 error_at (loc
, "no matching fixed-point overload found for %qs",
174 fold
= error_mark_node
;
178 fold
= targetm
.builtin_decl (id
, true);
180 if (fold
!= error_mark_node
)
181 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
185 case AVR_BUILTIN_COUNTLSFX
:
186 if (args
.length() != 1)
188 error_at (loc
, "%qs expects 1 argument but %d given",
189 "countlsfx", (int) args
.length());
191 fold
= error_mark_node
;
195 type0
= TREE_TYPE (args
[0]);
197 if (!FIXED_POINT_TYPE_P (type0
))
199 error_at (loc
, "%qs expects a fixed-point value as first argument",
202 fold
= error_mark_node
;
205 switch (TYPE_MODE (type0
))
207 case E_QQmode
: id
= AVR_BUILTIN_COUNTLSHR
; break;
208 case E_HQmode
: id
= AVR_BUILTIN_COUNTLSR
; break;
209 case E_SQmode
: id
= AVR_BUILTIN_COUNTLSLR
; break;
210 case E_DQmode
: id
= AVR_BUILTIN_COUNTLSLLR
; break;
212 case E_UQQmode
: id
= AVR_BUILTIN_COUNTLSUHR
; break;
213 case E_UHQmode
: id
= AVR_BUILTIN_COUNTLSUR
; break;
214 case E_USQmode
: id
= AVR_BUILTIN_COUNTLSULR
; break;
215 case E_UDQmode
: id
= AVR_BUILTIN_COUNTLSULLR
; break;
217 case E_HAmode
: id
= AVR_BUILTIN_COUNTLSHK
; break;
218 case E_SAmode
: id
= AVR_BUILTIN_COUNTLSK
; break;
219 case E_DAmode
: id
= AVR_BUILTIN_COUNTLSLK
; break;
220 case E_TAmode
: id
= AVR_BUILTIN_COUNTLSLLK
; break;
222 case E_UHAmode
: id
= AVR_BUILTIN_COUNTLSUHK
; break;
223 case E_USAmode
: id
= AVR_BUILTIN_COUNTLSUK
; break;
224 case E_UDAmode
: id
= AVR_BUILTIN_COUNTLSULK
; break;
225 case E_UTAmode
: id
= AVR_BUILTIN_COUNTLSULLK
; break;
228 error_at (loc
, "no matching fixed-point overload found for %qs",
231 fold
= error_mark_node
;
235 fold
= targetm
.builtin_decl (id
, true);
237 if (fold
!= error_mark_node
)
238 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
247 /* Implement `REGISTER_TARGET_PRAGMAS'. */
250 avr_register_target_pragmas (void)
252 gcc_assert (ADDR_SPACE_GENERIC
== ADDR_SPACE_RAM
);
254 /* Register address spaces. The order must be the same as in the respective
255 enum from avr.h (or designated initializers must be used in avr.c).
256 We always register all address spaces even if some of them make no
257 sense for some targets. Diagnose for non-supported spaces will be
258 emit by TARGET_ADDR_SPACE_DIAGNOSE_USAGE. */
260 for (int i
= 0; i
< ADDR_SPACE_COUNT
; i
++)
262 gcc_assert (i
== avr_addrspace
[i
].id
);
264 if (!ADDR_SPACE_GENERIC_P (i
))
265 c_register_addr_space (avr_addrspace
[i
].name
, avr_addrspace
[i
].id
);
268 targetm
.resolve_overloaded_builtin
= avr_resolve_overloaded_builtin
;
272 /* Transform LO into uppercase and write the result to UP.
273 You must provide enough space for UP. Return UP. */
276 avr_toupper (char *up
, const char *lo
)
280 for (; *lo
; lo
++, up
++)
288 /* Worker function for TARGET_CPU_CPP_BUILTINS. */
291 avr_cpu_cpp_builtins (struct cpp_reader
*pfile
)
293 builtin_define_std ("AVR");
295 /* __AVR_DEVICE_NAME__ and avr_mcu_types[].macro like __AVR_ATmega8__
296 are defined by -D command option, see device-specs file. */
299 cpp_define_formatted (pfile
, "__AVR_ARCH__=%s", avr_arch
->macro
);
300 if (AVR_HAVE_RAMPD
) cpp_define (pfile
, "__AVR_HAVE_RAMPD__");
301 if (AVR_HAVE_RAMPX
) cpp_define (pfile
, "__AVR_HAVE_RAMPX__");
302 if (AVR_HAVE_RAMPY
) cpp_define (pfile
, "__AVR_HAVE_RAMPY__");
303 if (AVR_HAVE_RAMPZ
) cpp_define (pfile
, "__AVR_HAVE_RAMPZ__");
304 if (AVR_HAVE_ELPM
) cpp_define (pfile
, "__AVR_HAVE_ELPM__");
305 if (AVR_HAVE_ELPMX
) cpp_define (pfile
, "__AVR_HAVE_ELPMX__");
306 if (AVR_HAVE_MOVW
) cpp_define (pfile
, "__AVR_HAVE_MOVW__");
307 if (AVR_HAVE_LPMX
) cpp_define (pfile
, "__AVR_HAVE_LPMX__");
309 if (avr_arch
->asm_only
)
310 cpp_define (pfile
, "__AVR_ASM_ONLY__");
313 cpp_define (pfile
, "__AVR_ENHANCED__");
314 cpp_define (pfile
, "__AVR_HAVE_MUL__");
317 if (AVR_HAVE_JMP_CALL
)
318 cpp_define (pfile
, "__AVR_HAVE_JMP_CALL__");
320 if (avr_arch
->have_jmp_call
)
321 cpp_define (pfile
, "__AVR_MEGA__");
324 cpp_define (pfile
, "__AVR_SHORT_CALLS__");
327 cpp_define (pfile
, "__AVR_XMEGA__");
331 cpp_define (pfile
, "__AVR_TINY__");
333 /* Define macro "__AVR_TINY_PM_BASE_ADDRESS__" with mapped program memory
334 start address. This macro shall be used where mapped program
335 memory is accessed, eg. copying data section (__do_copy_data)
336 contents to data memory region.
338 Program memory of AVR_TINY devices cannot be accessed directly,
339 it has been mapped to the data memory. For AVR_TINY devices
340 (ATtiny4/5/9/10/20 and 40) mapped program memory starts at 0x4000. */
342 cpp_define_formatted (pfile
, "__AVR_TINY_PM_BASE_ADDRESS__=0x%x",
343 avr_arch
->flash_pm_offset
);
346 if (avr_arch
->flash_pm_offset
)
347 cpp_define_formatted (pfile
, "__AVR_PM_BASE_ADDRESS__=0x%x",
348 avr_arch
->flash_pm_offset
);
350 if (AVR_HAVE_EIJMP_EICALL
)
352 cpp_define (pfile
, "__AVR_HAVE_EIJMP_EICALL__");
353 cpp_define (pfile
, "__AVR_3_BYTE_PC__");
357 cpp_define (pfile
, "__AVR_2_BYTE_PC__");
360 if (AVR_HAVE_8BIT_SP
)
361 cpp_define (pfile
, "__AVR_HAVE_8BIT_SP__");
363 cpp_define (pfile
, "__AVR_HAVE_16BIT_SP__");
366 cpp_define (pfile
, "__AVR_HAVE_SPH__");
368 cpp_define (pfile
, "__AVR_SP8__");
370 if (TARGET_NO_INTERRUPTS
)
371 cpp_define (pfile
, "__NO_INTERRUPTS__");
375 cpp_define (pfile
, "__AVR_ERRATA_SKIP__");
377 if (AVR_HAVE_JMP_CALL
)
378 cpp_define (pfile
, "__AVR_ERRATA_SKIP_JMP_CALL__");
382 cpp_define (pfile
, "__AVR_ISA_RMW__");
384 cpp_define_formatted (pfile
, "__AVR_SFR_OFFSET__=0x%x",
385 avr_arch
->sfr_offset
);
388 cpp_define (pfile
, "__WITH_AVRLIBC__");
389 #endif /* WITH_AVRLIBC */
391 /* Define builtin macros so that the user can easily query whether
392 non-generic address spaces (and which) are supported or not.
393 This is only supported for C. For C++, a language extension is needed
394 (as mentioned in ISO/IEC DTR 18037; Annex F.2) which is not
395 implemented in GCC up to now. */
399 for (int i
= 0; i
< ADDR_SPACE_COUNT
; i
++)
400 if (!ADDR_SPACE_GENERIC_P (i
)
401 /* Only supply __FLASH<n> macro if the address space is reasonable
402 for this target. The address space qualifier itself is still
403 supported, but using it will throw an error. */
404 && avr_addr_space_supported_p ((addr_space_t
) i
))
406 const char *name
= avr_addrspace
[i
].name
;
407 char *Name
= (char*) alloca (1 + strlen (name
));
409 cpp_define (pfile
, avr_toupper (Name
, name
));
413 /* Define builtin macros so that the user can easily query whether or
414 not a specific builtin is available. */
416 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME) \
417 cpp_define (pfile, "__BUILTIN_AVR_" #NAME);
418 #include "builtins.def"
421 /* Builtin macros for the __int24 and __uint24 type. */
423 cpp_define_formatted (pfile
, "__INT24_MAX__=8388607%s",
424 INT_TYPE_SIZE
== 8 ? "LL" : "L");
425 cpp_define (pfile
, "__INT24_MIN__=(-__INT24_MAX__-1)");
426 cpp_define_formatted (pfile
, "__UINT24_MAX__=16777215%s",
427 INT_TYPE_SIZE
== 8 ? "ULL" : "UL");