WasmParseTree.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include "WasmReaderPch.h"
  6. namespace Wasm
  7. {
  8. namespace Simd
  9. {
  10. bool IsEnabled()
  11. {
  12. #ifdef ENABLE_WASM_SIMD
  13. return CONFIG_FLAG_RELEASE(WasmSimd);
  14. #else
  15. return false;
  16. #endif
  17. }
  18. }
  19. namespace Threads
  20. {
  21. bool IsEnabled()
  22. {
  23. #ifdef ENABLE_WASM
  24. return CONFIG_FLAG(WasmThreads) && CONFIG_FLAG(ESSharedArrayBuffer);
  25. #else
  26. return false;
  27. #endif
  28. }
  29. }
  30. namespace WasmNontrapping
  31. {
  32. bool IsEnabled()
  33. {
  34. #ifdef ENABLE_WASM
  35. return CONFIG_FLAG(WasmNontrapping);
  36. #else
  37. return false;
  38. #endif
  39. }
  40. }
  41. namespace SignExtends
  42. {
  43. bool IsEnabled()
  44. {
  45. #ifdef ENABLE_WASM
  46. return CONFIG_FLAG(WasmSignExtends);
  47. #else
  48. return false;
  49. #endif
  50. }
  51. }
  52. }
  53. #ifdef ENABLE_WASM
  54. namespace Wasm
  55. {
  56. namespace Simd
  57. {
  58. void EnsureSimdIsEnabled()
  59. {
  60. if (!Wasm::Simd::IsEnabled())
  61. {
  62. throw WasmCompilationException(_u("Wasm.Simd support is not enabled"));
  63. }
  64. }
  65. }
  66. namespace WasmTypes
  67. {
  68. bool IsLocalType(WasmTypes::WasmType type)
  69. {
  70. // Check if type in range ]Void,Limit[
  71. #ifdef ENABLE_WASM_SIMD
  72. if (type == WasmTypes::M128 && !Simd::IsEnabled())
  73. {
  74. return false;
  75. }
  76. #endif
  77. return type >= WasmTypes::FirstLocalType && type < WasmTypes::Limit;
  78. }
  79. uint32 GetTypeByteSize(WasmType type)
  80. {
  81. switch (type)
  82. {
  83. case Void: return sizeof(Js::Var);
  84. case I32: return sizeof(int32);
  85. case I64: return sizeof(int64);
  86. case F32: return sizeof(float);
  87. case F64: return sizeof(double);
  88. #ifdef ENABLE_WASM_SIMD
  89. case M128:
  90. Simd::EnsureSimdIsEnabled();
  91. CompileAssert(sizeof(Simd::simdvec) == 16);
  92. return sizeof(Simd::simdvec);
  93. #endif
  94. case Ptr: return sizeof(void*);
  95. default:
  96. Js::Throw::InternalError();
  97. }
  98. }
  99. const char16 * GetTypeName(WasmType type)
  100. {
  101. switch (type) {
  102. case WasmTypes::WasmType::Void: return _u("void");
  103. case WasmTypes::WasmType::I32: return _u("i32");
  104. case WasmTypes::WasmType::I64: return _u("i64");
  105. case WasmTypes::WasmType::F32: return _u("f32");
  106. case WasmTypes::WasmType::F64: return _u("f64");
  107. #ifdef ENABLE_WASM_SIMD
  108. case WasmTypes::WasmType::M128:
  109. Simd::EnsureSimdIsEnabled();
  110. return _u("m128");
  111. #endif
  112. case WasmTypes::WasmType::Any: return _u("any");
  113. default: Assert(UNREACHED); break;
  114. }
  115. return _u("unknown");
  116. }
  117. } // namespace WasmTypes
  118. WasmTypes::WasmType LanguageTypes::ToWasmType(int8 binType)
  119. {
  120. switch (binType)
  121. {
  122. case LanguageTypes::i32: return WasmTypes::I32;
  123. case LanguageTypes::i64: return WasmTypes::I64;
  124. case LanguageTypes::f32: return WasmTypes::F32;
  125. case LanguageTypes::f64: return WasmTypes::F64;
  126. #ifdef ENABLE_WASM_SIMD
  127. case LanguageTypes::m128:
  128. Simd::EnsureSimdIsEnabled();
  129. return WasmTypes::M128;
  130. #endif
  131. default:
  132. throw WasmCompilationException(_u("Invalid binary type %d"), binType);
  133. }
  134. }
  135. bool FunctionIndexTypes::CanBeExported(FunctionIndexTypes::Type funcType)
  136. {
  137. return funcType == FunctionIndexTypes::Function || funcType == FunctionIndexTypes::ImportThunk;
  138. }
  139. } // namespace Wasm
  140. #endif // ENABLE_WASM