还是一样的郁闷呢|

gonghr

园龄:4年1个月粉丝:308关注:25

【南大静态代码分析】作业 1:活跃变量分析和迭代求解器

作业 1:活跃变量分析和迭代求解器

题目链接:https://tai-e.pascal-lab.net/pa1.html

评测链接:https://oj.pascal-lab.net/problem

作业出品:南京大学《软件分析》课程,谭添、李樾

实现活跃变量分析

  • newInitialFact :负责创建和初始化控制流图中除了 EntryExit 之外的结点的 Data Flow Fact

控制流图中一个结点的 INOUT 分别对应一个 Data Flow Fact ,记录当前程序点时变量的状态。

直接创建一个空的 CPFact 即可,方法体内还没有开始扫描。

@Override
public SetFact<Var> newInitialFact() {
    // TODO - finish me
    return new SetFact<>();
}
  • newBoundaryFact :负责创建和初始化虚拟结点的 Data Flow Fact。但是注意要把方法参数初始化为 NAC
@Override
public SetFact<Var> newBoundaryFact(CFG<Stmt> cfg) {
    // TODO - finish me
    return new SetFact<>();
}
  • meetInto :负责处理 transfer function 之前可能遇到多个 OUT 时的合并处理。

直接调用接口就行了。

@Override
public void meetInto(SetFact<Var> fact, SetFact<Var> target) {
    // TODO - finish me
    target.union(fact);
}
  • transferNode :负责实现控制流图中结点的 transfer function 。如果 IN 改变,返回 true ;否则返回 false
@Override
public boolean transferNode(Stmt stmt, SetFact<Var> in, SetFact<Var> out) {
    // TODO - finish me
    // IN = OUT \cup (use - def)
    Optional<LValue> def = stmt.getDef();
    List<RValue> uses = stmt.getUses();
    SetFact<Var> newSetFact = new SetFact<>();
    newSetFact.union(out);
    if (def.isPresent()) {
        LValue defValue = def.get();
        if (defValue instanceof Var) {
            newSetFact.remove((Var) defValue);
        }
    }
    for (RValue use : uses) {
        if (use instanceof Var) {
            newSetFact.add((Var) use);
        }
    }
    if (!in.equals(newSetFact)) {
        in.set(newSetFact);
        return true;
    }
    return false;
}

实现迭代求解器

  • doSolveBackward :完成 while 循环。
@Override
protected void doSolveBackward(CFG<Node> cfg, DataflowResult<Node, Fact> result) {
    // TODO - finish me

    boolean flag = true;
    while (flag) {
        flag = false;
        for (Node node : cfg) {
            if (cfg.isExit(node)) continue;
            Fact outFact = result.getOutFact(node);
            Fact inFact = result.getInFact(node);
            for (Node succs : cfg.getSuccsOf(node)) {
                Fact succsInFact = result.getInFact(succs);
                analysis.meetInto(succsInFact, outFact);
            }
            if (analysis.transferNode(node, inFact, outFact)) {
                flag = true;
            }
        }
    }
}
  • initializeBackward :实现算法前三行的初始化操作。
protected void initializeBackward(CFG<Node> cfg, DataflowResult<Node, Fact> result) {
    // TODO - finish me

    // Init Exit node
    result.setInFact(cfg.getExit(), analysis.newBoundaryFact(cfg));

    // Init other nodes
    for (Node node : cfg) {
        if (!cfg.isExit(node)) {
            result.setInFact(node, analysis.newInitialFact());
            result.setOutFact(node, analysis.newInitialFact());
        }
    }
}

评测结果

本文作者:GHR

本文链接:https://www.cnblogs.com/gonghr/p/17977971

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   gonghr  阅读(603)  评论(0编辑  收藏  举报
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示
  1. 1 Whenever you are ONE OK ROCK
Whenever you are - ONE OK ROCK
00:00 / 00:00
An audio error has occurred.

作词 : TAKA

作曲 : TAKA

I'm telling you

Tonight tonight

You are my angel

爱してるよ

爱してるよ

2人は一つに

Tonight tonight

I just say…

Wherever you are I always make you smile

Wherever you are I'm always by your side

Whatever you say 君を思う気持ち

I promise you forever right now

I don't need a reason

I don't need a reason

I just want you baby

Alright alright

Day after day

この先长いことずっと

この先长いことずっと

どうかこんな仆とずっと

死ぬまで Stay with me

We carry on…

Wherever you are I always make you smile

Wherever you are I'm always by your side

Whatever you say' 君を思う気持ち

I promise you forever right now

Wherever you are I never make you cry

Wherever you are I'never say goodbye

Whatever you say 君を思う気持ち

I promise you forever right now

仆らが出逢った日は2人にとって

一番目の记念すべき日だね

そして今日という日は2人にとって

二番目の记念すべき日だね

心から爱せる人

心から爱せる人

心から爱しい人

この仆の爱の真ん中には

いつも君(きみ)がいるから

Wherever you are I always make you smile

Wherever you are I'm always by your side

Whatever you say 君を思う気持ち

I promise you forever right now

Wherever you are wherever you are

Wherever you are

おわり

おわり

おわり