如何在LLVM中区分有符号和无符号整数 [英] How to distinguish signed and unsigned integer in LLVM
问题描述
如此处所述,LLVM项目不区分有符号整数和无符号整数.在某些情况下,您需要知道某个特定变量是应解释为有符号还是无符号,例如何时扩展大小或何时在除法中使用它.我的解决方案是为每个变量保留单独的类型信息,以描述它是整数还是基数类型.
The LLVM project does not distinguish between signed and unsigned integers as described here. There are situations where you need to know if a particular variable should be interpreted as signed or as unsigned though, for instance when it is size extended or when it is used in a division. My solution to this is to keep a separate type information for every variable that describes whether it is an integer or a cardinal type.
但是,我想知道,是否没有一种方法可以将LLVM中的类型归因"于此?我一直在寻找可以添加到类型中的某种用户数据",但似乎一无所获.创建类型时,由于LLVM中只生成一次相同的类型,因此必须以某种方式发生这种情况.
However, I am wondering, isn't there a way to "attribute" a type in LLVM that way? I was looking for some sort of "user data" that could be added to a type but there seems to be nothing. This would have to happen somehow when the type is created since equal types are generated only once in LLVM.
因此,我的问题是:
是否有一种方法可以跟踪LLVM基础结构中整数变量是应解释为带符号的还是无符号的?或者是像我一样确实保留单独信息的唯一方法吗?
Is there a way to track whether an integer variable should be interpreted as signed or unsigned within the LLVM infrastructure, or is the only way indeed to keep separate information like I do?
谢谢
推荐答案
首先,您必须确保需要插入额外的类型元数据,因为Clang已经适当地处理了带符号整数运算,例如使用sdiv
和srem
而不是udev
和urem
.
First of all, you have to be sure that you need inserting extra type meta-data since Clang already handles signed integer operations appropriately for example by using sdiv
and srem
rather than udev
and urem
.
此外,还可以利用它来实现一些轻量级的类型推断,具体取决于基于IR中变量的访问方式.请注意,由于add
之类的操作基于双补码表示,因此不需要签名信息.
Additionally, It's possible to utilize that to implement some lightweight type-inference based on how the variables are accessed in the IR. Note that an operation like add
doesn't need signdness info since it is based on two-complement representation.
否则,我认为最好的方法是修改前端(Clang)以添加一些自定义DWARF调试信息.这是一个链接,可能会让您入门.
Otherwise, I think that the best way to do that is to modify the front-end (Clang) to add some custom DWARF debug info. Here is a link that might get you started.
更新: 如果您的目标是直接在LLVM IR上进行静态分析.本文可以提供详尽的讨论.
UPDATE: If your goal is to implement static-analysis directly on LLVM IR. This paper can offer a thorough discussion.
Navas,J.A.,Schachte,P.,Søndergaard,H.,Stuckey,P.J .: 与签名无关的程序分析:低级代码的精确整数范围.在:Jhala,R.,Igarashi,A.(编辑)APLAS2012.LNCS, 卷7705,第115–130页.施普林格,海德堡(2012)
Navas, J.A., Schachte, P., Søndergaard, H., Stuckey, P.J.: Signedness-agnostic program analysis: Precise integer bounds for low-level code. In: Jhala, R., Igaras A. (eds.) APLAS 2012. LNCS, vol. 7705, pp. 115–130. Springer, Heidelberg (2012)
这篇关于如何在LLVM中区分有符号和无符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!