Skip to content

Commit

Permalink
Split binary translation memory operations into two separate steps
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Oct 25, 2023
1 parent 3c1dc5f commit 0978a37
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 326 deletions.
1 change: 1 addition & 0 deletions lib/libriscv/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ namespace riscv
// Linear arena at start of memory (mmap-backed)
PageData* m_arena = nullptr;
size_t m_arena_pages = 0;
friend struct CPU<W>;
};
#include "memory_inline.hpp"
#include "memory_inline_pages.hpp"
Expand Down
37 changes: 14 additions & 23 deletions lib/libriscv/tr_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace riscv {
# define XLEN 64
#endif
#define HOST_UNKNOWN 0
#define HOST_AMD64 1
typedef union {
int32_t i32[2];
float f32[2];
Expand Down Expand Up @@ -53,39 +56,27 @@ typedef struct {
fp64reg fr[32];
} CPU;
#define PUREFUNC /*__attribute__((pure))*/
#define VERYALIGNED(x) __builtin_assume_aligned(x, 64)
#define PAGENO(x) ((addr_t)(x) >> 12)
#define PAGEOFF(x) ((addr_t)(x) & 0xFFF)
static struct CallbackTable {
uint8_t (*mem_ld8)(const CPU*, addr_t);
uint16_t (*mem_ld16)(const CPU*, addr_t);
uint32_t (*mem_ld32)(const CPU*, addr_t);
uint64_t (*mem_ld64)(const CPU*, addr_t);
void (*mem_st8) (const CPU*, addr_t, uint8_t);
void (*mem_st16)(const CPU*, addr_t, uint16_t);
void (*mem_st32)(const CPU*, addr_t, uint32_t);
void (*mem_st64)(const CPU*, addr_t, uint64_t);
const char* (*mem_ld)(const CPU*, addr_t) PUREFUNC;
char* (*mem_st) (const CPU*, addr_t) PUREFUNC;
void (*jump)(const CPU*, addr_t);
int (*syscall)(CPU*, addr_t);
void (*stop)(CPU*);
void (*ebreak)(CPU*);
void (*stop)(CPU*) PUREFUNC;
void (*ebreak)(CPU*) PUREFUNC;
void (*system)(CPU*, uint32_t);
void (*execute)(CPU*, uint32_t);
void (*exception)(CPU*, int);
float (*sqrtf32)(float);
double (*sqrtf64)(double);
float (*sqrtf32)(float) PUREFUNC;
double (*sqrtf64)(double) PUREFUNC;
} api;
static uint64_t* cur_insn;
static uint64_t* max_insn;
void* memcpy(void * restrict dst, const void * restrict src, unsigned len)
{
char *src8 = (char *)src;
char *dst8 = (char *)dst;
for (unsigned i = 0; i < len; i++)
dst8[i] = src8[i];
return dst;
}
// https://stackoverflow.com/questions/28868367/getting-the-high-part-of-64-bit-integer-multiplication
// As written by catid
static inline uint64_t MUL128(
Expand Down
10 changes: 2 additions & 8 deletions lib/libriscv/tr_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
namespace riscv {
template <int W>
struct CallbackTable {
uint8_t (*mem_read8)(CPU<W>&, address_type<W> addr);
uint16_t (*mem_read16)(CPU<W>&, address_type<W> addr);
uint32_t (*mem_read32)(CPU<W>&, address_type<W> addr);
uint64_t (*mem_read64)(CPU<W>&, address_type<W> addr);
void (*mem_write8) (CPU<W>&, address_type<W> addr, uint8_t);
void (*mem_write16)(CPU<W>&, address_type<W> addr, uint16_t);
void (*mem_write32)(CPU<W>&, address_type<W> addr, uint32_t);
void (*mem_write64)(CPU<W>&, address_type<W> addr, uint64_t);
const void* (*mem_read)(CPU<W>&, address_type<W> addr);
void* (*mem_write) (CPU<W>&, address_type<W> addr);
void (*jump)(CPU<W>&, address_type<W>);
int (*syscall)(CPU<W>&, address_type<W>);
void (*stop)(CPU<W>&);
Expand Down
15 changes: 12 additions & 3 deletions lib/libriscv/tr_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ static bool verbose()
{
return getenv("VERBOSE") != nullptr;
}
static std::string host_arch()
{
#ifdef __x86_64__
return "HOST_AMD64";
#else
return "HOST_UNKNOWN";
#endif
}

namespace riscv
{
std::string compile_command(int arch)
{
return compiler() + " -O2 -s -std=c99 -fPIC -shared -rdynamic -x c "
" -ffreestanding -fno-builtin -nostdlib -fexceptions "
+ "-DRISCV_TRANSLATION_DYLIB=" + std::to_string(arch)
+ " -pipe " + cflags();
" -fexceptions "
"-DRISCV_TRANSLATION_DYLIB=" + std::to_string(arch)
+ " -DARCH=" + host_arch() + ""
" -pipe " + cflags();
}

void*
Expand Down
Loading

0 comments on commit 0978a37

Please sign in to comment.