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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 0. OVERVIEW AND ORGANIZATION 9<br />

modications to it, and optionally returns a result up to the caller in main.ml. We include this<br />

source le in the build by adding it to the list <strong>CIL</strong>TUT SRC in CMakeLists.txt.<br />

Functions, like tut0 below, that do this sort <strong>of</strong> work should be called from processOneFile in<br />

main.ml.<br />

0.2 tut0.ml<br />

The rst step in writing an OCaml module that uses the <strong>CIL</strong> library is to open the Cil module (or<br />

bind it to an alias, like we do with the Errormsg module). The CMakeLists.txt le provided with<br />

this tutorial ensures that the <strong>CIL</strong> library is correctly found and linked assuming that it has been<br />

correctly installed. From now on, in the following chapters, the code to open modules and create<br />

aliases will be included only for the rst mention <strong>of</strong> a module.<br />

open Cil (∗ The <strong>CIL</strong> library ∗)<br />

module E = Errormsg (∗ <strong>CIL</strong>'s error message library ∗)<br />

tut0 is a dummy function that doesn't do anything. However, there is certainly nothing preventing<br />

it from inspecting f and making changes to its mutable elds if it really wanted to.<br />

let tut0 (f : file) : unit =<br />

E.log "I'n in tut0 and I could change %s if I wanted to!\n" f.fileName;<br />

()<br />

When the compiler front-end embodied by this tutorial is built and installed, the code in this<br />

module may be run by providing the --enable-tut0 command line switch, like so:<br />

$ ciltutcc --enable-tut0 -o program program.c<br />

I'm in tut0 and I could change program.i if I wanted to!<br />

Finally, if ciltutcc fails due to an unhandled exception, a stack trace may be obtained by<br />

re-executing the command as follows:<br />

OCaml Style Note: Types are inferred, so you don't have to write them<br />

all the time. However, if you want to remember what your code does a month<br />

from now, writing in some <strong>of</strong> the types is a Good Idea.

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

Saved successfully!

Ooh no, something went wrong!