From 9a826136de9e5efe497ffeeb40feca1aec93b62c Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Mon, 19 Nov 2018 18:03:01 -0700 Subject: [PATCH] [WASM] Add minimal support for targeting wasm32-unknown-unknown-wasm. - This is nothing beyond preventing the compiler from rejecting this target triple. - There doesn't yet appear to be a well-defined convention around a triple which detects WASM based on an "OS" field, so we use the "wasm" indicator in the environment component. --- include/swift/Basic/LangOptions.h | 2 ++ lib/Basic/LangOptions.cpp | 12 ++++++++++++ lib/Basic/Platform.cpp | 5 +++++ lib/Driver/Driver.cpp | 5 +++++ 4 files changed, 24 insertions(+) diff --git a/include/swift/Basic/LangOptions.h b/include/swift/Basic/LangOptions.h index ab8c18a433750..d5bbc79f22f9c 100644 --- a/include/swift/Basic/LangOptions.h +++ b/include/swift/Basic/LangOptions.h @@ -316,6 +316,8 @@ namespace swift { Target.isPS4() || Target.isOSHaiku() || Target.getTriple().empty()) { major = minor = revision = 0; + } else if (Target.isOSBinFormatWasm()) { + major = minor = revision = 0; } else { llvm_unreachable("Unsupported target OS"); } diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index a2ac8787099fb..f1b0f39e0baff 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -192,6 +192,8 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { addPlatformConditionValue(PlatformConditionKind::OS, "PS4"); else if (Target.isOSHaiku()) addPlatformConditionValue(PlatformConditionKind::OS, "Haiku"); + else if (Target.isOSBinFormatWasm()) + addPlatformConditionValue(PlatformConditionKind::OS, "WebAssembly"); else UnsupportedOS = true; @@ -221,6 +223,12 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { case llvm::Triple::ArchType::systemz: addPlatformConditionValue(PlatformConditionKind::Arch, "s390x"); break; + case llvm::Triple::ArchType::wasm32: + addPlatformConditionValue(PlatformConditionKind::Arch, "wasm32"); + break; + case llvm::Triple::ArchType::wasm64: + addPlatformConditionValue(PlatformConditionKind::Arch, "wasm64"); + break; default: UnsupportedArch = true; } @@ -252,6 +260,10 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { case llvm::Triple::ArchType::systemz: addPlatformConditionValue(PlatformConditionKind::Endianness, "big"); break; + case llvm::Triple::ArchType::wasm32: + case llvm::Triple::ArchType::wasm64: + addPlatformConditionValue(PlatformConditionKind::Endianness, "little"); + break; default: llvm_unreachable("undefined architecture endianness"); } diff --git a/lib/Basic/Platform.cpp b/lib/Basic/Platform.cpp index 9e793fa5037a4..e67247060b0ae 100644 --- a/lib/Basic/Platform.cpp +++ b/lib/Basic/Platform.cpp @@ -116,6 +116,11 @@ static StringRef getPlatformNameForDarwin(const DarwinPlatformKind platform) { StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) { switch (triple.getOS()) { case llvm::Triple::UnknownOS: + // NOTE: WebAssembly doesn't yet have a defined OS convention in triples. + if (triple.isOSBinFormatWasm()) { + return "wasm"; + } + llvm_unreachable("unknown OS"); case llvm::Triple::Ananas: case llvm::Triple::CloudABI: diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index d69785e122272..68f0f72f439f4 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -286,6 +286,11 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) { case llvm::Triple::Haiku: return llvm::make_unique(*this, target); default: + // NOTE: WebAssembly doesn't yet have a defined OS convention in triples. + if (target.isOSBinFormatWasm()) { + return llvm::make_unique(*this, target); + } + Diags.diagnose(SourceLoc(), diag::error_unknown_target, ArgList.getLastArg(options::OPT_target)->getValue()); break;