aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRSDuck <rsduck@users.noreply.github.com>2019-07-14 19:24:00 +0200
committerRSDuck <rsduck@users.noreply.github.com>2020-04-26 13:03:06 +0200
commit411fb57c07c732a2b60e3566ae045f8f60eea29d (patch)
tree0373bd3e277c87b21038da489a69fac08762cea2
parent360317be8c3744c49f081dded95499f671641809 (diff)
jit: add compile option
-rw-r--r--CMakeLists.txt30
-rw-r--r--src/ARM.cpp13
-rw-r--r--src/ARM.h6
-rw-r--r--src/ARMJIT_x64/ARMJIT_Compiler.cpp61
-rw-r--r--src/ARMJIT_x64/ARMJIT_Compiler.h1
-rw-r--r--src/CMakeLists.txt25
-rw-r--r--src/CP15.cpp12
-rw-r--r--src/Config.cpp4
-rw-r--r--src/Config.h2
-rw-r--r--src/NDS.cpp26
-rw-r--r--src/dolphin/CodeBlock.h3
-rw-r--r--src/libui_sdl/DlgEmuSettings.cpp21
-rw-r--r--src/libui_sdl/main.cpp2
13 files changed, 151 insertions, 55 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 048dd44..d59e19c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,36 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
+include(CheckSymbolExists)
+function(detect_architecture symbol arch)
+ if (NOT DEFINED ARCHITECTURE)
+ set(CMAKE_REQUIRED_QUIET 1)
+ check_symbol_exists("${symbol}" "" ARCHITECTURE_${arch})
+ unset(CMAKE_REQUIRED_QUIET)
+
+ # The output variable needs to be unique across invocations otherwise
+ # CMake's crazy scope rules will keep it defined
+ if (ARCHITECTURE_${arch})
+ set(ARCHITECTURE "${arch}" PARENT_SCOPE)
+ set(ARCHITECTURE_${arch} 1 PARENT_SCOPE)
+ add_definitions(-DARCHITECTURE_${arch}=1)
+ endif()
+ endif()
+endfunction()
+
+detect_architecture("__x86_64__" x86_64)
+detect_architecture("__i386__" x86)
+detect_architecture("__arm__" ARM)
+detect_architecture("__aarch64__" ARM64)
+
+if (ARCHITECTURE STREQUAL x86_64)
+ option(ENABLE_JIT "Enable x64 JIT recompiler" ON)
+endif()
+
+if (ENABLE_JIT)
+ add_definitions(-DJIT_ENABLED)
+endif()
+
if (CMAKE_BUILD_TYPE STREQUAL Release)
option(ENABLE_LTO "Enable link-time optimization" ON)
else()
diff --git a/src/ARM.cpp b/src/ARM.cpp
index 6cc80c0..eb58d02 100644
--- a/src/ARM.cpp
+++ b/src/ARM.cpp
@@ -80,15 +80,8 @@ ARMv4::ARMv4() : ARM(1)
//
}
-namespace ARMJIT {extern int instructionPopularityARM[ARMInstrInfo::ak_Count];}
-
void ARM::Reset()
{
- FILE* blabla = fopen("fhhg", "w");
- for (int i = 0; i < ARMInstrInfo::ak_Count; i++)
- fprintf(blabla, "%d -> %dx\n", i, ARMJIT::instructionPopularityARM[i]);
- fclose(blabla);
-
Cycles = 0;
Halted = 0;
@@ -548,6 +541,7 @@ void ARMv5::Execute()
Halted = 0;
}
+#ifdef JIT_ENABLED
void ARMv5::ExecuteJIT()
{
if (Halted)
@@ -599,6 +593,7 @@ void ARMv5::ExecuteJIT()
if (Halted == 2)
Halted = 0;
}
+#endif
void ARMv4::Execute()
{
@@ -677,6 +672,7 @@ void ARMv4::Execute()
Halted = 0;
}
+#ifdef JIT_ENABLED
void ARMv4::ExecuteJIT()
{
if (Halted)
@@ -728,4 +724,5 @@ void ARMv4::ExecuteJIT()
if (Halted == 2)
Halted = 0;
-} \ No newline at end of file
+}
+#endif \ No newline at end of file
diff --git a/src/ARM.h b/src/ARM.h
index 0544301..ecdf5b4 100644
--- a/src/ARM.h
+++ b/src/ARM.h
@@ -52,7 +52,9 @@ public:
}
virtual void Execute() = 0;
+#ifdef ENABLE_JIT
virtual void ExecuteJIT() = 0;
+#endif
bool CheckCondition(u32 code)
{
@@ -152,7 +154,9 @@ public:
void DataAbort();
void Execute();
+#ifdef JIT_ENABLED
void ExecuteJIT();
+#endif
// all code accesses are forced nonseq 32bit
u32 CodeRead32(u32 addr, bool branch);
@@ -271,7 +275,9 @@ public:
void JumpTo(u32 addr, bool restorecpsr = false);
void Execute();
+#ifdef JIT_ENABLED
void ExecuteJIT();
+#endif
u16 CodeRead16(u32 addr)
{
diff --git a/src/ARMJIT_x64/ARMJIT_Compiler.cpp b/src/ARMJIT_x64/ARMJIT_Compiler.cpp
index fe23859..18cb27e 100644
--- a/src/ARMJIT_x64/ARMJIT_Compiler.cpp
+++ b/src/ARMJIT_x64/ARMJIT_Compiler.cpp
@@ -4,7 +4,10 @@
#include <assert.h>
+#include "../dolphin/CommonFuncs.h"
+
#ifdef _WIN32
+#include <windows.h>
#else
#include <sys/mman.h>
#include <unistd.h>
@@ -32,8 +35,6 @@ const int RegisterCache<Compiler, X64Reg>::NativeRegsAvailable =
#endif
;
-int instructionPopularityARM[ARMInstrInfo::ak_Count];
-
/*
We'll repurpose this .bss memory
@@ -42,29 +43,33 @@ u8 CodeMemory[1024 * 1024 * 32];
Compiler::Compiler()
{
-#ifdef _WIN32
-#else
- u64 pagesize = sysconf(_SC_PAGE_SIZE);
-#endif
-
- u8* pageAligned = (u8*)(((u64)CodeMemory & ~(pagesize - 1)) + pagesize);
- u64 alignedSize = (((u64)CodeMemory + sizeof(CodeMemory)) & ~(pagesize - 1)) - (u64)pageAligned;
-
-#ifdef _WIN32
-#else
- mprotect(pageAligned, alignedSize, PROT_EXEC | PROT_READ | PROT_WRITE);
-#endif
-
- region = pageAligned;
- region_size = alignedSize;
- total_region_size = region_size;
+ {
+ #ifdef _WIN32
+ SYSTEM_INFO sysInfo;
+ GetSystemInfo(&sysInfo);
+
+ u64 pageSize = (u64)sysInfo.dwPageSize;
+ #else
+ u64 pageSize = sysconf(_SC_PAGE_SIZE);
+ #endif
+
+ u8* pageAligned = (u8*)(((u64)CodeMemory & ~(pageSize - 1)) + pageSize);
+ u64 alignedSize = (((u64)CodeMemory + sizeof(CodeMemory)) & ~(pageSize - 1)) - (u64)pageAligned;
+
+ #ifdef _WIN32
+ DWORD dummy;
+ VirtualProtect(pageAligned, alignedSize, PAGE_EXECUTE_READWRITE, &dummy);
+ #else
+ mprotect(pageAligned, alignedSize, PROT_EXEC | PROT_READ | PROT_WRITE);
+ #endif
+
+ region = pageAligned;
+ region_size = alignedSize;
+ total_region_size = region_size;
+ }
ClearCodeSpace();
- SetCodePtr(pageAligned);
-
- memset(instructionPopularityARM, 0, sizeof(instructionPopularityARM));
-
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
@@ -118,7 +123,7 @@ Compiler::Compiler()
SetJumpTarget(und);
MOV(32, R(ABI_PARAM3), MComplex(RCPU, ABI_PARAM2, SCALE_4, offsetof(ARM, R_UND)));
RET();
- }
+ }
{
// RSCRATCH mode
// ABI_PARAM2 reg n
@@ -163,7 +168,10 @@ Compiler::Compiler()
RET();
}
- ResetStart = (void*)GetWritableCodePtr();
+ // move the region forward to prevent overwriting the generated functions
+ region_size -= GetWritableCodePtr() - region;
+ total_region_size = region_size;
+ region = GetWritableCodePtr();
}
void Compiler::LoadCPSR()
@@ -338,7 +346,7 @@ const Compiler::CompileFunc T_Comp[ARMInstrInfo::tk_Count] = {
void Compiler::Reset()
{
- SetCodePtr((u8*)ResetStart);
+ ClearCodeSpace();
}
CompiledBlock Compiler::CompileBlock(ARM* cpu, FetchedInstr instrs[], int instrsCount)
@@ -375,9 +383,6 @@ CompiledBlock Compiler::CompileBlock(ARM* cpu, FetchedInstr instrs[], int instrs
? T_Comp[CurInstr.Info.Kind]
: A_Comp[CurInstr.Info.Kind];
- if (!Thumb)
- instructionPopularityARM[CurInstr.Info.Kind] += comp == NULL;
-
if (comp == NULL || i == instrsCount - 1)
{
MOV(32, MDisp(RCPU, offsetof(ARM, R[15])), Imm32(R15));
diff --git a/src/ARMJIT_x64/ARMJIT_Compiler.h b/src/ARMJIT_x64/ARMJIT_Compiler.h
index cd58012..0ce7d8d 100644
--- a/src/ARMJIT_x64/ARMJIT_Compiler.h
+++ b/src/ARMJIT_x64/ARMJIT_Compiler.h
@@ -132,7 +132,6 @@ public:
return Gen::R(RegCache.Mapping[reg]);
}
- void* ResetStart;
void* MemoryFuncs9[3][2];
void* MemoryFuncs7[3][2][2];
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9401220..10428aa 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -30,19 +30,22 @@ add_library(core STATIC
SPU.cpp
Wifi.cpp
WifiAP.cpp
+)
- ARMJIT.cpp
- ARMJIT_x64/ARMJIT_Compiler.cpp
- ARMJIT_x64/ARMJIT_ALU.cpp
- ARMJIT_x64/ARMJIT_LoadStore.cpp
- ARMJIT_x64/ARMJIT_Branch.cpp
+if (ENABLE_JIT)
+ target_sources(core PRIVATE
+ ARMJIT.cpp
+ ARMJIT_x64/ARMJIT_Compiler.cpp
+ ARMJIT_x64/ARMJIT_ALU.cpp
+ ARMJIT_x64/ARMJIT_LoadStore.cpp
+ ARMJIT_x64/ARMJIT_Branch.cpp
- dolphin/CommonFuncs.cpp
- dolphin/x64ABI.cpp
- dolphin/x64CPUDetect.cpp
- dolphin/x64Emitter.cpp
- dolphin/MemoryUtil.cpp
-)
+ dolphin/CommonFuncs.cpp
+ dolphin/x64ABI.cpp
+ dolphin/x64CPUDetect.cpp
+ dolphin/x64Emitter.cpp
+ )
+endif()
if (WIN32)
target_link_libraries(core ole32 comctl32 ws2_32 opengl32)
diff --git a/src/CP15.cpp b/src/CP15.cpp
index f232bec..e6e91c3 100644
--- a/src/CP15.cpp
+++ b/src/CP15.cpp
@@ -812,7 +812,9 @@ void ARMv5::DataWrite8(u32 addr, u8 val)
{
DataCycles = 1;
*(u8*)&ITCM[addr & 0x7FFF] = val;
+#ifdef JIT_ENABLED
ARMJIT::cache.ARM9_ITCM[(addr & 0x7FFF) >> 1] = NULL;
+#endif
return;
}
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
@@ -834,7 +836,9 @@ void ARMv5::DataWrite16(u32 addr, u16 val)
{
DataCycles = 1;
*(u16*)&ITCM[addr & 0x7FFF] = val;
+#ifdef JIT_ENABLED
ARMJIT::cache.ARM9_ITCM[(addr & 0x7FFF) >> 1] = NULL;
+#endif
return;
}
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
@@ -856,8 +860,10 @@ void ARMv5::DataWrite32(u32 addr, u32 val)
{
DataCycles = 1;
*(u32*)&ITCM[addr & 0x7FFF] = val;
+#ifdef JIT_ENABLED
ARMJIT::cache.ARM9_ITCM[(addr & 0x7FFF) >> 1] = NULL;
ARMJIT::cache.ARM9_ITCM[((addr + 2) & 0x7FFF) >> 1] = NULL;
+#endif
return;
}
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
@@ -879,8 +885,10 @@ void ARMv5::DataWrite32S(u32 addr, u32 val)
{
DataCycles += 1;
*(u32*)&ITCM[addr & 0x7FFF] = val;
- ARMJIT::cache.ARM9_ITCM[(addr & 0x7FFF) / 2] = NULL;
- ARMJIT::cache.ARM9_ITCM[(addr & 0x7FFF) / 2 + 1] = NULL;
+#ifdef JIT_ENABLED
+ ARMJIT::cache.ARM9_ITCM[(addr & 0x7FFF) >> 1] = NULL;
+ ARMJIT::cache.ARM9_ITCM[((addr & 0x7FFF) >> 1) + 1] = NULL;
+#endif
return;
}
if (addr >= DTCMBase && addr < (DTCMBase + DTCMSize))
diff --git a/src/Config.cpp b/src/Config.cpp
index 37b701c..3cff0ed 100644
--- a/src/Config.cpp
+++ b/src/Config.cpp
@@ -34,8 +34,10 @@ int Threaded3D;
int GL_ScaleFactor;
int GL_Antialias;
+#ifdef JIT_ENABLED
bool JIT_Enable = false;
int JIT_MaxBlockSize = 12;
+#endif
ConfigEntry ConfigFile[] =
{
@@ -45,8 +47,10 @@ ConfigEntry ConfigFile[] =
{"GL_ScaleFactor", 0, &GL_ScaleFactor, 1, NULL, 0},
{"GL_Antialias", 0, &GL_Antialias, 0, NULL, 0},
+#ifdef JIT_ENABLED
{"JIT_Enable", 0, &JIT_Enable, 0, NULL, 0},
{"JIT_MaxBlockSize", 0, &JIT_MaxBlockSize, 10, NULL, 0},
+#endif
{"", -1, NULL, 0, NULL, 0}
};
diff --git a/src/Config.h b/src/Config.h
index 18a7910..c13eae3 100644
--- a/src/Config.h
+++ b/src/Config.h
@@ -46,8 +46,10 @@ extern int Threaded3D;
extern int GL_ScaleFactor;
extern int GL_Antialias;
+#ifdef JIT_ENABLED
extern bool JIT_Enable;
extern int JIT_MaxBlockSize;
+#endif
}
diff --git a/src/NDS.cpp b/src/NDS.cpp
index 4b50d9c..62a52aa 100644
--- a/src/NDS.cpp
+++ b/src/NDS.cpp
@@ -162,7 +162,9 @@ bool Init()
ARM9 = new ARMv5();
ARM7 = new ARMv4();
+#ifdef JIT_ENABLED
ARMJIT::Init();
+#endif
DMAs[0] = new DMA(0, 0);
DMAs[1] = new DMA(0, 1);
@@ -194,7 +196,9 @@ void DeInit()
delete ARM9;
delete ARM7;
+#ifdef JIT_ENABLED
ARMJIT::DeInit();
+#endif
for (int i = 0; i < 8; i++)
delete DMAs[i];
@@ -524,7 +528,9 @@ void Reset()
KeyCnt = 0;
RCnt = 0;
+#ifdef JIT_ENABLED
ARMJIT::InvalidateBlockCache();
+#endif
NDSCart::Reset();
GBACart::Reset();
@@ -741,10 +747,12 @@ bool DoSavestate(Savestate* file)
GPU::SetPowerCnt(PowerControl9);
}
+#ifdef JIT_ENABLED
if (!file->Saving)
{
ARMJIT::InvalidateBlockCache();
}
+#endif
return true;
}
@@ -864,9 +872,11 @@ u32 RunFrame()
}
else
{
+#ifdef JIT_ENABLED
if (EnableJIT)
ARM9->ExecuteJIT();
else
+#endif
ARM9->Execute();
}
@@ -889,9 +899,11 @@ u32 RunFrame()
}
else
{
+#ifdef JIT_ENABLED
if (EnableJIT)
ARM7->ExecuteJIT();
else
+#endif
ARM7->Execute();
}
@@ -924,9 +936,11 @@ u32 RunFrame()
u32 RunFrame()
{
+#ifdef JIT_ENABLED
if (Config::JIT_Enable)
return RunFrame<true>();
else
+#endif
return RunFrame<false>();
}
@@ -1849,7 +1863,9 @@ u32 ARM9Read32(u32 addr)
void ARM9Write8(u32 addr, u8 val)
{
+#ifdef JIT_ENABLED
ARMJIT::Invalidate16(0, addr);
+#endif
switch (addr & 0xFF000000)
{
@@ -1901,7 +1917,9 @@ void ARM9Write8(u32 addr, u8 val)
void ARM9Write16(u32 addr, u16 val)
{
+#ifdef JIT_ENABLED
ARMJIT::Invalidate16(0, addr);
+#endif
switch (addr & 0xFF000000)
{
@@ -1969,7 +1987,9 @@ void ARM9Write16(u32 addr, u16 val)
void ARM9Write32(u32 addr, u32 val)
{
+#ifdef JIT_ENABLED
ARMJIT::Invalidate32(0, addr);
+#endif
switch (addr & 0xFF000000)
{
@@ -2264,7 +2284,9 @@ u32 ARM7Read32(u32 addr)
void ARM7Write8(u32 addr, u8 val)
{
+#ifdef JIT_ENABLED
ARMJIT::Invalidate16(1, addr);
+#endif
switch (addr & 0xFF800000)
{
@@ -2325,7 +2347,9 @@ void ARM7Write8(u32 addr, u8 val)
void ARM7Write16(u32 addr, u16 val)
{
+#ifdef JIT_ENABLED
ARMJIT::Invalidate16(1, addr);
+#endif
switch (addr & 0xFF800000)
{
@@ -2396,7 +2420,9 @@ void ARM7Write16(u32 addr, u16 val)
void ARM7Write32(u32 addr, u32 val)
{
+#ifdef JIT_ENABLED
ARMJIT::Invalidate32(1, addr);
+#endif
switch (addr & 0xFF800000)
{
diff --git a/src/dolphin/CodeBlock.h b/src/dolphin/CodeBlock.h
index 31a8d93..e71cf6d 100644
--- a/src/dolphin/CodeBlock.h
+++ b/src/dolphin/CodeBlock.h
@@ -9,7 +9,6 @@
#include "Assert.h"
#include "../types.h"
-#include "MemoryUtil.h"
namespace Common
{
@@ -41,8 +40,6 @@ public:
CodeBlock() = default;
virtual ~CodeBlock()
{
- if (region)
- FreeCodeSpace();
}
CodeBlock(const CodeBlock&) = delete;
CodeBlock& operator=(const CodeBlock&) = delete;
diff --git a/src/libui_sdl/DlgEmuSettings.cpp b/src/libui_sdl/DlgEmuSettings.cpp
index 116d2da..46f5f9f 100644
--- a/src/libui_sdl/DlgEmuSettings.cpp
+++ b/src/libui_sdl/DlgEmuSettings.cpp
@@ -38,8 +38,10 @@ uiWindow* win;
uiCheckbox* cbDirectBoot;
+#ifdef JIT_ENABLED
uiCheckbox* cbJITEnabled;
uiEntry* enJITMaxBlockSize;
+#endif
int OnCloseWindow(uiWindow* window, void* blarg)
{
@@ -57,13 +59,17 @@ void OnOk(uiButton* btn, void* blarg)
{
Config::DirectBoot = uiCheckboxChecked(cbDirectBoot);
+#ifdef JIT_ENABLED
Config::JIT_Enable = uiCheckboxChecked(cbJITEnabled);
- long blockSize = strtol(uiEntryText(enJITMaxBlockSize), NULL, 10);
+ char* maxBlockSizeStr = uiEntryText(enJITMaxBlockSize);
+ long blockSize = strtol(maxBlockSizeStr, NULL, 10);
+ uiFreeText(maxBlockSizeStr);
if (blockSize < 1)
blockSize = 1;
if (blockSize > 32)
blockSize = 32;
Config::JIT_MaxBlockSize = blockSize;
+#endif
Config::Save();
@@ -73,6 +79,7 @@ void OnOk(uiButton* btn, void* blarg)
ApplyNewSettings(4);
}
+#ifdef JIT_ENABLED
void OnJITStateChanged(uiCheckbox* cb, void* blarg)
{
if (uiCheckboxChecked(cb))
@@ -80,6 +87,7 @@ void OnJITStateChanged(uiCheckbox* cb, void* blarg)
else
uiControlDisable(uiControl(enJITMaxBlockSize));
}
+#endif
void Open()
{
@@ -90,7 +98,7 @@ void Open()
}
opened = true;
- win = uiNewWindow("Emu settings - melonDS", 300, 170, 0, 0, 0);
+ win = uiNewWindow("Emu settings - melonDS", 300, 50, 0, 0, 0);
uiWindowSetMargined(win, 1);
uiWindowOnClosing(win, OnCloseWindow, NULL);
@@ -105,6 +113,7 @@ void Open()
uiBoxAppend(in_ctrl, uiControl(cbDirectBoot), 0);
}
+#ifdef JIT_ENABLED
{
uiLabel* dummy = uiNewLabel("");
uiBoxAppend(top, uiControl(dummy), 0);
@@ -133,6 +142,12 @@ void Open()
uiBoxAppend(row, uiControl(enJITMaxBlockSize), 0);
}
}
+#endif
+
+ {
+ uiLabel* dummy = uiNewLabel("");
+ uiBoxAppend(top, uiControl(dummy), 0);
+ }
{
uiBox* in_ctrl = uiNewHorizontalBox();
@@ -153,6 +168,7 @@ void Open()
uiCheckboxSetChecked(cbDirectBoot, Config::DirectBoot);
+#ifdef JIT_ENABLED
uiCheckboxSetChecked(cbJITEnabled, Config::JIT_Enable);
{
char maxBlockSizeStr[10];
@@ -160,6 +176,7 @@ void Open()
uiEntrySetText(enJITMaxBlockSize, maxBlockSizeStr);
}
OnJITStateChanged(cbJITEnabled, NULL);
+#endif
uiControlShow(uiControl(win));
}
diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp
index d6809c3..af05d7a 100644
--- a/src/libui_sdl/main.cpp
+++ b/src/libui_sdl/main.cpp
@@ -2411,8 +2411,10 @@ void ApplyNewSettings(int type)
}
else if (type == 4)
{
+#ifdef JIT_ENABLED
if (Config::JIT_Enable)
ARMJIT::InvalidateBlockCache();
+#endif
}
EmuRunning = prevstatus;