Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement movsq #40

Merged
merged 7 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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