Skip to content

Commit

Permalink
Synchronized with C++ snapshot.
Browse files Browse the repository at this point in the history
TDecompiler.CheckPrototype was returning False instead of True.
Removed instruction types (itUnknown...) from Def_disasm as unused.
Converted operand types to enumeration.
Moved OutputHex() from class MDisasm to unit Misc.
Fixed MDisasm.GetOpType to properly handle $,+,-
Refactored code for Apply button in EditFunctionDlg
In EditFunctionDlg, lRetBytes & lArgsBytes must not include text, only numbers
Refactored AddArgsFromDeclaration to use ExtractStrings
Fixed bug in Main.FindImports - there was an offset by 1 for replacing @
Added *.BPL in filters for OpenDlg
Label caption in FInputDlg was EP instead of Version when autodetection fails
Changed FlagList & InfoList to be dynamic arrays
Changed lbCodeDrawItem to use OutputHex()
vtUnitMouseMove now sets WhereSearch according to Sender
  • Loading branch information
tmcdos committed Jul 20, 2016
1 parent 998642e commit ea4e677
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 423 deletions.
116 changes: 101 additions & 15 deletions Decompiler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ constructor TDecompileEnv.Create(AStartAdr:Integer; ASize:Integer; recN:InfoRec)
if aInfo.TypeDef = '' then Exit;
end;
if (ARec.kind = ikFunc) and (ARec._type = '') Then Exit;
Result:=False;
Result:=True;
end;

