Skip to content

Commit

Permalink
[Shared] Add support for arm64 (Apple M1).
Browse files Browse the repository at this point in the history
  • Loading branch information
Daggolin authored and Razish committed Sep 17, 2023
1 parent 4834a1a commit 41ff78f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Expand Up @@ -112,7 +112,9 @@ if(WIN32)
endif()
else()
set(X86 OFF)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)$")
set(Architecture "arm64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
set(Architecture "arm")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
set(X86 ON)
Expand Down Expand Up @@ -388,4 +390,4 @@ endif()
if(BuildTests)
enable_testing()
add_subdirectory("tests")
endif()
endif()
6 changes: 5 additions & 1 deletion code/client/cl_cgame.cpp
Expand Up @@ -64,7 +64,11 @@ qboolean CL_InitCGameVM( void *gameLibrary )
typedef void DllEntryProc( SyscallProc * );

DllEntryProc *dllEntry = (DllEntryProc *)Sys_LoadFunction( gameLibrary, "dllEntry" );
cgvm.entryPoint = (intptr_t (*)(int,...))Sys_LoadFunction( gameLibrary, "vmMain" );

// NOTE: arm64 mac has a different calling convention for fixed parameters vs. variadic parameters.
// 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.
// See: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code
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" );

if ( !cgvm.entryPoint || !dllEntry ) {
#ifdef JK2_MODE
Expand Down
5 changes: 2 additions & 3 deletions code/client/vmachine.cpp
Expand Up @@ -34,7 +34,7 @@ VIRTUAL MACHINE
*/
intptr_t VM_Call( int callnum, ... )
{
intptr_t args[10] = { 0 };
intptr_t args[8] = { 0 };
va_list ap;

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

return cgvm.entryPoint( callnum, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7],
args[8], args[9]);
return cgvm.entryPoint( callnum, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7] );
}
return -1;
}
Expand Down
5 changes: 4 additions & 1 deletion code/client/vmachine.h
Expand Up @@ -71,7 +71,10 @@ VIRTUAL MACHINE
==============================================================
*/
typedef struct vm_s {
intptr_t (*entryPoint)( int callNum, ... );
// NOTE: arm64 mac has a different calling convention for fixed parameters vs. variadic parameters.
// 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.
// See: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code
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 );
} vm_t;

extern vm_t cgvm;
Expand Down
3 changes: 3 additions & 0 deletions shared/qcommon/q_platform.h
Expand Up @@ -108,6 +108,9 @@ along with this program; if not, see <http://www.gnu.org/licenses/>.
#define idx64
#define ARCH_STRING "x86_64"
#define Q3_LITTLE_ENDIAN
#elif defined(__arm64__)
#define ARCH_STRING "arm64"
#define Q3_LITTLE_ENDIAN
#endif

#define DLL_EXT ".dylib"
Expand Down

0 comments on commit 41ff78f

Please sign in to comment.