1 /* Copyright (C) 2009-2015 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"
30 #include "stor-layout.h"
32 #include "c-family/c-common.h"
33 #include "langhooks.h"
36 /* IDs for all the AVR builtins. */
40 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME) \
42 #include "builtins.def"
49 /* Implement `TARGET_RESOLVE_OVERLOADED_PLUGIN'. */
52 avr_resolve_overloaded_builtin (unsigned int iloc
, tree fndecl
, void *vargs
)
54 tree type0
, type1
, fold
= NULL_TREE
;
55 enum avr_builtin_id id
= AVR_BUILTIN_COUNT
;
56 location_t loc
= (location_t
) iloc
;
57 vec
<tree
, va_gc
> &args
= * (vec
<tree
, va_gc
>*) vargs
;
59 switch (DECL_FUNCTION_CODE (fndecl
))
64 case AVR_BUILTIN_ABSFX
:
65 if (args
.length() != 1)
67 error_at (loc
, "%qs expects 1 argument but %d given",
68 "absfx", (int) args
.length());
70 fold
= error_mark_node
;
74 type0
= TREE_TYPE (args
[0]);
76 if (!FIXED_POINT_TYPE_P (type0
))
78 error_at (loc
, "%qs expects a fixed-point value as argument",
81 fold
= error_mark_node
;
84 switch (TYPE_MODE (type0
))
86 case QQmode
: id
= AVR_BUILTIN_ABSHR
; break;
87 case HQmode
: id
= AVR_BUILTIN_ABSR
; break;
88 case SQmode
: id
= AVR_BUILTIN_ABSLR
; break;
89 case DQmode
: id
= AVR_BUILTIN_ABSLLR
; break;
91 case HAmode
: id
= AVR_BUILTIN_ABSHK
; break;
92 case SAmode
: id
= AVR_BUILTIN_ABSK
; break;
93 case DAmode
: id
= AVR_BUILTIN_ABSLK
; break;
94 case TAmode
: id
= AVR_BUILTIN_ABSLLK
; break;
104 warning_at (loc
, 0, "using %qs with unsigned type has no effect",
109 error_at (loc
, "no matching fixed-point overload found for %qs",
112 fold
= error_mark_node
;
116 fold
= targetm
.builtin_decl (id
, true);
118 if (fold
!= error_mark_node
)
119 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
123 case AVR_BUILTIN_ROUNDFX
:
124 if (args
.length() != 2)
126 error_at (loc
, "%qs expects 2 arguments but %d given",
127 "roundfx", (int) args
.length());
129 fold
= error_mark_node
;
133 type0
= TREE_TYPE (args
[0]);
134 type1
= TREE_TYPE (args
[1]);
136 if (!FIXED_POINT_TYPE_P (type0
))
138 error_at (loc
, "%qs expects a fixed-point value as first argument",
141 fold
= error_mark_node
;
144 if (!INTEGRAL_TYPE_P (type1
))
146 error_at (loc
, "%qs expects an integer value as second argument",
149 fold
= error_mark_node
;
152 switch (TYPE_MODE (type0
))
154 case QQmode
: id
= AVR_BUILTIN_ROUNDHR
; break;
155 case HQmode
: id
= AVR_BUILTIN_ROUNDR
; break;
156 case SQmode
: id
= AVR_BUILTIN_ROUNDLR
; break;
157 case DQmode
: id
= AVR_BUILTIN_ROUNDLLR
; break;
159 case UQQmode
: id
= AVR_BUILTIN_ROUNDUHR
; break;
160 case UHQmode
: id
= AVR_BUILTIN_ROUNDUR
; break;
161 case USQmode
: id
= AVR_BUILTIN_ROUNDULR
; break;
162 case UDQmode
: id
= AVR_BUILTIN_ROUNDULLR
; break;
164 case HAmode
: id
= AVR_BUILTIN_ROUNDHK
; break;
165 case SAmode
: id
= AVR_BUILTIN_ROUNDK
; break;
166 case DAmode
: id
= AVR_BUILTIN_ROUNDLK
; break;
167 case TAmode
: id
= AVR_BUILTIN_ROUNDLLK
; break;
169 case UHAmode
: id
= AVR_BUILTIN_ROUNDUHK
; break;
170 case USAmode
: id
= AVR_BUILTIN_ROUNDUK
; break;
171 case UDAmode
: id
= AVR_BUILTIN_ROUNDULK
; break;
172 case UTAmode
: id
= AVR_BUILTIN_ROUNDULLK
; break;
175 error_at (loc
, "no matching fixed-point overload found for %qs",
178 fold
= error_mark_node
;
182 fold
= targetm
.builtin_decl (id
, true);
184 if (fold
!= error_mark_node
)
185 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
189 case AVR_BUILTIN_COUNTLSFX
:
190 if (args
.length() != 1)
192 error_at (loc
, "%qs expects 1 argument but %d given",
193 "countlsfx", (int) args
.length());
195 fold
= error_mark_node
;
199 type0
= TREE_TYPE (args
[0]);
201 if (!FIXED_POINT_TYPE_P (type0
))
203 error_at (loc
, "%qs expects a fixed-point value as first argument",
206 fold
= error_mark_node
;
209 switch (TYPE_MODE (type0
))
211 case QQmode
: id
= AVR_BUILTIN_COUNTLSHR
; break;
212 case HQmode
: id
= AVR_BUILTIN_COUNTLSR
; break;
213 case SQmode
: id
= AVR_BUILTIN_COUNTLSLR
; break;
214 case DQmode
: id
= AVR_BUILTIN_COUNTLSLLR
; break;
216 case UQQmode
: id
= AVR_BUILTIN_COUNTLSUHR
; break;
217 case UHQmode
: id
= AVR_BUILTIN_COUNTLSUR
; break;
218 case USQmode
: id
= AVR_BUILTIN_COUNTLSULR
; break;
219 case UDQmode
: id
= AVR_BUILTIN_COUNTLSULLR
; break;
221 case HAmode
: id
= AVR_BUILTIN_COUNTLSHK
; break;
222 case SAmode
: id
= AVR_BUILTIN_COUNTLSK
; break;
223 case DAmode
: id
= AVR_BUILTIN_COUNTLSLK
; break;
224 case TAmode
: id
= AVR_BUILTIN_COUNTLSLLK
; break;
226 case UHAmode
: id
= AVR_BUILTIN_COUNTLSUHK
; break;
227 case USAmode
: id
= AVR_BUILTIN_COUNTLSUK
; break;
228 case UDAmode
: id
= AVR_BUILTIN_COUNTLSULK
; break;
229 case UTAmode
: id
= AVR_BUILTIN_COUNTLSULLK
; break;
232 error_at (loc
, "no matching fixed-point overload found for %qs",
235 fold
= error_mark_node
;
239 fold
= targetm
.builtin_decl (id
, true);
241 if (fold
!= error_mark_node
)
242 fold
= build_function_call_vec (loc
, vNULL
, fold
, &args
, NULL
);
251 /* Implement `REGISTER_TARGET_PRAGMAS'. */
254 avr_register_target_pragmas (void)
258 gcc_assert (ADDR_SPACE_GENERIC
== ADDR_SPACE_RAM
);
260 /* Register address spaces. The order must be the same as in the respective
261 enum from avr.h (or designated initializers must be used in avr.c). */
263 for (i
= 0; i
< ADDR_SPACE_COUNT
; i
++)
265 gcc_assert (i
== avr_addrspace
[i
].id
);
267 if (!ADDR_SPACE_GENERIC_P (i
))
268 c_register_addr_space (avr_addrspace
[i
].name
, avr_addrspace
[i
].id
);
271 targetm
.resolve_overloaded_builtin
= avr_resolve_overloaded_builtin
;
275 /* Transform LO into uppercase and write the result to UP.
276 You must provide enough space for UP. Return UP. */
279 avr_toupper (char *up
, const char *lo
)
283 for (; *lo
; lo
++, up
++)
291 /* Worker function for TARGET_CPU_CPP_BUILTINS. */
294 avr_cpu_cpp_builtins (struct cpp_reader
*pfile
)
298 builtin_define_std ("AVR");
300 /* __AVR_DEVICE_NAME__ and avr_mcu_types[].macro like __AVR_ATmega8__
301 are defined by -D command option, see device-specs file. */
304 cpp_define_formatted (pfile
, "__AVR_ARCH__=%s", avr_arch
->macro
);
305 if (AVR_HAVE_RAMPD
) cpp_define (pfile
, "__AVR_HAVE_RAMPD__");
306 if (AVR_HAVE_RAMPX
) cpp_define (pfile
, "__AVR_HAVE_RAMPX__");
307 if (AVR_HAVE_RAMPY
) cpp_define (pfile
, "__AVR_HAVE_RAMPY__");
308 if (AVR_HAVE_RAMPZ
) cpp_define (pfile
, "__AVR_HAVE_RAMPZ__");
309 if (AVR_HAVE_ELPM
) cpp_define (pfile
, "__AVR_HAVE_ELPM__");
310 if (AVR_HAVE_ELPMX
) cpp_define (pfile
, "__AVR_HAVE_ELPMX__");
311 if (AVR_HAVE_MOVW
) cpp_define (pfile
, "__AVR_HAVE_MOVW__");
312 if (AVR_HAVE_LPMX
) cpp_define (pfile
, "__AVR_HAVE_LPMX__");
314 if (avr_arch
->asm_only
)
315 cpp_define (pfile
, "__AVR_ASM_ONLY__");
318 cpp_define (pfile
, "__AVR_ENHANCED__");
319 cpp_define (pfile
, "__AVR_HAVE_MUL__");
321 if (avr_arch
->have_jmp_call
)
323 cpp_define (pfile
, "__AVR_MEGA__");
324 cpp_define (pfile
, "__AVR_HAVE_JMP_CALL__");
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 (pfile
, "__AVR_TINY_PM_BASE_ADDRESS__=0x4000");
345 if (AVR_HAVE_EIJMP_EICALL
)
347 cpp_define (pfile
, "__AVR_HAVE_EIJMP_EICALL__");
348 cpp_define (pfile
, "__AVR_3_BYTE_PC__");
352 cpp_define (pfile
, "__AVR_2_BYTE_PC__");
355 if (AVR_HAVE_8BIT_SP
)
356 cpp_define (pfile
, "__AVR_HAVE_8BIT_SP__");
358 cpp_define (pfile
, "__AVR_HAVE_16BIT_SP__");
361 cpp_define (pfile
, "__AVR_HAVE_SPH__");
363 cpp_define (pfile
, "__AVR_SP8__");
365 if (TARGET_NO_INTERRUPTS
)
366 cpp_define (pfile
, "__NO_INTERRUPTS__");
370 cpp_define (pfile
, "__AVR_ERRATA_SKIP__");
372 if (AVR_HAVE_JMP_CALL
)
373 cpp_define (pfile
, "__AVR_ERRATA_SKIP_JMP_CALL__");
377 cpp_define (pfile
, "__AVR_ISA_RMW__");
379 cpp_define_formatted (pfile
, "__AVR_SFR_OFFSET__=0x%x",
380 avr_arch
->sfr_offset
);
383 cpp_define (pfile
, "__WITH_AVRLIBC__");
384 #endif /* WITH_AVRLIBC */
386 /* Define builtin macros so that the user can easily query whether
387 non-generic address spaces (and which) are supported or not.
388 This is only supported for C. For C++, a language extension is needed
389 (as mentioned in ISO/IEC DTR 18037; Annex F.2) which is not
390 implemented in GCC up to now. */
394 for (i
= 0; i
< ADDR_SPACE_COUNT
; i
++)
395 if (!ADDR_SPACE_GENERIC_P (i
)
396 /* Only supply __FLASH<n> macro if the address space is reasonable
397 for this target. The address space qualifier itself is still
398 supported, but using it will throw an error. */
399 && avr_addrspace
[i
].segment
< avr_n_flash
400 /* Only support __MEMX macro if we have LPM. */
401 && (AVR_HAVE_LPM
|| avr_addrspace
[i
].pointer_size
<= 2))
404 const char *name
= avr_addrspace
[i
].name
;
405 char *Name
= (char*) alloca (1 + strlen (name
));
407 cpp_define (pfile
, avr_toupper (Name
, name
));
411 /* Define builtin macros so that the user can easily query whether or
412 not a specific builtin is available. */
414 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, CODE, LIBNAME) \
415 cpp_define (pfile, "__BUILTIN_AVR_" #NAME);
416 #include "builtins.def"
419 /* Builtin macros for the __int24 and __uint24 type. */
421 cpp_define_formatted (pfile
, "__INT24_MAX__=8388607%s",
422 INT_TYPE_SIZE
== 8 ? "LL" : "L");
423 cpp_define (pfile
, "__INT24_MIN__=(-__INT24_MAX__-1)");
424 cpp_define_formatted (pfile
, "__UINT24_MAX__=16777215%s",
425 INT_TYPE_SIZE
== 8 ? "ULL" : "UL");