diff options
| author | Mike Pall <mike> | 2022-06-08 14:24:57 +0200 |
|---|---|---|
| committer | Mike Pall <mike> | 2022-06-08 14:24:57 +0200 |
| commit | 7beb3375e34583e01e5c6ab370721aaea56226cc (patch) | |
| tree | 0c35ea9901f0e9ceed0fd7ee4096a318b980d536 /src | |
| parent | 96157d360db9dde81e7a3752f5c060f5111e1b07 (diff) | |
| download | luajit-7beb3375e34583e01e5c6ab370721aaea56226cc.tar.gz luajit-7beb3375e34583e01e5c6ab370721aaea56226cc.tar.bz2 luajit-7beb3375e34583e01e5c6ab370721aaea56226cc.zip | |
Add Nintendo Switch port.
Contributed by Swyter and vdweller84.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib_io.c | 2 | ||||
| -rw-r--r-- | src/lib_os.c | 2 | ||||
| -rw-r--r-- | src/lj_arch.h | 7 | ||||
| -rw-r--r-- | src/lj_dispatch.h | 4 | ||||
| -rw-r--r-- | src/lj_prng.c | 9 | ||||
| -rw-r--r-- | src/nxbuild.bat | 159 |
6 files changed, 179 insertions, 4 deletions
diff --git a/src/lib_io.c b/src/lib_io.c index 7f218e49..c22faa24 100644 --- a/src/lib_io.c +++ b/src/lib_io.c | |||
| @@ -439,7 +439,7 @@ LJLIB_CF(io_popen) | |||
| 439 | LJLIB_CF(io_tmpfile) | 439 | LJLIB_CF(io_tmpfile) |
| 440 | { | 440 | { |
| 441 | IOFileUD *iof = io_file_new(L); | 441 | IOFileUD *iof = io_file_new(L); |
| 442 | #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA | 442 | #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX |
| 443 | iof->fp = NULL; errno = ENOSYS; | 443 | iof->fp = NULL; errno = ENOSYS; |
| 444 | #else | 444 | #else |
| 445 | iof->fp = tmpfile(); | 445 | iof->fp = tmpfile(); |
diff --git a/src/lib_os.c b/src/lib_os.c index eb8704cf..6bcd0147 100644 --- a/src/lib_os.c +++ b/src/lib_os.c | |||
| @@ -76,7 +76,7 @@ LJLIB_CF(os_rename) | |||
| 76 | 76 | ||
| 77 | LJLIB_CF(os_tmpname) | 77 | LJLIB_CF(os_tmpname) |
| 78 | { | 78 | { |
| 79 | #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA | 79 | #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX |
| 80 | lj_err_caller(L, LJ_ERR_OSUNIQF); | 80 | lj_err_caller(L, LJ_ERR_OSUNIQF); |
| 81 | return 0; | 81 | return 0; |
| 82 | #else | 82 | #else |
diff --git a/src/lj_arch.h b/src/lj_arch.h index 1852c497..882c99cb 100644 --- a/src/lj_arch.h +++ b/src/lj_arch.h | |||
| @@ -162,6 +162,13 @@ | |||
| 162 | #define LJ_TARGET_GC64 1 | 162 | #define LJ_TARGET_GC64 1 |
| 163 | #endif | 163 | #endif |
| 164 | 164 | ||
| 165 | #ifdef __NX__ | ||
| 166 | #define LJ_TARGET_NX 1 | ||
| 167 | #define LJ_TARGET_CONSOLE 1 | ||
| 168 | #undef NULL | ||
| 169 | #define NULL ((void*)0) | ||
| 170 | #endif | ||
| 171 | |||
| 165 | #ifdef _UWP | 172 | #ifdef _UWP |
| 166 | #define LJ_TARGET_UWP 1 | 173 | #define LJ_TARGET_UWP 1 |
| 167 | #if LUAJIT_TARGET == LUAJIT_ARCH_X64 | 174 | #if LUAJIT_TARGET == LUAJIT_ARCH_X64 |
diff --git a/src/lj_dispatch.h b/src/lj_dispatch.h index 0594af51..52762eea 100644 --- a/src/lj_dispatch.h +++ b/src/lj_dispatch.h | |||
| @@ -89,7 +89,7 @@ typedef uint16_t HotCount; | |||
| 89 | typedef struct GG_State { | 89 | typedef struct GG_State { |
| 90 | lua_State L; /* Main thread. */ | 90 | lua_State L; /* Main thread. */ |
| 91 | global_State g; /* Global state. */ | 91 | global_State g; /* Global state. */ |
| 92 | #if LJ_TARGET_ARM | 92 | #if LJ_TARGET_ARM && !LJ_TARGET_NX |
| 93 | /* Make g reachable via K12 encoded DISPATCH-relative addressing. */ | 93 | /* Make g reachable via K12 encoded DISPATCH-relative addressing. */ |
| 94 | uint8_t align1[(16-sizeof(global_State))&15]; | 94 | uint8_t align1[(16-sizeof(global_State))&15]; |
| 95 | #endif | 95 | #endif |
| @@ -99,7 +99,7 @@ typedef struct GG_State { | |||
| 99 | #if LJ_HASJIT | 99 | #if LJ_HASJIT |
| 100 | jit_State J; /* JIT state. */ | 100 | jit_State J; /* JIT state. */ |
| 101 | HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */ | 101 | HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */ |
| 102 | #if LJ_TARGET_ARM | 102 | #if LJ_TARGET_ARM && !LJ_TARGET_NX |
| 103 | /* Ditto for J. */ | 103 | /* Ditto for J. */ |
| 104 | uint8_t align2[(16-sizeof(jit_State)-sizeof(HotCount)*HOTCOUNT_SIZE)&15]; | 104 | uint8_t align2[(16-sizeof(jit_State)-sizeof(HotCount)*HOTCOUNT_SIZE)&15]; |
| 105 | #endif | 105 | #endif |
diff --git a/src/lj_prng.c b/src/lj_prng.c index fd8219de..9e57505e 100644 --- a/src/lj_prng.c +++ b/src/lj_prng.c | |||
| @@ -87,6 +87,10 @@ extern int sys_get_random_number(void *buf, uint64_t len); | |||
| 87 | 87 | ||
| 88 | extern int sceRandomGetRandomNumber(void *buf, size_t len); | 88 | extern int sceRandomGetRandomNumber(void *buf, size_t len); |
| 89 | 89 | ||
| 90 | #elif LJ_TARGET_NX | ||
| 91 | |||
| 92 | #include <unistd.h> | ||
| 93 | |||
| 90 | #elif LJ_TARGET_WINDOWS || LJ_TARGET_XBOXONE | 94 | #elif LJ_TARGET_WINDOWS || LJ_TARGET_XBOXONE |
| 91 | 95 | ||
| 92 | #define WIN32_LEAN_AND_MEAN | 96 | #define WIN32_LEAN_AND_MEAN |
| @@ -176,6 +180,11 @@ int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs) | |||
| 176 | if (sceRandomGetRandomNumber(rs->u, sizeof(rs->u)) == 0) | 180 | if (sceRandomGetRandomNumber(rs->u, sizeof(rs->u)) == 0) |
| 177 | goto ok; | 181 | goto ok; |
| 178 | 182 | ||
| 183 | #elif LJ_TARGET_NX | ||
| 184 | |||
| 185 | if (getentropy(rs->u, sizeof(rs->u)) == 0) | ||
| 186 | goto ok; | ||
| 187 | |||
| 179 | #elif LJ_TARGET_UWP || LJ_TARGET_XBOXONE | 188 | #elif LJ_TARGET_UWP || LJ_TARGET_XBOXONE |
| 180 | 189 | ||
| 181 | if (BCryptGenRandom(NULL, (PUCHAR)(rs->u), (ULONG)sizeof(rs->u), | 190 | if (BCryptGenRandom(NULL, (PUCHAR)(rs->u), (ULONG)sizeof(rs->u), |
diff --git a/src/nxbuild.bat b/src/nxbuild.bat new file mode 100644 index 00000000..c4a21f05 --- /dev/null +++ b/src/nxbuild.bat | |||
| @@ -0,0 +1,159 @@ | |||
| 1 | @rem Script to build LuaJIT with NintendoSDK + NX Addon. | ||
| 2 | @rem Donated to the public domain by Swyter. | ||
| 3 | @rem | ||
| 4 | @rem To run this script you must open a "Native Tools Command Prompt for VS". | ||
| 5 | @rem | ||
| 6 | @rem Either the x86 version for NX32, or x64 for the NX64 target. | ||
| 7 | @rem This is because the pointer size of the LuaJIT host tools (buildvm.exe) | ||
| 8 | @rem must match the cross-compiled target (32 or 64 bits). | ||
| 9 | @rem | ||
| 10 | @rem Then cd to this directory and run this script. | ||
| 11 | @rem | ||
| 12 | @rem Recommended invocation: | ||
| 13 | @rem | ||
| 14 | @rem nxbuild # release build, amalgamated | ||
| 15 | @rem nxbuild debug # debug build, amalgamated | ||
| 16 | @rem | ||
| 17 | @rem Additional command-line options (not generally recommended): | ||
| 18 | @rem | ||
| 19 | @rem noamalg # (after debug) non-amalgamated build | ||
| 20 | |||
| 21 | @if not defined INCLUDE goto :FAIL | ||
| 22 | @if not defined NINTENDO_SDK_ROOT goto :FAIL | ||
| 23 | @if not defined PLATFORM goto :FAIL | ||
| 24 | |||
| 25 | @if "%platform%" == "x86" goto :DO_NX32 | ||
| 26 | @if "%platform%" == "x64" goto :DO_NX64 | ||
| 27 | |||
| 28 | @echo Error: Current host platform is %platform%! | ||
| 29 | @echo. | ||
| 30 | @goto :FAIL | ||
| 31 | |||
| 32 | @setlocal | ||
| 33 | |||
| 34 | :DO_NX32 | ||
| 35 | @set DASC=vm_arm.dasc | ||
| 36 | @set DASMFLAGS= -D HFABI -D FPU | ||
| 37 | @set DASMTARGET= -D LUAJIT_TARGET=LUAJIT_ARCH_ARM | ||
| 38 | @set HOST_PTR_SIZE=4 | ||
| 39 | goto :BEGIN | ||
| 40 | |||
| 41 | :DO_NX64 | ||
| 42 | @set DASC=vm_arm64.dasc | ||
| 43 | @set DASMFLAGS= -D ENDIAN_LE | ||
| 44 | @set DASMTARGET= -D LUAJIT_TARGET=LUAJIT_ARCH_ARM64 | ||
| 45 | @set HOST_PTR_SIZE=8 | ||
| 46 | |||
| 47 | :BEGIN | ||
| 48 | @rem ---- Host compiler ---- | ||
| 49 | @set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /wo4146 /wo4244 /D_CRT_SECURE_NO_DEPRECATE | ||
| 50 | @set LJLINK=link /nologo | ||
| 51 | @set LJMT=mt /nologo | ||
| 52 | @set DASMDIR=..\dynasm | ||
| 53 | @set DASM=%DASMDIR%\dynasm.lua | ||
| 54 | @set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c | ||
| 55 | |||
| 56 | %LJCOMPILE% host\minilua.c | ||
| 57 | @if errorlevel 1 goto :BAD | ||
| 58 | %LJLINK% /out:minilua.exe minilua.obj | ||
| 59 | @if errorlevel 1 goto :BAD | ||
| 60 | if exist minilua.exe.manifest^ | ||
| 61 | %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe | ||
| 62 | |||
| 63 | @rem Check that we have the right 32/64 bit host compiler to generate the right virtual machine files. | ||
| 64 | @minilua | ||
| 65 | @if "%ERRORLEVEL%" == "%HOST_PTR_SIZE%" goto :PASSED_PTR_CHECK | ||
| 66 | |||
| 67 | @echo The pointer size of the host in bytes (%HOST_PTR_SIZE%) does not match the expected value (%errorlevel%). | ||
| 68 | @echo Check that the script is being ran under the correct x86/x64 VS prompt. | ||
| 69 | @goto :BAD | ||
| 70 | |||
| 71 | :PASSED_PTR_CHECK | ||
| 72 | @set DASMFLAGS=%DASMFLAGS% %DASMTARGET% -D LJ_TARGET_NX -D LUAJIT_OS=LUAJIT_OS_OTHER -D LUAJIT_DISABLE_JIT -D LUAJIT_DISABLE_FFI | ||
| 73 | minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h %DASC% | ||
| 74 | @if errorlevel 1 goto :BAD | ||
| 75 | %LJCOMPILE% /I "." /I %DASMDIR% %DASMTARGET% -D LJ_TARGET_NX -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI host\buildvm*.c | ||
| 76 | @if errorlevel 1 goto :BAD | ||
| 77 | %LJLINK% /out:buildvm.exe buildvm*.obj | ||
| 78 | @if errorlevel 1 goto :BAD | ||
| 79 | if exist buildvm.exe.manifest^ | ||
| 80 | %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe | ||
| 81 | |||
| 82 | buildvm -m elfasm -o lj_vm.s | ||
| 83 | @if errorlevel 1 goto :BAD | ||
| 84 | buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% | ||
| 85 | @if errorlevel 1 goto :BAD | ||
| 86 | buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% | ||
| 87 | @if errorlevel 1 goto :BAD | ||
| 88 | buildvm -m libdef -o lj_libdef.h %ALL_LIB% | ||
| 89 | @if errorlevel 1 goto :BAD | ||
| 90 | buildvm -m recdef -o lj_recdef.h %ALL_LIB% | ||
| 91 | @if errorlevel 1 goto :BAD | ||
| 92 | buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% | ||
| 93 | @if errorlevel 1 goto :BAD | ||
| 94 | buildvm -m folddef -o lj_folddef.h lj_opt_fold.c | ||
| 95 | @if errorlevel 1 goto :BAD | ||
| 96 | |||
| 97 | @rem ---- Cross compiler ---- | ||
| 98 | @if "%platform%" neq "x64" goto :NX32_CROSSBUILD | ||
| 99 | @set LJCOMPILE="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\aarch64\bin\clang" -Wall -I%NINTENDO_SDK_ROOT%\Include %DASMTARGET% -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -c | ||
| 100 | @set LJLIB="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\aarch64\bin\aarch64-nintendo-nx-elf-ar" rc | ||
| 101 | @set TARGETLIB_SUFFIX=nx64 | ||
| 102 | |||
| 103 | %NINTENDO_SDK_ROOT%\Compilers\NX\nx\aarch64\bin\aarch64-nintendo-nx-elf-as -o lj_vm.o lj_vm.s | ||
| 104 | goto :DEBUGCHECK | ||
| 105 | |||
| 106 | :NX32_CROSSBUILD | ||
| 107 | @set LJCOMPILE="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\armv7l\bin\clang" -Wall -I%NINTENDO_SDK_ROOT%\Include %DASMTARGET% -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_USE_SYSMALLOC -c | ||
| 108 | @set LJLIB="%NINTENDO_SDK_ROOT%\Compilers\NX\nx\armv7l\bin\armv7l-nintendo-nx-eabihf-ar" rc | ||
| 109 | @set TARGETLIB_SUFFIX=nx32 | ||
| 110 | |||
| 111 | %NINTENDO_SDK_ROOT%\Compilers\NX\nx\armv7l\bin\armv7l-nintendo-nx-eabihf-as -o lj_vm.o lj_vm.s | ||
| 112 | :DEBUGCHECK | ||
| 113 | |||
| 114 | @if "%1" neq "debug" goto :NODEBUG | ||
| 115 | @shift | ||
| 116 | @set LJCOMPILE=%LJCOMPILE% -DNN_SDK_BUILD_DEBUG -g -O0 | ||
| 117 | @set TARGETLIB=libluajitD_%TARGETLIB_SUFFIX%.a | ||
| 118 | goto :BUILD | ||
| 119 | :NODEBUG | ||
| 120 | @set LJCOMPILE=%LJCOMPILE% -DNN_SDK_BUILD_RELEASE -O3 | ||
| 121 | @set TARGETLIB=libluajit_%TARGETLIB_SUFFIX%.a | ||
| 122 | :BUILD | ||
| 123 | del %TARGETLIB% | ||
| 124 | @if "%1" neq "noamalg" goto :AMALG | ||
| 125 | for %%f in (lj_*.c lib_*.c) do ( | ||
| 126 | %LJCOMPILE% %%f | ||
| 127 | @if errorlevel 1 goto :BAD | ||
| 128 | ) | ||
| 129 | |||
| 130 | %LJLIB% %TARGETLIB% lj_*.o lib_*.o | ||
| 131 | @if errorlevel 1 goto :BAD | ||
| 132 | @goto :NOAMALG | ||
| 133 | :AMALG | ||
| 134 | %LJCOMPILE% ljamalg.c | ||
| 135 | @if errorlevel 1 goto :BAD | ||
| 136 | %LJLIB% %TARGETLIB% ljamalg.o lj_vm.o | ||
| 137 | @if errorlevel 1 goto :BAD | ||
| 138 | :NOAMALG | ||
| 139 | |||
| 140 | @del *.o *.obj *.manifest minilua.exe buildvm.exe | ||
| 141 | @echo. | ||
| 142 | @echo === Successfully built LuaJIT for Nintendo Switch (%TARGETLIB_SUFFIX%) === | ||
| 143 | |||
| 144 | @goto :END | ||
| 145 | :BAD | ||
| 146 | @echo. | ||
| 147 | @echo ******************************************************* | ||
| 148 | @echo *** Build FAILED -- Please check the error messages *** | ||
| 149 | @echo ******************************************************* | ||
| 150 | @goto :END | ||
| 151 | :FAIL | ||
| 152 | @echo To run this script you must open a "Native Tools Command Prompt for VS". | ||
| 153 | @echo. | ||
| 154 | @echo Either the x86 version for NX32, or x64 for the NX64 target. | ||
| 155 | @echo This is because the pointer size of the LuaJIT host tools (buildvm.exe) | ||
| 156 | @echo must match the cross-compiled target (32 or 64 bits). | ||
| 157 | @echo. | ||
| 158 | @echo Keep in mind that NintendoSDK + NX Addon must be installed, too. | ||
| 159 | :END | ||
