29.01.2014 Views

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

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 0<br />

Overview and Organization<br />

Overall, this tutorial is organized as an OCaml program that uses <strong>CIL</strong> as a library. A Perl script<br />

sits in front <strong>of</strong> the resulting OCaml program to make the result resemble various o-the-shelf C<br />

compilers as much as possible. The code accompanying this tutorial provides nearly all <strong>of</strong> the<br />

necessary boilerplate, and will let you dive right in and start analyzing and manipulating the AST.<br />

0.1 Source le to AST<br />

Day-to-day, it is generally not needed to know how this boilerplate code produces an AST that we<br />

can work with in our OCaml program. It is good to understand how it works, though, in case your<br />

project requires you to modify the compilation process in some way, for example to force linking<br />

against a custom runtime library.<br />

By default, <strong>CIL</strong> operates on one source le at a time (whole-program analysis is discussed by<br />

Chapter 13). The result <strong>of</strong> parsing a preprocesssed source le is a data-structure <strong>of</strong> type Cil.file.<br />

This structure contains a list <strong>of</strong> C denitions and declarations at global scope.<br />

The details <strong>of</strong> parsing are handled in two places. Firstly in the Perl script mentioned above. It<br />

is called cil/lib/Cilly.pm, and is part <strong>of</strong> the <strong>CIL</strong> installation. For a project that uses the <strong>CIL</strong><br />

library, like this one, we use our own Perl script, lib/Ciltut.pm, that derives from, overrides, and<br />

extends Cilly.pm to specialize it to our purposes 1 . This Perl script also takes care <strong>of</strong> running the<br />

preprocessor on the source le, interpreting some <strong>of</strong> the compiler ags, and performing the nal link.<br />

Secondly, the OCaml program mentioned above reads in the preprocessed source le and passes it<br />

to Frontc.parse with cabs, which parses the le, and turns it into a Cil.file.<br />

The details <strong>of</strong> the Cil.file instance are covered in Chapter 1. For now it should suce to<br />

mention that the boilerplate code in src/main.ml takes care <strong>of</strong> setting up <strong>CIL</strong>'s internal datastructures<br />

and options, parsing the input source le, and later on, emitting the .cil.c le that the<br />

perl script passes to gcc, or whatever your favorite C compiler happens to be.<br />

Everything that happens in between parsing the input and emitting the output happens in source<br />

les like src/tut0.ml. Here, we write a function that takes a Cil.file, analyzes it, performs some<br />

1 This is where we could provide additional command line ags to the back-end compiler or to force linking with<br />

a custom runtime library, see the comments in that source le for details.<br />

8

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

Saved successfully!

Ooh no, something went wrong!