2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.Instruction;
|
|
|
|
using ChocolArm64.Memory;
|
2018-05-26 16:49:21 -04:00
|
|
|
using ChocolArm64.State;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System;
|
2018-02-22 14:26:11 -05:00
|
|
|
using System.Collections.Concurrent;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Decoder
|
|
|
|
{
|
|
|
|
static class ADecoder
|
|
|
|
{
|
2018-02-22 14:26:11 -05:00
|
|
|
private delegate object OpActivator(AInst Inst, long Position, int OpCode);
|
|
|
|
|
|
|
|
private static ConcurrentDictionary<Type, OpActivator> OpActivators;
|
|
|
|
|
|
|
|
static ADecoder()
|
|
|
|
{
|
|
|
|
OpActivators = new ConcurrentDictionary<Type, OpActivator>();
|
|
|
|
}
|
|
|
|
|
2018-09-19 16:07:56 -04:00
|
|
|
public static ABlock DecodeBasicBlock(AThreadState State, AMemory Memory, long Start)
|
2018-03-04 12:09:59 -05:00
|
|
|
{
|
|
|
|
ABlock Block = new ABlock(Start);
|
|
|
|
|
2018-05-26 16:49:21 -04:00
|
|
|
FillBlock(State, Memory, Block);
|
2018-03-04 12:09:59 -05:00
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
2018-02-25 20:14:58 -05:00
|
|
|
public static (ABlock[] Graph, ABlock Root) DecodeSubroutine(
|
2018-09-19 16:07:56 -04:00
|
|
|
ATranslatorCache Cache,
|
|
|
|
AThreadState State,
|
|
|
|
AMemory Memory,
|
|
|
|
long Start)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
Dictionary<long, ABlock> Visited = new Dictionary<long, ABlock>();
|
|
|
|
Dictionary<long, ABlock> VisitedEnd = new Dictionary<long, ABlock>();
|
|
|
|
|
|
|
|
Queue<ABlock> Blocks = new Queue<ABlock>();
|
|
|
|
|
|
|
|
ABlock Enqueue(long Position)
|
|
|
|
{
|
|
|
|
if (!Visited.TryGetValue(Position, out ABlock Output))
|
|
|
|
{
|
|
|
|
Output = new ABlock(Position);
|
|
|
|
|
|
|
|
Blocks.Enqueue(Output);
|
|
|
|
|
|
|
|
Visited.Add(Position, Output);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
ABlock Root = Enqueue(Start);
|
|
|
|
|
|
|
|
while (Blocks.Count > 0)
|
|
|
|
{
|
|
|
|
ABlock Current = Blocks.Dequeue();
|
|
|
|
|
2018-05-26 16:49:21 -04:00
|
|
|
FillBlock(State, Memory, Current);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
//Set child blocks. "Branch" is the block the branch instruction
|
|
|
|
//points to (when taken), "Next" is the block at the next address,
|
|
|
|
//executed when the branch is not taken. For Unconditional Branches
|
|
|
|
//(except BL/BLR that are sub calls) or end of executable, Next is null.
|
|
|
|
if (Current.OpCodes.Count > 0)
|
|
|
|
{
|
|
|
|
bool HasCachedSub = false;
|
|
|
|
|
|
|
|
AOpCode LastOp = Current.GetLastOp();
|
|
|
|
|
|
|
|
if (LastOp is AOpCodeBImm Op)
|
|
|
|
{
|
|
|
|
if (Op.Emitter == AInstEmit.Bl)
|
|
|
|
{
|
2018-09-19 16:07:56 -04:00
|
|
|
HasCachedSub = Cache.HasSubroutine(Op.Imm);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Current.Branch = Enqueue(Op.Imm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 12:09:59 -05:00
|
|
|
if (!((LastOp is AOpCodeBImmAl) ||
|
|
|
|
(LastOp is AOpCodeBReg)) || HasCachedSub)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
Current.Next = Enqueue(Current.EndPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 19:37:10 -04:00
|
|
|
//If we have on the graph two blocks with the same end position,
|
2018-02-04 18:08:20 -05:00
|
|
|
//then we need to split the bigger block and have two small blocks,
|
|
|
|
//the end position of the bigger "Current" block should then be == to
|
|
|
|
//the position of the "Smaller" block.
|
|
|
|
while (VisitedEnd.TryGetValue(Current.EndPosition, out ABlock Smaller))
|
|
|
|
{
|
|
|
|
if (Current.Position > Smaller.Position)
|
|
|
|
{
|
|
|
|
ABlock Temp = Smaller;
|
|
|
|
|
|
|
|
Smaller = Current;
|
|
|
|
Current = Temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Current.EndPosition = Smaller.Position;
|
|
|
|
Current.Next = Smaller;
|
|
|
|
Current.Branch = null;
|
|
|
|
|
|
|
|
Current.OpCodes.RemoveRange(
|
|
|
|
Current.OpCodes.Count - Smaller.OpCodes.Count,
|
|
|
|
Smaller.OpCodes.Count);
|
|
|
|
|
|
|
|
VisitedEnd[Smaller.EndPosition] = Smaller;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisitedEnd.Add(Current.EndPosition, Current);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make and sort Graph blocks array by position.
|
|
|
|
ABlock[] Graph = new ABlock[Visited.Count];
|
|
|
|
|
|
|
|
while (Visited.Count > 0)
|
|
|
|
{
|
|
|
|
ulong FirstPos = ulong.MaxValue;
|
|
|
|
|
|
|
|
foreach (ABlock Block in Visited.Values)
|
|
|
|
{
|
|
|
|
if (FirstPos > (ulong)Block.Position)
|
|
|
|
FirstPos = (ulong)Block.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
ABlock Current = Visited[(long)FirstPos];
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Graph[Graph.Length - Visited.Count] = Current;
|
|
|
|
|
|
|
|
Visited.Remove(Current.Position);
|
|
|
|
|
|
|
|
Current = Current.Next;
|
|
|
|
}
|
|
|
|
while (Current != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Graph, Root);
|
|
|
|
}
|
|
|
|
|
2018-05-26 16:49:21 -04:00
|
|
|
private static void FillBlock(AThreadState State, AMemory Memory, ABlock Block)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
long Position = Block.Position;
|
|
|
|
|
|
|
|
AOpCode OpCode;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-05-26 16:49:21 -04:00
|
|
|
//TODO: This needs to be changed to support both AArch32 and AArch64,
|
|
|
|
//once JIT support is introduced on AArch32 aswell.
|
|
|
|
OpCode = DecodeOpCode(State, Memory, Position);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
Block.OpCodes.Add(OpCode);
|
|
|
|
|
|
|
|
Position += 4;
|
|
|
|
}
|
|
|
|
while (!(IsBranch(OpCode) || IsException(OpCode)));
|
|
|
|
|
|
|
|
Block.EndPosition = Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsBranch(AOpCode OpCode)
|
|
|
|
{
|
|
|
|
return OpCode is AOpCodeBImm ||
|
|
|
|
OpCode is AOpCodeBReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsException(AOpCode OpCode)
|
|
|
|
{
|
2018-02-10 08:24:16 -05:00
|
|
|
return OpCode.Emitter == AInstEmit.Brk ||
|
|
|
|
OpCode.Emitter == AInstEmit.Svc ||
|
2018-02-04 18:08:20 -05:00
|
|
|
OpCode.Emitter == AInstEmit.Und;
|
|
|
|
}
|
|
|
|
|
2018-05-26 16:49:21 -04:00
|
|
|
public static AOpCode DecodeOpCode(AThreadState State, AMemory Memory, long Position)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
int OpCode = Memory.ReadInt32(Position);
|
|
|
|
|
2018-05-26 16:49:21 -04:00
|
|
|
AInst Inst;
|
|
|
|
|
|
|
|
if (State.ExecutionMode == AExecutionMode.AArch64)
|
|
|
|
{
|
|
|
|
Inst = AOpCodeTable.GetInstA64(OpCode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: Thumb support.
|
|
|
|
Inst = AOpCodeTable.GetInstA32(OpCode);
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-10 12:20:46 -05:00
|
|
|
AOpCode DecodedOpCode = new AOpCode(AInst.Undefined, Position, OpCode);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (Inst.Type != null)
|
|
|
|
{
|
2018-02-22 14:26:11 -05:00
|
|
|
DecodedOpCode = MakeOpCode(Inst.Type, Inst, Position, OpCode);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return DecodedOpCode;
|
|
|
|
}
|
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
private static AOpCode MakeOpCode(Type Type, AInst Inst, long Position, int OpCode)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
if (Type == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(Type));
|
|
|
|
}
|
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
OpActivator CreateInstance = OpActivators.GetOrAdd(Type, CacheOpActivator);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
return (AOpCode)CreateInstance(Inst, Position, OpCode);
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
private static OpActivator CacheOpActivator(Type Type)
|
|
|
|
{
|
|
|
|
Type[] ArgTypes = new Type[] { typeof(AInst), typeof(long), typeof(int) };
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
DynamicMethod Mthd = new DynamicMethod($"Make{Type.Name}", Type, ArgTypes);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
ILGenerator Generator = Mthd.GetILGenerator();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
Generator.Emit(OpCodes.Ldarg_0);
|
|
|
|
Generator.Emit(OpCodes.Ldarg_1);
|
|
|
|
Generator.Emit(OpCodes.Ldarg_2);
|
|
|
|
Generator.Emit(OpCodes.Newobj, Type.GetConstructor(ArgTypes));
|
|
|
|
Generator.Emit(OpCodes.Ret);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-02-22 14:26:11 -05:00
|
|
|
return (OpActivator)Mthd.CreateDelegate(typeof(OpActivator));
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|