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 
13 #ifndef LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
14 #define LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
15 
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Basic/TargetOptions.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/Compiler.h"
20 
21 namespace clang {
22 namespace targets {
23 
24 class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
25   static const Builtin::Info BuiltinInfo[];
26 
27   enum SIMDEnum {
28     NoSIMD,
29     SIMD128,
30     UnimplementedSIMD128,
31   } SIMDLevel = NoSIMD;
32 
33   bool HasNontrappingFPToInt = false;
34   bool HasSignExt = false;
35   bool HasExceptionHandling = false;
36   bool HasBulkMemory = false;
37   bool HasAtomics = false;
38   bool HasMutableGlobals = false;
39 
40 public:
41   explicit WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)
42       : TargetInfo(T) {
43     NoAsmVariants = true;
44     SuitableAlign = 128;
45     LargeArrayMinWidth = 128;
46     LargeArrayAlign = 128;
47     SimdDefaultAlign = 128;
48     SigAtomicType = SignedLong;
49     LongDoubleWidth = LongDoubleAlign = 128;
50     LongDoubleFormat = &llvm::APFloat::IEEEquad();
51     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
52     // size_t being unsigned long for both wasm32 and wasm64 makes mangled names
53     // more consistent between the two.
54     SizeType = UnsignedLong;
55     PtrDiffType = SignedLong;
56     IntPtrType = SignedLong;
57   }
58 
59 protected:
60   void getTargetDefines(const LangOptions &Opts,
61                         MacroBuilder &Builder) const override;
62 
63 private:
64   static void setSIMDLevel(llvm::StringMap<bool> &Features, SIMDEnum Level);
65 
66   bool
67   initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
68                  StringRef CPU,
69                  const std::vector<std::string> &FeaturesVec) const override;
70   bool hasFeature(StringRef Feature) const final;
71 
72   bool handleTargetFeatures(std::vector<std::string> &Features,
73                             DiagnosticsEngine &Diags) final;
74 
75   bool isValidCPUName(StringRef Name) const final;
76   void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const final;
77 
78   bool setCPU(const std::string &Name) final { return isValidCPUName(Name); }
79 
80   ArrayRef<Builtin::Info> getTargetBuiltins() const final;
81 
82   BuiltinVaListKind getBuiltinVaListKind() const final {
83     return VoidPtrBuiltinVaList;
84   }
85 
86   ArrayRef<const char *> getGCCRegNames() const final { return None; }
87 
88   ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const final {
89     return None;
90   }
91 
92   bool validateAsmConstraint(const char *&Name,
93                              TargetInfo::ConstraintInfo &Info) const final {
94     return false;
95   }
96 
97   const char *getClobbers() const final { return ""; }
98 
99   bool isCLZForZeroUndef() const final { return false; }
100 
101   bool hasInt128Type() const final { return true; }
102 
103   IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
104     // WebAssembly prefers long long for explicitly 64-bit integers.
105     return BitWidth == 64 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
106                           : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);
107   }
108 
109   IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
110     // WebAssembly uses long long for int_least64_t and int_fast64_t.
111     return BitWidth == 64
112                ? (IsSigned ? SignedLongLong : UnsignedLongLong)
113                : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
114   }
115 };
116 class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo
117     : public WebAssemblyTargetInfo {
118 public:
119   explicit WebAssembly32TargetInfo(const llvm::Triple &T,
120                                    const TargetOptions &Opts)
121       : WebAssemblyTargetInfo(T, Opts) {
122     resetDataLayout("e-m:e-p:32:32-i64:64-n32:64-S128");
123   }
124 
125 protected:
126   void getTargetDefines(const LangOptions &Opts,
127                         MacroBuilder &Builder) const override;
128 };
129 
130 class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo
131     : public WebAssemblyTargetInfo {
132 public:
133   explicit WebAssembly64TargetInfo(const llvm::Triple &T,
134                                    const TargetOptions &Opts)
135       : WebAssemblyTargetInfo(T, Opts) {
136     LongAlign = LongWidth = 64;
137     PointerAlign = PointerWidth = 64;
138     SizeType = UnsignedLong;
139     PtrDiffType = SignedLong;
140     IntPtrType = SignedLong;
141     resetDataLayout("e-m:e-p:64:64-i64:64-n32:64-S128");
142   }
143 
144 protected:
145   void getTargetDefines(const LangOptions &Opts,
146                         MacroBuilder &Builder) const override;
147 };
148 } // namespace targets
149 } // namespace clang
150 #endif // LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
151