WebAssembly.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //=== WebAssembly.h - Declare WebAssembly target feature support *- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file declares WebAssembly TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
  13. #define LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
  14. #include "clang/Basic/TargetInfo.h"
  15. #include "clang/Basic/TargetOptions.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/Support/Compiler.h"
  18. namespace clang {
  19. namespace targets {
  20. class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
  21. static const Builtin::Info BuiltinInfo[];
  22. enum SIMDEnum {
  23. NoSIMD,
  24. SIMD128,
  25. UnimplementedSIMD128,
  26. } SIMDLevel = NoSIMD;
  27. bool HasNontrappingFPToInt = false;
  28. bool HasSignExt = false;
  29. bool HasExceptionHandling = false;
  30. bool HasBulkMemory = false;
  31. bool HasAtomics = false;
  32. bool HasMutableGlobals = false;
  33. bool HasMultivalue = false;
  34. bool HasTailCall = false;
  35. public:
  36. explicit WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)
  37. : TargetInfo(T) {
  38. NoAsmVariants = true;
  39. SuitableAlign = 128;
  40. LargeArrayMinWidth = 128;
  41. LargeArrayAlign = 128;
  42. SimdDefaultAlign = 128;
  43. SigAtomicType = SignedLong;
  44. LongDoubleWidth = LongDoubleAlign = 128;
  45. LongDoubleFormat = &llvm::APFloat::IEEEquad();
  46. MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
  47. // size_t being unsigned long for both wasm32 and wasm64 makes mangled names
  48. // more consistent between the two.
  49. SizeType = UnsignedLong;
  50. PtrDiffType = SignedLong;
  51. IntPtrType = SignedLong;
  52. }
  53. protected:
  54. void getTargetDefines(const LangOptions &Opts,
  55. MacroBuilder &Builder) const override;
  56. private:
  57. static void setSIMDLevel(llvm::StringMap<bool> &Features, SIMDEnum Level);
  58. bool
  59. initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
  60. StringRef CPU,
  61. const std::vector<std::string> &FeaturesVec) const override;
  62. bool hasFeature(StringRef Feature) const final;
  63. bool handleTargetFeatures(std::vector<std::string> &Features,
  64. DiagnosticsEngine &Diags) final;
  65. bool isValidCPUName(StringRef Name) const final;
  66. void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const final;
  67. bool setCPU(const std::string &Name) final { return isValidCPUName(Name); }
  68. ArrayRef<Builtin::Info> getTargetBuiltins() const final;
  69. BuiltinVaListKind getBuiltinVaListKind() const final {
  70. return VoidPtrBuiltinVaList;
  71. }
  72. ArrayRef<const char *> getGCCRegNames() const final { return None; }
  73. ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const final {
  74. return None;
  75. }
  76. bool validateAsmConstraint(const char *&Name,
  77. TargetInfo::ConstraintInfo &Info) const final {
  78. return false;
  79. }
  80. const char *getClobbers() const final { return ""; }
  81. bool isCLZForZeroUndef() const final { return false; }
  82. bool hasInt128Type() const final { return true; }
  83. IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
  84. // WebAssembly prefers long long for explicitly 64-bit integers.
  85. return BitWidth == 64 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
  86. : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);
  87. }
  88. IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
  89. // WebAssembly uses long long for int_least64_t and int_fast64_t.
  90. return BitWidth == 64
  91. ? (IsSigned ? SignedLongLong : UnsignedLongLong)
  92. : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
  93. }
  94. };
  95. class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo
  96. : public WebAssemblyTargetInfo {
  97. public:
  98. explicit WebAssembly32TargetInfo(const llvm::Triple &T,
  99. const TargetOptions &Opts)
  100. : WebAssemblyTargetInfo(T, Opts) {
  101. resetDataLayout("e-m:e-p:32:32-i64:64-n32:64-S128");
  102. }
  103. protected:
  104. void getTargetDefines(const LangOptions &Opts,
  105. MacroBuilder &Builder) const override;
  106. };
  107. class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo
  108. : public WebAssemblyTargetInfo {
  109. public:
  110. explicit WebAssembly64TargetInfo(const llvm::Triple &T,
  111. const TargetOptions &Opts)
  112. : WebAssemblyTargetInfo(T, Opts) {
  113. LongAlign = LongWidth = 64;
  114. PointerAlign = PointerWidth = 64;
  115. SizeType = UnsignedLong;
  116. PtrDiffType = SignedLong;
  117. IntPtrType = SignedLong;
  118. resetDataLayout("e-m:e-p:64:64-i64:64-n32:64-S128");
  119. }
  120. protected:
  121. void getTargetDefines(const LangOptions &Opts,
  122. MacroBuilder &Builder) const override;
  123. };
  124. } // namespace targets
  125. } // namespace clang
  126. #endif // LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H