Skip to content

Commit 41ff78f

Browse files
DaggolinRazish
authored andcommitted
[Shared] Add support for arm64 (Apple M1).
1 parent 4834a1a commit 41ff78f

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ if(WIN32)
112112
endif()
113113
else()
114114
set(X86 OFF)
115-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
115+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)$")
116+
set(Architecture "arm64")
117+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
116118
set(Architecture "arm")
117119
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
118120
set(X86 ON)
@@ -388,4 +390,4 @@ endif()
388390
if(BuildTests)
389391
enable_testing()
390392
add_subdirectory("tests")
391-
endif()
393+
endif()

code/client/cl_cgame.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ qboolean CL_InitCGameVM( void *gameLibrary )
6464
typedef void DllEntryProc( SyscallProc * );
6565

6666
DllEntryProc *dllEntry = (DllEntryProc *)Sys_LoadFunction( gameLibrary, "dllEntry" );
67-
cgvm.entryPoint = (intptr_t (*)(int,...))Sys_LoadFunction( gameLibrary, "vmMain" );
67+
68+
// NOTE: arm64 mac has a different calling convention for fixed parameters vs. variadic parameters.
69+
// As the cgame entryPoints (vmMain) in jk2 and jka use fixed arg0 to arg7 we can't use "..." around here or we end up with undefined behavior.
70+
// See: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code
71+
cgvm.entryPoint = (intptr_t (*)(int,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t))Sys_LoadFunction( gameLibrary, "vmMain" );
6872

6973
if ( !cgvm.entryPoint || !dllEntry ) {
7074
#ifdef JK2_MODE

code/client/vmachine.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ VIRTUAL MACHINE
3434
*/
3535
intptr_t VM_Call( int callnum, ... )
3636
{
37-
intptr_t args[10] = { 0 };
37+
intptr_t args[8] = { 0 };
3838
va_list ap;
3939

4040
if ( cgvm.entryPoint ) {
@@ -43,8 +43,7 @@ intptr_t VM_Call( int callnum, ... )
4343
args[i] = va_arg( ap, intptr_t );
4444
va_end(ap);
4545

46-
return cgvm.entryPoint( callnum, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7],
47-
args[8], args[9]);
46+
return cgvm.entryPoint( callnum, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7] );
4847
}
4948
return -1;
5049
}

code/client/vmachine.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ VIRTUAL MACHINE
7171
==============================================================
7272
*/
7373
typedef struct vm_s {
74-
intptr_t (*entryPoint)( int callNum, ... );
74+
// NOTE: arm64 mac has a different calling convention for fixed parameters vs. variadic parameters.
75+
// As the cgame entryPoints (vmMain) in jk2 and jka use fixed arg0 to arg7 we can't use "..." around here or we end up with undefined behavior.
76+
// See: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code
77+
intptr_t (*entryPoint)( int callNum, intptr_t arg0, intptr_t arg1, intptr_t arg2, intptr_t arg3, intptr_t arg4, intptr_t arg5, intptr_t arg6, intptr_t arg7 );
7578
} vm_t;
7679

7780
extern vm_t cgvm;

shared/qcommon/q_platform.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ along with this program; if not, see <http://www.gnu.org/licenses/>.
108108
#define idx64
109109
#define ARCH_STRING "x86_64"
110110
#define Q3_LITTLE_ENDIAN
111+
#elif defined(__arm64__)
112+
#define ARCH_STRING "arm64"
113+
#define Q3_LITTLE_ENDIAN
111114
#endif
112115

113116
#define DLL_EXT ".dylib"

0 commit comments

Comments
 (0)