Skip to content

Commit

Permalink
Returning both SLIFO and non-SLIFO ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
avishek-sen-gupta committed Nov 17, 2024
1 parent 6eff5fb commit 62f5384
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -13,12 +15,17 @@ public SLIFORangeCriterionTask(Set<InvokingProcedureRange> rangesWithChildren) {
this.rangesWithChildren = rangesWithChildren;
}

public Pair<Set<InvokingProcedureRange>, Set<InvokingProcedureRange>> run() {
Set<InvokingProcedureRange> allSLIFORanges = allSLIFORanges(this.rangesWithChildren, ImmutableSet.of());
Set<InvokingProcedureRange> nonSLIFORanges = Sets.difference(rangesWithChildren, allSLIFORanges);
return ImmutablePair.of(allSLIFORanges, nonSLIFORanges);
}

public Set<ProcedureRange> rangesTerminatingIn(ProcedureRange range) {
Set<ProcedureRange> procedureRanges = rangesWithChildren.stream().map(InvokingProcedureRange::range).collect(Collectors.toUnmodifiableSet());
Set<TranspilerInstruction> bodyVertices = range.body().vertexSet();
Set<TranspilerInstruction> selfExit = bodyVertices.stream().filter(v -> v == range.exit()).collect(Collectors.toUnmodifiableSet());
Set<TranspilerInstruction> body = Sets.difference(bodyVertices, selfExit);
// Set<TranspilerInstruction> body = bodyVertices;
Set<ProcedureRange> rangesTerminatingInCurrentRangeIncludingSelf = procedureRanges.stream()
.filter(bbr -> body.contains(bbr.exit()))
.collect(Collectors.toUnmodifiableSet());
Expand All @@ -32,19 +39,15 @@ public boolean isSLIFO(InvokingProcedureRange rangeWithChildren, Set<ProcedureRa
&& Sets.difference(invokedRanges, existingSLIFORanges).isEmpty();
}

public Set<InvokingProcedureRange> allSLIFORanges(Set<InvokingProcedureRange> rangesWithChildren, Set<InvokingProcedureRange> existingSLIFORanges) {
Set<InvokingProcedureRange> newlyDiscoveredSLIFOProcedures = rangesWithChildren.stream()
private Set<InvokingProcedureRange> allSLIFORanges(Set<InvokingProcedureRange> uncategorisedRanges, Set<InvokingProcedureRange> existingSLIFORanges) {
Set<InvokingProcedureRange> newlyDiscoveredSLIFOProcedures = uncategorisedRanges.stream()
.filter(rwc -> isSLIFO(rwc, existingSLIFORanges.stream()
.map(InvokingProcedureRange::range)
.collect(Collectors.toUnmodifiableSet())))
.collect(Collectors.toUnmodifiableSet());
if (newlyDiscoveredSLIFOProcedures.isEmpty()) return ImmutableSet.of();
return Sets.union(newlyDiscoveredSLIFOProcedures, allSLIFORanges(
Sets.difference(rangesWithChildren, newlyDiscoveredSLIFOProcedures),
Sets.difference(uncategorisedRanges, newlyDiscoveredSLIFOProcedures),
Sets.union(existingSLIFORanges, newlyDiscoveredSLIFOProcedures)));
}

public Set<InvokingProcedureRange> allSLIFORanges(Set<InvokingProcedureRange> rangesWithChildren) {
return allSLIFORanges(rangesWithChildren, ImmutableSet.of());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.smojol.toolkit.examples;

import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.tuple.Pair;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultEdge;
import org.smojol.common.dialect.LanguageDialect;
Expand Down Expand Up @@ -44,8 +45,9 @@ LanguageDialect.IDMS, new FullProgram(FlowchartOutputFormat.MERMAID, idProvider)
Graph<TranspilerInstruction, DefaultEdge> implicitCFG = new BuildImplicitInstructionControlFlowgraphTask(instructions, ImmutableList.of()).run();
Set<InvokingProcedureRange> rangesWithChildren = new ProcedureBodyTask(tree, instructions, implicitCFG).run();
SLIFORangeCriterionTask task = new SLIFORangeCriterionTask(rangesWithChildren);
Set<InvokingProcedureRange> allSLIFORanges = task.allSLIFORanges(rangesWithChildren);
allSLIFORanges.forEach(range -> System.out.println(range.range()));
Pair<Set<InvokingProcedureRange>, Set<InvokingProcedureRange>> categorisedRanges = task.run();
categorisedRanges.getLeft().forEach(range -> System.out.println(range.range()));
categorisedRanges.getRight().forEach(range -> System.out.println(range.range()));

System.out.println("DONE");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.apache.commons.lang3.tuple.Pair;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultEdge;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -88,7 +89,8 @@ public void canDetermineSLIFORangesInductively() {
Set<InvokingProcedureRange> rangesWithChildren = new ProcedureBodyTask(program, instructions, implicitCFG).run();
SLIFORangeCriterionTask task = new SLIFORangeCriterionTask(rangesWithChildren);
assertEquals(2, rangesWithChildren.size());
Set<InvokingProcedureRange> allSLIFORanges = task.allSLIFORanges(rangesWithChildren);
Pair<Set<InvokingProcedureRange>, Set<InvokingProcedureRange>> categorisedRanges = task.run();
Set<InvokingProcedureRange> allSLIFORanges = categorisedRanges.getLeft();
assertEquals(2, allSLIFORanges.size());
}

Expand Down

0 comments on commit 62f5384

Please sign in to comment.