A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich A CIL Tutorial - Department of Computer Science - ETH Zürich

29.01.2014 Views

CHAPTER 0. OVERVIEW AND ORGANIZATION 10 $ OCAMLRUNPARAM=b ciltutcc --bytecode ...

Chapter 1 The AST The Concrete Syntax of a program, is the textual representation of the program. An Abstract Syntax Tree(AST) is the in-memory data-structure that represents the parsed program. Parsing turns the concrete syntax into an AST. Parsing, the specication of grammars, and the various algorithms for lexical analysis and parsing, are covered in detail in many traditional textbooks on compilers. The textbooks by Appel [2] and Muchnick [3] are good starting points. This chapter of the tutorial explains the structure of CIL's AST for C. The AST is dened in the Cil module in cil/src/cil.ml between lines 130 and 850. It might seem pretty complex at rst glance, but you will nd that it is very well documented, and the names of types, elds and constructors are logically assigned. Also, if you are familiar with other C or C++ front-ends, this AST is refreshingly straightforward. Finally, one of the key advantages to using CIL is that it makes a number of transformations that simplify analysis and instrumentation. Biggest among these is the absence of side-eects in expressions. Function calls and assignments are all lifted out of expressions into the Cil.instr nodes. In this example code, we go digging through the AST looking for a function called target, when we nd it, we lter out assignments to a global variable called deleted. In src/tut2.ml we'll see how to do this a bit more elegantly. Coding Hint: When working with CIL, have two windows of source on your screen. One window is the project you are hacking on. The other window is cil.ml, so you can refer quickly to the AST type denitions and utility functions. 11

Chapter 1<br />

The AST<br />

The Concrete Syntax <strong>of</strong> a program, is the textual representation <strong>of</strong> the program. An Abstract Syntax<br />

Tree(AST) is the in-memory data-structure that represents the parsed program. Parsing turns the<br />

concrete syntax into an AST. Parsing, the specication <strong>of</strong> grammars, and the various algorithms<br />

for lexical analysis and parsing, are covered in detail in many traditional textbooks on compilers.<br />

The textbooks by Appel [2] and Muchnick [3] are good starting points.<br />

This chapter <strong>of</strong> the tutorial explains the structure <strong>of</strong> <strong>CIL</strong>'s AST for C. The AST is dened in<br />

the Cil module in cil/src/cil.ml between lines 130 and 850. It might seem pretty complex at<br />

rst glance, but you will nd that it is very well documented, and the names <strong>of</strong> types, elds and<br />

constructors are logically assigned. Also, if you are familiar with other C or C++ front-ends, this<br />

AST is refreshingly straightforward. Finally, one <strong>of</strong> the key advantages to using <strong>CIL</strong> is that it makes<br />

a number <strong>of</strong> transformations that simplify analysis and instrumentation. Biggest among these is the<br />

absence <strong>of</strong> side-eects in expressions. Function calls and assignments are all lifted out <strong>of</strong> expressions<br />

into the Cil.instr nodes.<br />

In this example code, we go digging through the AST looking for a function called target, when<br />

we nd it, we lter out assignments to a global variable called deleted. In src/tut2.ml we'll see<br />

how to do this a bit more elegantly.<br />

Coding Hint: When working with <strong>CIL</strong>, have two windows <strong>of</strong> source on your<br />

screen. One window is the project you are hacking on. The other window<br />

is cil.ml, so you can refer quickly to the AST type denitions and utility<br />

functions.<br />

11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!