
it.unive.lisa.analysis.dataflow.Liveness Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lisa-analyses Show documentation
Show all versions of lisa-analyses Show documentation
A library for static analysis
The newest version!
package it.unive.lisa.analysis.dataflow;
import it.unive.lisa.analysis.ScopeToken;
import it.unive.lisa.analysis.SemanticException;
import it.unive.lisa.program.cfg.ProgramPoint;
import it.unive.lisa.symbolic.value.BinaryExpression;
import it.unive.lisa.symbolic.value.Identifier;
import it.unive.lisa.symbolic.value.TernaryExpression;
import it.unive.lisa.symbolic.value.UnaryExpression;
import it.unive.lisa.symbolic.value.ValueExpression;
import it.unive.lisa.util.representation.StringRepresentation;
import it.unive.lisa.util.representation.StructuredRepresentation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* An implementation of the liveness dataflow analysis, that determines which
* values might be used later on in the program.
*
* @author Luca Negrini
*/
public class Liveness
implements
DataflowElement, Liveness> {
private final Identifier id;
/**
* Builds an empty liveness element.
*/
public Liveness() {
this(null);
}
/**
* Builds the liveness element for the specified id.
*
* @param id the id
*/
public Liveness(
Identifier id) {
this.id = id;
}
private static Collection getIdentifierOperands(
ValueExpression expression) {
Collection result = new HashSet<>();
if (expression == null)
return result;
if (expression instanceof Identifier)
result.add((Identifier) expression);
if (expression instanceof UnaryExpression)
result.addAll(getIdentifierOperands((ValueExpression) ((UnaryExpression) expression).getExpression()));
if (expression instanceof BinaryExpression) {
BinaryExpression binary = (BinaryExpression) expression;
result.addAll(getIdentifierOperands((ValueExpression) binary.getLeft()));
result.addAll(getIdentifierOperands((ValueExpression) binary.getRight()));
}
if (expression instanceof TernaryExpression) {
TernaryExpression ternary = (TernaryExpression) expression;
result.addAll(getIdentifierOperands((ValueExpression) ternary.getLeft()));
result.addAll(getIdentifierOperands((ValueExpression) ternary.getMiddle()));
result.addAll(getIdentifierOperands((ValueExpression) ternary.getRight()));
}
return result;
}
@Override
public Liveness pushScope(
ScopeToken token)
throws SemanticException {
return new Liveness((Identifier) id.pushScope(token));
}
@Override
public Liveness popScope(
ScopeToken token)
throws SemanticException {
return new Liveness((Identifier) id.popScope(token));
}
@Override
public Collection getInvolvedIdentifiers() {
return Collections.singleton(id);
}
@Override
public StructuredRepresentation representation() {
return new StringRepresentation(id);
}
@Override
public String toString() {
return representation().toString();
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(
Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Liveness other = (Liveness) obj;
return Objects.equals(id, other.id);
}
@Override
public Collection gen(
Identifier id,
ValueExpression expression,
ProgramPoint pp,
PossibleDataflowDomain domain)
throws SemanticException {
Collection ids = getIdentifierOperands(expression);
return ids.stream().map(Liveness::new).collect(Collectors.toSet());
}
@Override
public Collection gen(
ValueExpression expression,
ProgramPoint pp,
PossibleDataflowDomain domain)
throws SemanticException {
Collection ids = getIdentifierOperands(expression);
return ids.stream().map(Liveness::new).collect(Collectors.toSet());
}
@Override
public Collection kill(
Identifier id,
ValueExpression expression,
ProgramPoint pp,
PossibleDataflowDomain domain)
throws SemanticException {
return Collections.singleton(new Liveness(id));
}
@Override
public Collection kill(
ValueExpression expression,
ProgramPoint pp,
PossibleDataflowDomain domain)
throws SemanticException {
return Collections.emptySet();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy