Skip to content

Commit

Permalink
Feature: improve code generation for SLICEs of conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
uxmal committed Sep 20, 2024
1 parent 6118ec9 commit 2613152
Show file tree
Hide file tree
Showing 89 changed files with 685,772 additions and 686,717 deletions.
37 changes: 36 additions & 1 deletion src/Decompiler/Evaluation/SliceConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Reko.Core.Expressions;
using Reko.Core.Types;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Reko.Evaluation
{
Expand Down Expand Up @@ -52,25 +53,59 @@ public SliceConvert()
return null;
}
Expression result;
if (slice.Expression is Conversion conv)
if (IsApplicableConversion(ctx, slice.Expression, out Conversion? conv))
{
if (IsUselessIntegralExtension(slice, conv))
{
result = slice.DataType.BitSize == conv.SourceDataType.BitSize
? conv.Expression
: new Slice(slice.DataType, conv.Expression, 0);
ctx.RemoveExpressionUse(slice);
ctx.UseExpression(result);
return result;
}
if (CanSliceConversion(slice, conv))
{
result = new Conversion(
conv.Expression, conv.SourceDataType, slice.DataType);
ctx.RemoveExpressionUse(slice);
ctx.UseExpression(result);
return result;
}
}
return null;
}

/// <summary>
/// A conversion is applicable if
/// </summary>
/// <param name="conv"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private bool IsApplicableConversion(
EvaluationContext ctx,
Expression sliceExp,
[MaybeNullWhen(false)] out Conversion conv)
{
if (sliceExp is Identifier id)
{
var def = ctx.GetDefiningExpression(id);
if (def is Conversion c2 &&
c2.Expression is Identifier)
{
conv = c2;
return true;
}
}
else if (sliceExp is Conversion c)
{
conv = c;
return true;
}
conv = null;
return false;
}

private static bool CanSliceConversion(Slice slice, Conversion conv)
{
if (IsSliceable(slice.DataType) &&
Expand Down
Loading

0 comments on commit 2613152

Please sign in to comment.