clang 19.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 HasHalfPrecision = 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
94protected:
95 void getTargetDefines(const LangOptions &Opts,
96 MacroBuilder &Builder) const override;
97
98private:
99 static void setSIMDLevel(llvm::StringMap<bool> &Features, SIMDEnum Level,
100 bool Enabled);
101
102 bool
103 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
104 StringRef CPU,
105 const std::vector<std::string> &FeaturesVec) const override;
106 bool hasFeature(StringRef Feature) const final;
107
108 void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,
109 bool Enabled) const final;
110
111 bool handleTargetFeatures(std::vector<std::string> &Features,
112 DiagnosticsEngine &Diags) final;
113
114 bool isValidCPUName(StringRef Name) const final;
115 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const final;
116
117 bool setCPU(const std::string &Name) final { return isValidCPUName(Name); }
118
119 ArrayRef<Builtin::Info> getTargetBuiltins() const final;
120
121 BuiltinVaListKind getBuiltinVaListKind() const final {
122 return VoidPtrBuiltinVaList;
123 }
124
125 ArrayRef<const char *> getGCCRegNames() const final { return std::nullopt; }
126
127 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const final {
128 return std::nullopt;
129 }
130
131 bool validateAsmConstraint(const char *&Name,
132 TargetInfo::ConstraintInfo &Info) const final {
133 return false;
134 }
135
136 std::string_view getClobbers() const final { return ""; }
137
138 bool isCLZForZeroUndef() const final { return false; }
139
140 bool hasInt128Type() const final { return true; }
141
142 IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
143 // WebAssembly prefers long long for explicitly 64-bit integers.
144 return BitWidth == 64 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
145 : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);
146 }
147
148 IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
149 // WebAssembly uses long long for int_least64_t and int_fast64_t.
150 return BitWidth == 64
151 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
152 : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
153 }
154
155 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
156 switch (CC) {
157 case CC_C:
158 case CC_Swift:
159 return CCCR_OK;
160 case CC_SwiftAsync:
161 return CCCR_Error;
162 default:
163 return CCCR_Warning;
164 }
165 }
166
167 bool hasBitIntType() const override { return true; }
168
169 bool hasProtectedVisibility() const override { return false; }
170
171 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;
172};
173
174class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo
175 : public WebAssemblyTargetInfo {
176public:
177 explicit WebAssembly32TargetInfo(const llvm::Triple &T,
178 const TargetOptions &Opts)
179 : WebAssemblyTargetInfo(T, Opts) {
180 if (T.isOSEmscripten())
181 resetDataLayout("e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
182 "S128-ni:1:10:20");
183 else
184 resetDataLayout(
185 "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
186 }
187
188protected:
189 void getTargetDefines(const LangOptions &Opts,
190 MacroBuilder &Builder) const override;
191};
192
193class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo
194 : public WebAssemblyTargetInfo {
195public:
196 explicit WebAssembly64TargetInfo(const llvm::Triple &T,
197 const TargetOptions &Opts)
198 : WebAssemblyTargetInfo(T, Opts) {
199 LongAlign = LongWidth = 64;
200 PointerAlign = PointerWidth = 64;
201 SizeType = UnsignedLong;
202 PtrDiffType = SignedLong;
203 IntPtrType = SignedLong;
204 if (T.isOSEmscripten())
205 resetDataLayout("e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-"
206 "S128-ni:1:10:20");
207 else
208 resetDataLayout(
209 "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20");
210 }
211
212protected:
213 void getTargetDefines(const LangOptions &Opts,
214 MacroBuilder &Builder) const override;
215};
216} // namespace targets
217} // namespace clang
218#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:461
Exposes information about the current target.
Definition: TargetInfo.h:214
Options for controlling the target.
Definition: TargetOptions.h:26
WebAssembly32TargetInfo(const llvm::Triple &T, const TargetOptions &Opts)
Definition: WebAssembly.h:177
WebAssembly64TargetInfo(const llvm::Triple &T, const TargetOptions &Opts)
Definition: WebAssembly.h:196
WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)
Definition: WebAssembly.h:72
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:290
@ CC_C
Definition: Specifiers.h:276
@ CC_SwiftAsync
Definition: Specifiers.h:291