Skip to content

Commit

Permalink
Apple Silicon support, miscellaneous
Browse files Browse the repository at this point in the history
- small changes e.g. alternatives for windows intrinsics
- build file updates
  • Loading branch information
rhuanjl committed Mar 22, 2023
1 parent 40b603e commit af58421
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 14 deletions.
31 changes: 29 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ if(CC_USES_SYSTEM_ARCH_SH OR NOT CHAKRACORE_BUILD_SH)
set(CC_TARGETS_AMD64_SH 1)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7l")
set(CC_TARGETS_ARM_SH 1)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
set(CC_TARGETS_ARM64_SH 1)
endif()
unset(CC_USES_SYSTEM_ARCH_SH CACHE)
endif()
Expand All @@ -56,6 +58,11 @@ elseif(CC_TARGETS_ARM_SH)
set(CC_TARGETS_ARM 1)
add_definitions(-D_ARM_=1)
set(CMAKE_SYSTEM_PROCESSOR "armv7l")
elseif(CC_TARGETS_ARM64_SH)
add_definitions(-D_ARM64_=1)
add_definitions(-D__arm64__=1)
set(CC_TARGETS_ARM64 1)
set(CMAKE_SYSTEM_PROCESSOr "arm64")
elseif(CC_TARGETS_X86_SH)
set(CC_TARGETS_X86 1)
set(CMAKE_SYSTEM_PROCESSOR "i386")
Expand Down Expand Up @@ -273,8 +280,15 @@ elseif(CC_TARGETS_ARM)
# reduce link time memory usage
set(LINKER_REDUCED_MEMORY "-Xlinker --no-keep-memory")
endif()
elseif(CC_TARGETS_ARM64)
add_definitions(-D__aarch64__)
add_definitions(-DTARGET_64)
add_definitions(-D_M_ARM32_OR_ARM64)
if(CC_TARGET_OS_OSX)
add_compile_options(-arch arm64)
endif()
else()
message(FATAL_ERROR "Only AMD64, ARM and I386 are supported")
message(FATAL_ERROR "Only AMD64, ARM, ARM64 and I386 are supported")
endif()

if(CAN_BUILD_WABT)
Expand Down Expand Up @@ -341,7 +355,10 @@ if(CLR_CMAKE_PLATFORM_XPLAT)
if(CC_TARGETS_AMD64)
set(IS_64BIT_BUILD 1)
add_definitions(-D_M_X64 -D_M_AMD64 -D_AMD64_)
endif(CC_TARGETS_AMD64)
elseif(CC_TARGETS_ARM64)
set(IS_64BIT_BUILD 1)
add_definitions(-D_M_ARM64 -D_ARM64_)
endif()

