Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
acbaac3
Start adding wasm
zhuowei Apr 9, 2019
a39bc59
Add wasm object emit support
zhuowei Apr 9, 2019
cc85a9a
build-script: parse skip wasm command line args
zhuowei Apr 9, 2019
cc98380
Handle WASM sdk in cmake
zhuowei Apr 9, 2019
8118f81
Integrate Emscripten sdk path
zhuowei Apr 10, 2019
86b4238
start adding Emscripten defines in stdlib
zhuowei Apr 10, 2019
f519ab8
actually pass the icu paths into CMake
zhuowei Apr 10, 2019
7f10472
do not link WebAssembly shared libraries for now
zhuowei Apr 10, 2019
1b1b4f4
add script to launch build
zhuowei Apr 10, 2019
f31a795
start adding wasm32 to 32-bit checks in stdlib
zhuowei Apr 11, 2019
a84cc90
WebAssembly: more conditional compile defines
zhuowei Apr 11, 2019
60f91ba
Hack: Remove returnaddress calls emitted by exclusivity checks
zhuowei Apr 11, 2019
af88c19
WebAssembly: Hack: disable all atomic instructions
zhuowei Apr 11, 2019
9ece2c6
change non-Emscripten specific ifdefs to check for __wasm__ instead
zhuowei Apr 13, 2019
a4b0344
Switch to WASI
zhuowei Apr 13, 2019
407ddcb
fix WASI header includes
zhuowei Apr 13, 2019
3d771f5
WebAssembly: Disable Comdat for reflection metadata
zhuowei Apr 14, 2019
15a1742
Disable generating PC-relative relocations in constant builder
zhuowei Apr 14, 2019
64ac940
WebAssembly: remove even more PCRel relocations
zhuowei Apr 14, 2019
d0f54a4
WebAssembly: remove more PCRel relocations
zhuowei Apr 15, 2019
853e209
WebAssembly: disable relative pointers in the runtime
zhuowei Apr 15, 2019
27c10cd
WebAssembly: HACK: disable generating lldb-optimized debug info
zhuowei Apr 15, 2019
6fdcc47
Start exporting WASI musl in Glibc modulemap
zhuowei Apr 16, 2019
e9cb3e5
WebAssembly: disable subprocesses
zhuowei Apr 16, 2019
79ff1fc
WebAssembly: add EINVAL constant manually
zhuowei Apr 17, 2019
75fd3cd
WebAssembly: add more wasm Glibc target dependencies to cmakefiles
zhuowei Apr 17, 2019
3b316b1
WebAssembly: hack: Disable tests for now
zhuowei Apr 17, 2019
1808f9b
WebAssembly: add more ifdefs for WebAssembly
zhuowei Apr 17, 2019
3bdd62b
build static stdlibs in build script
zhuowei Apr 17, 2019
2735a32
disable DWARF5 and atomics on WebAssembly
zhuowei Apr 18, 2019
252b211
remove the older dwarf5 and atomics patch
zhuowei Apr 18, 2019
202d534
fix wasi sdk path
zhuowei Apr 19, 2019
af9f176
WebAssembly: HACK: use llvm-ar from the WASI sdk
zhuowei Apr 19, 2019
f7ed0bb
WebAssembly: remove file locking on stdin; WASI doesn't support file …
zhuowei Apr 20, 2019
ba02375
WebAssembly: compile ImageInspectionELF for WebAssembly for now
zhuowei Apr 20, 2019
7a60c22
update icu include path
zhuowei Apr 20, 2019
0f88a25
[stdlib] Fix return type of swift_{uint64,int64,float*}ToString
zhuowei Apr 20, 2019
4ca637c
[stdlib] fix return type of getNumRuntimeFunctionCounters
zhuowei Apr 20, 2019
38c3d53
WebAssembly: add a sample script for linking a wasm file
zhuowei Apr 20, 2019
f5e3e01
WebAssembly: use getentropy on Wasi
zhuowei Apr 20, 2019
113f7af
fix path to fakeld
zhuowei Apr 21, 2019
846d293
WebAssembly: add start/end objects for metadata
zhuowei Apr 22, 2019
c4d116d
attempt to fix swift_once; doesn't work
zhuowei Apr 24, 2019
4b7bed3
whoops, missed a swift_once
zhuowei Apr 24, 2019
e622bd2
Add wasm branch scheme to update-checkout-config
MaxDesiatov Apr 29, 2019
0d1be95
WebAssembly: fix metadata table sizes
zhuowei Apr 30, 2019
cdb0988
WebAssembly: add a ton of logging when looking up metadata
zhuowei Apr 30, 2019
dce6da8
WebAssembly: fix a typo accidentally introduced into CMake file
zhuowei Apr 30, 2019
d58328c
WebAssembly: fix GOT-relative pointers without PCrel
zhuowei Apr 30, 2019
dd54892
WebAssembly: add logging in swift_getAssociatedTypeWitness
zhuowei Apr 30, 2019
a8dcf8b
Revert "WebAssembly: add logging in swift_getAssociatedTypeWitness"
zhuowei May 2, 2019
6db8f2c
Revert "WebAssembly: add a ton of logging when looking up metadata"
zhuowei May 2, 2019
049121f
WebAssembly: add replac2 section to start/end objects
zhuowei Jun 9, 2019
2b6dbb3
Update default checkout scheme to "wasm".
kverrier Jun 15, 2019
9466d12
Merge pull request #4 from kverrier/swiftwasm
zhuowei Jun 15, 2019
8d801d1
Add when wasm
kateinoigakukun Sep 4, 2019
b6a183f
remove assert
kateinoigakukun Sep 5, 2019
ed31c34
Revert "remove assert"
kateinoigakukun Sep 5, 2019
032a056
Revert "Add when wasm"
kateinoigakukun Sep 5, 2019
3f5dcab
@Add swifterror for callee
kateinoigakukun Sep 5, 2019
842ec04
Add caller support
kateinoigakukun Sep 5, 2019
e45dcf8
pass pure_f.wasm
kateinoigakukun Sep 6, 2019
bca7921
support selfValue
kateinoigakukun Sep 6, 2019
3bb81d3
Revert "support selfValue"
kateinoigakukun Sep 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ ENABLE_LANGUAGE(C)
include(SwiftUtils)
include(CheckSymbolExists)

