|
1 | 1 | package tip.analysis |
2 | 2 |
|
3 | | -import tip.ast._ |
4 | | -import tip.cfg.{CfgNode, CfgStmtNode, IntraproceduralProgramCfg} |
5 | | -import tip.lattices.{FlatLattice, MapLattice} |
6 | | -import tip.solvers.{SimpleMapLatticeFixpointSolver, SimpleWorklistFixpointSolver} |
7 | | -import tip.ast.AstNodeData.{AstNodeWithDeclaration, DeclarationData} |
8 | | - |
9 | | -abstract class ConstantPropagationAnalysis(cfg: IntraproceduralProgramCfg)(implicit declData: DeclarationData) extends FlowSensitiveAnalysis[CfgNode](cfg) { |
10 | | - |
11 | | - import tip.cfg.CfgOps._ |
12 | | - |
13 | | - val declaredVars: Set[ADeclaration] = cfg.nodes.flatMap(_.declaredVars) |
14 | | - |
15 | | - val lattice = new MapLattice(cfg.nodes, new MapLattice(declaredVars, new FlatLattice[Int]())) |
16 | | - |
17 | | - def transfer(n: CfgNode, s: lattice.sublattice.Element): lattice.sublattice.Element = |
18 | | - n match { |
19 | | - case r: CfgStmtNode => |
20 | | - r.data match { |
21 | | - case varr: AVarStmt => |
22 | | - varr.declIds.foldLeft(s) { (acc, id) => |
23 | | - acc + (id -> lattice.sublattice.sublattice.Top) |
24 | | - } |
25 | | - case ass: AAssignStmt => |
26 | | - ass.left match { |
27 | | - case id: AIdentifier => |
28 | | - val vdef: ADeclaration = id.declaration |
29 | | - s + (vdef -> absEval(ass.right, s)) |
30 | | - case _ => ??? |
31 | | - } |
32 | | - case _ => s |
33 | | - } |
34 | | - case _ => s |
35 | | - } |
36 | | - |
37 | | - private def absEval(exp: AExpr, env: Map[ADeclaration, lattice.sublattice.sublattice.Element]): lattice.sublattice.sublattice.Element = |
38 | | - exp match { |
39 | | - case id: AIdentifier => env(id.declaration) |
40 | | - case num: ANumber => lattice.sublattice.sublattice.FlatEl(num.value) |
41 | | - case bin: ABinaryOp => |
42 | | - val left = absEval(bin.left, env) |
43 | | - val right = absEval(bin.right, env) |
44 | | - (left, right) match { |
45 | | - case (lattice.sublattice.sublattice.FlatEl(x), lattice.sublattice.sublattice.FlatEl(y)) => |
46 | | - bin.operator match { |
47 | | - case Eqq => lattice.sublattice.sublattice.FlatEl(if (x == y) 1 else 0) |
48 | | - case Divide => if (y != 0) lattice.sublattice.sublattice.FlatEl(x / y) else lattice.sublattice.sublattice.Top |
49 | | - case GreatThan => lattice.sublattice.sublattice.FlatEl(if (x > y) 1 else 0) |
50 | | - case Minus => lattice.sublattice.sublattice.FlatEl(x - y) |
51 | | - case Plus => lattice.sublattice.sublattice.FlatEl(x + y) |
52 | | - case Times => lattice.sublattice.sublattice.FlatEl(x * y) |
53 | | - case _ => ??? |
54 | | - } |
55 | | - case (lattice.sublattice.sublattice.Bot, _) => lattice.sublattice.sublattice.Bot |
56 | | - case (_, lattice.sublattice.sublattice.Bot) => lattice.sublattice.sublattice.Bot |
57 | | - case (_, lattice.sublattice.sublattice.Top) => lattice.sublattice.sublattice.Top |
58 | | - case (lattice.sublattice.sublattice.Top, _) => lattice.sublattice.sublattice.Top |
59 | | - } |
60 | | - case input: AInput => lattice.sublattice.sublattice.Top |
61 | | - case _ => ??? |
62 | | - |
63 | | - } |
| 3 | +import tip.ast.AstNodeData.DeclarationData |
| 4 | +import tip.cfg.{InterproceduralProgramCfg, IntraproceduralProgramCfg} |
| 5 | +import tip.lattices.ConstantPropagationLattice |
| 6 | + |
| 7 | +object ConstantPropagationAnalysis { |
| 8 | + |
| 9 | + object Intraprocedural { |
| 10 | + |
| 11 | + /** |
| 12 | + * Intraprocedural analysis that uses the simple fixpoint solver. |
| 13 | + */ |
| 14 | + class SimpleSolver(cfg: IntraproceduralProgramCfg)(implicit declData: DeclarationData) |
| 15 | + extends IntraprocValueAnalysisSimpleSolver(cfg, ConstantPropagationLattice) |
| 16 | + |
| 17 | + /** |
| 18 | + * Intraprocedural analysis that uses the worklist solver. |
| 19 | + */ |
| 20 | + class WorklistSolver(cfg: IntraproceduralProgramCfg)(implicit declData: DeclarationData) |
| 21 | + extends IntraprocValueAnalysisWorklistSolver(cfg, ConstantPropagationLattice) |
| 22 | + |
| 23 | + /** |
| 24 | + * Intraprocedural analysis that uses the worklist solver with reachability. |
| 25 | + */ |
| 26 | + class WorklistSolverWithReachability(cfg: IntraproceduralProgramCfg)(implicit declData: DeclarationData) |
| 27 | + extends IntraprocValueAnalysisWorklistSolverWithReachability(cfg, ConstantPropagationLattice) |
| 28 | + |
| 29 | + /** |
| 30 | + * Intraprocedural analysis that uses the worklist solver with reachability and propagation-style. |
| 31 | + */ |
| 32 | + class WorklistSolverWithReachabilityAndPropagation(cfg: IntraproceduralProgramCfg)(implicit declData: DeclarationData) |
| 33 | + extends IntraprocValueAnalysisWorklistSolverWithReachabilityAndPropagation(cfg, ConstantPropagationLattice) |
| 34 | + } |
| 35 | + |
| 36 | + object Interprocedural { |
| 37 | + |
| 38 | + /** |
| 39 | + * Interprocedural analysis that uses the worklist solver with reachability. |
| 40 | + */ |
| 41 | + class WorklistSolverWithReachability(cfg: InterproceduralProgramCfg)(implicit declData: DeclarationData) |
| 42 | + extends InterprocValueAnalysisWorklistSolverWithReachability(cfg, ConstantPropagationLattice) |
| 43 | + |
| 44 | + /** |
| 45 | + * Interprocedural analysis that uses the worklist solver with reachability and propagation-style. |
| 46 | + */ |
| 47 | + class WorklistSolverWithReachabilityAndPropagation(cfg: InterproceduralProgramCfg)(implicit declData: DeclarationData) |
| 48 | + extends InterprocValueAnalysisWorklistSolverWithReachabilityAndPropagation(cfg, ConstantPropagationLattice) |
| 49 | + |
| 50 | + /** |
| 51 | + * Interprocedural analysis that uses the worklist solver with reachability and propagation-style. |
| 52 | + * with call-string context sensivity. |
| 53 | + */ |
| 54 | + class CallString(cfg: InterproceduralProgramCfg)(implicit declData: DeclarationData) extends CallStringValueAnalysis(cfg, ConstantPropagationLattice) |
| 55 | + |
| 56 | + /** |
| 57 | + * Interprocedural analysis that uses the worklist solver with reachability and propagation-style. |
| 58 | + * with functional-approach context sensivity. |
| 59 | + */ |
| 60 | + class Functional(cfg: InterproceduralProgramCfg)(implicit declData: DeclarationData) extends FunctionalValueAnalysis(cfg, ConstantPropagationLattice) |
| 61 | + } |
64 | 62 | } |
65 | | - |
66 | | -/** |
67 | | - * Constant propagation analysis that uses the simple fipoint solver. |
68 | | - */ |
69 | | -class ConstantPropagationAnalysisSimpleSolver(cfg: IntraproceduralProgramCfg)(implicit declData: DeclarationData) |
70 | | - extends ConstantPropagationAnalysis(cfg) |
71 | | - with SimpleMapLatticeFixpointSolver[CfgNode] |
72 | | - with ForwardDependencies |
73 | | - |
74 | | -/** |
75 | | - * Constant propagation analysis that uses the worklist solver. |
76 | | - */ |
77 | | -class ConstantPropagationAnalysisWorklistSolver(cfg: IntraproceduralProgramCfg)(implicit declData: DeclarationData) |
78 | | - extends ConstantPropagationAnalysis(cfg) |
79 | | - with SimpleWorklistFixpointSolver[CfgNode] |
80 | | - with ForwardDependencies |
0 commit comments