Skip to content

Commit 40b603e

Browse files
committed
Incomplete Backend support for Apple Silicon
- Enables dynamic interpretor thunks on Apple Silicon - Remainder of the JIT will compile with these edits but not yet work
1 parent edb1ee9 commit 40b603e

8 files changed

+249
-27
lines changed

lib/Backend/CMakeLists.txt

+57-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
1+
if(CC_TARGETS_AMD64)
2+
set (CC_BACKEND_ARCH_FOLDER amd64)
3+
set (CC_BACKEND_ARCH_FILES
4+
amd64/EncoderMD.cpp
5+
amd64/LinearScanMD.cpp
6+
amd64/LowererMDArch.cpp
7+
amd64/PeepsMD.cpp
8+
amd64/PrologEncoderMD.cpp
9+
amd64/LinearScanMdA.S
10+
amd64/Thunks.S
11+
AgenPeeps.cpp
12+
EhFrame.cpp
13+
LowerMDShared.cpp
14+
LowerMDSharedSimd128.cpp
15+
PrologEncoder.cpp
16+
)
17+
elseif(CC_TARGETS_X86)
18+
set (CC_BACKEND_ARCH_FOLDER i386)
19+
set (CC_BACKEND_ARCH_FILES
20+
i386/EncoderMD.cpp
21+
i386/LinearScanMD.cpp
22+
i386/LowererMDArch.cpp
23+
i386/PeepsMD.cpp
24+
AgenPeeps.cpp
25+
LowerMDShared.cpp
26+
LowerMDSharedSimd128.cpp
27+
)
28+
elseif(CC_TARGETS_ARM64)
29+
set (CC_BACKEND_ARCH_FOLDER arm64)
30+
set (CC_BACKEND_ARCH_FILES
31+
arm64/ARM64LogicalImmediates.cpp
32+
arm64/ARM64UnwindEncoder.cpp
33+
arm64/EncoderMD.cpp
34+
arm64/LegalizeMD.cpp
35+
arm64/LinearScanMD.cpp
36+
arm64/LinearScanMdA.S
37+
arm64/LowerMD.cpp
38+
arm64/PeepsMD.cpp
39+
arm64/Thunks.S
40+
arm64/UnwindInfoManager.cpp
41+
)
42+
elseif(CC_TARGETS_ARM)
43+
set (CC_BACKEND_ARCH_FOLDER arm)
44+
set (CC_BACKEND_ARCH_FILES
45+
arm/EncoderMD.cpp
46+
arm/LegalizeMD.cpp
47+
arm/LinearScanMD.cpp
48+
arm/LinearScanMdA.asm
49+
arm/LowerMD.cpp
50+
arm/PeepsMD.cpp
51+
arm/Thunks.asm
52+
arm/UnwindInfoManager.cpp
53+
)
54+
endif()
55+
156
add_library (Chakra.Backend OBJECT
2-
AgenPeeps.cpp
357
AsmJsJITInfo.cpp
458
Backend.cpp
559
BackendApi.cpp
@@ -12,7 +66,6 @@ add_library (Chakra.Backend OBJECT
1266
CodeGenWorkItem.cpp
1367
DbCheckPostLower.cpp
1468
Debug.cpp
15-
EhFrame.cpp
1669
EmitBuffer.cpp
1770
Encoder.cpp
1871
EquivalentTypeSet.cpp
@@ -57,8 +110,6 @@ add_library (Chakra.Backend OBJECT
57110
JnHelperMethod.cpp
58111
LinearScan.cpp
59112
Lower.cpp
60-
LowerMDShared.cpp
61-
LowerMDSharedSimd128.cpp
62113
NativeCodeData.cpp
63114
NativeCodeGenerator.cpp
64115
NativeEntryPointData.cpp
@@ -68,7 +119,6 @@ add_library (Chakra.Backend OBJECT
68119
PageAllocatorPool.cpp
69120
Peeps.cpp
70121
PreLowerPeeps.cpp
71-
PrologEncoder.cpp
72122
QueuedFullJitWorkItem.cpp
73123
Region.cpp
74124
SccLiveness.cpp
@@ -83,30 +133,12 @@ add_library (Chakra.Backend OBJECT
83133
TempTracker.cpp
84134
ValueInfo.cpp
85135
ValueRelativeOffset.cpp
86-
amd64/EncoderMD.cpp
87-
amd64/LinearScanMD.cpp
88-
amd64/LowererMDArch.cpp
89-
amd64/PeepsMD.cpp
90-
amd64/PrologEncoderMD.cpp
91-
amd64/LinearScanMdA.S
92-
amd64/Thunks.S
93-
# arm64/EncoderMD.cpp
94-
# arm64/LowerMD.cpp
95-
# arm/EncoderMD.cpp
96-
# arm/LegalizeMD.cpp
97-
# arm/LinearScanMD.cpp
98-
# arm/LowerMD.cpp
99-
# arm/PeepsMD.cpp
100-
# arm/UnwindInfoManager.cpp
101-
# i386/EncoderMD.cpp
102-
# i386/LinearScanMD.cpp
103-
# i386/LowererMDArch.cpp
104-
# i386/PeepsMD.cpp
136+
${CC_BACKEND_ARCH_FILES}
105137
)
106138

107139
target_include_directories (
108140
Chakra.Backend PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
109-
amd64
141+
${CC_BACKEND_ARCH_FOLDER}
110142
../Common
111143
../JITIDL
112144
../Runtime

lib/Backend/InterpreterThunkEmitter.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
#include "Backend.h"
@@ -157,19 +158,31 @@ constexpr BYTE Epilog[] = {
157158
};
158159
#elif defined(_M_ARM64)
159160

161+
#ifdef _WIN32
160162
constexpr BYTE FunctionInfoOffset = 24;
161163
constexpr BYTE FunctionProxyOffset = 28;
162164
constexpr BYTE DynamicThunkAddressOffset = 32;
163165
constexpr BYTE ThunkAddressOffset = 36;
166+
#else
167+
constexpr BYTE FunctionInfoOffset = 8;
168+
constexpr BYTE FunctionProxyOffset = 12;
169+
constexpr BYTE DynamicThunkAddressOffset = 16;
170+
constexpr BYTE ThunkAddressOffset = 20;
171+
#endif
164172

165173
//TODO: saravind :Implement Range Check for ARM64
166174
constexpr BYTE InterpreterThunk[InterpreterThunkEmitter::InterpreterThunkSize] = {
175+
#ifdef _WIN32
167176
0xFD, 0x7B, 0xBB, 0xA9, //stp fp, lr, [sp, #-80]! ;Prologue
168177
0xFD, 0x03, 0x00, 0x91, //mov fp, sp ;update frame pointer to the stack pointer
169178
0xE0, 0x07, 0x01, 0xA9, //stp x0, x1, [sp, #16] ;Prologue again; save all registers
170179
0xE2, 0x0F, 0x02, 0xA9, //stp x2, x3, [sp, #32]
171180
0xE4, 0x17, 0x03, 0xA9, //stp x4, x5, [sp, #48]
172181
0xE6, 0x1F, 0x04, 0xA9, //stp x6, x7, [sp, #64]
182+
#else
183+
0xFD, 0x7B, 0xBF, 0xA9, //stp fp, lr, [sp, #-16]! ;Prologue
184+
0xFD, 0x03, 0x00, 0x91, //mov fp, sp ;update frame pointer to the stack pointer
185+
#endif
173186
0x02, 0x00, 0x40, 0xF9, //ldr x2, [x0, #0x00] ;offset will be replaced with Offset of FunctionInfo
174187
0x40, 0x00, 0x40, 0xF9, //ldr x0, [x2, #0x00] ;offset will be replaced with Offset of FunctionProxy
175188
0x03, 0x00, 0x40, 0xF9, //ldr x3, [x0, #0x00] ;offset will be replaced with offset of DynamicInterpreterThunk
@@ -191,7 +204,11 @@ constexpr BYTE Call[] = {
191204
};
192205

193206
constexpr BYTE Epilog[] = {
207+
#ifdef _WIN32
194208
0xfd, 0x7b, 0xc5, 0xa8, // ldp fp, lr, [sp], #80
209+
#else
210+
0xfd, 0x7b, 0xc1, 0xa8, // ldp fp, lr, [sp], #16
211+
#endif
195212
0xc0, 0x03, 0x5f, 0xd6 // ret
196213
};
197214
#else // x86

lib/Backend/InterpreterThunkEmitter.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
#pragma once
@@ -68,7 +69,11 @@ class InterpreterThunkEmitter
6869
#elif defined(_M_ARM)
6970
static constexpr size_t InterpreterThunkSize = 72;
7071
#elif defined(_M_ARM64)
72+
#ifdef _WIN32
7173
static constexpr size_t InterpreterThunkSize = 64;
74+
#else
75+
static constexpr size_t InterpreterThunkSize = 48;
76+
#endif
7277
#else
7378
static constexpr size_t InterpreterThunkSize = 56;
7479
#endif

lib/Backend/LinearScan.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
3-
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
44
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
55
//-------------------------------------------------------------------------------------------------------
66

@@ -4948,6 +4948,8 @@ LinearScan::GeneratorBailIn::GeneratorBailIn(Func* func, LinearScan* linearScan)
49484948
RegRAX, RegRCX
49494949
#elif defined(_M_IX86)
49504950
RegEAX, RegECX
4951+
#elif defined(_M_ARM64)
4952+
RegR0, RegR1
49514953
#endif
49524954
},
49534955
interpreterFrameRegOpnd { IR::RegOpnd::New(nullptr, regs[0], TyMachPtr, func) },

lib/Backend/arm64/ARM64Encoder.h

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
#pragma once
@@ -817,6 +818,17 @@ EmitDiv0Exception(
817818
//
818819
// MRS dest, systemreg
819820
//
821+
#ifndef _WIN32
822+
#define ARM64_SYSREG(op0, op1, crn, crm, op2) \
823+
( ((op0 & 1) << 14) | \
824+
((op1 & 7) << 11) | \
825+
((crn & 15) << 7) | \
826+
((crm & 15) << 3) | \
827+
((op2 & 7) << 0) )
828+
829+
#define ARM64_FPCR ARM64_SYSREG(3, 3, 4, 4, 0) // Floating point control register (EL0)
830+
#define ARM64_FPSR ARM64_SYSREG(3, 3, 4, 4, 1) // Floating point status register (EL0)
831+
#endif
820832

821833
#define ARM64_NZCV ARM64_SYSREG(3,3, 4, 2,0) // Flags (EL0); arm64_x.h
822834
#define ARM64_CNTVCT ARM64_SYSREG(3,3,14, 0,2) // Generic Timer virtual count

lib/Backend/arm64/LinearScanMdA.S

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
;-------------------------------------------------------------------------------------------------------
2+
; Copyright (C) Microsoft. All rights reserved.
3+
; Copyright (c) ChakraCore Project Contributors. All rights reserved.
4+
; Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
5+
;-------------------------------------------------------------------------------------------------------
6+
7+
#include "unixasmmacros.inc"
8+
9+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10+
; LinearScanMD::SaveAllRegistersAndBailOut(BailOutRecord *const bailOutRecord)
11+
12+
NESTED_ENTRY _ZN12LinearScanMD26SaveAllRegistersAndBailOutEP13BailOutRecord, _TEXT, NoHandler
13+
14+
; x0 == bailOutRecord
15+
; lr == return address
16+
17+
; Save all registers except the above, which would have already been saved by jitted code if necessary
18+
ldr x17, [x0] ; bailOutRecord->globalBailOutRecordDataTable
19+
ldr x17, [x17] ; bailOutRecord->globalBailOutRecordDataTable->registerSaveSpace
20+
str x1, [x17, #1*8]
21+
stp x2, x3, [x17, #2*8]
22+
stp x4, x5, [x17, #4*8]
23+
stp x6, x7, [x17, #6*8]
24+
stp x8, x9, [x17, #8*8]
25+
stp x10, x11, [x17, #10*8]
26+
stp x12, x13, [x17, #12*8]
27+
stp x14, x15, [x17, #14*8]
28+
str x16, [x17, #16*8]
29+
; skip x17/x18
30+
stp x19, x20, [x17, #19*8]
31+
stp x21, x22, [x17, #21*8]
32+
stp x23, x24, [x17, #23*8]
33+
stp x25, x26, [x17, #25*8]
34+
stp x27, x28, [x17, #27*8]
35+
str fp, [x17, #29*8]
36+
; skip lr, sp, zr
37+
add x17, x17, #33*8
38+
stp d0, d1, [x17, #0*8]
39+
stp d2, d3, [x17, #2*8]
40+
stp d4, d5, [x17, #4*8]
41+
stp d6, d7, [x17, #6*8]
42+
stp d8, d9, [x17, #8*8]
43+
stp d10, d11, [x17, #10*8]
44+
stp d12, d13, [x17, #12*8]
45+
stp d14, d15, [x17, #14*8]
46+
stp d16, d17, [x17, #16*8]
47+
stp d18, d19, [x17, #18*8]
48+
stp d20, d21, [x17, #20*8]
49+
stp d22, d23, [x17, #22*8]
50+
stp d24, d25, [x17, #24*8]
51+
stp d26, d27, [x17, #26*8]
52+
stp d28, d29, [x17, #28*8]
53+
;stp d30, d31, [x17, #30*8]
54+
55+
b C_FUNC(_ZN13BailOutRecord7BailOutEPKS_)
56+
57+
NESTED_END _ZN12LinearScanMD26SaveAllRegistersAndBailOutEP13BailOutRecord, _TEXT
58+
59+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
60+
; LinearScanMD::SaveAllRegistersAndBranchBailOut(BranchBailOutRecord *const bailOutRecord, const BOOL condition)
61+
62+
NESTED_ENTRY _ZN12LinearScanMD32SaveAllRegistersAndBranchBailOutEP19BranchBailOutRecordi, _TEXT, NoHandler
63+
64+
; x0 == bailOutRecord
65+
; x1 == condition
66+
; lr == return address
67+
68+
; Save all registers except the above, which would have already been saved by jitted code if necessary
69+
ldr x17, [x0] ; bailOutRecord->globalBailOutRecordDataTable
70+
ldr x17, [x17] ; bailOutRecord->globalBailOutRecordDataTable->registerSaveSpace
71+
stp x2, x3, [x17, #2*8]
72+
stp x4, x5, [x17, #4*8]
73+
stp x6, x7, [x17, #6*8]
74+
stp x8, x9, [x17, #8*8]
75+
stp x10, x11, [x17, #10*8]
76+
stp x12, x13, [x17, #12*8]
77+
stp x14, x15, [x17, #14*8]
78+
str x16, [x17, #16*8]
79+
; skip x17/x18
80+
stp x19, x20, [x17, #19*8]
81+
stp x21, x22, [x17, #21*8]
82+
stp x23, x24, [x17, #23*8]
83+
stp x25, x26, [x17, #25*8]
84+
stp x27, x28, [x17, #27*8]
85+
str fp, [x17, #29*8]
86+
; skip lr, sp, zr
87+
add x17, x17, #33*8
88+
stp d0, d1, [x17, #0*8]
89+
stp d2, d3, [x17, #2*8]
90+
stp d4, d5, [x17, #4*8]
91+
stp d6, d7, [x17, #6*8]
92+
stp d8, d9, [x17, #8*8]
93+
stp d10, d11, [x17, #10*8]
94+
stp d12, d13, [x17, #12*8]
95+
stp d14, d15, [x17, #14*8]
96+
stp d16, d17, [x17, #16*8]
97+
stp d18, d19, [x17, #18*8]
98+
stp d20, d21, [x17, #20*8]
99+
stp d22, d23, [x17, #22*8]
100+
stp d24, d25, [x17, #24*8]
101+
stp d26, d27, [x17, #26*8]
102+
stp d28, d29, [x17, #28*8]
103+
;stp d30, d31, [x17, #30*8]
104+
105+
b C_FUNC(_ZN19BranchBailOutRecord7BailOutEPKS_i)
106+
107+
NESTED_END _ZN12LinearScanMD32SaveAllRegistersAndBranchBailOutEP19BranchBailOutRecordi, _TEXT

lib/Backend/arm64/LowerMD.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
#pragma once
@@ -105,7 +106,7 @@ class LowererMD
105106
template <bool Saturate>
106107
void GenerateTruncWithCheck(IR::Instr * instr) { Assert(UNREACHED); }
107108
void GenerateFastDivByPow2(IR::Instr *instr);
108-
bool GenerateFastDivAndRem(IR::Instr* instrDiv, IR::LabelInstr* bailOutLabel = false);
109+
bool GenerateFastDivAndRem(IR::Instr* instrDiv, IR::LabelInstr* bailOutLabel = nullptr);
109110
bool GenerateFastAdd(IR::Instr * instrAdd);
110111
bool GenerateFastSub(IR::Instr * instrSub);
111112
bool GenerateFastMul(IR::Instr * instrMul);

0 commit comments

Comments
 (0)