2018-12-13 22:11:55 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2018 tevador
|
|
|
|
|
|
|
|
This file is part of RandomX.
|
|
|
|
|
|
|
|
RandomX is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
RandomX is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with RandomX. If not, see<http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-12-15 22:13:17 +00:00
|
|
|
//#define TRACE
|
2019-01-11 15:53:52 +00:00
|
|
|
#define MAGIC_DIVISION
|
2018-12-13 22:11:55 +00:00
|
|
|
#include "AssemblyGeneratorX86.hpp"
|
|
|
|
#include "Pcg32.hpp"
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "instructions.hpp"
|
2019-01-11 13:08:21 +00:00
|
|
|
#ifdef MAGIC_DIVISION
|
|
|
|
#include "divideByConstantCodegen.h"
|
|
|
|
#endif
|
2018-12-13 22:11:55 +00:00
|
|
|
|
|
|
|
namespace RandomX {
|
|
|
|
|
2018-12-16 12:43:18 +00:00
|
|
|
static const char* regR[8] = { "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
|
|
|
|
static const char* regR32[8] = { "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" };
|
|
|
|
static const char* regF[8] = { "xmm8", "xmm9", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7" };
|
2018-12-13 22:11:55 +00:00
|
|
|
|
2019-01-08 13:50:31 +00:00
|
|
|
static const char* regMx = "rbp";
|
|
|
|
static const char* regIc = "ebx";
|
|
|
|
static const char* regIc8 = "bl";
|
|
|
|
static const char* regStackBeginAddr = "rdi";
|
2019-01-04 18:44:15 +00:00
|
|
|
static const char* regScratchpadAddr = "rsi";
|
|
|
|
|
2018-12-13 22:11:55 +00:00
|
|
|
void AssemblyGeneratorX86::generateProgram(const void* seed) {
|
|
|
|
asmCode.str(std::string()); //clear
|
|
|
|
Pcg32 gen(seed);
|
|
|
|
for (unsigned i = 0; i < sizeof(RegisterFile) / sizeof(Pcg32::result_type); ++i) {
|
|
|
|
gen();
|
|
|
|
}
|
|
|
|
Instruction instr;
|
|
|
|
for (unsigned i = 0; i < ProgramLength; ++i) {
|
|
|
|
for (unsigned j = 0; j < sizeof(instr) / sizeof(Pcg32::result_type); ++j) {
|
|
|
|
*(((uint32_t*)&instr) + j) = gen();
|
|
|
|
}
|
|
|
|
generateCode(instr, i);
|
|
|
|
asmCode << std::endl;
|
|
|
|
}
|
|
|
|
if(ProgramLength > 0)
|
|
|
|
asmCode << "\tjmp rx_i_0" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::generateCode(Instruction& instr, int i) {
|
|
|
|
asmCode << "rx_i_" << i << ": ;" << instr.getName() << std::endl;
|
2019-01-04 18:44:15 +00:00
|
|
|
asmCode << "\tdec " << regIc << std::endl;
|
2018-12-28 13:18:41 +00:00
|
|
|
asmCode << "\tjz rx_finish" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
auto generator = engine[instr.opcode];
|
|
|
|
(this->*generator)(instr, i);
|
|
|
|
}
|
|
|
|
|
2019-01-11 15:53:52 +00:00
|
|
|
void AssemblyGeneratorX86::gena(Instruction& instr, int i) {
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << "\txor " << regR[instr.rega % RegistersCount] << ", 0" << std::hex << instr.addra << "h" << std::dec << std::endl;
|
2019-01-04 18:44:15 +00:00
|
|
|
asmCode << "\tmov ecx, " << regR32[instr.rega % RegistersCount] << std::endl;
|
2019-01-08 13:50:31 +00:00
|
|
|
asmCode << "\ttest " << regIc8 << ", 63" << std::endl;
|
2019-01-07 16:44:43 +00:00
|
|
|
asmCode << "\tjnz short rx_body_" << i << std::endl;
|
2019-01-11 15:53:52 +00:00
|
|
|
if (instr.loca & 3) {
|
|
|
|
asmCode << "\tcall rx_read_l1" << std::endl;
|
|
|
|
asmCode << "rx_body_" << i << ":" << std::endl;
|
|
|
|
if ((instr.loca & 192) == 0)
|
|
|
|
asmCode << "\txor " << regMx << ", rcx" << std::endl;
|
|
|
|
asmCode << "\tand ecx, " << (ScratchpadL1 - 1) << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
2019-01-11 15:53:52 +00:00
|
|
|
else {
|
|
|
|
asmCode << "\tcall rx_read_l2" << std::endl;
|
|
|
|
asmCode << "rx_body_" << i << ":" << std::endl;
|
|
|
|
if ((instr.loca & 192) == 0)
|
|
|
|
asmCode << "\txor " << regMx << ", rcx" << std::endl;
|
|
|
|
asmCode << "\tand ecx, " << (ScratchpadL2 - 1) << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::genar(Instruction& instr, int i) {
|
|
|
|
gena(instr, i);
|
2019-01-08 11:19:19 +00:00
|
|
|
asmCode << "\tmov rax, qword ptr [" << regScratchpadAddr << "+rcx*8]" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 18:06:45 +00:00
|
|
|
|
2019-01-07 16:44:43 +00:00
|
|
|
void AssemblyGeneratorX86::genaf(Instruction& instr, int i) {
|
2019-01-11 15:53:52 +00:00
|
|
|
gena(instr, i);
|
2019-01-08 11:19:19 +00:00
|
|
|
asmCode << "\tcvtdq2pd xmm0, qword ptr [" << regScratchpadAddr << "+rcx*8]" << std::endl;
|
2018-12-31 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:53:52 +00:00
|
|
|
void AssemblyGeneratorX86::genbiashift(Instruction& instr, const char* instrx86) {
|
|
|
|
if (instr.locb & 1) {
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov rcx, " << regR[instr.regb % RegistersCount] << std::endl;
|
|
|
|
asmCode << "\t" << instrx86 << " rax, cl" << std::endl;
|
2019-01-11 15:53:52 +00:00
|
|
|
} else {
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << "\t" << instrx86 << " rax, " << (instr.imm8 & 63) << std::endl;;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 15:53:52 +00:00
|
|
|
void AssemblyGeneratorX86::genbia(Instruction& instr) {
|
|
|
|
if (instr.locb & 3) {
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << regR[instr.regb % RegistersCount] << std::endl;
|
2019-01-11 15:53:52 +00:00
|
|
|
} else {
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << instr.imm32 << std::endl;;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 15:53:52 +00:00
|
|
|
void AssemblyGeneratorX86::genbia32(Instruction& instr) {
|
|
|
|
if (instr.locb & 3) {
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << regR32[instr.regb % RegistersCount] << std::endl;
|
2019-01-11 15:53:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << instr.imm32 << std::endl;;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::genbf(Instruction& instr, const char* instrx86) {
|
2018-12-31 18:06:45 +00:00
|
|
|
asmCode << "\t" << instrx86 << " xmm0, " << regF[instr.regb % RegistersCount] << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 21:04:55 +00:00
|
|
|
void AssemblyGeneratorX86::gencr(Instruction& instr, bool rax = true) {
|
2018-12-13 22:11:55 +00:00
|
|
|
switch (instr.locc & 7)
|
|
|
|
{
|
|
|
|
case 0:
|
2019-01-10 21:04:55 +00:00
|
|
|
if(rax)
|
|
|
|
asmCode << "\tmov rcx, rax" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov eax, " << regR32[instr.regc % RegistersCount] << std::endl;
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << "\txor eax, 0" << std::hex << instr.addrc << "h" << std::dec << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tand eax, " << (ScratchpadL2 - 1) << std::endl;
|
2019-01-04 18:44:15 +00:00
|
|
|
asmCode << "\tmov qword ptr [" << regScratchpadAddr << " + rax * 8], rcx" << std::endl;
|
2018-12-15 22:13:17 +00:00
|
|
|
if (trace) {
|
2019-01-08 11:19:19 +00:00
|
|
|
asmCode << "\tmov qword ptr [" << regScratchpadAddr << " + " << regIc << " * 8 + 262136], rcx" << std::endl;
|
2018-12-15 22:13:17 +00:00
|
|
|
}
|
2018-12-13 22:11:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2019-01-10 21:04:55 +00:00
|
|
|
if (rax)
|
|
|
|
asmCode << "\tmov rcx, rax" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov eax, " << regR32[instr.regc % RegistersCount] << std::endl;
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << "\txor eax, 0" << std::hex << instr.addrc << "h" << std::dec << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tand eax, " << (ScratchpadL1 - 1) << std::endl;
|
2019-01-04 18:44:15 +00:00
|
|
|
asmCode << "\tmov qword ptr [" << regScratchpadAddr << " + rax * 8], rcx" << std::endl;
|
2018-12-15 22:13:17 +00:00
|
|
|
if (trace) {
|
2019-01-08 11:19:19 +00:00
|
|
|
asmCode << "\tmov qword ptr [" << regScratchpadAddr << " + " << regIc << " * 8 + 262136], rcx" << std::endl;
|
2018-12-15 22:13:17 +00:00
|
|
|
}
|
2018-12-13 22:11:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2019-01-10 21:04:55 +00:00
|
|
|
asmCode << "\tmov " << regR[instr.regc % RegistersCount] << ", " << (rax ? "rax" : "rcx") << std::endl;
|
2018-12-15 22:13:17 +00:00
|
|
|
if (trace) {
|
2019-01-10 21:04:55 +00:00
|
|
|
asmCode << "\tmov qword ptr [" << regScratchpadAddr << " + " << regIc << " * 8 + 262136], " << (rax ? "rax" : "rcx") << std::endl;
|
2018-12-15 22:13:17 +00:00
|
|
|
}
|
2019-01-04 18:44:15 +00:00
|
|
|
return;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:50:31 +00:00
|
|
|
void AssemblyGeneratorX86::gencf(Instruction& instr, bool move = true) {
|
|
|
|
if(move)
|
|
|
|
asmCode << "\tmovaps " << regF[instr.regc % RegistersCount] << ", xmm0" << std::endl;
|
2019-01-10 21:04:55 +00:00
|
|
|
const char* store = (instr.locc & 128) ? "movhpd" : "movlpd";
|
2018-12-13 22:11:55 +00:00
|
|
|
switch (instr.locc & 7)
|
|
|
|
{
|
2018-12-31 18:06:45 +00:00
|
|
|
case 4:
|
|
|
|
asmCode << "\tmov eax, " << regR32[instr.regc % RegistersCount] << std::endl;
|
|
|
|
asmCode << "\txor eax, 0" << std::hex << instr.addrc << "h" << std::dec << std::endl;
|
|
|
|
asmCode << "\tand eax, " << (ScratchpadL2 - 1) << std::endl;
|
2019-01-04 18:44:15 +00:00
|
|
|
asmCode << "\t" << store << " qword ptr [" << regScratchpadAddr << " + rax * 8], " << regF[instr.regc % RegistersCount] << std::endl;
|
2018-12-31 18:06:45 +00:00
|
|
|
break;
|
2018-12-13 22:11:55 +00:00
|
|
|
|
2018-12-31 18:06:45 +00:00
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
asmCode << "\tmov eax, " << regR32[instr.regc % RegistersCount] << std::endl;
|
|
|
|
asmCode << "\txor eax, 0" << std::hex << instr.addrc << "h" << std::dec << std::endl;
|
|
|
|
asmCode << "\tand eax, " << (ScratchpadL1 - 1) << std::endl;
|
2019-01-04 18:44:15 +00:00
|
|
|
asmCode << "\t" << store << " qword ptr [" << regScratchpadAddr << " + rax * 8], " << regF[instr.regc % RegistersCount] << std::endl;
|
2018-12-31 18:06:45 +00:00
|
|
|
break;
|
2018-12-15 22:13:17 +00:00
|
|
|
}
|
|
|
|
if (trace) {
|
2019-01-08 11:19:19 +00:00
|
|
|
asmCode << "\t" << store << " qword ptr [" << regScratchpadAddr << " + " << regIc << " * 8 + 262136], " << regF[instr.regc % RegistersCount] << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_ADD_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tadd rax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_ADD_32(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tadd eax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia32(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_SUB_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tsub rax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_SUB_32(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tsub eax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia32(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_MUL_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\timul rax, ";
|
|
|
|
if ((instr.locb & 7) >= 6) {
|
|
|
|
asmCode << "rax, ";
|
|
|
|
}
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_MULH_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov rcx, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmul rcx" << std::endl;
|
|
|
|
asmCode << "\tmov rax, rdx" << std::endl;
|
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_MUL_32(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov ecx, eax" << std::endl;
|
|
|
|
asmCode << "\tmov eax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia32(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\timul rax, rcx" << std::endl;
|
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_IMUL_32(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmovsxd rcx, eax" << std::endl;
|
|
|
|
if ((instr.locb & 7) >= 6) {
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << "\tmov rax, " << instr.imm32 << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
asmCode << "\tmovsxd rax, " << regR32[instr.regb % RegistersCount] << std::endl;
|
|
|
|
}
|
|
|
|
asmCode << "\timul rax, rcx" << std::endl;
|
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_IMULH_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov rcx, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\timul rcx" << std::endl;
|
|
|
|
asmCode << "\tmov rax, rdx" << std::endl;
|
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_DIV_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-11 15:53:52 +00:00
|
|
|
if (instr.locb & 3) {
|
2019-01-11 13:08:21 +00:00
|
|
|
#ifdef MAGIC_DIVISION
|
|
|
|
if (instr.imm32 != 0) {
|
|
|
|
uint32_t divisor = instr.imm32;
|
|
|
|
asmCode << "\t; magic divide by " << divisor << std::endl;
|
|
|
|
if (divisor & (divisor - 1)) {
|
|
|
|
magicu_info mi = compute_unsigned_magic_info(divisor, sizeof(uint64_t) * 8);
|
|
|
|
if (mi.pre_shift > 0)
|
|
|
|
asmCode << "\tshr rax, " << mi.pre_shift << std::endl;
|
|
|
|
if (mi.increment) {
|
|
|
|
asmCode << "\tadd rax, 1" << std::endl;
|
|
|
|
asmCode << "\tsbb rax, 0" << std::endl;
|
|
|
|
}
|
|
|
|
asmCode << "\tmov rcx, " << mi.multiplier << std::endl;
|
|
|
|
asmCode << "\tmul rcx" << std::endl;
|
|
|
|
asmCode << "\tmov rax, rdx" << std::endl;
|
|
|
|
if (mi.post_shift > 0)
|
|
|
|
asmCode << "\tshr rax, " << mi.post_shift << std::endl;
|
|
|
|
}
|
|
|
|
else { //divisor is a power of two
|
|
|
|
int shift = 0;
|
|
|
|
while (divisor >>= 1)
|
|
|
|
++shift;
|
|
|
|
if(shift > 0)
|
|
|
|
asmCode << "\tshr rax, " << shift << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2018-12-20 17:36:09 +00:00
|
|
|
if (instr.imm32 == 0) {
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov ecx, 1" << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << "\tmov ecx, " << instr.imm32 << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
2019-01-11 13:08:21 +00:00
|
|
|
#endif
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-12-21 21:41:35 +00:00
|
|
|
asmCode << "\tmov ecx, 1" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tmov edx, " << regR32[instr.regb % RegistersCount] << std::endl;
|
|
|
|
asmCode << "\ttest edx, edx" << std::endl;
|
|
|
|
asmCode << "\tcmovne ecx, edx" << std::endl;
|
2019-01-11 13:08:21 +00:00
|
|
|
#ifdef MAGIC_DIVISION
|
|
|
|
asmCode << "\txor edx, edx" << std::endl;
|
|
|
|
asmCode << "\tdiv rcx" << std::endl;
|
|
|
|
#endif
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
2019-01-11 13:08:21 +00:00
|
|
|
#ifndef MAGIC_DIVISION
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\txor edx, edx" << std::endl;
|
|
|
|
asmCode << "\tdiv rcx" << std::endl;
|
2019-01-11 13:08:21 +00:00
|
|
|
#endif
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_IDIV_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-11 15:53:52 +00:00
|
|
|
if (instr.locb & 3) {
|
2019-01-11 13:08:21 +00:00
|
|
|
#ifdef MAGIC_DIVISION
|
|
|
|
int64_t divisor = instr.imm32;
|
|
|
|
asmCode << "\t; magic divide by " << divisor << std::endl;
|
|
|
|
if ((divisor & -divisor) == divisor || (divisor & -divisor) == -divisor) {
|
|
|
|
// +/- power of two
|
|
|
|
bool negative = divisor < 0;
|
|
|
|
if (negative)
|
|
|
|
divisor = -divisor;
|
|
|
|
int shift = 0;
|
|
|
|
uint64_t unsignedDivisor = divisor;
|
|
|
|
while (unsignedDivisor >>= 1)
|
|
|
|
++shift;
|
|
|
|
if (shift > 0) {
|
|
|
|
asmCode << "\tmov rcx, rax" << std::endl;
|
|
|
|
asmCode << "\tsar rcx, 63" << std::endl;
|
|
|
|
uint32_t mask = (1ULL << shift) + 0xFFFFFFFF;
|
|
|
|
asmCode << "\tand ecx, 0" << std::hex << mask << std::dec << "h" << std::endl;
|
|
|
|
asmCode << "\tadd rax, rcx" << std::endl;
|
|
|
|
asmCode << "\tsar rax, " << shift << std::endl;
|
|
|
|
}
|
2019-01-11 15:53:52 +00:00
|
|
|
if (negative)
|
2019-01-11 13:08:21 +00:00
|
|
|
asmCode << "\tneg rax" << std::endl;
|
2019-01-11 15:53:52 +00:00
|
|
|
}
|
|
|
|
else if (divisor != 0) {
|
2019-01-11 13:08:21 +00:00
|
|
|
magics_info mi = compute_signed_magic_info(divisor);
|
|
|
|
if ((divisor >= 0) != (mi.multiplier >= 0))
|
|
|
|
asmCode << "\tmov rcx, rax" << std::endl;
|
|
|
|
asmCode << "\tmov rdx, " << mi.multiplier << std::endl;
|
|
|
|
asmCode << "\timul rdx" << std::endl;
|
|
|
|
asmCode << "\tmov rax, rdx" << std::endl;
|
|
|
|
asmCode << "\txor edx, edx" << std::endl;
|
|
|
|
bool haveSF = false;
|
|
|
|
if (divisor > 0 && mi.multiplier < 0) {
|
|
|
|
asmCode << "\tadd rax, rcx" << std::endl;
|
|
|
|
haveSF = true;
|
|
|
|
}
|
|
|
|
if (divisor < 0 && mi.multiplier > 0) {
|
|
|
|
asmCode << "\tsub rax, rcx" << std::endl;
|
|
|
|
haveSF = true;
|
|
|
|
}
|
|
|
|
if (mi.shift > 0) {
|
|
|
|
asmCode << "\tsar rax, " << mi.shift << std::endl;
|
|
|
|
haveSF = true;
|
|
|
|
}
|
|
|
|
if (!haveSF)
|
|
|
|
asmCode << "\ttest rax, rax" << std::endl;
|
|
|
|
asmCode << "\tsets dl" << std::endl;
|
|
|
|
asmCode << "\tadd rax, rdx" << std::endl;
|
|
|
|
}
|
2019-01-11 15:53:52 +00:00
|
|
|
#else
|
|
|
|
asmCode << "\tmov edx, " << instr.imm32 << std::endl;
|
|
|
|
#endif
|
2019-01-11 13:08:21 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-01-11 15:53:52 +00:00
|
|
|
asmCode << "\tmov edx, " << regR32[instr.regb % RegistersCount] << std::endl;
|
|
|
|
#ifndef MAGIC_DIVISION
|
|
|
|
}
|
2019-01-11 13:08:21 +00:00
|
|
|
#endif
|
2019-01-11 15:53:52 +00:00
|
|
|
asmCode << "\tcmp edx, -1" << std::endl;
|
|
|
|
asmCode << "\tjne short body_idiv_" << i << std::endl;
|
|
|
|
asmCode << "\tneg rax" << std::endl;
|
|
|
|
asmCode << "\tjmp short result_idiv_" << i << std::endl;
|
|
|
|
asmCode << "body_idiv_" << i << ":" << std::endl;
|
|
|
|
asmCode << "\tmov ecx, 1" << std::endl;
|
|
|
|
asmCode << "\ttest edx, edx" << std::endl;
|
|
|
|
asmCode << "\tcmovne ecx, edx" << std::endl;
|
|
|
|
asmCode << "\tmovsxd rcx, ecx" << std::endl;
|
|
|
|
asmCode << "\tcqo" << std::endl;
|
|
|
|
asmCode << "\tidiv rcx" << std::endl;
|
|
|
|
asmCode << "result_idiv_" << i << ":" << std::endl;
|
2019-01-11 13:08:21 +00:00
|
|
|
#ifdef MAGIC_DIVISION
|
2019-01-11 15:53:52 +00:00
|
|
|
}
|
2019-01-11 13:08:21 +00:00
|
|
|
#endif
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_AND_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tand rax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_AND_32(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tand eax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia32(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_OR_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tor rax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_OR_32(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tor eax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia32(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_XOR_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\txor rax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_XOR_32(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\txor eax, ";
|
2019-01-11 15:53:52 +00:00
|
|
|
genbia32(instr);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_SHL_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-11 15:53:52 +00:00
|
|
|
genbiashift(instr, "shl");
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_SHR_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-11 15:53:52 +00:00
|
|
|
genbiashift(instr, "shr");
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_SAR_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-11 15:53:52 +00:00
|
|
|
genbiashift(instr, "sar");
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_ROL_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-11 15:53:52 +00:00
|
|
|
genbiashift(instr, "rol");
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_ROR_64(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-11 15:53:52 +00:00
|
|
|
genbiashift(instr, "ror");
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_FPADD(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genaf(instr, i);
|
2018-12-31 18:06:45 +00:00
|
|
|
genbf(instr, "addpd");
|
2018-12-13 22:11:55 +00:00
|
|
|
gencf(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_FPSUB(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genaf(instr, i);
|
2018-12-31 18:06:45 +00:00
|
|
|
genbf(instr, "subpd");
|
2018-12-13 22:11:55 +00:00
|
|
|
gencf(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_FPMUL(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genaf(instr, i);
|
2018-12-31 18:06:45 +00:00
|
|
|
genbf(instr, "mulpd");
|
|
|
|
asmCode << "\tmovaps xmm1, xmm0" << std::endl;
|
|
|
|
asmCode << "\tcmpeqpd xmm1, xmm1" << std::endl;
|
|
|
|
asmCode << "\tandps xmm0, xmm1" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
gencf(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_FPDIV(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genaf(instr, i);
|
2018-12-31 18:06:45 +00:00
|
|
|
genbf(instr, "divpd");
|
|
|
|
asmCode << "\tmovaps xmm1, xmm0" << std::endl;
|
|
|
|
asmCode << "\tcmpeqpd xmm1, xmm1" << std::endl;
|
|
|
|
asmCode << "\tandps xmm0, xmm1" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
gencf(instr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_FPSQRT(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genaf(instr, i);
|
2018-12-31 18:06:45 +00:00
|
|
|
asmCode << "\tandps xmm0, xmm10" << std::endl;
|
2019-01-08 13:50:31 +00:00
|
|
|
asmCode << "\tsqrtpd " << regF[instr.regc % RegistersCount] << ", xmm0" << std::endl;
|
|
|
|
gencf(instr, false);
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_FPROUND(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2019-01-10 21:04:55 +00:00
|
|
|
asmCode << "\tmov rcx, rax" << std::endl;
|
2019-01-11 09:52:12 +00:00
|
|
|
int rotate = (13 - (instr.imm8 & 63)) & 63;
|
|
|
|
if (rotate != 0)
|
|
|
|
asmCode << "\trol rax, " << rotate << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
asmCode << "\tand eax, 24576" << std::endl;
|
|
|
|
asmCode << "\tor eax, 40896" << std::endl;
|
|
|
|
asmCode << "\tmov dword ptr [rsp - 8], eax" << std::endl;
|
|
|
|
asmCode << "\tldmxcsr dword ptr [rsp - 8]" << std::endl;
|
2019-01-10 21:04:55 +00:00
|
|
|
gencr(instr, false);
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 21:41:35 +00:00
|
|
|
static inline const char* jumpCondition(Instruction& instr, bool invert = false) {
|
|
|
|
switch ((instr.locb & 7) ^ invert)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return "jbe";
|
|
|
|
case 1:
|
|
|
|
return "ja";
|
|
|
|
case 2:
|
|
|
|
return "js";
|
|
|
|
case 3:
|
|
|
|
return "jns";
|
|
|
|
case 4:
|
|
|
|
return "jo";
|
|
|
|
case 5:
|
|
|
|
return "jno";
|
|
|
|
case 6:
|
|
|
|
return "jl";
|
|
|
|
case 7:
|
|
|
|
return "jge";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 08:58:06 +00:00
|
|
|
void AssemblyGeneratorX86::h_JUMP(Instruction& instr, int i) {
|
|
|
|
genar(instr, i);
|
|
|
|
gencr(instr);
|
|
|
|
asmCode << "\tcmp " << regR32[instr.regb % RegistersCount] << ", " << instr.imm32 << std::endl;
|
|
|
|
asmCode << "\t" << jumpCondition(instr);
|
|
|
|
asmCode << " rx_i_" << wrapInstr(i + (instr.imm8 & 127) + 2) << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-12-13 22:11:55 +00:00
|
|
|
void AssemblyGeneratorX86::h_CALL(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-21 21:41:35 +00:00
|
|
|
gencr(instr);
|
2019-01-11 09:18:24 +00:00
|
|
|
asmCode << "\tcmp " << regR32[instr.regb % RegistersCount] << ", " << instr.imm32 << std::endl;
|
|
|
|
asmCode << "\t" << jumpCondition(instr, true);
|
|
|
|
asmCode << " short rx_i_" << wrapInstr(i + 1) << std::endl;
|
2018-12-20 17:36:09 +00:00
|
|
|
asmCode << "\tcall rx_i_" << wrapInstr(i + (instr.imm8 & 127) + 2) << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AssemblyGeneratorX86::h_RET(Instruction& instr, int i) {
|
2019-01-07 16:44:43 +00:00
|
|
|
genar(instr, i);
|
2018-12-13 22:11:55 +00:00
|
|
|
gencr(instr);
|
2019-01-11 09:18:24 +00:00
|
|
|
asmCode << "\tcmp rsp, " << regStackBeginAddr << std::endl;
|
|
|
|
asmCode << "\tje short rx_i_" << wrapInstr(i + 1) << std::endl;
|
|
|
|
asmCode << "\tret" << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "instructionWeights.hpp"
|
|
|
|
#define INST_HANDLE(x) REPN(&AssemblyGeneratorX86::h_##x, WT(x))
|
|
|
|
|
|
|
|
InstructionGenerator AssemblyGeneratorX86::engine[256] = {
|
|
|
|
INST_HANDLE(ADD_64)
|
|
|
|
INST_HANDLE(ADD_32)
|
|
|
|
INST_HANDLE(SUB_64)
|
|
|
|
INST_HANDLE(SUB_32)
|
|
|
|
INST_HANDLE(MUL_64)
|
|
|
|
INST_HANDLE(MULH_64)
|
|
|
|
INST_HANDLE(MUL_32)
|
|
|
|
INST_HANDLE(IMUL_32)
|
|
|
|
INST_HANDLE(IMULH_64)
|
|
|
|
INST_HANDLE(DIV_64)
|
|
|
|
INST_HANDLE(IDIV_64)
|
|
|
|
INST_HANDLE(AND_64)
|
|
|
|
INST_HANDLE(AND_32)
|
|
|
|
INST_HANDLE(OR_64)
|
|
|
|
INST_HANDLE(OR_32)
|
|
|
|
INST_HANDLE(XOR_64)
|
|
|
|
INST_HANDLE(XOR_32)
|
|
|
|
INST_HANDLE(SHL_64)
|
|
|
|
INST_HANDLE(SHR_64)
|
|
|
|
INST_HANDLE(SAR_64)
|
|
|
|
INST_HANDLE(ROL_64)
|
|
|
|
INST_HANDLE(ROR_64)
|
|
|
|
INST_HANDLE(FPADD)
|
|
|
|
INST_HANDLE(FPSUB)
|
|
|
|
INST_HANDLE(FPMUL)
|
|
|
|
INST_HANDLE(FPDIV)
|
|
|
|
INST_HANDLE(FPSQRT)
|
|
|
|
INST_HANDLE(FPROUND)
|
2019-01-11 08:58:06 +00:00
|
|
|
INST_HANDLE(JUMP)
|
2018-12-13 22:11:55 +00:00
|
|
|
INST_HANDLE(CALL)
|
|
|
|
INST_HANDLE(RET)
|
|
|
|
};
|
|
|
|
}
|