Function TDecompiler.Init (fromAdr:Integer):Boolean;
Expand Down Expand Up @@ -1782,7 +1782,9 @@ constructor TDecompileEnv.Create(AStartAdr:Integer; ASize:Integer; recN:InfoRec)
if DeFlags[DisaInfo.Immediate - Env.StartAdr] = 1 then
Begin
//SetFlag(cfPass, _fromPos);
Inc(curPos, instrLen);
//check Exit
if IsExit(DisaInfo.Immediate) then Env.AddToBody('Exit;');
Inc(curPos, instrLen);
Inc(curAdr, instrLen);
break;
End;
Expand Down Expand Up @@ -1855,13 +1857,22 @@ constructor TDecompileEnv.Create(AStartAdr:Integer; ASize:Integer; recN:InfoRec)
Env.CmpStack.Add(cmpItem);
//skip jcc
instrLen := frmDisasm.Disassemble(Code + curPos, curAdr, Nil, Nil);
Inc(curPos, instrLen);
Inc(curPos, instrLen);
Inc(curAdr, instrLen);
continue; //???
End;
if cmpRes = CMP_BRANCH then
Begin
instrLen := frmDisasm.Disassemble(Code + curPos, curAdr, @disInfo, Nil);
//Exit
if IsExit(disInfo.Immediate) then
begin
line := 'if (' + CompInfo.L + ' ' + GetDirectCondition(CompInfo.O) + ' ' + CompInfo.R + ') then Exit;';
Env.AddToBody(line);
Inc(curPos, instrLen);
Inc(curAdr, instrLen);
continue;
end;
//jcc up
if disInfo.Immediate < curAdr then
Begin
Expand Down Expand Up @@ -5524,7 +5535,7 @@ constructor TDecompileEnv.Create(AStartAdr:Integer; ASize:Integer; recN:InfoRec)
CompInfo.R := itemSrc.Value;
Exit;
End
else if (Op = OP_ADD) or (Op = OP_SUB) or (Op = OP_XOR) then
else if Op in [OP_ADD, OP_SUB, OP_XOR] then
Begin
InitItem(@item);
item.Precedence := PRECEDENCE_ADD;
Expand All @@ -5536,7 +5547,7 @@ constructor TDecompileEnv.Create(AStartAdr:Integer; ASize:Integer; recN:InfoRec)
Env.AddToBody(line);
Exit;
End
else if (Op = OP_MUL) or (Op = OP_IMUL) or (Op = OP_AND) then
else if Op in [OP_MUL, OP_IMUL, OP_AND] then
Begin
InitItem(@item);
item.Precedence := PRECEDENCE_MULT;
Expand Down Expand Up @@ -5661,6 +5672,13 @@ constructor TDecompileEnv.Create(AStartAdr:Integer; ASize:Integer; recN:InfoRec)
Env.AddToBody(line);
Exit;
End
else if Op = OP_OR then
begin
Env.Stack[itemDst.IntValue].Value := _name;
line := _name + ' := ' + _name + ' Or ' + imm + ';';
Env.AddToBody(line);
Exit;
end
else if Op = OP_XOR then
Begin
Env.Stack[itemDst.IntValue].Value := _name;
Expand Down Expand Up @@ -6578,7 +6596,7 @@ function TDecompiler.DecompileTry(fromAdr:Integer; flags:TDecomCset; loopInfo:TL
_adr,n,r:Integer;
item, item1, item2, item3, item4:TItem;
recN:InfoRec;
line, _value, value1, value2, _typeName:AnsiString;
line, _value, value1, value2, _typeName,_op:AnsiString;
_int64Val:Int64;
Begin
Result:=False;
Expand Down Expand Up @@ -7203,19 +7221,86 @@ function TDecompiler.DecompileTry(fromAdr:Integer; flags:TDecomCset; loopInfo:TL
Env.AddToBody(line);
Exit;
end
else if SameText(name, '@VarAdd') then
else if SameText(name, '@VarAdd') or
SameText(name, '@VarSub') or
SameText(name, '@VarMul') or
SameText(name, '@VarDiv') or
SameText(name, '@VarMod') or
SameText(name, '@VarAnd') or
SameText(name, '@VarOr') or
SameText(name, '@VarXor') or
SameText(name, '@VarShl') or
SameText(name, '@VarShr') or
SameText(name, '@VarRDiv') then
begin
//eax=eax+edx
_op := Copy(name,5, Length(name));
GetRegItem(16, item1);
GetRegItem(18, item2);
if IF_STACK_PTR in item1.Flags then
begin
Env.Stack[item1.IntValue]._Type := 'Variant';
item1 := Env.Stack[item1.IntValue];
end;
if IF_STACK_PTR in item2.Flags then
begin
Env.Stack[item2.IntValue]._Type := 'Variant';
line := item1.Value + ' := ' + item1.Value + ' + ' + item2.Value + ';';
item2 := Env.Stack[item2.IntValue];
end;
line := item1.Name + ' := ' + item1.Name + ' ' + _op + ' ' + item2.Name + ';';
Env.AddToBody(line);
Exit;
end
else if SameText(name, '@VarNeg') or SameText(name, '@VarNot') then
begin
_op := Copy(name,5, Length(name));
GetRegItem(16, item1);
if IF_STACK_PTR in item1.Flags then
begin
Env.Stack[item1.IntValue]._Type := 'Variant';
item1 := Env.Stack[item1.IntValue];
end;
line := item1.Name + ' := ' + _op + ' ' + item1.Name + ';';
Env.AddToBody(line);
Exit;
end
else if SameText(Copy(name,1, 7), '@VarCmp') then
begin
GetCmpInfo(procAdr + instrLen);
if (name[8] = 'E') and (name[9] = 'Q') then
CompInfo.O := 'E' //JZ
else if (name[8] = 'N') and (name[9] = 'E') then
CompInfo.O := 'F' //JNZ
else if name[8] = 'L' then
begin
if name[9] = 'E' then
CompInfo.O := 'O' //JLE
else if name[9] = 'T' then
CompInfo.O := 'M'; //JL
end
else if name[8] = 'G' then
begin
if name[9] = 'E' then
CompInfo.O := 'N' //JGE
else if name[9] = 'T' then
CompInfo.O := 'P'; //JG
end;
GetRegItem(16, item1); //eax - Left argument
if IF_STACK_PTR in item1.Flags then
begin
Env.Stack[item1.IntValue]._Type := 'Variant';
item1 := Env.Stack[item1.IntValue];
CompInfo.L := item1.Name;
end;
GetRegItem(18, item2); //edx - Right argument
if IF_STACK_PTR in item2.Flags then
begin
Env.Stack[item2.IntValue]._Type := 'Variant';
item2 := Env.Stack[item2.IntValue];
CompInfo.R := item2.Name;
end;
Result:=true;
Exit;
end
//Cast to Variant
else if SameText(name, '@VarFromBool') or
SameText(name, '@VarFromInt') or
Expand All @@ -7229,22 +7314,22 @@ function TDecompiler.DecompileTry(fromAdr:Integer; flags:TDecomCset; loopInfo:TL
if IF_STACK_PTR in item1.Flags then
begin
Env.Stack[item1.IntValue]._Type := 'Variant';
line := Env.GetLvarName(item1.IntValue);
item1 := Env.Stack[item1.IntValue];
end;
GetRegItem(18, item2);
line := line + ' := Variant(' + item2.Value + ');';
line := item1.Name + ' := Variant(' + item2.Name + ');';
Env.AddToBody(line);
Exit;
End
else if SameText(name, '@VarFromTDateTime') then
else if SameText(name, '@VarFromTDateTime') or SameText(name, '@VarFromCurr') then
Begin
GetRegItem(16, item1);
if IF_STACK_PTR in item1.Flags then
begin
Env.Stack[item1.IntValue]._Type := 'Variant';
line := Env.GetLvarName(item1.IntValue);
item1 := Env.Stack[item1.IntValue];
end;
line:=line + ' := Variant(' + FPop.Value + ')'; //FGet(0)
line:=item1.Name + ' := Variant(' + FPop.Value + ')'; //FGet(0)
Env.AddToBody(line);
FPop;
Exit;
Expand Down Expand Up @@ -9695,6 +9780,7 @@ procedure TDecompiler.GetFloatItemFromStack(Esp:Integer; Dst:PITEM; FloatType:TF
try
Env.AddToBody('begin');
_curAdr := de.Decompile(_bodyBegAdr, [], loopInfo);
if (_jmpAdr<>0) and IsExit(_jmpAdr) then Env.AddToBody('Exit;');
Env.AddToBody('end');
Except
on E:exception do
Expand All @@ -9705,7 +9791,7 @@ procedure TDecompiler.GetFloatItemFromStack(Esp:Integer; Dst:PITEM; FloatType:TF
end;
Env.RestoreContext(_bodyBegAdr); //if (_jmpAdr || de.WasRet)
de.Free;
if _jmpAdr<>0 then
if (_jmpAdr<>0)and not IsExit(_jmpAdr) then
Begin
Env.AddToBody('else');
_begAdr := _curAdr;
Expand Down
19 changes: 3 additions & 16 deletions Def_disasm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ interface
Const
ASMMAXCOPLEN = 12;

//Instruction type
itUnknown = 0; //Unknown instruction
itTransfer = 1; //Data transfer instruction
itArifm = 2; //Ariphmetical instruction
itLogic = 3; //Logical instruction
itControl = 4; //Control flow instruction
itString = 5; //String instruction
itFloat = 6; //Coprocessor instruction

otUND = 0;
otIMM = 1;
otREG = 2;
otMEM = 3;
otFST = 4;

OP_RESET = $80;
OP_A2 = $40; //2 or 3 operands

Expand Down Expand Up @@ -91,6 +76,8 @@ interface
);

Type
TOperType = (otUND, otIMM, otREG, otMEM, otFST);

TDisInfo = record
Mnem:String[32];
Op1:String[64];
Expand All @@ -116,7 +103,7 @@ TDisInfo = record
RepPrefix:Integer;
SegPrefix:Integer;
OpNum:Byte;
OpType:Array [0..2] of Byte;
OpType:Array [0..2] of TOperType;
End;
PDisInfo = ^TDisInfo;

Expand Down
Loading

0 comments on commit ea4e677

Please sign in to comment.