Skip to content

Commit

Permalink
ExpressionTypeDescender: push array element if binary expression matc…
Browse files Browse the repository at this point in the history
…hes with array pattern
  • Loading branch information
ptomin committed Dec 2, 2023
1 parent aca8ae6 commit fdb97f3
Show file tree
Hide file tree
Showing 25 changed files with 50,188 additions and 47,506 deletions.
39 changes: 38 additions & 1 deletion src/Decompiler/Typing/ExpressionTypeDescender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class ExpressionTypeDescender : ExpressionVisitor<bool, TypeVariable>
protected readonly TypeStore store;
protected readonly TypeFactory factory;
protected readonly Unifier unifier;
protected readonly ArrayExpressionMatcher aem;
protected readonly Identifier globals;
protected readonly IReadOnlyDictionary<Identifier,LinearInductionVariable> ivs;

Expand All @@ -67,6 +68,7 @@ public ExpressionTypeDescender(IReadOnlyProgram program, TypeStore store, TypeFa
this.store = store;
this.factory = factory;
this.unifier = new DataTypeBuilderUnifier(factory, store);
this.aem = new ArrayExpressionMatcher(program.Platform.PointerType);
}

protected virtual TypeVariable TypeVar(Expression exp)
Expand Down Expand Up @@ -216,7 +218,19 @@ private TypeVariable ArrayField(
TypeVariable tvField)
{
var dtElement = factory.CreateStructureType(null, elementSize);
dtElement.Fields.Add(0, tvField);
if (
tvField.DataType is StructureType strField &&
strField.Size == 0)
{
foreach (var f in strField.Fields)
{
dtElement.Fields.Add(f);
}
}
else
{
dtElement.Fields.Add(0, tvField);
}
var tvElement = store.CreateTypeVariable(factory);
tvElement.DataType = dtElement;
tvElement.OriginalDataType = dtElement;
Expand Down Expand Up @@ -263,6 +277,7 @@ public bool VisitBinaryExpression(BinaryExpression binExp, TypeVariable tv)
{
case OperatorType.IAdd:
{
PushArrayElement(binExp);
var dt = PushAddendDataType(tvBin.DataType, tvRight.DataType);
if (dt != null)
MeetDataType(eLeft, dt);
Expand Down Expand Up @@ -439,6 +454,28 @@ public bool VisitBinaryExpression(BinaryExpression binExp, TypeVariable tv)
return dtSum;
}

private void PushArrayElement(BinaryExpression binExp)
{
var eLeft = binExp.Left;
var tvBin = TypeVar(binExp);
if (tvBin.DataType is not Pointer binPtr)
return;
if (!aem.Match(binExp))
return;
if (aem.ArrayPointer == eLeft)
{
if (binPtr.Pointee is not TypeVariable tvField)
{
tvField = store.CreateTypeVariable(factory);
tvField.DataType = binPtr.Pointee;
tvField.OriginalDataType = binPtr.Pointee;
}
ArrayField(
null, eLeft, eLeft.DataType.BitSize, 0,
OffsetOf(aem.ElementSize!), 0, tvField);
}
}

private static DataType PushMinuendDataType(DataType dtDiff, DataType dtSub)
{
if (dtDiff.Domain == Domain.Pointer)
Expand Down
Loading

0 comments on commit fdb97f3

Please sign in to comment.