Skip to content

Swift Runtime on WebAssembly #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 8, 2019
Merged
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ jobs:
-DSWIFT_SDKS='WASM;LINUX' \
-DSWIFT_BUILD_SOURCEKIT=FALSE \
-DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \
-DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \
-DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \
" \
--build-stdlib-deployment-targets "wasm-wasm32" \
--build-swift-static-stdlib \
Expand All @@ -73,7 +75,7 @@ jobs:
- uses: actions/checkout@v1
- name: Run a multi-line script
run: |
brew install cmake ninja
brew install cmake ninja llvm
./utils/update-checkout --clone --scheme wasm
git checkout $GITHUB_SHA
export sourcedir=$PWD/..
Expand All @@ -96,6 +98,8 @@ jobs:
-DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \
-DSWIFT_BUILD_SOURCEKIT=FALSE \
-DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \
-DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \
-DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \
" \
--build-stdlib-deployment-targets "wasm-wasm32" \
--build-swift-dynamic-sdk-overlay false \
Expand Down
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -850,11 +850,6 @@ if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM")
# message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android")
#endif()

if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
# 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")
endif()

if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "")
set(SWIFT_SDK_WASM_ARCHITECTURES wasm32)
endif()
Expand Down
19 changes: 12 additions & 7 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2268,14 +2268,19 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type,
IGM.Int32Ty);
stringAddrOffset = subIGF.Builder.CreateSExtOrBitCast(stringAddrOffset,
IGM.SizeTy);
auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy);
if (IGM.getModule()->getDataLayout().isBigEndian()) {
stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase,
llvm::ConstantInt::get(IGM.SizeTy, 4));
llvm::Value *stringAddr;
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) {
stringAddr = subIGF.Builder.CreateIntToPtr(stringAddrOffset, IGM.Int8PtrTy);
} else {
auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy);
if (IGM.getModule()->getDataLayout().isBigEndian()) {
stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase,
llvm::ConstantInt::get(IGM.SizeTy, 4));
}
stringAddr = subIGF.Builder.CreateAdd(stringAddrBase,
stringAddrOffset);
stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy);
}
auto stringAddr = subIGF.Builder.CreateAdd(stringAddrBase,
stringAddrOffset);
stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy);

auto call =
subIGF.Builder.CreateCall(IGM.getGetTypeByMangledNameInContextFn(),
Expand Down
11 changes: 9 additions & 2 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags})
list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS)
list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include)

set(sdk "${SWIFT_HOST_VARIANT_SDK}")
if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
set(image_inspection_shared_sdk)
if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX")
set(image_inspection_shared_sdk "${SWIFT_HOST_VARIANT_SDK}")
elseif("${SWIFT_PRIMARY_VARIANT_SDK}" STREQUAL "WASM")
set(image_inspection_shared_sdk "${SWIFT_PRIMARY_VARIANT_SDK}")
endif()

if(NOT "${image_inspection_shared_sdk}" STREQUAL "")
set(sdk "${image_inspection_shared_sdk}")
list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp)
set(static_binary_lnk_file_list)
string(TOLOWER "${sdk}" lowercase_sdk)
Expand Down
75 changes: 75 additions & 0 deletions stdlib/public/runtime/SwiftRT-WASM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,78 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "ImageInspectionELF.h"

#include <cstddef>

// Create empty sections to ensure that the start/stop symbols are synthesized
// by the linker. Otherwise, we may end up with undefined symbol references as
// the linker table section was never constructed.

#define DECLARE_SWIFT_SECTION(name) \
__attribute__((__used__,__section__(#name),__aligned__(1))) const char __dummy_##name = 0x00; \
__attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \
__attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name;

extern "C" {
DECLARE_SWIFT_SECTION(swift5_protocols)
DECLARE_SWIFT_SECTION(swift5_protocol_conformances)
DECLARE_SWIFT_SECTION(swift5_type_metadata)

DECLARE_SWIFT_SECTION(swift5_typeref)
DECLARE_SWIFT_SECTION(swift5_reflstr)
DECLARE_SWIFT_SECTION(swift5_fieldmd)
DECLARE_SWIFT_SECTION(swift5_assocty)
DECLARE_SWIFT_SECTION(swift5_replace)
DECLARE_SWIFT_SECTION(swift5_replac2)
DECLARE_SWIFT_SECTION(swift5_builtin)
DECLARE_SWIFT_SECTION(swift5_capture)
}

#undef DECLARE_SWIFT_SECTION

namespace {
static swift::MetadataSections sections{};
}

__attribute__((__constructor__))
static void swift_image_constructor() {
#define SWIFT_SECTION_RANGE(name) \
{ reinterpret_cast<uintptr_t>(&__start_##name), \
static_cast<uintptr_t>(&__stop_##name - &__start_##name) }

sections = {
swift::CurrentSectionMetadataVersion,
0,

nullptr,
nullptr,

SWIFT_SECTION_RANGE(swift5_protocols),
SWIFT_SECTION_RANGE(swift5_protocol_conformances),
SWIFT_SECTION_RANGE(swift5_type_metadata),

SWIFT_SECTION_RANGE(swift5_typeref),
SWIFT_SECTION_RANGE(swift5_reflstr),
SWIFT_SECTION_RANGE(swift5_fieldmd),
SWIFT_SECTION_RANGE(swift5_assocty),
SWIFT_SECTION_RANGE(swift5_replace),
SWIFT_SECTION_RANGE(swift5_replac2),
SWIFT_SECTION_RANGE(swift5_builtin),
SWIFT_SECTION_RANGE(swift5_capture),
};

#undef SWIFT_SECTION_RANGE

swift_addNewDSOImage(&sections);
}

static __attribute__((__used__))
__attribute__((__section__(".note.swift_reflection_metadata")))
__attribute__((__aligned__(1)))
struct {
const char MagicString[sizeof(SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING)];
const swift::MetadataSections *Sections;
} __attribute__((__packed__))
Note = {SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING, &sections};
30 changes: 0 additions & 30 deletions swift_end.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions swift_start.cpp

This file was deleted.