-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation for live variable analysis.
- Loading branch information
1 parent
ec84102
commit d516eb2
Showing
11 changed files
with
287 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
CURRENT_DIR=$(dirname "$(readlink -f "$0")") | ||
|
||
pushd $CURRENT_DIR/.. | ||
|
||
rm -rf net-ssa-cli/obj net-ssa-lib/obj unit-tests/obj net-ssa-cli/bin net-ssa-lib/bin unit-tests/bin | ||
|
||
dotnet clean | ||
rm -rf /tmp/build | ||
|
||
dotnet build | ||
./ci/nuget-pack.sh "0.0.0" "0.0.0" "0.0.0" "0.0.0" "/tmp/build/bin/net-ssa/package" | ||
|
||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
integration-test/net-ssa-lib/live-variables/test0/program.cs.exclude
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
class HelloWorld | ||
{ | ||
static void Main() | ||
{ | ||
for (int i=0; i < 10; i++){ | ||
Console.WriteLine(i); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// RUN: %mcs -out:%T/Test.dll %S/program.cs.exclude | ||
// RUN: (export BINARY_PATH=%T/Test.dll && dotnet repl --output-path %t.trx --run %s --exit-after-run) | ||
|
||
#i "nuget:/tmp/build/bin/net-ssa/package" | ||
#r "nuget: net-ssa-lib, 0.0.0" | ||
#r "nuget: Mono.Cecil, 0.11.3" | ||
#r "nuget: NUnit, 3.12.0" | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Mono.Cecil; | ||
using Mono.Cecil.Cil; | ||
using Mono.Cecil.Rocks; | ||
|
||
using NetSsa.Analyses; | ||
using NetSsa.Instructions; | ||
|
||
using NUnit.Framework; | ||
using System.Linq; | ||
|
||
public static String GetRegisters(IList<Register> registers, BitArray liveVariables){ | ||
String result = ""; | ||
for (int i=0; i < liveVariables.Length; i++){ | ||
if (liveVariables[i]){ | ||
result += registers[i].Name + " "; | ||
} | ||
} | ||
return result.Trim(); | ||
} | ||
|
||
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(Environment.GetEnvironmentVariable("BINARY_PATH")); | ||
|
||
var method = assembly.MainModule.GetType("HelloWorld").GetMethods().First(); | ||
Mono.Cecil.Cil.MethodBody body = method.Body; | ||
IRBody irBody = Unstacker.Compute(body); | ||
ControlFlowGraph cfg = new ControlFlowGraph(irBody); | ||
LiveVariableAnalysis lva = new LiveVariableAnalysis(cfg); | ||
lva.Flow(); | ||
|
||
/* | ||
L_0000: label | ||
L_0001: nop | ||
L_0002: s0 = ldc.i4.0 | ||
L_0003: l0 = stloc.0 [s0] -- s0 | ||
L_0004: br L_000c | ||
L_0005: label | ||
L_0006: s0 = ldloc.0 [l0] | ||
L_0007: call System.Void System.Console::WriteLine(System.Int32) [s0] -- s0 | ||
L_0008: s0 = ldloc.0 [l0] | ||
L_0009: s1 = ldc.i4.1 ---- [s0] | ||
L_000a: s0 = add [s0, s1] --- [s0, s1] | ||
L_000b: l0 = stloc.0 [s0] --- [s0] | ||
L_000c: label | ||
L_000d: s0 = ldloc.0 [l0] | ||
L_000e: s1 = ldc.i4.s 10 ---- [s0] | ||
L_000f: blt L_0005 [s0, s1] -- s0 s1 | ||
L_0010: label | ||
L_0011: ret | ||
*/ | ||
|
||
Assert.AreEqual(irBody.Instructions.Count, 0x12); | ||
|
||
Dictionary<int, string> results = new Dictionary<int, string>() | ||
{ | ||
{ 0x0, "" }, | ||
{ 0x1, "" }, | ||
{ 0x2, "" }, | ||
{ 0x3, "s0" }, | ||
{ 0x4, "" }, | ||
{ 0x5, "" }, | ||
{ 0x6, "" }, | ||
{ 0x7, "s0" }, | ||
{ 0x8, "" }, | ||
{ 0x9, "s0" }, | ||
{ 0xa, "s0 s1" }, | ||
{ 0xb, "s0" }, | ||
{ 0xc, "" }, | ||
{ 0xd, "" }, | ||
{ 0xe, "s0" }, | ||
{ 0xf, "s0 s1" }, | ||
{ 0x10, "" }, | ||
{ 0x11, "" }, | ||
}; | ||
|
||
for (int i=0; i < 0x12; i++){ | ||
Assert.AreEqual(results[i], GetRegisters(irBody.Registers, lva.IN[irBody.Instructions.ElementAt(i)]), i.ToString("x")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Collections.Generic; | ||
using NetSsa.Instructions; | ||
using System.Linq; | ||
|
||
namespace NetSsa.Analyses | ||
{ | ||
public abstract class BackwardsDataFlowAnalysis<Data> | ||
{ | ||
public BackwardsDataFlowAnalysis(ControlFlowGraph cfg){ | ||
this.cfg = cfg; | ||
this.irBody = cfg.IRBody; | ||
} | ||
|
||
protected ControlFlowGraph cfg; | ||
protected IRBody irBody; | ||
|
||
protected abstract Data Meet(Data a, Data b); // This is expected to create a copy of the input data | ||
protected virtual Data MeetSuccessors(TacInstruction leader){ | ||
IList<TacInstruction> successors = cfg.BasicBlockSuccessors(leader); | ||
Data result = IN[successors[0]]; | ||
for (int i = 1; i < successors.Count; i++){ | ||
result = Meet(result, IN[successors[i]]); | ||
} | ||
return result; | ||
} | ||
|
||
protected abstract Data Gen(TacInstruction leader); | ||
protected abstract Data Kill(TacInstruction leader); | ||
protected abstract bool TransferInstruction(TacInstruction instruction, Data incomingData); | ||
|
||
protected virtual void Transfer(TacInstruction leader, Data incomingData, ref bool changed){ | ||
IList<TacInstruction> instructions = cfg.BasicBlockInstructions(leader).ToList(); | ||
|
||
bool currentChanged = TransferInstruction(instructions[instructions.Count-1], incomingData); | ||
for (int i=instructions.Count-2; i >= 0; i--){ | ||
currentChanged = currentChanged || TransferInstruction(instructions[i], IN[instructions[i+1]]); | ||
} | ||
|
||
changed = currentChanged; | ||
} | ||
|
||
protected abstract Data InitBasicBlock(TacInstruction leader); | ||
|
||
public IDictionary<TacInstruction, Data> IN = new Dictionary<TacInstruction, Data>(); | ||
public IDictionary<TacInstruction, Data> OUT = new Dictionary<TacInstruction, Data>(); | ||
|
||
protected virtual IEnumerable<TacInstruction> GetExitBasicBlocks(){ | ||
foreach (TacInstruction leader in cfg.Leaders()){ | ||
if (cfg.BasicBlockSuccessors(leader).Count == 0){ | ||
yield return leader; | ||
} | ||
} | ||
} | ||
|
||
protected void InitializeBasicBlocks(){ | ||
foreach (TacInstruction leader in cfg.Leaders()){ | ||
foreach (TacInstruction instruction in cfg.BasicBlockInstructions(leader)){ | ||
IN[instruction] = InitBasicBlock(leader); | ||
} | ||
} | ||
} | ||
|
||
public void Flow(){ | ||
InitializeBasicBlocks(); | ||
bool inChanged; | ||
do { | ||
inChanged = false; | ||
foreach (TacInstruction leader in cfg.Leaders().Except(GetExitBasicBlocks())){ | ||
OUT[leader] = MeetSuccessors(leader); | ||
Transfer(leader, OUT[leader], ref inChanged); | ||
} | ||
} while (inChanged); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections.Generic; | ||
using System.Collections; | ||
using System; | ||
using NetSsa.Instructions; | ||
using System.Linq; | ||
|
||
namespace NetSsa.Analyses{ | ||
public class LiveVariableAnalysis : BackwardsDataFlowAnalysis<BitArray> { | ||
public LiveVariableAnalysis(ControlFlowGraph cfg) : base(cfg) {} | ||
|
||
private int NumberOfRegisters(){ | ||
return irBody.Registers.Count; | ||
} | ||
|
||
// TO-DO: Find faster implementation | ||
private int RegisterIndex(Register register){ | ||
return irBody.Registers.IndexOf(register); | ||
} | ||
|
||
// TO-DO: Find faster implementation | ||
private bool AreEqual(BitArray a, BitArray b) { | ||
if (a.Length != b.Length){ | ||
return false; | ||
} | ||
|
||
for (int i=0; i < a.Length; i++){ | ||
if (a[i] != b[i]){ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected override BitArray Meet(BitArray a, BitArray b) { | ||
return a.Or(b); | ||
} | ||
|
||
protected override BitArray Gen(TacInstruction instruction) { | ||
BitArray result = new BitArray(NumberOfRegisters()); | ||
foreach (int index in instruction.Operands.OfType<Register>().Select(r => RegisterIndex(r))){ | ||
result.Set(index, true); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected override BitArray Kill(TacInstruction instruction) { | ||
BitArray result = new BitArray(NumberOfRegisters()); | ||
|
||
if (instruction.Result is Register){ | ||
result.Set(RegisterIndex((Register)instruction.Result), true); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected override bool TransferInstruction(TacInstruction instruction, BitArray incomingData){ | ||
BitArray gen = Gen(instruction); | ||
BitArray kill = Kill(instruction); | ||
|
||
// The difference of two sets S T is computed by | ||
// complementing the bit vector of T, and then taking the logical AND of that | ||
// complement, with the bit vector for S. | ||
|
||
BitArray newValue = gen.Or(kill.Not().And(incomingData)); | ||
|
||
BitArray old = IN[instruction]; | ||
if (AreEqual(old, newValue)){ | ||
return false; | ||
} else { | ||
IN[instruction] = newValue; | ||
return true; | ||
} | ||
} | ||
|
||
protected override BitArray InitBasicBlock(TacInstruction leader){ | ||
return new BitArray(NumberOfRegisters()); | ||
} | ||
} | ||
} |