博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Static Program Analysis - Chapter 3】Type Analysis
阅读量:6902 次
发布时间:2019-06-27

本文共 1110 字,大约阅读时间需要 3 分钟。

类型分析,个人理解就是(通过静态分析技术)分析出代码中,哪些地方只能是某种或某几种数据类型,这是一种约束。

 

例如,给定一个程序:

NewImage

其中,我们可以很直接地得到一些约束:

NewImage

最后,经过简化可以得到:

NewImage

对于给定的变量类型,如果他们不符合这个约束,则说明,他们是不合法的。

那么,怎么去提取以及维护这些约束呢?

采用一种“并查集”的结构:一个有向图,每个节点有一条边指向父节点(父节点则指向自己)。如果两个节点具有相同的父节点,那么,这个两节点就认为是等价的,即含有相同的数据类型。

以下是并查集的基本算法:

NewImage

The unification algorithm uses union-find by associating a node with each term (including sub-terms) in the constraint system.

For each term τ we initially invoke MakeSet(τ ).

Note that each term at this point is either a type variable or a proper type (i.e. integer, heap pointer, or function); μ terms are only produced for presenting solutions to constraints, as explained below.

For each constraint τ1 = τ2 we invoke Unify(τ1, τ2), which unifies the two terms if possible and enforces the general term equality axiom by unifiying sub-terms recursively: 

NewImage

NewImage

Unification fails if attempting to unify two terms with different constructor (where function constructors are considered different if they have different arity). 

再来看个例子:

NewImage

NewImage

NewImage

对于递归:

NewImage

Limitations of the Type Analysis 

例子:

NewImage 

运行的时候没问题,但是,遵循之前的方法会报错,之前的方法并不考虑程序的顺序执行给数据类型转换的影响。即X=42在X=alloc之后,因此,最终返回的一定是int型。

另一个例子:

NewImage

转载地址:http://plpdl.baihongyu.com/

你可能感兴趣的文章
Leetcode#75Sort Colorsetcode
查看>>
3月30日作业
查看>>
公司电话突然不能打外线故障处理过程
查看>>
Windows Server 2008流媒体服务器---创建播放列表
查看>>
centos添加批量添加ip提示无效参数
查看>>
PHP mkdir函数
查看>>
Linux基础命令---检查密码文件pwck
查看>>
python这+=和=的拓展知识
查看>>
oracle集群件
查看>>
linux shell 中"2>&1"含义
查看>>
oracle 11g RAC grid安装前准备
查看>>
01背包 暴力搜索
查看>>
RIP区域和OSPF区域通信
查看>>
MySQL
查看>>
k3cloud开发环境引入dll与net源代码
查看>>
网络安全系列之四十 在Linux中设置SET位权限
查看>>
SCCM OSD部署排错
查看>>
十道非常好的shell脚本试题
查看>>
app项目案例一手机浏览器
查看>>
linuxmint安装配置
查看>>