2015-07-22 00:08:49 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-30 15:34:51 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/memory_util.h"
|
2015-07-22 00:08:49 +00:00
|
|
|
|
|
|
|
// Everything that needs to generate code should inherit from this.
|
|
|
|
// You get memory management for free, plus, you can use all emitter functions without
|
|
|
|
// having to prefix them with gen-> or something similar.
|
|
|
|
// Example implementation:
|
|
|
|
// class JIT : public CodeBlock<ARMXEmitter> {}
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
class CodeBlock : public T, NonCopyable {
|
2015-07-22 00:08:49 +00:00
|
|
|
private:
|
|
|
|
// A privately used function to set the executable RAM space to something invalid.
|
2016-09-18 00:38:01 +00:00
|
|
|
// For debugging usefulness it should be used to set the RAM to a host specific breakpoint
|
|
|
|
// instruction
|
2015-07-22 00:08:49 +00:00
|
|
|
virtual void PoisonMemory() = 0;
|
|
|
|
|
|
|
|
protected:
|
2016-09-18 00:38:01 +00:00
|
|
|
u8* region;
|
2015-07-22 00:08:49 +00:00
|
|
|
size_t region_size;
|
|
|
|
|
|
|
|
public:
|
2016-09-18 00:38:01 +00:00
|
|
|
CodeBlock() : region(nullptr), region_size(0) {
|
|
|
|
}
|
|
|
|
virtual ~CodeBlock() {
|
|
|
|
if (region)
|
|
|
|
FreeCodeSpace();
|
|
|
|
}
|
2015-07-22 00:08:49 +00:00
|
|
|
|
|
|
|
// Call this before you generate any code.
|
2016-09-18 00:38:01 +00:00
|
|
|
void AllocCodeSpace(int size) {
|
2015-07-22 00:08:49 +00:00
|
|
|
region_size = size;
|
|
|
|
region = (u8*)AllocateExecutableMemory(region_size);
|
|
|
|
T::SetCodePtr(region);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always clear code space with breakpoints, so that if someone accidentally executes
|
|
|
|
// uninitialized, it just breaks into the debugger.
|
2016-09-18 00:38:01 +00:00
|
|
|
void ClearCodeSpace() {
|
2015-07-22 00:08:49 +00:00
|
|
|
PoisonMemory();
|
|
|
|
ResetCodePtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call this when shutting down. Don't rely on the destructor, even though it'll do the job.
|
2016-09-18 00:38:01 +00:00
|
|
|
void FreeCodeSpace() {
|
2015-07-22 00:08:49 +00:00
|
|
|
#ifdef __SYMBIAN32__
|
|
|
|
ResetExecutableMemory(region);
|
|
|
|
#else
|
|
|
|
FreeMemoryPages(region, region_size);
|
|
|
|
#endif
|
|
|
|
region = nullptr;
|
|
|
|
region_size = 0;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
bool IsInSpace(const u8* ptr) {
|
2015-07-22 00:08:49 +00:00
|
|
|
return (ptr >= region) && (ptr < (region + region_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot currently be undone. Will write protect the entire code region.
|
|
|
|
// Start over if you need to change the code (call FreeCodeSpace(), AllocCodeSpace()).
|
2016-09-18 00:38:01 +00:00
|
|
|
void WriteProtect() {
|
2015-07-22 00:08:49 +00:00
|
|
|
WriteProtectMemory(region, region_size, true);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void ResetCodePtr() {
|
2015-07-22 00:08:49 +00:00
|
|
|
T::SetCodePtr(region);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
size_t GetSpaceLeft() const {
|
2015-07-22 00:08:49 +00:00
|
|
|
return region_size - (T::GetCodePtr() - region);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
u8* GetBasePtr() {
|
2015-07-22 00:08:49 +00:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
size_t GetOffset(const u8* ptr) const {
|
2015-07-22 00:08:49 +00:00
|
|
|
return ptr - region;
|
|
|
|
}
|
|
|
|
};
|