1//
2// m3_config_platforms.h
3//
4// Created by Volodymyr Shymanskyy on 11/20/19.
5// Copyright © 2019 Volodymyr Shymanskyy. All rights reserved.
6//
7
8#ifndef m3_config_platforms_h
9#define m3_config_platforms_h
10
11#include "wasm3_defs.h"
12
13/*
14 * Internal helpers
15 */
16
17# if !defined(__cplusplus) || defined(_MSC_VER)
18# define not !
19# define and &&
20# define or ||
21# endif
22
23/*
24 * Detect/define features
25 */
26
27# if defined(M3_COMPILER_MSVC)
28# include <stdint.h>
29# if UINTPTR_MAX == 0xFFFFFFFF
30# define M3_SIZEOF_PTR 4
31# elif UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu
32# define M3_SIZEOF_PTR 8
33# else
34# error "Pointer size not supported"
35# endif
36# elif defined(__SIZEOF_POINTER__)
37# define M3_SIZEOF_PTR __SIZEOF_POINTER__
38#else
39# error "Pointer size not detected"
40# endif
41
42# if defined(M3_BIG_ENDIAN)
43# define M3_BSWAP_u8(X) {}
44# define M3_BSWAP_u16(X) { (X)=m3_bswap16((X)); }
45# define M3_BSWAP_u32(X) { (X)=m3_bswap32((X)); }
46# define M3_BSWAP_u64(X) { (X)=m3_bswap64((X)); }
47# define M3_BSWAP_i8(X) {}
48# define M3_BSWAP_i16(X) M3_BSWAP_u16(X)
49# define M3_BSWAP_i32(X) M3_BSWAP_u32(X)
50# define M3_BSWAP_i64(X) M3_BSWAP_u64(X)
51# define M3_BSWAP_f32(X) { union { f32 f; u32 i; } u; u.f = (X); M3_BSWAP_u32(u.i); (X) = u.f; }
52# define M3_BSWAP_f64(X) { union { f64 f; u64 i; } u; u.f = (X); M3_BSWAP_u64(u.i); (X) = u.f; }
53# else
54# define M3_BSWAP_u8(X) {}
55# define M3_BSWAP_u16(x) {}
56# define M3_BSWAP_u32(x) {}
57# define M3_BSWAP_u64(x) {}
58# define M3_BSWAP_i8(X) {}
59# define M3_BSWAP_i16(X) {}
60# define M3_BSWAP_i32(X) {}
61# define M3_BSWAP_i64(X) {}
62# define M3_BSWAP_f32(X) {}
63# define M3_BSWAP_f64(X) {}
64# endif
65
66# if defined(M3_COMPILER_MSVC)
67# define M3_WEAK //__declspec(selectany)
68# define M3_NO_UBSAN
69# define M3_NOINLINE
70# elif defined(__MINGW32__) || defined(__CYGWIN__)
71# define M3_WEAK //__attribute__((selectany))
72# define M3_NO_UBSAN
73# define M3_NOINLINE __attribute__((noinline))
74# else
75# define M3_WEAK __attribute__((weak))
76# define M3_NO_UBSAN //__attribute__((no_sanitize("undefined")))
77// Workaround for Cosmopolitan noinline conflict: https://github.com/jart/cosmopolitan/issues/310
78# if defined(noinline)
79# define M3_NOINLINE noinline
80# else
81# define M3_NOINLINE __attribute__((noinline))
82# endif
83# endif
84
85# ifndef M3_MIN
86# define M3_MIN(A,B) (((A) < (B)) ? (A) : (B))
87# endif
88# ifndef M3_MAX
89# define M3_MAX(A,B) (((A) > (B)) ? (A) : (B))
90# endif
91
92#define M3_INIT(field) memset(&field, 0, sizeof(field))
93
94#define M3_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
95
96#if defined(__AVR__)
97
98#include <inttypes.h>
99
100# define PRIu64 "llu"
101# define PRIi64 "lli"
102
103# define d_m3ShortTypesDefined
104typedef double f64;
105typedef float f32;
106typedef uint64_t u64;
107typedef int64_t i64;
108typedef uint32_t u32;
109typedef int32_t i32;
110typedef short unsigned u16;
111typedef short i16;
112typedef uint8_t u8;
113typedef int8_t i8;
114
115#endif
116
117/*
118 * Apply settings
119 */
120
121# if defined (M3_COMPILER_MSVC)
122# define vectorcall // For MSVC, better not to specify any call convention
123# elif defined(__x86_64__)
124# define vectorcall __attribute__((aligned(32)))
125//# elif defined(__riscv) && (__riscv_xlen == 64)
126//# define vectorcall __attribute__((aligned(16)))
127# elif defined(__MINGW32__)
128# define vectorcall
129# elif defined(WIN32)
130# define vectorcall __vectorcall
131# elif defined (ESP8266)
132# include <c_types.h>
133# define vectorcall //ICACHE_FLASH_ATTR
134# elif defined (ESP32)
135# if defined(M3_IN_IRAM) // the interpreter is in IRAM, attribute not needed
136# define vectorcall
137# else
138# include "esp_system.h"
139# define vectorcall IRAM_ATTR
140# endif
141# elif defined (FOMU)
142# define vectorcall __attribute__((section(".ramtext")))
143# endif
144
145#ifndef vectorcall
146#define vectorcall
147#endif
148
149
150/*
151 * Device-specific defaults
152 */
153
154# ifndef d_m3MaxFunctionStackHeight
155# if defined(ESP8266) || defined(ESP32) || defined(ARDUINO_AMEBA) || defined(TEENSYDUINO)
156# define d_m3MaxFunctionStackHeight 128
157# endif
158# endif
159
160# ifndef d_m3FixedHeap
161# if defined(ARDUINO_AMEBA)
162# define d_m3FixedHeap (128*1024)
163# elif defined(BLUE_PILL) || defined(FOMU)
164# define d_m3FixedHeap (12*1024)
165# elif defined(ARDUINO_ARCH_ARC32) // Arduino 101
166# define d_m3FixedHeap (10*1024)
167# endif
168# endif
169
170/*
171 * Platform-specific defaults
172 */
173
174# if defined(ARDUINO) || defined(PARTICLE) || defined(PLATFORMIO) || defined(__MBED__) || \
175 defined(ESP8266) || defined(ESP32) || defined(BLUE_PILL) || defined(WM_W600) || defined(FOMU)
176# ifndef d_m3CascadedOpcodes
177# define d_m3CascadedOpcodes 0
178# endif
179# ifndef d_m3VerboseErrorMessages
180# define d_m3VerboseErrorMessages 0
181# endif
182# ifndef d_m3MaxConstantTableSize
183# define d_m3MaxConstantTableSize 64
184# endif
185# ifndef d_m3MaxFunctionStackHeight
186# define d_m3MaxFunctionStackHeight 64
187# endif
188# ifndef d_m3CodePageAlignSize
189# define d_m3CodePageAlignSize 1024
190# endif
191# endif
192
193/*
194 * Arch-specific defaults
195 */
196#if defined(__riscv) && (__riscv_xlen == 64)
197# ifndef d_m3Use32BitSlots
198# define d_m3Use32BitSlots 0
199# endif
200#endif
201
202#endif // m3_config_platforms_h
203