compiler.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /****************************************************************************
  2. * include/nuttx/compiler.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_NUTTX_COMPILER_H
  21. #define __INCLUDE_NUTTX_COMPILER_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. /****************************************************************************
  27. * Pre-processor Definitions
  28. ****************************************************************************/
  29. /* GCC-specific definitions *************************************************/
  30. #ifdef __GNUC__
  31. /* Pre-processor */
  32. # define CONFIG_CPP_HAVE_VARARGS 1 /* Supports variable argument macros */
  33. # define CONFIG_CPP_HAVE_WARNING 1 /* Supports #warning */
  34. /* Intriniscs. GCC supports __func__ but provides __FUNCTION__ for backward
  35. * compatibility with older versions of GCC.
  36. */
  37. # define CONFIG_HAVE_FUNCTIONNAME 1 /* Has __FUNCTION__ */
  38. # define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
  39. /* Indicate that a local variable is not used */
  40. # define UNUSED(a) ((void)(a))
  41. /* Built-in functions */
  42. /* GCC 4.x have __builtin_ctz(|l|ll) and __builtin_clz(|l|ll). These count
  43. * trailing/leading zeros of input number and typically will generate few
  44. * fast bit-counting instructions. Inputting zero to these functions is
  45. * undefined and needs to be taken care of by the caller.
  46. */
  47. #if __GNUC__ >= 4
  48. # define CONFIG_HAVE_BUILTIN_CTZ 1
  49. # define CONFIG_HAVE_BUILTIN_CLZ 1
  50. #endif
  51. /* C++ support */
  52. #if defined(__cplusplus) && __cplusplus >= 201402L
  53. # define CONFIG_HAVE_CXX14 1
  54. #else
  55. # undef CONFIG_HAVE_CXX14
  56. #endif
  57. /* Attributes
  58. *
  59. * GCC supports weak symbols which can be used to reduce code size because
  60. * unnecessary "weak" functions can be excluded from the link.
  61. */
  62. # if !defined(__CYGWIN__) && !defined(CONFIG_ARCH_GNU_NO_WEAKFUNCTIONS)
  63. # define CONFIG_HAVE_WEAKFUNCTIONS 1
  64. # define weak_alias(name, aliasname) \
  65. extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
  66. # define weak_data __attribute__ ((weak))
  67. # define weak_function __attribute__ ((weak))
  68. # define weak_const_function __attribute__ ((weak, __const__))
  69. # else
  70. # undef CONFIG_HAVE_WEAKFUNCTIONS
  71. # define weak_alias(name, aliasname)
  72. # define weak_data
  73. # define weak_function
  74. # define weak_const_function
  75. # endif
  76. /* The noreturn attribute informs GCC that the function will not return.
  77. * C11 adds _Noreturn keyword (see stdnoreturn.h)
  78. */
  79. # define noreturn_function __attribute__ ((noreturn))
  80. /* The farcall_function attribute informs GCC that is should use long calls
  81. * (even though -mlong-calls does not appear in the compilation options)
  82. */
  83. # define farcall_function __attribute__ ((long_call))
  84. /* Code locate */
  85. # define locate_code(n) __attribute__ ((section(n)))
  86. /* Data alignment */
  87. # define aligned_data(n) __attribute__ ((aligned(n)))
  88. /* Data location */
  89. # define locate_data(n) __attribute__ ((section(n)))
  90. /* The packed attribute informs GCC that the structure elements are packed,
  91. * ignoring other alignment rules.
  92. */
  93. # define begin_packed_struct
  94. # define end_packed_struct __attribute__ ((packed))
  95. /* GCC does not support the reentrant attribute */
  96. # define reentrant_function
  97. /* The naked attribute informs GCC that the programmer will take care of
  98. * the function prolog and epilog.
  99. */
  100. # define naked_function __attribute__ ((naked,no_instrument_function))
  101. /* The inline_function attribute informs GCC that the function should always
  102. * be inlined, regardless of the level of optimization. The
  103. * noinline_function indicates that the function should never be inlined.
  104. */
  105. # define inline_function __attribute__ ((always_inline,no_instrument_function))
  106. # define noinline_function __attribute__ ((noinline))
  107. /* GCC does not use storage classes to qualify addressing */
  108. # define FAR
  109. # define NEAR
  110. # define DSEG
  111. # define CODE
  112. /* Handle cases where sizeof(int) is 16-bits, sizeof(long) is 32-bits, and
  113. * pointers are 16-bits.
  114. */
  115. #if defined(__m32c__)
  116. /* No I-space access qualifiers */
  117. # define IOBJ
  118. # define IPTR
  119. /* Select the small, 16-bit addressing model */
  120. # define CONFIG_SMALL_MEMORY 1
  121. /* Long and int are not the same size */
  122. # define CONFIG_LONG_IS_NOT_INT 1
  123. /* Pointers and int are the same size */
  124. # undef CONFIG_PTR_IS_NOT_INT
  125. #elif defined(__AVR__)
  126. # if defined(CONFIG_AVR_HAS_MEMX_PTR)
  127. /* I-space access qualifiers needed by Harvard architecture */
  128. # define IOBJ __flash
  129. # define IPTR __memx
  130. # else
  131. /* No I-space access qualifiers */
  132. # define IOBJ
  133. # define IPTR
  134. # endif
  135. /* Select the small, 16-bit addressing model (for D-Space) */
  136. # define CONFIG_SMALL_MEMORY 1
  137. /* Long and int are not the same size */
  138. # define CONFIG_LONG_IS_NOT_INT 1
  139. /* Pointers and int are the same size */
  140. # undef CONFIG_PTR_IS_NOT_INT
  141. /* Uses a 32-bit FAR pointer only from accessing data outside of the 16-bit
  142. * data space.
  143. */
  144. # define CONFIG_HAVE_FARPOINTER 1
  145. #elif defined(__mc68hc1x__)
  146. /* No I-space access qualifiers */
  147. # define IOBJ
  148. # define IPTR
  149. /* Select the small, 16-bit addressing model */
  150. # define CONFIG_SMALL_MEMORY 1
  151. /* Normally, mc68hc1x code is compiled with the -mshort option
  152. * which results in a 16-bit integer. If -mnoshort is defined
  153. * then an integer is 32-bits. GCC will defined __INT__ accordingly:
  154. */
  155. # if __INT__ == 16
  156. /* int is 16-bits, long is 32-bits */
  157. # define CONFIG_LONG_IS_NOT_INT 1
  158. /* Pointers and int are the same size (16-bits) */
  159. # undef CONFIG_PTR_IS_NOT_INT
  160. # else
  161. /* int and long are both 32-bits */
  162. # undef CONFIG_LONG_IS_NOT_INT
  163. /* Pointers and int are NOT the same size */
  164. # define CONFIG_PTR_IS_NOT_INT 1
  165. # endif
  166. #else
  167. /* No I-space access qualifiers */
  168. # define IOBJ
  169. # define IPTR
  170. /* Select the large, 32-bit addressing model */
  171. # undef CONFIG_SMALL_MEMORY
  172. /* Long and int are (probably) the same size (32-bits) */
  173. # undef CONFIG_LONG_IS_NOT_INT
  174. /* Pointers and int are the same size (32-bits) */
  175. # undef CONFIG_PTR_IS_NOT_INT
  176. #endif
  177. /* ISO C11 supports anonymous (unnamed) structures and unions, added in
  178. * GCC 4.6 (but might be suppressed with -std= option). ISO C++11 also
  179. * adds un-named unions, but NOT unnamed structures (although compilers
  180. * may support them).
  181. *
  182. * CAREFUL: This can cause issues for shared data structures shared between
  183. * C and C++ if the two versions do not support the same features.
  184. * Structures and unions can lose binary compatibility!
  185. *
  186. * NOTE: The NuttX coding standard forbids the use of unnamed structures and
  187. * unions within the OS.
  188. */
  189. # undef CONFIG_HAVE_ANONYMOUS_STRUCT
  190. # undef CONFIG_HAVE_ANONYMOUS_UNION
  191. # if (defined(__cplusplus) && __cplusplus >= 201103L) || \
  192. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
  193. # define CONFIG_HAVE_ANONYMOUS_STRUCT 1
  194. # define CONFIG_HAVE_ANONYMOUS_UNION 1
  195. # endif
  196. # define CONFIG_HAVE_LONG_LONG 1
  197. # define CONFIG_HAVE_FLOAT 1
  198. # define CONFIG_HAVE_DOUBLE 1
  199. # define CONFIG_HAVE_LONG_DOUBLE 1
  200. /* Indicate that a local variable is not used */
  201. # define UNUSED(a) ((void)(a))
  202. /* SDCC-specific definitions ************************************************/
  203. #elif defined(SDCC) || defined(__SDCC)
  204. /* No I-space access qualifiers */
  205. # define IOBJ
  206. # define IPTR
  207. /* Pre-processor */
  208. # define CONFIG_CPP_HAVE_VARARGS 1 /* Supports variable argument macros */
  209. # define CONFIG_CPP_HAVE_WARNING 1 /* Supports #warning */
  210. /* Intriniscs */
  211. # define CONFIG_HAVE_FUNCTIONNAME 1 /* Has __FUNCTION__ */
  212. # define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
  213. # define __FUNCTION__ __func__ /* SDCC supports on __func__ */
  214. /* Pragmas
  215. *
  216. * Disable warnings for unused function arguments
  217. */
  218. # pragma disable_warning 85
  219. /* C++ support */
  220. # undef CONFIG_HAVE_CXX14
  221. /* Attributes
  222. *
  223. * SDCC does not support weak symbols
  224. */
  225. # undef CONFIG_HAVE_WEAKFUNCTIONS
  226. # define weak_alias(name, aliasname)
  227. # define weak_data
  228. # define weak_function
  229. # define weak_const_function
  230. # define restrict /* REVISIT */
  231. /* SDCC does not support the noreturn or packed attributes */
  232. /* Current SDCC supports noreturn via C11 _Noreturn keyword (see
  233. * stdnoreturn.h).
  234. */
  235. # define noreturn_function
  236. # define locate_code(n)
  237. # define aligned_data(n)
  238. # define locate_data(n)
  239. # define begin_packed_struct
  240. # define end_packed_struct
  241. /* REVISIT: */
  242. # define farcall_function
  243. /* SDCC does support "naked" functions */
  244. # define naked_function __naked
  245. /* SDCC does not support forced inlining. */
  246. # define inline_function
  247. # define noinline_function
  248. /* The reentrant attribute informs SDCC that the function
  249. * must be reentrant. In this case, SDCC will store input
  250. * arguments on the stack to support reentrancy.
  251. *
  252. * SDCC functions are always reentrant (except for the mcs51,
  253. * ds390, hc08 and s08 backends)
  254. */
  255. # define reentrant_function __reentrant
  256. /* ISO C11 supports anonymous (unnamed) structures and unions. Does SDCC? */
  257. # undef CONFIG_HAVE_ANONYMOUS_STRUCT
  258. # undef CONFIG_HAVE_ANONYMOUS_UNION
  259. /* Indicate that a local variable is not used */
  260. # define UNUSED(a) ((void)(a))
  261. /* It is assumed that the system is build using the small
  262. * data model with storage defaulting to internal RAM.
  263. * The NEAR storage class can also be used to address data
  264. * in internal RAM; FAR can be used to address data in
  265. * external RAM.
  266. */
  267. #if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_gbz80)
  268. # define FAR
  269. # define NEAR
  270. # define CODE
  271. # define DSEG
  272. #else
  273. # define FAR __xdata
  274. # define NEAR __data
  275. # define CODE __code
  276. # if defined(SDCC_MODEL_SMALL)
  277. # define DSEG __data
  278. # else
  279. # define DSEG __xdata
  280. # endif
  281. #endif
  282. /* Select small, 16-bit address model */
  283. # define CONFIG_SMALL_MEMORY 1
  284. /* Long and int are not the same size */
  285. # define CONFIG_LONG_IS_NOT_INT 1
  286. /* The generic pointer and int are not the same size (for some SDCC
  287. * architectures). REVISIT: SDCC now has more backends where pointers are
  288. * the same size as int than just z80 and z180.
  289. */
  290. #if !defined(__z80) && !defined(__gbz80)
  291. # define CONFIG_PTR_IS_NOT_INT 1
  292. #endif
  293. /* SDCC does types long long and float, but not types double and long
  294. * double.
  295. */
  296. # define CONFIG_HAVE_LONG_LONG 1
  297. # define CONFIG_HAVE_FLOAT 1
  298. # undef CONFIG_HAVE_DOUBLE
  299. # undef CONFIG_HAVE_LONG_DOUBLE
  300. /* Indicate that a local variable is not used */
  301. # define UNUSED(a) ((void)(a))
  302. /* Zilog-specific definitions ***********************************************/
  303. #elif defined(__ZILOG__)
  304. /* At present, only the following Zilog compilers are recognized */
  305. # if !defined(__ZNEO__) && !defined(__EZ8__) && !defined(__EZ80__)
  306. # warning "Unrecognized Zilog compiler"
  307. # endif
  308. /* Pre-processor */
  309. # undef CONFIG_CPP_HAVE_VARARGS /* No variable argument macros */
  310. # undef CONFIG_CPP_HAVE_WARNING /* Does not support #warning */
  311. /* Intrinsics */
  312. # define CONFIG_HAVE_FUNCTIONNAME 1 /* Has __FUNCTION__ */
  313. # define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
  314. /* No I-space access qualifiers */
  315. # define IOBJ
  316. # define IPTR
  317. /* C++ support */
  318. # undef CONFIG_HAVE_CXX14
  319. /* Attributes
  320. *
  321. * The Zilog compiler does not support weak symbols
  322. */
  323. # undef CONFIG_HAVE_WEAKFUNCTIONS
  324. # define weak_alias(name, aliasname)
  325. # define weak_data
  326. # define weak_function
  327. # define weak_const_function
  328. # define restrict
  329. /* The Zilog compiler does not support the noreturn, packed, naked
  330. * attributes.
  331. */
  332. # define noreturn_function
  333. # define aligned_data(n)
  334. # define locate_code(n)
  335. # define locate_data(n)
  336. # define begin_packed_struct
  337. # define end_packed_struct
  338. # define naked_function
  339. # define inline_function
  340. # define noinline_function
  341. /* REVISIT: */
  342. # define farcall_function
  343. /* The Zilog compiler does not support the reentrant attribute */
  344. # define reentrant_function
  345. /* Addressing.
  346. *
  347. * Z16F ZNEO: Far is 24-bits; near is 16-bits of address.
  348. * The supported model is (1) all code on ROM, and (2) all data
  349. * and stacks in external (far) RAM.
  350. * Z8Encore!: Far is 16-bits; near is 8-bits of address.
  351. * The supported model is (1) all code on ROM, and (2) all data
  352. * and stacks in internal (far) RAM.
  353. * Z8Acclaim: In Z80 mode, all pointers are 16-bits. In ADL mode, all
  354. * pointers are 24 bits.
  355. */
  356. # if defined(__ZNEO__)
  357. # define FAR _Far
  358. # define NEAR _Near
  359. # define DSEG _Far
  360. # define CODE _Erom
  361. # undef CONFIG_SMALL_MEMORY /* Select the large, 32-bit addressing model */
  362. # undef CONFIG_LONG_IS_NOT_INT /* Long and int are the same size */
  363. # undef CONFIG_PTR_IS_NOT_INT /* FAR pointers and int are the same size */
  364. # elif defined(__EZ8__)
  365. # define FAR far
  366. # define NEAR near
  367. # define DSEG far
  368. # define CODE rom
  369. # define CONFIG_SMALL_MEMORY 1 /* Select small, 16-bit address model */
  370. # define CONFIG_LONG_IS_NOT_INT 1 /* Long and int are not the same size */
  371. # undef CONFIG_PTR_IS_NOT_INT /* FAR pointers and int are the same size */
  372. # elif defined(__EZ80__)
  373. # define FAR
  374. # define NEAR
  375. # define DSEG
  376. # define CODE
  377. # undef CONFIG_SMALL_MEMORY /* Select the large, 32-bit addressing model */
  378. # define CONFIG_LONG_IS_NOT_INT 1 /* Long and int are not the same size */
  379. # ifdef CONFIG_EZ80_Z80MODE
  380. # define CONFIG_PTR_IS_NOT_INT 1 /* Pointers and int are not the same size */
  381. # else
  382. # undef CONFIG_PTR_IS_NOT_INT /* Pointers and int are the same size */
  383. # endif
  384. # endif
  385. /* ISO C11 supports anonymous (unnamed) structures and unions. Zilog does
  386. * not support C11
  387. */
  388. # undef CONFIG_HAVE_ANONYMOUS_STRUCT
  389. # undef CONFIG_HAVE_ANONYMOUS_UNION
  390. /* Older Zilog compilers support both types double and long long, but the
  391. * size is 32-bits (same as long and single precision) so it is safer to say
  392. * that they are not supported. Later versions are more ANSII compliant and
  393. * simply do not support long long or double.
  394. */
  395. # undef CONFIG_HAVE_LONG_LONG
  396. # define CONFIG_HAVE_FLOAT 1
  397. # undef CONFIG_HAVE_DOUBLE
  398. # undef CONFIG_HAVE_LONG_DOUBLE
  399. /* Indicate that a local variable is not used */
  400. # define UNUSED(a) ((void)(a))
  401. /* ICCARM-specific definitions **********************************************/
  402. #elif defined(__ICCARM__)
  403. # define CONFIG_CPP_HAVE_VARARGS 1 /* Supports variable argument macros */
  404. # define CONFIG_HAVE_FILENAME 1 /* Has __FILE__ */
  405. # define CONFIG_HAVE_FLOAT 1
  406. /* Indicate that a local variable is not used */
  407. # define UNUSED(a) ((void)(a))
  408. # define weak_alias(name, aliasname)
  409. # define weak_data __weak
  410. # define weak_function __weak
  411. # define weak_const_function
  412. # define noreturn_function
  413. # define farcall_function
  414. # define locate_code(n)
  415. # define aligned_data(n)
  416. # define locate_data(n)
  417. # define begin_packed_struct __packed
  418. # define end_packed_struct
  419. # define reentrant_function
  420. # define naked_function
  421. # define inline_function
  422. # define noinline_function
  423. # define FAR
  424. # define NEAR
  425. # define DSEG
  426. # define CODE
  427. # define IOBJ
  428. # define IPTR
  429. # define __asm__ asm
  430. # define __volatile__ volatile
  431. /* For operatots __sfb() and __sfe() */
  432. # pragma section = ".bss"
  433. # pragma section = ".data"
  434. # pragma section = ".data_init"
  435. # pragma section = ".text"
  436. /* C++ support */
  437. # undef CONFIG_HAVE_CXX14
  438. /* ISO C11 supports anonymous (unnamed) structures and unions. Does
  439. * ICCARM?
  440. */
  441. # undef CONFIG_HAVE_ANONYMOUS_STRUCT
  442. # undef CONFIG_HAVE_ANONYMOUS_UNION
  443. /* Unknown compiler *********************************************************/
  444. #else
  445. # undef CONFIG_CPP_HAVE_VARARGS
  446. # undef CONFIG_CPP_HAVE_WARNING
  447. # undef CONFIG_HAVE_FUNCTIONNAME
  448. # undef CONFIG_HAVE_FILENAME
  449. # undef CONFIG_HAVE_WEAKFUNCTIONS
  450. # undef CONFIG_HAVE_CXX14
  451. # define weak_alias(name, aliasname)
  452. # define weak_data
  453. # define weak_function
  454. # define weak_const_function
  455. # define restrict
  456. # define noreturn_function
  457. # define farcall_function
  458. # define aligned_data(n)
  459. # define locate_code(n)
  460. # define locate_data(n)
  461. # define begin_packed_struct
  462. # define end_packed_struct
  463. # define reentrant_function
  464. # define naked_function
  465. # define inline_function
  466. # define noinline_function
  467. # define FAR
  468. # define NEAR
  469. # define DSEG
  470. # define CODE
  471. # undef CONFIG_SMALL_MEMORY
  472. # undef CONFIG_LONG_IS_NOT_INT
  473. # undef CONFIG_PTR_IS_NOT_INT
  474. # undef CONFIG_HAVE_LONG_LONG
  475. # define CONFIG_HAVE_FLOAT 1
  476. # undef CONFIG_HAVE_DOUBLE
  477. # undef CONFIG_HAVE_LONG_DOUBLE
  478. # undef CONFIG_HAVE_ANONYMOUS_STRUCT
  479. # undef CONFIG_HAVE_ANONYMOUS_UNION
  480. # define UNUSED(a) ((void)(a))
  481. #endif
  482. /****************************************************************************
  483. * Public Function Prototypes
  484. ****************************************************************************/
  485. /****************************************************************************
  486. * Public Function Prototypes
  487. ****************************************************************************/
  488. #ifdef __cplusplus
  489. #define EXTERN extern "C"
  490. extern "C"
  491. {
  492. #else
  493. #define EXTERN extern
  494. #endif
  495. #undef EXTERN
  496. #ifdef __cplusplus
  497. }
  498. #endif
  499. #endif /* __INCLUDE_NUTTX_COMPILER_H */