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/Support/Compiler.h"
19#include "llvm/TargetParser/Triple.h"
20
21namespace clang {
22namespace targets {
23
24static const unsigned WebAssemblyAddrSpaceMap[] = {
25 0, // Default
26 0, // opencl_global
27 0, // opencl_local
28 0, // opencl_constant
29 0, // opencl_private
30 0, // opencl_generic
31 0, // opencl_global_device
32 0, // opencl_global_host
33 0, // cuda_device
34 0, // cuda_constant
35 0, // cuda_shared
36 0, // sycl_global
37 0, // sycl_global_device
38 0, // sycl_global_host
39 0, // sycl_local
40 0, // sycl_private
41 0, // ptr32_sptr
42 0, // ptr32_uptr
43 0, // ptr64
44 0, // hlsl_groupshared
45 20, // wasm_funcref
46};
47
48class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
49
50 enum SIMDEnum {
51 NoSIMD,
52 SIMD128,
53 RelaxedSIMD,
54 } SIMDLevel = NoSIMD;
55
56 bool HasNontrappingFPToInt = false;
57 bool HasSignExt = false;
58 bool HasExceptionHandling = false;
59 bool HasBulkMemory = false;
60 bool HasAtomics = false;
61 bool HasMutableGlobals = false;
62 bool HasMultivalue = false;
63 bool HasTailCall = false;
64 bool HasReferenceTypes = false;
65 bool HasExtendedConst = false;
66 bool HasMultiMemory = false;
67
68 std::string ABI;
69
70public:
71 explicit WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)
72 : TargetInfo(T) {
73 AddrSpaceMap = &WebAssemblyAddrSpaceMap;
74 NoAsmVariants = true;
75 SuitableAlign = 128;
76 LargeArrayMinWidth = 128;
77 LargeArrayAlign = 128;
78 SigAtomicType = SignedLong;
79 LongDoubleWidth = LongDoubleAlign = 128;
80 LongDoubleFormat = &llvm::APFloat::IEEEquad();
81 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
82 // size_t being unsigned long for both wasm32 and wasm64 makes mangled names
83 // more consistent between the two.
84 SizeType = UnsignedLong;
85 PtrDiffType = SignedLong;
86 IntPtrType = SignedLong;
87 HasUnalignedAccess = true;
88 }
89
90 StringRef getABI() const override;
91 bool setABI(const std::string &Name) override;
92
93protected:
94 void getTargetDefines(const LangOptions &Opts,
95 MacroBuilder &Builder) const override;
96
97private:
98 static void setSIMDLevel(llvm::StringMap<bool> &Features, SIMDEnum Level,
99 bool Enabled);
100
101 bool
102 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
103 StringRef CPU,
104 const std::vector<std::string> &FeaturesVec) const override;
105 bool hasFeature(StringRef Feature) const final;
106
107 void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,
108 bool Enabled) const final;
109
110 bool handleTargetFeatures(std::vector<std::string> &Features,
111 DiagnosticsEngine &Diags) final;
112
113 bool isValidCPUName(StringRef Name) const final;
114 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const final;
115
116 bool setCPU(const std::string &Name) final { return isValidCPUName(Name); }
117
118 ArrayRef<Builtin::Info> getTargetBuiltins() const final;
119
120 BuiltinVaListKind getBuiltinVaListKind() const final {
121 return VoidPtrBuiltinVaList;
122 }
123
124 ArrayRef<const char *> getGCCRegNames() const final { return std::nullopt; }
125
126 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const final {
127 return std::nullopt;
128 }
129
130 bool validateAsmConstraint(const char *&Name,
131 TargetInfo::ConstraintInfo &Info) const final {
132 return false;
133 }
134
135 std::string_view getClobbers() const final { return ""; }
136
137 bool isCLZForZeroUndef() const final { return false; }
138
139 bool hasInt128Type() const final { return true; }
140
141 IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
142 // WebAssembly prefers long long for explicitly 64-bit integers.
143 return BitWidth == 64 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
144 : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);
145 }
146
147 IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
148 // WebAssembly uses long long for int_least64_t and int_fast64_t.
149 return BitWidth == 64
150 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
151 : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
152 }
153
154 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
155 switch (CC) {
156 case CC_C:
157 case CC_Swift:
158 return CCCR_OK;
159 case CC_SwiftAsync:
160 return CCCR_Error;
161 default:
162 return CCCR_Warning;
163 }
164 }
165
166 bool hasBitIntType() const override { return true; }
167
168 bool hasProtectedVisibility() const override { return false; }
169
170 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;
171};
172
173class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo
174 : public WebAssemblyTargetInfo {
175public:
176 explicit WebAssembly32TargetInfo(const llvm::Triple &T,
177 const TargetOptions &Opts)
178 : WebAssemblyTargetInfo(T, Opts) {
179 if (T.isOSEmscripten())
180 resetDataLayout(DL: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
181 "S128-ni:1:10:20");
182 else
183 resetDataLayout(
184 DL: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
185 }
186
187protected:
188 void getTargetDefines(const LangOptions &Opts,
189 MacroBuilder &Builder) const override;
190};
191
192class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo
193 : public WebAssemblyTargetInfo {
194public:
195 explicit WebAssembly64TargetInfo(const llvm::Triple &T,
196 const TargetOptions &Opts)
197 : WebAssemblyTargetInfo(T, Opts) {
198 LongAlign = LongWidth = 64;
199 PointerAlign = PointerWidth = 64;
200 SizeType = UnsignedLong;
201 PtrDiffType = SignedLong;
202 IntPtrType = SignedLong;
203 if (T.isOSEmscripten())
204 resetDataLayout(DL: "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
205 "S128-ni:1:10:20");
206 else
207 resetDataLayout(
208 DL: "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
209 }
210
211protected:
212 void getTargetDefines(const LangOptions &Opts,
213 MacroBuilder &Builder) const override;
214};
215} // namespace targets
216} // namespace clang
217#endif // LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
218

source code of clang/lib/Basic/Targets/WebAssembly.h