Skip to content

Commit

Permalink
Merge pull request #40 from oureveryday/main
Browse files Browse the repository at this point in the history
Implement movsq
  • Loading branch information
NaC-L authored Oct 18, 2024
2 parents e10640c + ecbf49a commit 3e8c18d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
6 changes: 2 additions & 4 deletions lifter/OperandUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,7 @@ Value* lifterClass::setFlag(const Flag flag, Value* newValue) {
LLVMContext& context = builder.getContext();
newValue = createTruncFolder(newValue, Type::getInt1Ty(context));
printvalue2((int32_t)flag) printvalue(newValue);
if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF ||
flag == FLAG_DF)
if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF)
return nullptr;

FlagList[flag].set(newValue); // Set the new value directly
Expand All @@ -1158,8 +1157,7 @@ Value* lifterClass::setFlag(const Flag flag, Value* newValue) {
void lifterClass::setFlag(const Flag flag,
std::function<Value*()> calculation) {
// If the flag is one of the reserved ones, do not modify
if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF ||
flag == FLAG_DF)
if (flag == FLAG_RESERVED1 || flag == FLAG_RESERVED5 || flag == FLAG_IF)
return;

// lazy calculation
Expand Down
61 changes: 24 additions & 37 deletions lifter/Semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,76 +259,60 @@ void lifterClass::branchHelper(Value* condition, const string& instname,
// cout << "pathInfo:" << pathInfo << " dest: " << destination <<
// "\n";
}

void lifterClass::lift_movsb() {
void lifterClass::lift_movs_X() {
LLVMContext& context = builder.getContext();
// Get the size based on the operand
int size = operands[1].size;

// DEST := SRC;
// [esi] = [edi]
// Parameterize size and direction
// sign = DF (-1/+1)
// incdecv = size*sign (sb means size is 1)
// esi += incdecv
// edi += incdecv
//

// Value* SRCptrvalue =
// GetOperandValue(operands[0],operands[0].size);

Value* DSTptrvalue = GetOperandValue(operands[1], operands[1].size);
// incdecv = size*sign

Value* DSTptrvalue = GetOperandValue(operands[1], size);
SetOperandValue(operands[0], DSTptrvalue);

bool isREP = (instruction.attributes & ZYDIS_ATTRIB_HAS_REP) != 0;

Value* DF = getFlag(FLAG_DF);
auto one = ConstantInt::get(DF->getType(), 1);
// sign = (x*(x+1)) - 1
// v = sign * bytesize ; bytesize is 1

Value* Direction =
createSubFolder(createMulFolder(DF, createAddFolder(DF, one)), one);
// sign = (DF*(DF+1)) - 1
// v = sign * byteSize

auto byteSizeValue = size;

auto SRCop = operands[2 + isREP];
auto DSTop = operands[3 + isREP];

printvalue(DF);
Value* Direction = createSelectFolder(DF, ConstantInt::get(Type::getIntNTy(context, SRCop.size),1 * byteSizeValue),
ConstantInt::get(Type::getIntNTy(context, SRCop.size),-1 * byteSizeValue));
printvalue(Direction);

Value* SRCvalue = GetOperandValue(SRCop, SRCop.size);
Value* DSTvalue = GetOperandValue(DSTop, DSTop.size);

if (isREP) {
// if REP, operands[1] will be e/rax
// in that case, repeat and decrement e/rax until its 0

// we can create a loop but I dont know how that would effect our
// optimizations
Value* count = GetOperandValue(operands[2], operands[2].size);
if (auto countci = dyn_cast<ConstantInt>(count)) {
Value* UpdateSRCvalue = SRCvalue;
Value* UpdateDSTvalue = DSTvalue;
uint64_t looptime = countci->getZExtValue();
printvalue2(looptime);

for (int i = looptime; i > 0; i--) {
// TODO: fix this loop

// Value* SRCptrvalue = GetOperandValue(
// operands[0],
// operands[0].size);
DSTptrvalue = GetOperandValue(operands[1], operands[1].size);

DSTptrvalue = GetOperandValue(operands[1], size);
SetOperandValue(operands[0], DSTptrvalue);

UpdateSRCvalue = createAddFolder(UpdateSRCvalue, Direction);
UpdateDSTvalue = createAddFolder(UpdateDSTvalue, Direction);
printvalue(UpdateDSTvalue) printvalue(UpdateSRCvalue);

SetOperandValue(SRCop, UpdateSRCvalue);
SetOperandValue(DSTop, UpdateDSTvalue);
// bad cheat

if (i > 1)
debugging::increaseInstCounter();
}

SetOperandValue(operands[2], ConstantInt::get(count->getType(), 0));

return;
} else {
UNREACHABLE("fix rep");
Expand All @@ -341,6 +325,7 @@ void lifterClass::lift_movsb() {
SetOperandValue(SRCop, UpdateSRCvalue);
SetOperandValue(DSTop, UpdateDSTvalue);
}

void lifterClass::lift_movaps() {
auto dest = operands[0];
auto src = operands[1];
Expand Down Expand Up @@ -3669,11 +3654,13 @@ void lifterClass::liftInstructionSemantics() {
lift_mov();
break;
}
case ZYDIS_MNEMONIC_MOVSB: {
lift_movsb();
case ZYDIS_MNEMONIC_MOVSB:
case ZYDIS_MNEMONIC_MOVSW:
case ZYDIS_MNEMONIC_MOVSD:
case ZYDIS_MNEMONIC_MOVSQ: {
lift_movs_X();
break;
}

// cmov
case ZYDIS_MNEMONIC_CMOVZ: {
lift_cmovz();
Expand Down
2 changes: 1 addition & 1 deletion lifter/lifterClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class lifterClass {
// end folders

// semantics definition
DEFINE_FUNCTION(movsb);
DEFINE_FUNCTION(movs_X);
DEFINE_FUNCTION(movaps);
DEFINE_FUNCTION(mov);
DEFINE_FUNCTION(cmovbz);
Expand Down

0 comments on commit 3e8c18d

Please sign in to comment.