1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef TOOLCHAIN_H
18 #define TOOLCHAIN_H
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #if defined (__ICCARM__)
25 #include <stdint.h>
26 #include "intrinsics.h"
27 
28 /* IAR DLib errno.h does not include all the necessary error codes by default. */
29 #define EIO         5
30 #define ENXIO       6
31 #define ENOMEM      12
32 #define EBUSY       16
33 #define ENODEV      19
34 #define EINVAL      22
35 #define EOPNOTSUPP  95
36 
37 /* IAR does not support LIKELY() and UNLIKELY() for optimization purposes. */
38 #define LIKELY(x)       (x)
39 #define UNLIKELY(x)     (x)
40 #define CLZ             __CLZ
41 
42 #define KEEP_SYMBOL  __root static void
43 #define NO_RETURN __noreturn void
44 #define VOID_WEAK __weak void
45 #define WEAK_ALIAS(X,Y) _Pragma(PRAGMA_HELPER(X,Y,weak)) void X(void)
46 #define PLACE_IN(loc, type, name) _Pragma(PRAGMA_HELPER(location,loc,)) type name
47 #define PLACE_IN_NAKED(loc, type, name) _Pragma(PRAGMA_HELPER(location,loc,)) __task type name
48 #define APP_ENTRY   _Pragma(PRAGMA_HELPER(location,".internal_app_init",)) __root static const struct AppHdr
49 #define APP_ENTRY2    _Pragma(PRAGMA_HELPER(location,".app_init",)) __root static struct AppFuncs
50 
51 #define STACKLESS      __stackless
52 
53 
54 #define PRINTF_ATTRIBUTE
55 
56 #define SET_PACKED_STRUCT_MODE_ON       _Pragma("pack(push, 1)")
57 #define SET_PACKED_STRUCT_MODE_OFF      _Pragma("pack(pop)")
58 
59 #define ATTRIBUTE_PACKED
60 
61 #define UNROLLED
62 
63 #define SET_INTERNAL_LOCATION(x, y)           _Pragma((x, y)) __root
64 
65 #define SET_INTERNAL_LOCATION_ATTRIBUTES(x, y)
66 
67 #define SET_EXTERNAL_APP_ATTRIBUTES(x, y, z)
68 
69 #define SET_EXTERNAL_APP_VERSION(x, y, z)
70 
71 #define DECLARE_OS_ALIGNMENT(nam, size, extra_keyword, struct_type)    _Pragma("data_alignment=4") extra_keyword uint8_t _##nam##_store [size]; extra_keyword #struct_type *nam = (#struct_type *)_##nam##_store
72 
73 #elif defined(__GNUC__)
74 
75 #define STRINGIFY(s) _STRINGIFY(s)
76 #define _STRINGIFY(s) #s
77 
78 #define NO_RETURN void __attribute__((noreturn))
79 #define LIKELY(x)   (__builtin_expect(x, 1))
80 #define UNLIKELY(x) (__builtin_expect(x, 0))
81 
82 #define CLZ             __builtin_clz
83 #define APP_ENTRY   static struct AppEntry __attribute__((used,section (".app_init")))
84 #define KEEP_SYMBOL  static void __attribute__((used))
85 #define PLACE_IN(loc, type, name) type __attribute__ ((section(loc))) name
86 #define PLACE_IN_NAKED(loc, type, name) type __attribute__ ((naked, section(loc))) name
87 #define VOID_WEAK void __attribute__ ((weak))
88 #define WEAK_ALIAS(X,Y) void X(void) __attribute__ ((weak, alias (STRINGIFY(Y))))
89 
90 #define STACKLESS __attribute__((naked))
91 
92 #define PRINTF_ATTRIBUTE __attribute__((format(printf, 2, 3)))
93 
94 #define SET_PACKED_STRUCT_MODE_ON
95 #define SET_PACKED_STRUCT_MODE_OFF
96 
97 #define ATTRIBUTE_PACKED __attribute__((packed))
98 
99 #define UNROLLED   __attribute__((optimize("unroll-loops")))
100 
101 #define SET_INTERNAL_LOCATION(x, y)
102 
103 #define SET_INTERNAL_LOCATION_ATTRIBUTES(x, y) __attribute__((x,y))
104 
105 #define SET_EXTERNAL_APP_ATTRIBUTES(x, y, z) __attribute__((x, y, z))
106 
107 #define SET_EXTERNAL_APP_VERSION(x, y, z) __attribute__((x, y, z))
108 
109 #define DECLARE_OS_ALIGNMENT(nam, size, extra_keyword, struct_type) extra_keyword uint8_t _##nam##_store [size] __attribute__((aligned(4))); extra_keyword struct_type *nam = (struct_type *)_##nam##_store
110 
111 #endif
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif
118