1 /* Copyright (C) 2009-2016 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"
31 /* IDs for all the AVR builtins. */
35 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME) \
37 #include "builtins.def"
44 /* Implement `TARGET_RESOLVE_OVERLOADED_PLUGIN'. */
47 avr_resolve_overloaded_builtin (unsigned int iloc
, tree fndecl
, void *vargs
)
49 tree type0
, type1
, fold
= NULL_TREE
;
50 enum avr_builtin_id id
= AVR_BUILTIN_COUNT
;
51 location_t loc
= (location_t
) iloc
;
52 vec
<tree
, va_gc
> &args
= * (vec
<tree
, va_gc
>*) vargs
;
54 switch (DECL_FUNCTION_CODE (fndecl
))
59 case AVR_BUILTIN_ABSFX
:
60 if (args
.length() != 1)
62 error_at (loc
, "%qs expects 1 argument but %d given",
63 "absfx", (int) args
.length());
65 fold
= error_mark_node
;
69 type0
= TREE_TYPE (args
[0]);
71 if (!FIXED_POINT_TYPE_P (type0
))
73 error_at (loc
, "%qs expects a fixed-point value as argument",
76 fold
= error_mark_node
;
79 switch (TYPE_MODE (type0
))
81 case QQmode
: id
= AVR_BUILTIN_ABSHR
; break;
82 case HQmode
: id
= AVR_BUILTIN_ABSR
; break;
83 case SQmode
: id
= AVR_BUILTIN_ABSLR
; break;
84 case DQmode
: id
= AVR_BUILTIN_ABSLLR
; break;
86 case HAmode
: id
= AVR_BUILTIN_ABSHK
; break;
87 case SAmode
: id
= AVR_BUILTIN_ABSK
; break;
88 case DAmode
: id
= AVR_BUILTIN_ABSLK
; break;
89 case TAmode
: id
= AVR_BUILTIN_ABSLLK
; break;
99 warning_at (loc
, 0, "using %qs with unsigned type has no effect",
104 error_at (loc
, "no matching fixed-point overload found for %qs",
107 fold
= error_mark_node
;
111 fold
= targetm
.builtin_decl (id
, true);
113 if (fold
!= error_mark_node
)
114 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
118 case AVR_BUILTIN_ROUNDFX
:
119 if (args
.length() != 2)
121 error_at (loc
, "%qs expects 2 arguments but %d given",
122 "roundfx", (int) args
.length());
124 fold
= error_mark_node
;
128 type0
= TREE_TYPE (args
[0]);
129 type1
= TREE_TYPE (args
[1]);
131 if (!FIXED_POINT_TYPE_P (type0
))
133 error_at (loc
, "%qs expects a fixed-point value as first argument",
136 fold
= error_mark_node
;
139 if (!INTEGRAL_TYPE_P (type1
))
141 error_at (loc
, "%qs expects an integer value as second argument",
144 fold
= error_mark_node
;
147 switch (TYPE_MODE (type0
))
149 case QQmode
: id
= AVR_BUILTIN_ROUNDHR
; break;
150 case HQmode
: id
= AVR_BUILTIN_ROUNDR
; break;
151 case SQmode
: id
= AVR_BUILTIN_ROUNDLR
; break;
152 case DQmode
: id
= AVR_BUILTIN_ROUNDLLR
; break;
154 case UQQmode
: id
= AVR_BUILTIN_ROUNDUHR
; break;
155 case UHQmode
: id
= AVR_BUILTIN_ROUNDUR
; break;
156 case USQmode
: id
= AVR_BUILTIN_ROUNDULR
; break;
157 case UDQmode
: id
= AVR_BUILTIN_ROUNDULLR
; break;
159 case HAmode
: id
= AVR_BUILTIN_ROUNDHK
; break;
160 case SAmode
: id
= AVR_BUILTIN_ROUNDK
; break;
161 case DAmode
: id
= AVR_BUILTIN_ROUNDLK
; break;
162 case TAmode
: id
= AVR_BUILTIN_ROUNDLLK
; break;
164 case UHAmode
: id
= AVR_BUILTIN_ROUNDUHK
; break;
165 case USAmode
: id
= AVR_BUILTIN_ROUNDUK
; break;
166 case UDAmode
: id
= AVR_BUILTIN_ROUNDULK
; break;
167 case UTAmode
: id
= AVR_BUILTIN_ROUNDULLK
; break;
170 error_at (loc
, "no matching fixed-point overload found for %qs",
173 fold
= error_mark_node
;
177 fold
= targetm
.builtin_decl (id
, true);
179 if (fold
!= error_mark_node
)
180 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
184 case AVR_BUILTIN_COUNTLSFX
:
185 if (args
.length() != 1)
187 error_at (loc
, "%qs expects 1 argument but %d given",
188 "countlsfx", (int) args
.length());
190 fold
= error_mark_node
;
194 type0
= TREE_TYPE (args
[0]);
196 if (!FIXED_POINT_TYPE_P (type0
))
198 error_at (loc
, "%qs expects a fixed-point value as first argument",
201 fold
= error_mark_node
;
204 switch (TYPE_MODE (type0
))
206 case QQmode
: id
= AVR_BUILTIN_COUNTLSHR
; break;
207 case HQmode
: id
= AVR_BUILTIN_COUNTLSR
; break;
208 case SQmode
: id
= AVR_BUILTIN_COUNTLSLR
; break;
209 case DQmode
: id
= AVR_BUILTIN_COUNTLSLLR
; break;
211 case UQQmode
: id
= AVR_BUILTIN_COUNTLSUHR
; break;
212 case UHQmode
: id
= AVR_BUILTIN_COUNTLSUR
; break;
213 case USQmode
: id
= AVR_BUILTIN_COUNTLSULR
; break;
214 case UDQmode
: id
= AVR_BUILTIN_COUNTLSULLR
; break;
216 case HAmode
: id
= AVR_BUILTIN_COUNTLSHK
; break;
217 case SAmode
: id
= AVR_BUILTIN_COUNTLSK
; break;
218 case DAmode
: id
= AVR_BUILTIN_COUNTLSLK
; break;
219 case TAmode
: id
= AVR_BUILTIN_COUNTLSLLK
; break;
221 case UHAmode
: id
= AVR_BUILTIN_COUNTLSUHK
; break;
222 case USAmode
: id
= AVR_BUILTIN_COUNTLSUK
; break;
223 case UDAmode
: id
= AVR_BUILTIN_COUNTLSULK
; break;
224 case UTAmode
: id
= AVR_BUILTIN_COUNTLSULLK
; break;
227 error_at (loc
, "no matching fixed-point overload found for %qs",
230 fold
= error_mark_node
;
234 fold
= targetm
.builtin_decl (id
, true);
236 if (fold
!= error_mark_node
)
237 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
246 /* Implement `REGISTER_TARGET_PRAGMAS'. */
249 avr_register_target_pragmas (void)
253 gcc_assert (ADDR_SPACE_GENERIC
== ADDR_SPACE_RAM
);
255 /* Register address spaces. The order must be the same as in the respective
256 enum from avr.h (or designated initializers must be used in avr.c). */
258 for (i
= 0; i
< ADDR_SPACE_COUNT
; i
++)
260 gcc_assert (i
== avr_addrspace
[i
].id
);
262 if (!ADDR_SPACE_GENERIC_P (i
))
263 c_register_addr_space (avr_addrspace
[i
].name
, avr_addrspace
[i
].id
);
266 targetm
.resolve_overloaded_builtin
= avr_resolve_overloaded_builtin
;
270 /* Transform LO into uppercase and write the result to UP.
271 You must provide enough space for UP. Return UP. */
274 avr_toupper (char *up
, const char *lo
)
278 for (; *lo
; lo
++, up
++)
286 /* Worker function for TARGET_CPU_CPP_BUILTINS. */
289 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__");
316 if (avr_arch
->have_jmp_call
)
318 cpp_define (pfile
, "__AVR_MEGA__");
319 cpp_define (pfile
, "__AVR_HAVE_JMP_CALL__");
322 cpp_define (pfile
, "__AVR_XMEGA__");
326 cpp_define (pfile
, "__AVR_TINY__");
328 /* Define macro "__AVR_TINY_PM_BASE_ADDRESS__" with mapped program memory
329 start address. This macro shall be used where mapped program
330 memory is accessed, eg. copying data section (__do_copy_data)
331 contents to data memory region.
333 Program memory of AVR_TINY devices cannot be accessed directly,
334 it has been mapped to the data memory. For AVR_TINY devices
335 (ATtiny4/5/9/10/20 and 40) mapped program memory starts at 0x4000. */
337 cpp_define (pfile
, "__AVR_TINY_PM_BASE_ADDRESS__=0x4000");
340 if (AVR_HAVE_EIJMP_EICALL
)
342 cpp_define (pfile
, "__AVR_HAVE_EIJMP_EICALL__");
343 cpp_define (pfile
, "__AVR_3_BYTE_PC__");
347 cpp_define (pfile
, "__AVR_2_BYTE_PC__");
350 if (AVR_HAVE_8BIT_SP
)
351 cpp_define (pfile
, "__AVR_HAVE_8BIT_SP__");
353 cpp_define (pfile
, "__AVR_HAVE_16BIT_SP__");
356 cpp_define (pfile
, "__AVR_HAVE_SPH__");
358 cpp_define (pfile
, "__AVR_SP8__");
360 if (TARGET_NO_INTERRUPTS
)
361 cpp_define (pfile
, "__NO_INTERRUPTS__");
365 cpp_define (pfile
, "__AVR_ERRATA_SKIP__");
367 if (AVR_HAVE_JMP_CALL
)
368 cpp_define (pfile
, "__AVR_ERRATA_SKIP_JMP_CALL__");
372 cpp_define (pfile
, "__AVR_ISA_RMW__");
374 cpp_define_formatted (pfile
, "__AVR_SFR_OFFSET__=0x%x",
375 avr_arch
->sfr_offset
);
378 cpp_define (pfile
, "__WITH_AVRLIBC__");
379 #endif /* WITH_AVRLIBC */
381 /* Define builtin macros so that the user can easily query whether
382 non-generic address spaces (and which) are supported or not.
383 This is only supported for C. For C++, a language extension is needed
384 (as mentioned in ISO/IEC DTR 18037; Annex F.2) which is not
385 implemented in GCC up to now. */
389 for (i
= 0; i
< ADDR_SPACE_COUNT
; i
++)
390 if (!ADDR_SPACE_GENERIC_P (i
)
391 /* Only supply __FLASH<n> macro if the address space is reasonable
392 for this target. The address space qualifier itself is still
393 supported, but using it will throw an error. */
394 && avr_addrspace
[i
].segment
< avr_n_flash
395 /* Only support __MEMX macro if we have LPM. */
396 && (AVR_HAVE_LPM
|| avr_addrspace
[i
].pointer_size
<= 2))
399 const char *name
= avr_addrspace
[i
].name
;
400 char *Name
= (char*) alloca (1 + strlen (name
));
402 cpp_define (pfile
, avr_toupper (Name
, name
));
406 /* Define builtin macros so that the user can easily query whether or
407 not a specific builtin is available. */
409 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME) \
410 cpp_define (pfile, "__BUILTIN_AVR_" #NAME);
411 #include "builtins.def"
414 /* Builtin macros for the __int24 and __uint24 type. */
416 cpp_define_formatted (pfile
, "__INT24_MAX__=8388607%s",
417 INT_TYPE_SIZE
== 8 ? "LL" : "L");
418 cpp_define (pfile
, "__INT24_MIN__=(-__INT24_MAX__-1)");
419 cpp_define_formatted (pfile
, "__UINT24_MAX__=16777215%s",
420 INT_TYPE_SIZE
== 8 ? "ULL" : "UL");