1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 // Regular replacement funcs are just C functions. These take care of their
19 // own parameter parsing using the old school PARAM macros.
20 // The return value is the number of cycles to eat.
21 
22 // JIT replacefuncs can be for inline or "outline" replacement.
23 // With inline replacement, we recognize the call to the functions
24 // at jal time already. With outline replacement, we just replace the
25 // implementation.
26 
27 // In both cases the jit needs to know how much to subtract downcount.
28 //
29 // If the replacement func returned a positive number, this will be treated
30 // as the number of cycles to subtract.
31 // If the replacement func returns -1, it will be assumed that the subtraction
32 // was done by the replacement func.
33 
34 #pragma once
35 
36 #include <map>
37 
38 #include "Common/CommonTypes.h"
39 #include "Core/MIPS/JitCommon/JitCommon.h"
40 
41 typedef int (* ReplaceFunc)();
42 
43 enum {
44 	REPFLAG_ALLOWINLINE = 0x01,
45 	// Used to keep things around but disable them.
46 	REPFLAG_DISABLED = 0x02,
47 	// Note that this will re-execute in a function that loops at start.
48 	REPFLAG_HOOKENTER = 0x04,
49 	// Only hooks jr ra, so only use on funcs that have that.
50 	REPFLAG_HOOKEXIT = 0x08,
51 };
52 
53 // Kind of similar to HLE functions but with different data.
54 struct ReplacementTableEntry {
55 	const char *name;
56 	ReplaceFunc replaceFunc;
57 	MIPSComp::MIPSReplaceFunc jitReplaceFunc;
58 	int flags;
59 	s32 hookOffset;
60 };
61 
62 void Replacement_Init();
63 void Replacement_Shutdown();
64 
65 int GetNumReplacementFuncs();
66 std::vector<int> GetReplacementFuncIndexes(u64 hash, int funcSize);
67 const ReplacementTableEntry *GetReplacementFunc(int index);
68 
69 void WriteReplaceInstructions(u32 address, u64 hash, int size);
70 void RestoreReplacedInstruction(u32 address);
71 void RestoreReplacedInstructions(u32 startAddr, u32 endAddr);
72 bool GetReplacedOpAt(u32 address, u32 *op);
73 bool CanReplaceJalTo(u32 dest, const ReplacementTableEntry **entry, u32 *funcSize);
74 
75 // For savestates.  If you call SaveAndClearReplacements(), you must call RestoreSavedReplacements().
76 std::map<u32, u32> SaveAndClearReplacements();
77 void RestoreSavedReplacements(const std::map<u32, u32> &saved);
78