From 1a73fb21cea559e8da2cabb43176b0f183a937ee Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 7 Feb 2020 21:02:12 -0500 Subject: Add HLIL instructions for variable declarations --- highlevelilinstruction.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) (limited to 'highlevelilinstruction.cpp') diff --git a/highlevelilinstruction.cpp b/highlevelilinstruction.cpp index 65b51ac4..609b7f77 100644 --- a/highlevelilinstruction.cpp +++ b/highlevelilinstruction.cpp @@ -37,6 +37,7 @@ unordered_map HighLevelILInstructionBase::operandTypeForUsage = { {SourceExprHighLevelOperandUsage, ExprHighLevelOperand}, {VariableHighLevelOperandUsage, VariableHighLevelOperand}, + {DestVariableHighLevelOperandUsage, VariableHighLevelOperand}, {SSAVariableHighLevelOperandUsage, SSAVariableHighLevelOperand}, {DestSSAVariableHighLevelOperandUsage, SSAVariableHighLevelOperand}, {DestExprHighLevelOperandUsage, ExprHighLevelOperand}, @@ -95,6 +96,9 @@ unordered_map> {HLIL_RET, {SourceExprsHighLevelOperandUsage}}, {HLIL_GOTO, {TargetHighLevelOperandUsage}}, {HLIL_LABEL, {TargetHighLevelOperandUsage}}, + {HLIL_VAR_DECLARE, {VariableHighLevelOperandUsage}}, + {HLIL_VAR_INIT, {DestVariableHighLevelOperandUsage, SourceExprHighLevelOperandUsage}}, + {HLIL_VAR_INIT_SSA, {DestSSAVariableHighLevelOperandUsage, SourceExprHighLevelOperandUsage}}, {HLIL_ASSIGN, {DestExprHighLevelOperandUsage, SourceExprHighLevelOperandUsage}}, {HLIL_ASSIGN_UNPACK, {DestExprsHighLevelOperandUsage, SourceExprHighLevelOperandUsage}}, {HLIL_ASSIGN_MEM_SSA, {DestExprHighLevelOperandUsage, DestMemoryVersionHighLevelOperandUsage, @@ -912,6 +916,12 @@ void HighLevelILInstruction::VisitExprs(const std::functionexprIndex); break; + case HLIL_VAR_INIT: + toProcess.push(cur.GetSourceExpr().exprIndex); + break; + case HLIL_VAR_INIT_SSA: + toProcess.push(cur.GetSourceExpr().exprIndex); + break; case HLIL_ASSIGN: toProcess.push(cur.GetSourceExpr().exprIndex); toProcess.push(cur.GetDestExpr().exprIndex); @@ -1111,8 +1121,8 @@ ExprId HighLevelILInstruction::CopyTo(HighLevelILFunction* dest, return dest->While(subExprHandler(GetConditionExpr()), subExprHandler(GetLoopExpr()), *this); case HLIL_DO_WHILE: - return dest->DoWhile(subExprHandler(GetLoopExpr()), - subExprHandler(GetConditionExpr()), *this); + return dest->DoWhile(subExprHandler(GetLoopExpr()), + subExprHandler(GetConditionExpr()), *this); case HLIL_FOR: return dest->For(subExprHandler(GetInitExpr()), subExprHandler(GetConditionExpr()), subExprHandler(GetUpdateExpr()), @@ -1130,6 +1140,18 @@ ExprId HighLevelILInstruction::CopyTo(HighLevelILFunction* dest, return dest->Break(*this); case HLIL_CONTINUE: return dest->Continue(*this); + case HLIL_GOTO: + return dest->Goto(GetTarget(), *this); + case HLIL_LABEL: + return dest->Label(GetTarget(), *this); + case HLIL_VAR_DECLARE: + return dest->VarDeclare(GetVariable(), *this); + case HLIL_VAR_INIT: + return dest->VarInit(size, GetDestVariable(), + subExprHandler(GetSourceExpr()), *this); + case HLIL_VAR_INIT_SSA: + return dest->VarInitSSA(size, GetDestSSAVariable(), + subExprHandler(GetSourceExpr()), *this); case HLIL_ASSIGN: return dest->Assign(size, subExprHandler(GetDestExpr()), subExprHandler(GetSourceExpr()), *this); @@ -1409,6 +1431,28 @@ bool HighLevelILInstruction::operator<(const HighLevelILInstruction& other) cons return GetTarget() < other.GetTarget(); case HLIL_LABEL: return GetTarget() < other.GetTarget(); + case HLIL_VAR_DECLARE: + return GetVariable() < other.GetVariable(); + case HLIL_VAR_INIT: + if (size < other.size) + return true; + if (size > other.size) + return false; + if (GetDestVariable() < other.GetDestVariable()) + return true; + if (other.GetDestVariable() < GetDestVariable()) + return false; + return GetSourceExpr() < other.GetSourceExpr(); + case HLIL_VAR_INIT_SSA: + if (size < other.size) + return true; + if (size > other.size) + return false; + if (GetDestSSAVariable() < other.GetDestSSAVariable()) + return true; + if (other.GetDestSSAVariable() < GetDestSSAVariable()) + return false; + return GetSourceExpr() < other.GetSourceExpr(); case HLIL_ASSIGN: if (size < other.size) return true; @@ -1821,6 +1865,15 @@ Variable HighLevelILInstruction::GetVariable() const } +Variable HighLevelILInstruction::GetDestVariable() const +{ + size_t operandIndex; + if (GetOperandIndexForUsage(DestVariableHighLevelOperandUsage, operandIndex)) + return GetRawOperandAsVariable(operandIndex); + throw HighLevelILInstructionAccessException(); +} + + SSAVariable HighLevelILInstruction::GetSSAVariable() const { size_t operandIndex; @@ -2203,6 +2256,24 @@ ExprId HighLevelILFunction::Label(size_t target, const ILSourceLocation& loc) } +ExprId HighLevelILFunction::VarDeclare(const Variable& var, const ILSourceLocation& loc) +{ + return AddExprWithLocation(HLIL_VAR_DECLARE, loc, 0, var.ToIdentifier()); +} + + +ExprId HighLevelILFunction::VarInit(size_t size, const Variable& dest, ExprId src, const ILSourceLocation& loc) +{ + return AddExprWithLocation(HLIL_VAR_INIT, loc, size, dest.ToIdentifier(), src); +} + + +ExprId HighLevelILFunction::VarInitSSA(size_t size, const SSAVariable& dest, ExprId src, const ILSourceLocation& loc) +{ + return AddExprWithLocation(HLIL_VAR_INIT_SSA, loc, size, dest.var.ToIdentifier(), dest.version, src); +} + + ExprId HighLevelILFunction::Assign(size_t size, ExprId dest, ExprId src, const ILSourceLocation& loc) { return AddExprWithLocation(HLIL_ASSIGN, loc, size, dest, src); -- cgit v1.3.1