Debuggable assembly generator

This commit is contained in:
tevador 2018-12-21 21:09:55 +01:00
parent fce6e75689
commit 55afe9646f
5 changed files with 34 additions and 8 deletions

View file

@ -49,6 +49,7 @@ namespace RandomX {
}
void CompiledVirtualMachine::execute() {
//executeProgram(reg, mem, scratchpad, readDataset);
compiler.getProgramFunc()(reg, mem, scratchpad);
#ifdef TRACE
for (int32_t i = InstructionCount - 1; i >= 0; --i) {

View file

@ -34,9 +34,9 @@ namespace RandomX {
return compiler.getCode();
}
private:
JitCompilerX86 compiler;
#ifdef TRACE
convertible_t tracepad[InstructionCount];
#endif
JitCompilerX86 compiler;
};
}

View file

@ -106,6 +106,6 @@ namespace RandomX {
typedef void(*ProgramFunc)(RegisterFile&, MemoryRegisters&, convertible_t*);
extern "C" {
void executeProgram(RegisterFile& registerFile, MemoryRegisters& memory, convertible_t* scratchpad);
void executeProgram(RegisterFile&, MemoryRegisters&, convertible_t*, DatasetReadFunc);
}
}

View file

@ -71,7 +71,7 @@ executeProgram PROC
push r13
push r14
push r15
sub rsp, 72
sub rsp, 64
movdqu xmmword ptr [rsp+48], xmm6
movdqu xmmword ptr [rsp+32], xmm7
movdqu xmmword ptr [rsp+16], xmm8
@ -81,6 +81,7 @@ executeProgram PROC
push rcx ; RegisterFile& registerFile
mov rbx, rdx ; MemoryRegisters& memory
mov rsi, r8 ; convertible_t& scratchpad
push r9
mov rbp, rsp ; beginning of VM stack
mov rdi, 1048576 ; number of VM instructions to execute
@ -115,6 +116,7 @@ rx_finish:
; save VM register values
pop rcx
pop rcx
mov qword ptr [rcx+0], r8
mov qword ptr [rcx+8], r9
mov qword ptr [rcx+16], r10
@ -137,7 +139,7 @@ rx_finish:
movdqu xmm8, xmmword ptr [rsp+16]
movdqu xmm7, xmmword ptr [rsp+32]
movdqu xmm6, xmmword ptr [rsp+48]
add rsp, 72
add rsp, 64
pop r15
pop r14
pop r13
@ -150,11 +152,12 @@ rx_finish:
; return
ret 0
rx_read_dataset_light:
push rdx
rx_read_dataset:
push r8
push r9
push r10
push r11
mov rdx, rbx
movd qword ptr [rsp - 8], xmm1
movd qword ptr [rsp - 16], xmm2
sub rsp, 48
@ -165,10 +168,10 @@ rx_read_dataset_light:
pop r11
pop r10
pop r9
pop rdx
pop r8
ret 0
rx_read_dataset:
rx_read_dataset_full:
mov edx, dword ptr [rbx] ; ma
mov rax, qword ptr [rbx+8] ; dataset
mov rax, qword ptr [rax+rdx]

View file

@ -110,6 +110,22 @@ private:
std::atomic<uint64_t> hash[4];
};
void generateAsm(int nonce) {
uint64_t hash[4];
unsigned char blockTemplate[] = {
0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca,
0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09
};
int* noncePtr = (int*)(blockTemplate + 39);
*noncePtr = nonce;
blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
RandomX::AssemblyGeneratorX86 asmX86;
asmX86.generateProgram(hash);
asmX86.printCode(std::cout);
}
void mine(RandomX::VirtualMachine* vm, std::atomic<int>& atomicNonce, AtomicHash& result, int noncesCount, int thread) {
uint64_t hash[4];
unsigned char blockTemplate[] = {
@ -128,6 +144,7 @@ void mine(RandomX::VirtualMachine* vm, std::atomic<int>& atomicNonce, AtomicHash
int spIndex = ((uint8_t*)hash)[24] | ((((uint8_t*)hash)[25] & 63) << 8);
vm->initializeScratchpad(spIndex);
vm->initializeProgram(hash);
//dump((char*)((RandomX::CompiledVirtualMachine*)vm)->getProgram(), RandomX::CodeSize, "code-1337-jmp.txt");
vm->execute();
vm->getResult(hash);
result.xorWith(hash);
@ -150,6 +167,11 @@ int main(int argc, char** argv) {
readIntOption("--threads", argc, argv, threadCount, 1);
readIntOption("--nonces", argc, argv, programCount, 1000);
if (genAsm) {
generateAsm(programCount);
return 0;
}
std::atomic<int> atomicNonce(0);
AtomicHash result;
std::vector<RandomX::VirtualMachine*> vms;