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