<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
On 9/23/11 12:24 PM, Himanshu Shekhar wrote:
<blockquote
cite="mid:CAOGs9vWrXNe3qf=noJZm9ARGetv63=2F3FenS=PuPFA2jNDuAQ@mail.gmail.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
I just read that LLVM project could be used to do static analysis
on C/C++ codes using the analyzer Clang which the front end of
LLVM. I wanted to know if it is possible to extract all the
accesses to memory(variables, local as well as global) in the
source code using LLVM.
<br>
</blockquote>
<br>
When doing analysis with Clang and LLVM, you first must make a
choice about which IR to use: Clang's Abstract Syntax Tree (AST) or
LLVM's SSA Intermediate Representation (IR). Clang takes source
code and converts it into an AST; it later takes the AST and
converts it to LLVM IR. LLVM then performs mid-level compiler
analysis and optimization on code in LLVM IR form and then
translates from LLVM IR to native code.<br>
<br>
Clang ASTs will give you much higher level information than LLVM
IR. On the other hand, LLVM IR is probably easier to work with and
is programming language agnostic.<br>
<br>
You might want to read about the LLVM Language Reference Manual
(<a class="moz-txt-link-freetext" href="http://llvm.org/docs/LangRef.html">http://llvm.org/docs/LangRef.html</a>) to get a feel of whether it is
suitable for your analysis. There may be a similar document for
Clang, but I'm not familiar with it since I haven't worked with
Clang ASTs myself.<br>
<br>
<blockquote
cite="mid:CAOGs9vWrXNe3qf=noJZm9ARGetv63=2F3FenS=PuPFA2jNDuAQ@mail.gmail.com"
type="cite">Is there any inbuilt library present in LLVM which I
could use to extract this information.
If not please suggest me how to write functions to do the
same.(existing source code, reference, tutorial, example...)
<br>
</blockquote>
<br>
It is easy to write an LLVM pass that plugs into the opt tool that
searches for explicit accesses to memory. The LLVM load and store
instructions access memory (similar to how loads and stores are used
to access memory in a RISC instruction set). That said, it is not
clear whether this is what you want to do. Some source-level
variables are translated into one or more SSA virtual registers, so
you'll never see a load or store to them (as they may never exist in
memory but only in registers). Additionally, some loads and stores
to memory are not visible at the LLVM IR level. For example, loads
and stores to stack spill slots are not visible at the LLVM IR level
because they're only created during code generation (and
technically, they're generated in a third IR called Machine
Instructions that is used specifically for code generation).<br>
<br>
<br>
<blockquote
cite="mid:CAOGs9vWrXNe3qf=noJZm9ARGetv63=2F3FenS=PuPFA2jNDuAQ@mail.gmail.com"
type="cite">Of what i studied is, I need to first convert the
source code into LLVM IR and then make an instrumenting pass which
would go over this bitcode file and insert calls to do the
analysis, but don't know exactly how to do it.<br clear="all">
</blockquote>
<br>
The first thing you need to do is figure out which representation of
the program (Clang ASTs, LLVM IR, LLVM's code generation IR) is the
best for solving your particular problem. If you want, you can
provide more details on what you're trying to do; people on the list
can then provide feedback on which representation is most suitable
for what you want to do.<br>
<br>
If you decide to work with LLVM IR, I then recommend reading the
"How to Write an LLVM Pass" document
(<a class="moz-txt-link-freetext" href="http://llvm.org/docs/WritingAnLLVMPass.html">http://llvm.org/docs/WritingAnLLVMPass.html</a>) as well as the
Programmer's Guide (<a class="moz-txt-link-freetext" href="http://llvm.org/docs/ProgrammersManual.html">http://llvm.org/docs/ProgrammersManual.html</a>).
Doxygen is also valuable (<a class="moz-txt-link-freetext" href="http://llvm.org/doxygen/">http://llvm.org/doxygen/</a>).<br>
<br>
For an example of a pass that adds run-time checks to LLVM IR loads
and stores, look at SAFECode's load/store instrumentation pass
(<a class="moz-txt-link-freetext" href="http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/LoadStoreChecks.h?view=markup">http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/LoadStoreChecks.h?view=markup</a>
and
<a class="moz-txt-link-freetext" href="http://llvm.org/viewvc/llvm-project/safecode/trunk/lib/InsertPoolChecks/LoadStoreChecks.cpp?view=markup">http://llvm.org/viewvc/llvm-project/safecode/trunk/lib/InsertPoolChecks/LoadStoreChecks.cpp?view=markup</a>).
It's about as simple as an instrumentation pass gets.<br>
<br>
-- John T.<br>
<blockquote
cite="mid:CAOGs9vWrXNe3qf=noJZm9ARGetv63=2F3FenS=PuPFA2jNDuAQ@mail.gmail.com"
type="cite"><br>
Please suggest me how to go about it .<br>
thanks <br>
himanshu<br>
-- <br>
<br>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a class="moz-txt-link-freetext" href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
</blockquote>
<br>
</body>
</html>