2018-12-18 21:00:58 +00:00
|
|
|
/*
|
2019-05-18 12:21:47 +00:00
|
|
|
Copyright (c) 2018-2019, tevador <tevador@gmail.com>
|
2018-12-18 21:00:58 +00:00
|
|
|
|
2019-05-18 12:21:47 +00:00
|
|
|
All rights reserved.
|
2018-12-18 21:00:58 +00:00
|
|
|
|
2019-05-18 12:21:47 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the copyright holder nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
2018-12-18 21:00:58 +00:00
|
|
|
|
2019-05-18 12:21:47 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-12-18 21:00:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-04-20 14:53:06 +00:00
|
|
|
#include <cstdint>
|
2018-12-18 21:00:58 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <vector>
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "common.hpp"
|
2018-12-18 21:00:58 +00:00
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
namespace randomx {
|
2018-12-18 21:00:58 +00:00
|
|
|
|
2019-02-09 14:45:26 +00:00
|
|
|
class Program;
|
2019-04-17 14:18:02 +00:00
|
|
|
class ProgramConfiguration;
|
2019-04-12 17:36:08 +00:00
|
|
|
class SuperscalarProgram;
|
2018-12-18 21:00:58 +00:00
|
|
|
class JitCompilerX86;
|
2019-04-20 14:53:06 +00:00
|
|
|
class Instruction;
|
2018-12-18 21:00:58 +00:00
|
|
|
|
2019-03-17 22:09:11 +00:00
|
|
|
typedef void(JitCompilerX86::*InstructionGeneratorX86)(Instruction&, int);
|
2018-12-18 21:00:58 +00:00
|
|
|
|
|
|
|
constexpr uint32_t CodeSize = 64 * 1024;
|
|
|
|
|
|
|
|
class JitCompilerX86 {
|
|
|
|
public:
|
|
|
|
JitCompilerX86();
|
2019-04-06 10:00:56 +00:00
|
|
|
~JitCompilerX86();
|
2019-04-17 14:18:02 +00:00
|
|
|
void generateProgram(Program&, ProgramConfiguration&);
|
2019-04-26 14:05:30 +00:00
|
|
|
void generateProgramLight(Program&, ProgramConfiguration&, uint32_t);
|
2019-04-06 10:00:56 +00:00
|
|
|
template<size_t N>
|
2019-04-20 09:08:01 +00:00
|
|
|
void generateSuperscalarHash(SuperscalarProgram (&programs)[N], std::vector<uint64_t> &);
|
|
|
|
void generateDatasetInitCode();
|
2019-04-28 16:17:29 +00:00
|
|
|
ProgramFunc* getProgramFunc() {
|
|
|
|
return (ProgramFunc*)code;
|
2018-12-18 21:00:58 +00:00
|
|
|
}
|
2019-04-28 16:17:29 +00:00
|
|
|
DatasetInitFunc* getDatasetInitFunc() {
|
|
|
|
return (DatasetInitFunc*)code;
|
2019-04-06 10:00:56 +00:00
|
|
|
}
|
2018-12-18 21:00:58 +00:00
|
|
|
uint8_t* getCode() {
|
|
|
|
return code;
|
|
|
|
}
|
2019-01-12 15:05:09 +00:00
|
|
|
size_t getCodeSize();
|
2018-12-18 21:00:58 +00:00
|
|
|
private:
|
|
|
|
static InstructionGeneratorX86 engine[256];
|
2019-03-17 22:09:11 +00:00
|
|
|
std::vector<int32_t> instructionOffsets;
|
2019-04-30 08:20:28 +00:00
|
|
|
RegisterUsage registerUsage[RegistersCount];
|
2018-12-18 21:00:58 +00:00
|
|
|
uint8_t* code;
|
|
|
|
int32_t codePos;
|
2019-01-27 09:52:30 +00:00
|
|
|
|
2019-04-17 14:18:02 +00:00
|
|
|
void generateProgramPrologue(Program&, ProgramConfiguration&);
|
2019-03-21 19:44:59 +00:00
|
|
|
void generateProgramEpilogue(Program&);
|
2019-01-27 09:52:30 +00:00
|
|
|
void genAddressReg(Instruction&, bool);
|
2019-05-10 14:43:24 +00:00
|
|
|
void genAddressRegDst(Instruction&);
|
2019-01-27 09:52:30 +00:00
|
|
|
void genAddressImm(Instruction&);
|
|
|
|
void genSIB(int scale, int index, int base);
|
|
|
|
|
2019-03-17 22:09:11 +00:00
|
|
|
void generateCode(Instruction&, int);
|
2019-04-20 09:08:01 +00:00
|
|
|
void generateSuperscalarCode(Instruction &, std::vector<uint64_t> &);
|
2018-12-18 21:00:58 +00:00
|
|
|
|
|
|
|
void emitByte(uint8_t val) {
|
|
|
|
code[codePos] = val;
|
|
|
|
codePos++;
|
|
|
|
}
|
|
|
|
|
2019-01-27 09:52:30 +00:00
|
|
|
void emit32(uint32_t val) {
|
2019-05-04 17:40:25 +00:00
|
|
|
memcpy(code + codePos, &val, sizeof val);
|
|
|
|
codePos += sizeof val;
|
2019-01-27 09:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void emit64(uint64_t val) {
|
2019-05-04 17:40:25 +00:00
|
|
|
memcpy(code + codePos, &val, sizeof val);
|
|
|
|
codePos += sizeof val;
|
2019-01-27 09:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
void emit(const uint8_t (&src)[N]) {
|
2019-04-06 10:00:56 +00:00
|
|
|
emit(src, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
void emit(const uint8_t* src, size_t count) {
|
|
|
|
memcpy(code + codePos, src, count);
|
|
|
|
codePos += count;
|
2018-12-18 21:00:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-28 14:42:45 +00:00
|
|
|
void h_IADD_RS(Instruction&, int);
|
|
|
|
void h_IADD_M(Instruction&, int);
|
|
|
|
void h_ISUB_R(Instruction&, int);
|
|
|
|
void h_ISUB_M(Instruction&, int);
|
|
|
|
void h_IMUL_R(Instruction&, int);
|
|
|
|
void h_IMUL_M(Instruction&, int);
|
|
|
|
void h_IMULH_R(Instruction&, int);
|
|
|
|
void h_IMULH_M(Instruction&, int);
|
|
|
|
void h_ISMULH_R(Instruction&, int);
|
|
|
|
void h_ISMULH_M(Instruction&, int);
|
|
|
|
void h_IMUL_RCP(Instruction&, int);
|
|
|
|
void h_INEG_R(Instruction&, int);
|
|
|
|
void h_IXOR_R(Instruction&, int);
|
|
|
|
void h_IXOR_M(Instruction&, int);
|
|
|
|
void h_IROR_R(Instruction&, int);
|
|
|
|
void h_IROL_R(Instruction&, int);
|
|
|
|
void h_ISWAP_R(Instruction&, int);
|
|
|
|
void h_FSWAP_R(Instruction&, int);
|
|
|
|
void h_FADD_R(Instruction&, int);
|
|
|
|
void h_FADD_M(Instruction&, int);
|
|
|
|
void h_FSUB_R(Instruction&, int);
|
|
|
|
void h_FSUB_M(Instruction&, int);
|
|
|
|
void h_FSCAL_R(Instruction&, int);
|
|
|
|
void h_FMUL_R(Instruction&, int);
|
|
|
|
void h_FDIV_M(Instruction&, int);
|
|
|
|
void h_FSQRT_R(Instruction&, int);
|
2019-04-29 21:38:23 +00:00
|
|
|
void h_CBRANCH(Instruction&, int);
|
2019-04-28 14:42:45 +00:00
|
|
|
void h_CFROUND(Instruction&, int);
|
|
|
|
void h_ISTORE(Instruction&, int);
|
|
|
|
void h_NOP(Instruction&, int);
|
2018-12-18 21:00:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|