add_definitions(
-DUNICODE
Expand Down Expand Up @@ -495,6 +512,16 @@ else()
set(DYN_LIB_EXT "so")
endif()

if(CC_TARGETS_ARM64)
if(CC_TARGET_OS_LINUX)
message(WARNING "ARM64 linux build has not yet been tested, this build is unsupported.")
endif()
if(BuildJIT)
message(WARNING "ARM64 Jit not yet functional on platforms other than windows.")
message(WARNING "For use rather than development please build with Jit disabled --no-jit with ./build.sh or -DDISABLE_JIT=1 if using CMake directly")
endif()
endif()

################# Write-barrier check/analyze ##################
if (WB_CHECK_SH OR WB_ANALYZE_SH)
add_definitions(
Expand Down
7 changes: 5 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
#-------------------------------------------------------------------------------------------------------
# Copyright (C) Microsoft. All rights reserved.
# Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
# Copyright (c) ChakraCore Project Contributors. All rights reserved.
# Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
#-------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -122,7 +122,7 @@ WB_CHECK=
WB_ANALYZE=
WB_ARGS=
TARGET_PATH=0
VALGRIND=0
VALGRIND=""
# -DCMAKE_EXPORT_COMPILE_COMMANDS=ON useful for clang-query tool
CMAKE_EXPORT_COMPILE_COMMANDS="-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
LIBS_ONLY_BUILD=
Expand Down Expand Up @@ -620,6 +620,9 @@ if [[ $ARCH =~ "x86" ]]; then
elif [[ $ARCH =~ "arm" ]]; then
ARCH="-DCC_TARGETS_ARM_SH=1"
echo "Compile Target : arm"
elif [[ $ARCH =~ "arm64" ]]; then
ARCH="-DCC_TARGETS_ARM64_SH=1"
echo "Compile Target : arm64"
elif [[ $ARCH =~ "amd64" ]]; then
ARCH="-DCC_TARGETS_AMD64_SH=1"
echo "Compile Target : amd64"
Expand Down
8 changes: 6 additions & 2 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ if (EMBED_ICU)
# Declare ICU dependencies in bulk.
# TODO Not the most idiomatic solution, need to understand if all of those
# libraries needed to depend on it
add_dependencies(Chakra.Backend ${EMBEDDED_ICU_TARGET})
add_dependencies(Chakra.Jsrt ${EMBEDDED_ICU_TARGET})
add_dependencies(Chakra.Jsrt.Core ${EMBEDDED_ICU_TARGET})
add_dependencies(Chakra.Parser ${EMBEDDED_ICU_TARGET})
Expand All @@ -138,5 +137,10 @@ if (EMBED_ICU)
add_dependencies(Chakra.Runtime.PlatformAgnostic ${EMBEDDED_ICU_TARGET})
add_dependencies(Chakra.Runtime.Types ${EMBEDDED_ICU_TARGET})
add_dependencies(Chakra.SCACore ${EMBEDDED_ICU_TARGET})
add_dependencies(Chakra.WasmReader ${EMBEDDED_ICU_TARGET})
if (BuildJIT)
add_dependencies(Chakra.Backend ${EMBEDDED_ICU_TARGET})
if (CC_TARGETS_AMD64)
add_dependencies(Chakra.WasmReader ${EMBEDDED_ICU_TARGET})
endif()
endif()
endif()
2 changes: 2 additions & 0 deletions lib/Common/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
if(CC_TARGETS_ARM)
set(ARCH_SOURCES arm/arm_GET_CURRENT_FRAME.S)
elseif(CC_TARGETS_ARM64)
set(ARCH_SOURCES arm64/arm64_GET_CURRENT_FRAME.S)
endif()

add_library (Chakra.Common.Common OBJECT
Expand Down
9 changes: 5 additions & 4 deletions lib/Common/Common/NumberUtilities.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -176,7 +177,7 @@ namespace Js
{
#if defined(_AMD64_)
return _mm_cvtsi128_si64(_mm_castpd_si128(_mm_set_sd(value)));
#elif defined(_M_ARM32_OR_ARM64)
#elif defined(_M_ARM32_OR_ARM64) && defined(_CopyInt64FromDouble)
return _CopyInt64FromDouble(value);
#else
return *(reinterpret_cast<uint64 *>(&value));
Expand All @@ -187,7 +188,7 @@ namespace Js
{
#if defined(_AMD64_) || _M_IX86_FP >= 2 || defined(__AVX__)
return _mm_cvtsi128_si32(_mm_castps_si128(_mm_set_ss(value)));
#elif defined(_M_ARM32_OR_ARM64)
#elif defined(_M_ARM32_OR_ARM64) && defined(_CopyInt32FromFloat)
return _CopyInt32FromFloat(value);
#else
return *(reinterpret_cast<uint32 *>(&value));
Expand All @@ -198,7 +199,7 @@ namespace Js
{
#if defined(_AMD64_) || _M_IX86_FP >= 2 || defined(__AVX__)
return _mm_cvtss_f32(_mm_castsi128_ps(_mm_cvtsi32_si128(value)));
#elif defined(_M_ARM32_OR_ARM64)
#elif defined(_M_ARM32_OR_ARM64) && defined(_CopyFloatFromInt32)
return _CopyFloatFromInt32(value);
#else
return *(reinterpret_cast<float *>(&value));
Expand All @@ -209,7 +210,7 @@ namespace Js
{
#if defined(_AMD64_)
return _mm_cvtsd_f64(_mm_castsi128_pd(_mm_cvtsi64_si128(value)));
#elif defined(_M_ARM32_OR_ARM64)
#elif defined(_M_ARM32_OR_ARM64) && defined(_CopyDoubleFromInt64)
return _CopyDoubleFromInt64(value);
#else
return *(reinterpret_cast<double *>(&value));
Expand Down
14 changes: 14 additions & 0 deletions lib/Common/Common/arm64/arm64_Get_Current_Frame.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

#include "unixasmmacros.inc"

NESTED_ENTRY arm64_GET_CURRENT_FRAME, _TEXT, NoHandler

mov x0,x29
br lr

NESTED_END arm64_GET_CURRENT_FRAME
21 changes: 17 additions & 4 deletions lib/Common/CommonPal.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
Expand Down Expand Up @@ -388,6 +389,18 @@ typedef union _SLIST_HEADER {
} DUMMYSTRUCTNAME;
} SLIST_HEADER, *PSLIST_HEADER;

#elif defined(_ARM64_)

typedef union _SLIST_HEADER {
ULONGLONG Alignment;
struct {
SLIST_ENTRY Next;
WORD Depth;
WORD Reserved;
} DUMMYSTRUCTNAME;
} SLIST_HEADER, *PSLIST_HEADER;


#endif

PALIMPORT VOID PALAPI InitializeSListHead(IN OUT PSLIST_HEADER ListHead);
Expand Down Expand Up @@ -663,7 +676,7 @@ namespace PlatformAgnostic
{
__forceinline unsigned char _BitTestAndSet(LONG *_BitBase, int _BitPos)
{
#if defined(__clang__) && !defined(_ARM_)
#if defined(__clang__) && !defined(_ARM_) && !defined(_ARM64_)
// Clang doesn't expand _bittestandset intrinic to bts, and it's implemention also doesn't work for _BitPos >= 32
unsigned char retval = 0;
asm(
Expand All @@ -681,7 +694,7 @@ namespace PlatformAgnostic

__forceinline unsigned char _BitTest(LONG *_BitBase, int _BitPos)
{
#if defined(__clang__) && !defined(_ARM_)
#if defined(__clang__) && !defined(_ARM_) && !defined(_ARM64_)
// Clang doesn't expand _bittest intrinic to bt, and it's implemention also doesn't work for _BitPos >= 32
unsigned char retval;
asm(
Expand All @@ -699,7 +712,7 @@ namespace PlatformAgnostic

__forceinline unsigned char _InterlockedBitTestAndSet(volatile LONG *_BitBase, int _BitPos)
{
#if defined(__clang__) && !defined(_ARM_)
#if defined(__clang__) && !defined(_ARM_) && !defined(_ARM64_)
// Clang doesn't expand _interlockedbittestandset intrinic to lock bts, and it's implemention also doesn't work for _BitPos >= 32
unsigned char retval;
asm(
Expand All @@ -717,7 +730,7 @@ namespace PlatformAgnostic

__forceinline unsigned char _InterlockedBitTestAndReset(volatile LONG *_BitBase, int _BitPos)
{
#if defined(__clang__) && !defined(_ARM_)
#if defined(__clang__) && !defined(_ARM_) && !defined(_ARM64_)
// Clang doesn't expand _interlockedbittestandset intrinic to lock btr, and it's implemention also doesn't work for _BitPos >= 32
unsigned char retval;
asm(
Expand Down
4 changes: 4 additions & 0 deletions lib/Common/PlatformAgnostic/AssemblyCommon.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
Expand All @@ -24,6 +25,9 @@ void mac_fde_wrapper(const char *dataStart, mac_fde_reg_op reg_op);
#define __REGISTER_FRAME(addr) __register_frame(addr)
#define __DEREGISTER_FRAME(addr) __deregister_frame(addr)
#endif // __APPLE__
#elif defined(_M_ARM64) // _AMD64_ && !DISABLE_JIT
#define __REGISTER_FRAME(addr) __register_frame(addr)
#define __DEREGISTER_FRAME(addr) __deregister_frame(addr)
#else
#define __REGISTER_FRAME(addr)
#define __DEREGISTER_FRAME(addr)
Expand Down
6 changes: 6 additions & 0 deletions lib/Common/arm64.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
// ARM64-specific macro definitions
Expand All @@ -10,7 +11,12 @@
#error Include arm64.h in builds of ARM64 targets only.
#endif

#ifdef __getReg
#define arm64_GET_CURRENT_FRAME() ((LPVOID)__getReg(29))
#else
extern "C" LPVOID arm64_GET_CURRENT_FRAME(void);
#endif

extern "C" VOID arm64_SAVE_REGISTERS(void*);

/*
Expand Down

0 comments on commit af58421

Please # to comment.