Skip to content

Commit

Permalink
Refactor: remove RtlMicroLabel.cs
Browse files Browse the repository at this point in the history
Intra-instruction microgotos have been simplified to just specify the index of the RTL instruction to jump to.
  • Loading branch information
uxmal committed Jun 10, 2024
1 parent ccee2ab commit 6aae3be
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 239 deletions.
6 changes: 2 additions & 4 deletions src/Arch/Loongson/LoongArchRewriter.Alu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ private void RewriteLoadBoundsCheck(PrimitiveType dt, Func<Expression, Expressio
{
var ea = binder.EnsureRegister((RegisterStorage) instr.Operands[1]);
var limit = binder.EnsureRegister((RegisterStorage) instr.Operands[2]);
m.MicroBranch(fn(ea, limit), "skip");
m.MicroBranch(fn(ea, limit), this.rtls.Count + 2);
m.SideEffect(m.Fn(raise_exception, m.Word32(0xA)));
m.MicroLabel("skip");
Assign(instr.Operands[0], m.Mem(dt, ea));
}

Expand Down Expand Up @@ -347,9 +346,8 @@ private void RewriteStoreBoundsCheck(PrimitiveType dt, Func<Expression, Expressi
var src = RewriteStoreSource(dt);
var ea = binder.EnsureRegister((RegisterStorage) instr.Operands[1]);
var limit = binder.EnsureRegister((RegisterStorage) instr.Operands[2]);
m.MicroBranch(fn(ea, limit), "skip");
m.MicroBranch(fn(ea, limit), this.rtls.Count + 2);
m.SideEffect(m.Fn(raise_exception, m.Word32(0xA)));
m.MicroLabel("skip");
m.Assign(m.Mem(dt, ea), src);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Arch/Loongson/LoongArchRewriter.Fpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ private void RewriteFLoadBoundaryCheck(PrimitiveType dt, Func<Expression,Express
{
var ea = binder.EnsureRegister((RegisterStorage) instr.Operands[1]);
var limit = binder.EnsureRegister((RegisterStorage) instr.Operands[2]);
m.MicroBranch(fn(ea, limit), "skip");
m.MicroBranch(fn(ea, limit), this.rtls.Count + 2);
m.SideEffect(m.Fn(raise_exception, m.Word32(0xA)));
m.MicroLabel("skip");
FpuAssign(0, m.Mem(dt, ea));
}

Expand Down
36 changes: 13 additions & 23 deletions src/Core/Rtl/RtlEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,37 +264,27 @@ public RtlEmitter Invalid()
}

/// <summary>
/// Generates an unconditional <see cref="RtlMicroGoto"/> to a <see cref="RtlMicroLabel" />.
/// Generates an unconditional <see cref="RtlMicroGoto"/> to another instruction inside
/// the current <see cref="RtlInstructionCluster"/>.
/// </summary>
/// <param name="microLabel">The RtlMicroLabel to go to.</param>
/// <returns>A reference to this RtlEmitter.</returns>
public RtlEmitter MicroGoto(string microLabel)
/// <param name="index">The index of the target <see cref="RtlInstruction"/>
/// <returns>A reference to this <see cref="RtlEmitter"/>.</returns>
public RtlEmitter MicroGoto(int index)
{
Instructions.Add(new RtlMicroGoto(null, microLabel));
Instructions.Add(new RtlMicroGoto(null, index));
return this;
}

/// <summary>
/// Generates a conditional <see cref="RtlMicroGoto"/> to a <see cref="RtlMicroLabel" />.
/// Generates a conditional <see cref="RtlMicroGoto"/> to another instruction inside
/// the current <see cref="RtlInstructionCluster"/>.
/// </summary>
/// <param name="cond">Condition which, when true, causes a transfer to the targetted microlabel</param>
/// <param name="microLabel">That RtlMicroLabel to go to.</param>
/// <returns>A reference to this RtlEmitter.</returns>
public RtlEmitter MicroBranch(Expression cond, string microLabel)
{
Instructions.Add(new RtlMicroGoto(cond, microLabel));
return this;
}

/// <summary>
/// Generates a microlabel, only reachable via a corresponding
/// <see cref="RtlMicroGoto"/> or <see cref="RtlMicroBranch"/> instruction.
/// </summary>
/// <param name="name"></param>
/// <returns>A reference to this RtlEmitter.</returns>
public RtlEmitter MicroLabel(string name)
/// <param name="cond">Condition which, when true, causes a transfer to the targetted instruction</param>
/// <param name="index">The index of the target <see cref="RtlInstruction"/>.
/// <returns>A reference to this <see cref="RtlEmitter"/>.</returns>
public RtlEmitter MicroBranch(Expression cond, int index)
{
Instructions.Add(new RtlMicroLabel(name));
Instructions.Add(new RtlMicroGoto(cond, index));
return this;
}

Expand Down
6 changes: 0 additions & 6 deletions src/Core/Rtl/RtlInstructionMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ public bool VisitMicroGoto(RtlMicroGoto mgo, ExpressionMatch m)
return mgo.Target == mgPat.Target;
}

public bool VisitMicroLabel(RtlMicroLabel mlabel, ExpressionMatch m)
{
return (pattern is RtlMicroLabel mpattern &&
mlabel.Name == mpattern.Name);
}

public bool VisitIf(RtlIf rtlIf, ExpressionMatch m)
{
if (pattern is not RtlIf pIf)
Expand Down
2 changes: 0 additions & 2 deletions src/Core/Rtl/RtlInstructionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public interface RtlInstructionVisitor<T>
T VisitIf(RtlIf rtlIf);
T VisitInvalid(RtlInvalid invalid);
T VisitMicroGoto(RtlMicroGoto uGoto);
T VisitMicroLabel(RtlMicroLabel uLabel);
T VisitNop(RtlNop rtlNop);
T VisitReturn(RtlReturn ret);
T VisitSideEffect(RtlSideEffect side);
Expand All @@ -49,7 +48,6 @@ public interface IRtlInstructionVisitor<T, C>
T VisitIf(RtlIf rtlIf, C ctx);
T VisitInvalid(RtlInvalid invalid, C ctx);
T VisitMicroGoto(RtlMicroGoto uGoto, C ctx);
T VisitMicroLabel(RtlMicroLabel uLabel, C ctx);
T VisitNop(RtlNop rtlNop, C ctx);
T VisitReturn(RtlReturn ret, C ctx);
T VisitSideEffect(RtlSideEffect side, C ctx);
Expand Down
22 changes: 9 additions & 13 deletions src/Core/Rtl/RtlMicroGoto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,22 @@ namespace Reko.Core.Rtl
/// control flow in RTL code.
/// </summary>
/// <remarks>
/// Micro-gotos are restricted to labels within the same <see cref="RtlInstructionCluster"/>.
/// Micro-gotos are restricted to offsets within the same <see cref="RtlInstructionCluster"/>.
/// If you want to jump outside of the cluster, you may only jump to the start of a rewritten
/// instruction using the <see cref="RtlGoto"/> instruction.
/// </remarks>
public class RtlMicroGoto : RtlInstruction
{
public RtlMicroGoto(object microTarget)
public RtlMicroGoto(int indexTarget)
{
Debug.Assert(microTarget is string || microTarget is RtlLocation);
this.Target = microTarget;
this.Target = indexTarget;
this.Class = InstrClass.Transfer;
}

public RtlMicroGoto(Expression? condition, object microTarget)
public RtlMicroGoto(Expression? condition, int indexTarget)
{
Debug.Assert(microTarget is string || microTarget is RtlLocation);
this.Condition = condition;
this.Target = microTarget;
this.Target = indexTarget;
this.Class = InstrClass.ConditionalTransfer;
}

Expand All @@ -62,14 +60,12 @@ public RtlMicroGoto(Expression? condition, object microTarget)
public Expression? Condition { get; }

/// <summary>
/// Name of the microLabelName to jump to. The micro label must be inside
/// Name of the microLabelName to jump to.
/// The target must be an index into the <see cref="RtlInstruction"/>s of the
/// current <see cref="RtlInstructionCluster"/>.
/// the current RtlInstructionCluster.
/// </summary>
/// <remarks>
/// The target can either be a symbolic string or an RtlLocation. Symbolic
/// strings are translated to RtlLocations during scanning.
/// </remarks>
public object Target { get; }
public int Target { get; }

public override T Accept<T>(RtlInstructionVisitor<T> visitor)
{
Expand Down
58 changes: 0 additions & 58 deletions src/Core/Rtl/RtlMicroLabel.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/Decompiler/Scanning/BackwardSlicer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,11 +1330,6 @@ public SlicerResult VisitMicroGoto(RtlMicroGoto uGoto)
{
throw new NotImplementedException();
}

public SlicerResult VisitMicroLabel(RtlMicroLabel uLabel)
{
throw new NotImplementedException();
}
}

public class SlicerResult
Expand Down
9 changes: 0 additions & 9 deletions src/Decompiler/Scanning/BlockConstantPropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@
using Reko.Core.Memory;
using Reko.Core.Operators;
using Reko.Core.Rtl;
using Reko.Core.Services;
using Reko.Evaluation;
using Reko.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Reko.Scanning
{

/// <summary>
/// Forward analysis that propagates constants,
/// but only within a single linear block.
Expand Down Expand Up @@ -112,11 +108,6 @@ public RtlInstruction VisitMicroGoto(RtlMicroGoto uGoto)
return uGoto;
}

public RtlInstruction VisitMicroLabel(RtlMicroLabel uLabel)
{
return uLabel;
}

public RtlInstruction VisitNop(RtlNop rtlNop)
{
return rtlNop;
Expand Down
5 changes: 0 additions & 5 deletions src/Decompiler/Scanning/BlockWorkitem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,6 @@ public bool VisitMicroGoto(RtlMicroGoto uGoto)
throw new NotImplementedException();
}

public bool VisitMicroLabel(RtlMicroLabel uLabel)
{
throw new NotImplementedException();
}

public bool VisitReturn(RtlReturn ret)
{
if ((ret.Class & InstrClass.Delay) != 0)
Expand Down
5 changes: 0 additions & 5 deletions src/Decompiler/Scanning/ProcedureGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@ public Instruction VisitMicroGoto(RtlMicroGoto uGoto, Context ctx)
throw new NotImplementedException();
}

public Instruction VisitMicroLabel(RtlMicroLabel uLabel, Context ctx)
{
throw new NotImplementedException();
}

public Instruction VisitNop(RtlNop rtlNop, Context ctx)
{
return null!;
Expand Down
Loading

0 comments on commit 6aae3be

Please sign in to comment.