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/>.
|
|
|
|
*/
|
|
|
|
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "instruction.hpp"
|
2019-01-24 18:29:59 +00:00
|
|
|
#include "common.hpp"
|
2018-12-13 22:11:55 +00:00
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
namespace randomx {
|
2018-12-13 22:11:55 +00:00
|
|
|
|
|
|
|
void Instruction::print(std::ostream& os) const {
|
2019-01-24 18:29:59 +00:00
|
|
|
os << names[opcode] << " ";
|
|
|
|
auto handler = engine[opcode];
|
|
|
|
(this->*handler)(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::genAddressReg(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
os << (getModMem() ? "L1" : "L2") << "[r" << (int)src << std::showpos << (int32_t)getImm32() << std::noshowpos << "]";
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 09:52:30 +00:00
|
|
|
void Instruction::genAddressRegDst(std::ostream& os) const {
|
2019-04-29 21:38:23 +00:00
|
|
|
if (getModCond() < StoreL3Condition)
|
2019-04-28 14:42:45 +00:00
|
|
|
os << (getModMem() ? "L1" : "L2");
|
2019-04-16 16:58:44 +00:00
|
|
|
else
|
|
|
|
os << "L3";
|
|
|
|
os << "[r" << (int)dst << std::showpos << (int32_t)getImm32() << std::noshowpos << "]";
|
2019-01-27 09:52:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 18:29:59 +00:00
|
|
|
void Instruction::genAddressImm(std::ostream& os) const {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "L3" << "[" << (getImm32() & ScratchpadL3Mask) << "]";
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 10:00:56 +00:00
|
|
|
void Instruction::h_IADD_RS(std::ostream& os) const {
|
2019-04-14 15:21:26 +00:00
|
|
|
os << "r" << (int)dst << ", r" << (int)src;
|
|
|
|
if(dst == RegisterNeedsDisplacement) {
|
|
|
|
os << ", " << (int32_t)getImm32();
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
2019-04-29 21:38:23 +00:00
|
|
|
os << ", SHFT " << (int)getModShift() << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IADD_M(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressImm(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_ISUB_R(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "r" << (int)dst << ", " << (int32_t)getImm32() << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_ISUB_M(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressImm(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IMUL_R(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "r" << (int)dst << ", " << (int32_t)getImm32() << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IMUL_M(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressImm(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IMULH_R(std::ostream& os) const {
|
2019-02-09 14:45:26 +00:00
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IMULH_M(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressImm(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_ISMULH_R(std::ostream& os) const {
|
2019-02-09 14:45:26 +00:00
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_ISMULH_M(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressImm(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_INEG_R(std::ostream& os) const {
|
|
|
|
os << "r" << (int)dst << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IXOR_R(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "r" << (int)dst << ", " << (int32_t)getImm32() << std::endl;
|
2018-12-13 22:11:55 +00:00
|
|
|
}
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IXOR_M(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os << "r" << (int)dst << ", ";
|
|
|
|
genAddressImm(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IROR_R(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "r" << (int)dst << ", " << (getImm32() & 63) << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_IROL_R(std::ostream& os) const {
|
|
|
|
if (src != dst) {
|
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "r" << (int)dst << ", " << (getImm32() & 63) << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:48:26 +00:00
|
|
|
void Instruction::h_IMUL_RCP(std::ostream& os) const {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "r" << (int)dst << ", " << getImm32() << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:07:00 +00:00
|
|
|
void Instruction::h_ISWAP_R(std::ostream& os) const {
|
|
|
|
os << "r" << (int)dst << ", r" << (int)src << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FSWAP_R(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
const char reg = (dst >= RegisterCountFlt) ? 'e' : 'f';
|
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << reg << dstIndex << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FADD_R(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
|
|
|
auto srcIndex = src % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << "f" << dstIndex << ", a" << srcIndex << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FADD_M(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << "f" << dstIndex << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FSUB_R(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
|
|
|
auto srcIndex = src % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << "f" << dstIndex << ", a" << srcIndex << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FSUB_M(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << "f" << dstIndex << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-12 23:01:34 +00:00
|
|
|
void Instruction::h_FSCAL_R(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
2019-02-09 14:45:26 +00:00
|
|
|
os << "f" << dstIndex << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FMUL_R(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
|
|
|
auto srcIndex = src % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << "e" << dstIndex << ", a" << srcIndex << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FDIV_M(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << "e" << dstIndex << ", ";
|
|
|
|
genAddressReg(os);
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:43:57 +00:00
|
|
|
void Instruction::h_FSQRT_R(std::ostream& os) const {
|
2019-04-28 14:42:45 +00:00
|
|
|
auto dstIndex = dst % RegisterCountFlt;
|
2019-01-24 18:29:59 +00:00
|
|
|
os << "e" << dstIndex << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instruction::h_CFROUND(std::ostream& os) const {
|
2019-03-11 22:04:34 +00:00
|
|
|
os << "r" << (int)src << ", " << (getImm32() & 63) << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char* condition(int index) {
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return "be";
|
|
|
|
case 1:
|
|
|
|
return "ab";
|
|
|
|
case 2:
|
|
|
|
return "sg";
|
|
|
|
case 3:
|
|
|
|
return "ns";
|
|
|
|
case 4:
|
|
|
|
return "of";
|
|
|
|
case 5:
|
|
|
|
return "no";
|
|
|
|
case 6:
|
|
|
|
return "lt";
|
|
|
|
case 7:
|
|
|
|
return "ge";
|
2019-02-09 18:32:53 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 21:38:23 +00:00
|
|
|
void Instruction::h_CBRANCH(std::ostream& os) const {
|
|
|
|
os << (int32_t)getImm32() << ", COND " << (int)(getModCond()) << std::endl;
|
2019-01-24 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 09:52:30 +00:00
|
|
|
void Instruction::h_ISTORE(std::ostream& os) const {
|
|
|
|
genAddressRegDst(os);
|
|
|
|
os << ", r" << (int)src << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:19:49 +00:00
|
|
|
void Instruction::h_NOP(std::ostream& os) const {
|
|
|
|
os << std::endl;
|
|
|
|
}
|
|
|
|
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "instruction_weights.hpp"
|
2018-12-13 22:11:55 +00:00
|
|
|
#define INST_NAME(x) REPN(#x, WT(x))
|
2019-01-24 18:29:59 +00:00
|
|
|
#define INST_HANDLE(x) REPN(&Instruction::h_##x, WT(x))
|
2018-12-13 22:11:55 +00:00
|
|
|
|
|
|
|
const char* Instruction::names[256] = {
|
2019-04-06 10:00:56 +00:00
|
|
|
INST_NAME(IADD_RS)
|
2019-01-24 18:29:59 +00:00
|
|
|
INST_NAME(IADD_M)
|
|
|
|
INST_NAME(ISUB_R)
|
|
|
|
INST_NAME(ISUB_M)
|
|
|
|
INST_NAME(IMUL_R)
|
|
|
|
INST_NAME(IMUL_M)
|
|
|
|
INST_NAME(IMULH_R)
|
|
|
|
INST_NAME(IMULH_M)
|
|
|
|
INST_NAME(ISMULH_R)
|
|
|
|
INST_NAME(ISMULH_M)
|
2019-02-22 16:48:26 +00:00
|
|
|
INST_NAME(IMUL_RCP)
|
2019-01-24 18:29:59 +00:00
|
|
|
INST_NAME(INEG_R)
|
|
|
|
INST_NAME(IXOR_R)
|
|
|
|
INST_NAME(IXOR_M)
|
|
|
|
INST_NAME(IROR_R)
|
2019-02-04 16:07:00 +00:00
|
|
|
INST_NAME(ISWAP_R)
|
2019-02-05 22:43:57 +00:00
|
|
|
INST_NAME(FSWAP_R)
|
|
|
|
INST_NAME(FADD_R)
|
|
|
|
INST_NAME(FADD_M)
|
|
|
|
INST_NAME(FSUB_R)
|
|
|
|
INST_NAME(FSUB_M)
|
2019-02-12 23:01:34 +00:00
|
|
|
INST_NAME(FSCAL_R)
|
2019-02-05 22:43:57 +00:00
|
|
|
INST_NAME(FMUL_R)
|
|
|
|
INST_NAME(FDIV_M)
|
|
|
|
INST_NAME(FSQRT_R)
|
2019-04-29 21:38:23 +00:00
|
|
|
INST_NAME(CBRANCH)
|
2019-01-24 18:29:59 +00:00
|
|
|
INST_NAME(CFROUND)
|
2019-01-27 09:52:30 +00:00
|
|
|
INST_NAME(ISTORE)
|
2019-01-27 17:19:49 +00:00
|
|
|
INST_NAME(NOP)
|
2019-01-24 18:29:59 +00:00
|
|
|
};
|
|
|
|
|
2019-04-20 14:53:06 +00:00
|
|
|
InstructionFormatter Instruction::engine[256] = {
|
2019-04-06 10:00:56 +00:00
|
|
|
INST_HANDLE(IADD_RS)
|
2019-01-24 18:29:59 +00:00
|
|
|
INST_HANDLE(IADD_M)
|
|
|
|
INST_HANDLE(ISUB_R)
|
|
|
|
INST_HANDLE(ISUB_M)
|
|
|
|
INST_HANDLE(IMUL_R)
|
|
|
|
INST_HANDLE(IMUL_M)
|
|
|
|
INST_HANDLE(IMULH_R)
|
|
|
|
INST_HANDLE(IMULH_M)
|
|
|
|
INST_HANDLE(ISMULH_R)
|
|
|
|
INST_HANDLE(ISMULH_M)
|
2019-02-22 16:48:26 +00:00
|
|
|
INST_HANDLE(IMUL_RCP)
|
2019-01-24 18:29:59 +00:00
|
|
|
INST_HANDLE(INEG_R)
|
|
|
|
INST_HANDLE(IXOR_R)
|
|
|
|
INST_HANDLE(IXOR_M)
|
|
|
|
INST_HANDLE(IROR_R)
|
|
|
|
INST_HANDLE(IROL_R)
|
2019-02-04 16:07:00 +00:00
|
|
|
INST_HANDLE(ISWAP_R)
|
2019-02-05 22:43:57 +00:00
|
|
|
INST_HANDLE(FSWAP_R)
|
|
|
|
INST_HANDLE(FADD_R)
|
|
|
|
INST_HANDLE(FADD_M)
|
|
|
|
INST_HANDLE(FSUB_R)
|
|
|
|
INST_HANDLE(FSUB_M)
|
2019-02-12 23:01:34 +00:00
|
|
|
INST_HANDLE(FSCAL_R)
|
2019-02-05 22:43:57 +00:00
|
|
|
INST_HANDLE(FMUL_R)
|
|
|
|
INST_HANDLE(FDIV_M)
|
|
|
|
INST_HANDLE(FSQRT_R)
|
2019-04-29 21:38:23 +00:00
|
|
|
INST_HANDLE(CBRANCH)
|
2019-01-24 18:29:59 +00:00
|
|
|
INST_HANDLE(CFROUND)
|
2019-01-27 09:52:30 +00:00
|
|
|
INST_HANDLE(ISTORE)
|
2019-01-27 17:19:49 +00:00
|
|
|
INST_HANDLE(NOP)
|
2018-12-13 22:11:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|