# WebAssembly: hack: use llvm-ar for creating static libraries; Ubuntu's GNU ar doesn't work with wasm-ld
set(CMAKE_AR "${SWIFT_WASM_WASI_SDK_PATH}/bin/llvm-ar")

#
# User-configurable options that control the inclusion and default build
# behavior for components which may not strictly be necessary (tools, examples,
Expand Down Expand Up @@ -238,11 +241,11 @@ set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING
"Path on an Android device where build products will be pushed. These are used when running the test suite against the device")

#
# User-configurable ICU specific options for Android, FreeBSD, Linux and Haiku.
# User-configurable ICU specific options for Android, FreeBSD, Linux, Haiku, and WebAssembly.
#

foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU)
foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;x86_64)
foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU;WASM)
foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;wasm32;x86_64)
set(SWIFT_${sdk}_${arch}_ICU_UC "" CACHE STRING
"Path to a directory containing the icuuc library for ${sdk}")
set(SWIFT_${sdk}_${arch}_ICU_UC_INCLUDE "" CACHE STRING
Expand Down Expand Up @@ -620,6 +623,8 @@ else()
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64le")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
set(SWIFT_HOST_VARIANT_ARCH_default "s390x")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32")
set(SWIFT_HOST_VARIANT_ARCH_default "wasm32")
# FIXME: Only matches v6l/v7l - by far the most common variants
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
set(SWIFT_HOST_VARIANT_ARCH_default "armv6")
Expand Down Expand Up @@ -799,6 +804,22 @@ if(swift_build_windows AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
configure_sdk_windows("Windows" "msvc" "${SWIFT_SDK_WINDOWS_ARCHITECTURES}")
endif()

# Should we cross-compile the standard library for WebAssembly (WASI)?
is_sdk_requested(WASM swift_build_wasm)
if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM")
#if ("${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
# message(FATAL_ERROR "You must set SWIFT_ANDROID_NDK_PATH to cross-compile the Swift runtime for Android")
#endif()
#if (NOT ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Darwin" OR "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux"))
# message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android")
#endif()

if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "")
set(SWIFT_SDK_WASM_ARCHITECTURES wasm32)
endif()
configure_sdk_unix("Wasm" "${SWIFT_SDK_WASM_ARCHITECTURES}")
endif()

if("${SWIFT_SDKS}" STREQUAL "")
set(SWIFT_SDKS "${SWIFT_CONFIGURED_SDKS}")
endif()
Expand Down Expand Up @@ -1103,8 +1124,8 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
endif()

if(SWIFT_INCLUDE_TESTS)
add_subdirectory(test)
add_subdirectory(unittests)
#add_subdirectory(test)
#add_subdirectory(unittests)
endif()
if(SWIFT_INCLUDE_DOCS)
add_subdirectory(docs)
Expand Down
4 changes: 4 additions & 0 deletions buildstartend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e
/home/zhuowei/wasi-sdk/bin/clang++ -c swift_start.cpp
/home/zhuowei/wasi-sdk/bin/clang++ -c swift_end.cpp
13 changes: 12 additions & 1 deletion cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ function(_add_variant_link_flags)
foreach(path IN LISTS ${LFLAGS_ARCH}_LIB)
list(APPEND library_search_directories ${path})
endforeach()
elseif("${LFLAGS_SDK}" STREQUAL "WASM")
# No extra libraries needed.
else()
# If lto is enabled, we need to add the object path flag so that the LTO code
# generator leaves the intermediate object file in a place where it will not
Expand Down Expand Up @@ -507,7 +509,9 @@ function(_add_variant_link_flags)
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
# FIXME: On Apple platforms, find_program needs to look for "ld64.lld"
find_program(LDLLD_PATH "ld.lld")
if((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR
if("${LFLAGS_SDK}" STREQUAL "WASM")
list(APPEND result "-fuse-ld=${CMAKE_SOURCE_DIR}/fakeld")
elseif((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR
("${LFLAGS_SDK}" STREQUAL "WINDOWS" AND
NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WINDOWS"))
list(APPEND result "-fuse-ld=lld")
Expand Down Expand Up @@ -1544,6 +1548,9 @@ endfunction()
# SWIFT_MODULE_DEPENDS_HAIKU
# Swift modules this library depends on when built for Haiku.
#
# SWIFT_MODULE_DEPENDS_WASM
# Swift modules this library depends on when built for WebAssembly.
#
# FRAMEWORK_DEPENDS
# System frameworks this library depends on.
#
Expand Down Expand Up @@ -1656,6 +1663,7 @@ function(add_swift_target_library name)
SWIFT_MODULE_DEPENDS_OSX
SWIFT_MODULE_DEPENDS_TVOS
SWIFT_MODULE_DEPENDS_WATCHOS
SWIFT_MODULE_DEPENDS_WASM
SWIFT_MODULE_DEPENDS_WINDOWS
TARGET_SDKS)

Expand Down Expand Up @@ -1779,6 +1787,9 @@ function(add_swift_target_library name)
elseif(${sdk} STREQUAL HAIKU)
list(APPEND swiftlib_module_depends_flattened
${SWIFTLIB_SWIFT_MODULE_DEPENDS_HAIKU})
elseif(${sdk} STREQUAL WASM)
list(APPEND swiftlib_module_depends_flattened
${SWIFTLIB_SWIFT_MODULE_DEPENDS_WASM})
elseif(${sdk} STREQUAL WINDOWS)
list(APPEND swiftlib_module_depends_flattened
${SWIFTLIB_SWIFT_MODULE_DEPENDS_WINDOWS})
Expand Down
7 changes: 7 additions & 0 deletions cmake/modules/SwiftConfigureSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ macro(configure_sdk_unix name architectures)
message(FATAL_ERROR "unsupported arch for Haiku: ${arch}")
endif()
set(SWIFT_SDK_HAIKU_ARCH_x86_64_TRIPLE "x86_64-unknown-haiku")
elseif("${prefix}" STREQUAL "WASM")
if(NOT arch STREQUAL wasm32)
message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}")
endif()
set(SWIFT_SDK_WASM_ARCH_wasm32_PATH "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot")
# fixme: Wasi is wasm32-unknown-wasi-musl. This LLVM doesn't have it yet.
set(SWIFT_SDK_WASM_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasm")
else()
message(FATAL_ERROR "unknown Unix OS: ${prefix}")
endif()
Expand Down
3 changes: 2 additions & 1 deletion cmake/modules/SwiftSetIfArchBitness.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function(set_if_arch_bitness var_name)
"${SIA_ARCH}" STREQUAL "armv6" OR
"${SIA_ARCH}" STREQUAL "armv7" OR
"${SIA_ARCH}" STREQUAL "armv7k" OR
"${SIA_ARCH}" STREQUAL "armv7s")
"${SIA_ARCH}" STREQUAL "armv7s" OR
"${SIA_ARCH}" STREQUAL "wasm32")
set("${var_name}" "${SIA_CASE_32_BIT}" PARENT_SCOPE)
elseif("${SIA_ARCH}" STREQUAL "x86_64" OR
"${SIA_ARCH}" STREQUAL "arm64" OR
Expand Down
9 changes: 9 additions & 0 deletions fakeld
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
import sys
def outputname():
for i in range(len(sys.argv)):
if sys.argv[i] == "-o":
return sys.argv[i + 1]
return "a.out"
with open(outputname(), "wb") as outfile:
pass
1 change: 1 addition & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ namespace swift {
} else if (Target.isOSLinux() || Target.isOSFreeBSD() ||
Target.isAndroid() || Target.isOSWindows() ||
Target.isPS4() || Target.isOSHaiku() ||
Target.isOSBinFormatWasm() ||
Target.getTriple().empty()) {
major = minor = revision = 0;
} else {
Expand Down
12 changes: 12 additions & 0 deletions include/swift/Basic/RelativePointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ static inline uintptr_t applyRelativeOffset(BasePtrTy *basePtr, Offset offset) {
std::is_signed<Offset>::value,
"offset type should be signed integer");

#ifdef __wasm__
// WebAssembly: hack: disable relative pointers
return (uintptr_t)(intptr_t)offset;
#endif

auto base = reinterpret_cast<uintptr_t>(basePtr);
// We want to do wrapping arithmetic, but with a sign-extended
// offset. To do this in C, we need to do signed promotion to get
Expand All @@ -162,6 +167,13 @@ static inline Offset measureRelativeOffset(A *referent, B *base) {
static_assert(std::is_integral<Offset>::value &&
std::is_signed<Offset>::value,
"offset type should be signed integer");
#ifdef __wasm__
// WebAssembly: hack: disable relative pointers
auto offset = (Offset)(uintptr_t)referent;
assert((intptr_t)offset == (intptr_t)(uintptr_t)referent
&& "pointer too large to fit in offset type");
return offset;
#endif

auto distance = (uintptr_t)referent - (uintptr_t)base;
// Truncate as unsigned, then wrap around to signed.
Expand Down
5 changes: 5 additions & 0 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,11 @@ class ApplyIRLinkage {
// TODO: BFD and gold do not handle COMDATs properly
if (Triple.isOSBinFormatELF())
return;
// WebAssembly: hack: comdat + custom section = explosion
// the comdat code assumes section name would be unique for each comdat
// this doesn't happen for metadata.
if (Triple.isOSBinFormatWasm())
return;

if (IRL.Linkage == llvm::GlobalValue::LinkOnceODRLinkage ||
IRL.Linkage == llvm::GlobalValue::WeakODRLinkage)
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Runtime/Mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <type_traits>

#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__))
#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__wasi__))
#include "swift/Runtime/MutexPThread.h"
#elif defined(_WIN32)
#include "swift/Runtime/MutexWin32.h"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Runtime/MutexPThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef pthread_cond_t ConditionHandle;
typedef pthread_mutex_t MutexHandle;
typedef pthread_rwlock_t ReadWriteLockHandle;

#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__)
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__wasi__)
// At the moment CYGWIN pthreads implementation doesn't support the use of
// constexpr for static allocation versions. The way they define things
// results in a reinterpret_cast which violates constexpr. Similarly, Android's
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Runtime/Once.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ typedef std::once_flag swift_once_t;
/// extent of type swift_once_t.
SWIFT_RUNTIME_EXPORT
void swift_once(swift_once_t *predicate, void (*fn)(void *), void *context);
#ifdef __wasm__
// WebAssembly: hack
void swift_once_real(swift_once_t *predicate, void (*fn)(void *), void *context);
#endif

}

Expand Down
4 changes: 2 additions & 2 deletions include/swift/SwiftRemoteMirror/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" {
#endif

#if defined(swiftRemoteMirror_EXPORTS)
# if defined(__ELF__)
# if defined(__ELF__) || defined(__wasm__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("protected")))
# elif defined(__MACH__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
Expand All @@ -30,7 +30,7 @@ extern "C" {
# endif
# endif
#else
# if defined(__ELF__)
# if defined(__ELF__) || defined(__wasm__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
# elif defined(__MACH__)
# define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default")))
Expand Down
12 changes: 11 additions & 1 deletion lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const StringRef SupportedConditionalCompilationOSs[] = {
"PS4",
"Cygwin",
"Haiku",
"Wasm",
};

static const StringRef SupportedConditionalCompilationArches[] = {
Expand All @@ -48,7 +49,8 @@ static const StringRef SupportedConditionalCompilationArches[] = {
"x86_64",
"powerpc64",
"powerpc64le",
"s390x"
"s390x",
"wasm32"
};

static const StringRef SupportedConditionalCompilationEndianness[] = {
Expand Down Expand Up @@ -192,6 +194,8 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
addPlatformConditionValue(PlatformConditionKind::OS, "PS4");
else if (Target.isOSHaiku())
addPlatformConditionValue(PlatformConditionKind::OS, "Haiku");
else if (Target.isOSBinFormatWasm())
addPlatformConditionValue(PlatformConditionKind::OS, "Wasm");
else
UnsupportedOS = true;

Expand Down Expand Up @@ -221,6 +225,9 @@ std::pair<bool, bool> 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;
default:
UnsupportedArch = true;
}
Expand Down Expand Up @@ -252,6 +259,9 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue(PlatformConditionKind::Endianness, "big");
break;
case llvm::Triple::ArchType::wasm32:
addPlatformConditionValue(PlatformConditionKind::Endianness, "little");
break;
default:
llvm_unreachable("undefined architecture endianness");
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Basic/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ static StringRef getPlatformNameForDarwin(const DarwinPlatformKind platform) {
StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
switch (triple.getOS()) {
case llvm::Triple::UnknownOS:
if (triple.isOSBinFormatWasm())
return "wasm";
llvm_unreachable("unknown OS");
case llvm::Triple::Ananas:
case llvm::Triple::CloudABI:
Expand Down
5 changes: 5 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) {
return llvm::make_unique<toolchains::Windows>(*this, target);
case llvm::Triple::Haiku:
return llvm::make_unique<toolchains::GenericUnix>(*this, target);
case llvm::Triple::UnknownOS:
// WebAssembly: hack: WASI isn't defined in this LLVM version yet
if (target.isOSBinFormatWasm())
return llvm::make_unique<toolchains::GenericUnix>(*this, target);
LLVM_FALLTHROUGH;
default:
Diags.diagnose(SourceLoc(), diag::error_unknown_target,
ArgList.getLastArg(options::OPT_target)->getValue());
Expand Down
25 changes: 25 additions & 0 deletions lib/IRGen/ConstantBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class ConstantAggregateBuilderBase

void addRelativeAddress(llvm::Constant *target) {
assert(!isa<llvm::ConstantPointerNull>(target));
if (IGM().TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
// WebAssembly: hack: doesn't support PCrel data relocations
add(llvm::ConstantExpr::getPtrToInt(target, IGM().RelativeAddressTy, false));
return;
}
addRelativeOffset(IGM().RelativeAddressTy, target);
}

Expand All @@ -90,6 +95,21 @@ class ConstantAggregateBuilderBase
/// a "GOT-equivalent", i.e. a pointer to an external object; if so,
/// set the low bit of the offset to indicate that this is true.
void addRelativeAddress(ConstantReference reference) {
if (IGM().TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
// WebAssembly: hack: doesn't support PCrel data relocations
// also, we should set the lowest bit, but I don't know how to do that
// there's no GOT on WebAssembly anyways though


llvm::Constant *offset = llvm::ConstantExpr::getPtrToInt(reference.getValue(), IGM().RelativeAddressTy, false);
// borrowed from addTaggedRelativeOffset
unsigned tag = unsigned(reference.isIndirect());
if (tag) {
offset = llvm::ConstantExpr::getAdd(offset, llvm::ConstantInt::get(IGM().RelativeAddressTy, tag));
}
add(offset);
return;
}
addTaggedRelativeOffset(IGM().RelativeAddressTy,
reference.getValue(),
unsigned(reference.isIndirect()));
Expand All @@ -99,6 +119,11 @@ class ConstantAggregateBuilderBase
/// The target must be a "GOT-equivalent", i.e. a pointer to an
/// external object.
void addIndirectRelativeAddress(ConstantReference reference) {
if (IGM().TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
// WebAssembly: hack: doesn't support PCrel data relocations
add(llvm::ConstantExpr::getPtrToInt(reference.getValue(), IGM().RelativeAddressTy, false));
return;
}
assert(reference.isIndirect());
addRelativeOffset(IGM().RelativeAddressTy,
reference.getValue());
Expand Down
12 changes: 12 additions & 0 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,7 @@ void SignatureExpansion::expandParameters() {
"adding 'self' added unexpected number of parameters");
} else {
auto needsContext = [=]() -> bool {
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) return true;
switch (FnType->getRepresentation()) {
case SILFunctionType::Representation::Block:
llvm_unreachable("adding block parameter in Swift CC expansion?");
Expand Down Expand Up @@ -1393,6 +1394,17 @@ void SignatureExpansion::expandParameters() {
llvm::Type *errorType = IGM.getStorageType(
getSILFuncConventions().getSILType(FnType->getErrorResult()));
ParamIRTypes.push_back(errorType->getPointerTo());

} else if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
if (claimError())
IGM.addSwiftErrorAttributes(Attrs, ParamIRTypes.size());
SILType exnType = SILType::getExceptionType(IGM.Context);
assert(exnType.isObject());
auto errorResult = SILResultInfo(exnType.getASTType(),
ResultConvention::Owned);
llvm::Type *errorType = IGM.getStorageType(
getSILFuncConventions().getSILType(errorResult));
ParamIRTypes.push_back(errorType->getPointerTo());
}

// Witness methods have some extra parameter types.
Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2958,6 +2958,11 @@ IRGenModule::emitDirectRelativeReference(llvm::Constant *target,
// Convert the target to an integer.
auto targetAddr = llvm::ConstantExpr::getPtrToInt(target, SizeTy);

// WebAssembly hack: WebAssembly doesn't support PC-relative references
if (TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
return targetAddr;
}

SmallVector<llvm::Constant*, 4> indices;
indices.push_back(llvm::ConstantInt::get(Int32Ty, 0));
for (unsigned baseIndex : baseIndices) {
Expand Down
Loading