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 #ifndef PRINTF_ATTRIBUTE
54 #define PRINTF_ATTRIBUTE(string_index, first_to_check)
55 #endif
56 
57 #define SET_PACKED_STRUCT_MODE_ON       _Pragma("pack(push, 1)")
58 #define SET_PACKED_STRUCT_MODE_OFF      _Pragma("pack(pop)")
59 
60 #ifndef ATTRIBUTE_PACKED
61 #define ATTRIBUTE_PACKED
62 #endif
63 
64 #define UNROLLED
65 
66 #define SET_INTERNAL_LOCATION(x, y)           _Pragma((x, y)) __root
67 
68 #define SET_INTERNAL_LOCATION_ATTRIBUTES(x, y)
69 
70 #define SET_EXTERNAL_APP_ATTRIBUTES(x, y, z)
71 
72 #define SET_EXTERNAL_APP_VERSION(x, y, z)
73 
74 #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
75 
76 #elif defined(__GNUC__)
77 
78 #define STRINGIFY(s) _STRINGIFY(s)
79 #define _STRINGIFY(s) #s
80 
81 #define NO_RETURN void __attribute__((noreturn))
82 #define LIKELY(x)   (__builtin_expect(x, 1))
83 #define UNLIKELY(x) (__builtin_expect(x, 0))
84 
85 #define CLZ             __builtin_clz
86 #define APP_ENTRY   static struct AppEntry __attribute__((used,section (".app_init")))
87 #define KEEP_SYMBOL  static void __attribute__((used))
88 #define PLACE_IN(loc, type, name) type __attribute__ ((section(loc))) name
89 #define PLACE_IN_NAKED(loc, type, name) type __attribute__ ((naked, section(loc))) name
90 #define VOID_WEAK void __attribute__ ((weak))
91 #define WEAK_ALIAS(X,Y) void X(void) __attribute__ ((weak, alias (STRINGIFY(Y))))
92 
93 #define STACKLESS __attribute__((naked))
94 
95 #ifndef PRINTF_ATTRIBUTE
96 #define PRINTF_ATTRIBUTE(string_index, first_to_check) \
97     __attribute__((format(printf, string_index, first_to_check)))
98 #endif
99 
100 #define SET_PACKED_STRUCT_MODE_ON
101 #define SET_PACKED_STRUCT_MODE_OFF
102 
103 #ifndef ATTRIBUTE_PACKED
104 #define ATTRIBUTE_PACKED __attribute__((packed))
105 #endif
106 
107 #define UNROLLED   __attribute__((optimize("unroll-loops")))
108 
109 #define SET_INTERNAL_LOCATION(x, y)
110 
111 #define SET_INTERNAL_LOCATION_ATTRIBUTES(x, y) __attribute__((x,y))
112 
113 #define SET_EXTERNAL_APP_ATTRIBUTES(x, y, z) __attribute__((x, y, z))
114 
115 #define SET_EXTERNAL_APP_VERSION(x, y, z) __attribute__((x, y, z))
116 
117 #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
118 
119 #endif
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 #endif
126