clang 20.0.0git
WebAssembly.h
Go to the documentation of this file.
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
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 HasAtomics = false;
57 bool HasBulkMemory = false;
58 bool HasExceptionHandling = false;
59 bool HasExtendedConst = false;
60 bool HasFP16 = false;
61 bool HasMultiMemory = false;
62 bool HasMultivalue = false;
63 bool HasMutableGlobals = false;
64 bool HasNontrappingFPToInt = false;
65 bool HasReferenceTypes = false;
66 bool HasSignExt = false;
67 bool HasTailCall = false;
68
69 std::string ABI;
70
71public:
72 explicit WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)
73 : TargetInfo(T) {
74 AddrSpaceMap = &WebAssemblyAddrSpaceMap;
75 NoAsmVariants = true;
76 SuitableAlign = 128;
77 LargeArrayMinWidth = 128;
78 LargeArrayAlign = 128;
79 SigAtomicType = SignedLong;
80 LongDoubleWidth = LongDoubleAlign = 128;
81 LongDoubleFormat = &llvm::APFloat::IEEEquad();
82 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
83 // size_t being unsigned long for both wasm32 and wasm64 makes mangled names
84 // more consistent between the two.
85 SizeType = UnsignedLong;
86 PtrDiffType = SignedLong;
87 IntPtrType = SignedLong;
88 HasUnalignedAccess = true;
89 }
90
91 StringRef getABI() const override;
92 bool setABI(const std::string &Name) override;
93 bool useFP16ConversionIntrinsics() const override { return !HasFP16; }
94
95protected:
96 void getTargetDefines(const LangOptions &Opts,
97 MacroBuilder &Builder) const override;
98
99private:
100 static void setSIMDLevel(llvm::StringMap<bool> &Features, SIMDEnum Level,
101 bool Enabled);
102
103 bool
104 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
105 StringRef CPU,
106 const std::vector<std::string> &FeaturesVec) const override;
107 bool hasFeature(StringRef Feature) const final;
108
109 void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,
110 bool Enabled) const final;
111
112 bool handleTargetFeatures(std::vector<std::string> &Features,
113 DiagnosticsEngine &Diags) final;
114
115 bool isValidCPUName(StringRef Name) const final;
116 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const final;
117
118 bool setCPU(const std::string &Name) final { return isValidCPUName(Name); }
119
120 ArrayRef<Builtin::Info> getTargetBuiltins() const final;
121
122 BuiltinVaListKind getBuiltinVaListKind() const final {
123 return VoidPtrBuiltinVaList;
124 }
125
126 ArrayRef<const char *> getGCCRegNames() const final { return std::nullopt; }
127
128 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const final {
129 return std::nullopt;
130 }
131
132 bool validateAsmConstraint(const char *&Name,
133 TargetInfo::ConstraintInfo &Info) const final {
134 return false;
135 }
136
137 std::string_view getClobbers() const final { return ""; }
138
139 bool isCLZForZeroUndef() const final { return false; }
140
141 bool hasInt128Type() const final { return true; }
142
143 IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
144 // WebAssembly prefers long long for explicitly 64-bit integers.
145 return BitWidth == 64 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
146 : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);
147 }
148
149 IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
150 // WebAssembly uses long long for int_least64_t and int_fast64_t.
151 return BitWidth == 64
152 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
153 : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
154 }
155
156 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
157 switch (CC) {
158 case CC_C:
159 case CC_Swift:
160 return CCCR_OK;
161 case CC_SwiftAsync:
162 return CCCR_Error;
163 default:
164 return CCCR_Warning;
165 }
166 }
167
168 bool hasBitIntType() const override { return true; }
169
170 bool hasProtectedVisibility() const override { return false; }
171
172 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;
173};
174
175class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo
176 : public WebAssemblyTargetInfo {
177public:
178 explicit WebAssembly32TargetInfo(const llvm::Triple &T,
179 const TargetOptions &Opts)
180 : WebAssemblyTargetInfo(T, Opts) {
181 if (T.isOSEmscripten())
182 resetDataLayout("e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
183 "S128-ni:1:10:20");
184 else
185 resetDataLayout(
186 "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
187 }
188
189protected:
190 void getTargetDefines(const LangOptions &Opts,
191 MacroBuilder &Builder) const override;
192};
193
194class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo
195 : public WebAssemblyTargetInfo {
196public:
197 explicit WebAssembly64TargetInfo(const llvm::Triple &T,
198 const TargetOptions &Opts)
199 : WebAssemblyTargetInfo(T, Opts) {
200 LongAlign = LongWidth = 64;
201 PointerAlign = PointerWidth = 64;
202 SizeType = UnsignedLong;
203 PtrDiffType = SignedLong;
204 IntPtrType = SignedLong;
205 if (T.isOSEmscripten())
206 resetDataLayout("e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
207 "S128-ni:1:10:20");
208 else
209 resetDataLayout(
210 "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
211 }
212
213protected:
214 void getTargetDefines(const LangOptions &Opts,
215 MacroBuilder &Builder) const override;
216};
217} // namespace targets
218} // namespace clang
219#endif // LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, const TargetInfo &Target)
Determine whether a translation unit built using the current language options has the given feature.
Definition: Module.cpp:100
Defines the clang::TargetOptions class.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:476
Exposes information about the current target.
Definition: TargetInfo.h:218
Options for controlling the target.
Definition: TargetOptions.h:26
WebAssembly32TargetInfo(const llvm::Triple &T, const TargetOptions &Opts)
Definition: WebAssembly.h:178
WebAssembly64TargetInfo(const llvm::Triple &T, const TargetOptions &Opts)
Definition: WebAssembly.h:197
WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)
Definition: WebAssembly.h:72
bool useFP16ConversionIntrinsics() const override
Check whether llvm intrinsics such as llvm.convert.to.fp16 should be used to convert to and from __fp...
Definition: WebAssembly.h:93
Defines the clang::TargetInfo interface.
static const unsigned WebAssemblyAddrSpaceMap[]
Definition: WebAssembly.h:24
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
@ CC_Swift
Definition: Specifiers.h:293
@ CC_C
Definition: Specifiers.h:279
@ CC_SwiftAsync
Definition: Specifiers.h:294