2
0

WasmParseTree.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Wasm
  7. {
  8. const uint16 EXTENDED_OFFSET = 256;
  9. namespace Simd {
  10. const size_t VEC_WIDTH = 4;
  11. typedef uint32 simdvec [VEC_WIDTH]; //TODO: maybe we should pull in SIMDValue?
  12. const size_t MAX_LANES = 16;
  13. void EnsureSimdIsEnabled();
  14. bool IsEnabled();
  15. }
  16. namespace Threads
  17. {
  18. bool IsEnabled();
  19. };
  20. namespace WasmNontrapping
  21. {
  22. bool IsEnabled();
  23. };
  24. namespace SignExtends
  25. {
  26. bool IsEnabled();
  27. };
  28. namespace WasmTypes
  29. {
  30. enum WasmType
  31. {
  32. // based on binary format encoding values
  33. Void = 0,
  34. I32 = 1,
  35. I64 = 2,
  36. F32 = 3,
  37. F64 = 4,
  38. #ifdef ENABLE_WASM_SIMD
  39. V128 = 5,
  40. #endif
  41. Limit,
  42. Ptr,
  43. Any,
  44. FirstLocalType = I32,
  45. AllLocalTypes =
  46. 1 << I32
  47. | 1 << I64
  48. | 1 << F32
  49. | 1 << F64
  50. #ifdef ENABLE_WASM_SIMD
  51. | 1 << V128
  52. #endif
  53. };
  54. namespace SwitchCaseChecks
  55. {
  56. template<WasmType... T>
  57. struct bv;
  58. template<>
  59. struct bv<>
  60. {
  61. static constexpr uint value = 0;
  62. };
  63. template<WasmType... K>
  64. struct bv<Limit, K...>
  65. {
  66. static constexpr uint value = bv<K...>::value;
  67. };
  68. template<WasmType K1, WasmType... K>
  69. struct bv<K1, K...>
  70. {
  71. static constexpr uint value = (1 << K1) | bv<K...>::value;
  72. };
  73. }
  74. #ifdef ENABLE_WASM_SIMD
  75. #define WASM_V128_CHECK_TYPE Wasm::WasmTypes::V128
  76. #else
  77. #define WASM_V128_CHECK_TYPE Wasm::WasmTypes::Limit
  78. #endif
  79. template<WasmType... T>
  80. __declspec(noreturn) void CompileAssertCases()
  81. {
  82. CompileAssertMsg(SwitchCaseChecks::bv<T...>::value == AllLocalTypes, "WasmTypes missing in switch-case");
  83. AssertOrFailFastMsg(UNREACHED, "The WasmType case should have been handled");
  84. }
  85. template<WasmType... T>
  86. void CompileAssertCasesNoFailFast()
  87. {
  88. CompileAssertMsg(SwitchCaseChecks::bv<T...>::value == AllLocalTypes, "WasmTypes missing in switch-case");
  89. AssertMsg(UNREACHED, "The WasmType case should have been handled");
  90. }
  91. extern const char16* const strIds[Limit];
  92. bool IsLocalType(WasmTypes::WasmType type);
  93. uint32 GetTypeByteSize(WasmType type);
  94. const char16* GetTypeName(WasmType type);
  95. }
  96. typedef WasmTypes::WasmType Local;
  97. enum class ExternalKinds: uint8
  98. {
  99. Function = 0,
  100. Table = 1,
  101. Memory = 2,
  102. Global = 3,
  103. Limit
  104. };
  105. namespace FunctionIndexTypes
  106. {
  107. enum Type
  108. {
  109. Invalid = -1,
  110. ImportThunk,
  111. Function,
  112. Import
  113. };
  114. bool CanBeExported(Type funcType);
  115. }
  116. namespace GlobalReferenceTypes
  117. {
  118. enum Type
  119. {
  120. Invalid, Const, LocalReference, ImportedReference
  121. };
  122. }
  123. struct WasmOpCodeSignatures
  124. {
  125. #define WASM_SIGNATURE(id, nTypes, ...) static const WasmTypes::WasmType id[nTypes]; DebugOnly(static const int n##id = nTypes;)
  126. #include "WasmBinaryOpCodes.h"
  127. };
  128. enum WasmOp : uint16
  129. {
  130. #define WASM_OPCODE(opname, opcode, ...) wb##opname = opcode,
  131. // Add prefix to the enum to get a compiler error if there is a collision between operators and prefixes
  132. #define WASM_PREFIX(name, value, ...) prefix##name = value,
  133. #include "WasmBinaryOpCodes.h"
  134. };
  135. struct WasmConstLitNode
  136. {
  137. union
  138. {
  139. float f32;
  140. double f64;
  141. int32 i32;
  142. int64 i64;
  143. Simd::simdvec v128;
  144. };
  145. };
  146. struct WasmShuffleNode
  147. {
  148. uint8 indices[Simd::MAX_LANES];
  149. };
  150. struct WasmLaneNode
  151. {
  152. uint index;
  153. };
  154. struct WasmVarNode
  155. {
  156. uint32 num;
  157. };
  158. struct WasmMemOpNode
  159. {
  160. uint32 offset;
  161. uint8 alignment;
  162. };
  163. struct WasmBrNode
  164. {
  165. uint32 depth;
  166. };
  167. struct WasmBrTableNode
  168. {
  169. uint32 numTargets;
  170. uint32* targetTable;
  171. uint32 defaultTarget;
  172. };
  173. struct WasmCallNode
  174. {
  175. uint32 num; // function id
  176. FunctionIndexTypes::Type funcType;
  177. };
  178. struct WasmBlock
  179. {
  180. private:
  181. bool isSingleResult;
  182. union
  183. {
  184. WasmTypes::WasmType singleResult;
  185. uint32 signatureId;
  186. };
  187. public:
  188. bool IsSingleResult() const { return isSingleResult; }
  189. void SetSignatureId(uint32 id)
  190. {
  191. isSingleResult = false;
  192. signatureId = id;
  193. }
  194. uint32 GetSignatureId() const
  195. {
  196. Assert(!isSingleResult);
  197. return signatureId;
  198. }
  199. void SetSingleResult(WasmTypes::WasmType type)
  200. {
  201. isSingleResult = true;
  202. singleResult = type;
  203. }
  204. WasmTypes::WasmType GetSingleResult() const
  205. {
  206. Assert(isSingleResult);
  207. return singleResult;
  208. }
  209. };
  210. struct WasmNode
  211. {
  212. WasmOp op;
  213. union
  214. {
  215. WasmBlock block;
  216. WasmBrNode br;
  217. WasmBrTableNode brTable;
  218. WasmCallNode call;
  219. WasmConstLitNode cnst;
  220. WasmMemOpNode mem;
  221. WasmVarNode var;
  222. WasmLaneNode lane;
  223. WasmShuffleNode shuffle;
  224. };
  225. };
  226. struct WasmExport
  227. {
  228. uint32 index;
  229. uint32 nameLength;
  230. const char16* name;
  231. ExternalKinds kind;
  232. };
  233. struct WasmImport
  234. {
  235. ExternalKinds kind;
  236. uint32 modNameLen;
  237. const char16* modName;
  238. uint32 importNameLen;
  239. const char16* importName;
  240. };
  241. struct CustomSection
  242. {
  243. const char16* name;
  244. charcount_t nameLength;
  245. const byte* payload;
  246. uint32 payloadSize;
  247. };
  248. }