Commits

Zachary Anderson  committed c742ad9

Fix for ciltutcc script, etc.

  • Participants
  • Parent commits 58fc0e7

Comments (0)

Files changed (9)

 
 Author: Zach Anderson (zachary.anderson@inf.ethz.ch)
 
-Intro:
+1. Intro
+2. Building and Installing
+3. Documentation
+4. Files
+5. Citation
+
+1. Intro:
 ------
 
 CIL is a frontend for C compilers. It takes C code as input, which can then be
 build process. This tutorial creates such a frontend, and demonstrates, in
 tutorial form, some nifty things that might be done with it.
 
-Building and Installing:
+2. Building and Installing:
 ------------------------
 
 There are a few dependencies:
 A file called test1.cil.c will be created showing the source file created by
 the frontend before it is passed on to gcc.
 
-Documentation
+3. Documentation
 -------------
 (This is to remind me how to build the docs in my private repo, and so they
 don't apply to the code under this tree. The documentation of which this section
 
 Pass --disable-docs to the configure script to disable document building.
 
-Files:
-------
+4. Files:
+---------
 
-Makefile.in - Edit the "MODULES =" line to add additional ocaml modules. Other
+Makefile.in - Edit the "MODULES =" line to add additional OCaml modules. Other
 parts of the Makefile can probably remain unchanged unless you need to do
 something weird.
 
 
 test/tut*.c - Test programs for the various tutorial modules.
 
+LICENSE - All the code in this project is licensed under a standard 3-clause
+BSD license.
 
+5. Citation:
+------------
+
+If you use cil-template as a starting point for your project, please make the
+following citation:
+
+Zachary Anderson. A CIL Tutorial: Using CIL for Language Extensions and Program
+  Analysis, <the date given on the document>. <http://the.download.url>.
+
+At the time this README file was written, this evaluates to:
+
+Zachary Anderson. A CIL Tutorial: Using CIL for Language Extensions and Program
+  Analysis, February 2012. http://www.inf.ethz.ch/~azachary.
+
+Or in bibtex format:
+
+@misc{ciltut,
+  title = {A {CIL} Tutorial: Using {CIL} for Language Extensions and Program Analysis},
+  author = {Zachary Anderson},
+  month = Feb,
+  year = 2012,
+  note = {\url{http://www.inf.ethz.ch/~azachary/}},
+}
 Should do:
-. Chapter on comments
 . Chapter on Adding a constructor to do something with globals.
-. Chapter on whole program analysis
+. Chapter on DSLs
+
+argument(type, name, [optional, mandatory]) {
+  "-a", "Help text", [default], [arg_assert(exp)]
+};
 
 Would be nice:
 . Further Readings

File bin/ciltutcc

 use strict;
 
 use FindBin;
+use lib "$FindBin::RealBin/../..";
 use lib "$FindBin::RealBin/../lib";
 use lib "$FindBin::RealBin/../cil/bin";
 use lib "$FindBin::RealBin/../cil/lib";

File ciltut-include/ciltut.h

 uint64_t perf_get_cache_miss();
 uint64_t tut_get_time();
 
+#define argument(argtype, argname, ...) \
+struct ciltut_##argname { \
+  char *small; \
+  char *help; \
+  argtype def; \
+  void *req; \
+} __attribute__((ciltutarg, __VA_ARGS__)) argname =
+
+#define arg_assert(e) (void *__attribute__((ciltut_assert((e)))))NULL
+
 #endif 
 
 

File lib/Ciltut.pm

-#
-#
-# Copyright (c) 2001-2002, 
-#  George C. Necula    <necula@cs.berkeley.edu>
-#  Scott McPeak        <smcpeak@cs.berkeley.edu>
-#  Wes Weimer          <weimer@cs.berkeley.edu>
-# All rights reserved.
-# 
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-#
-# 1. Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-#
-# 2. Redistributions in binary form must reproduce the above copyright
-# notice, this list of conditions and the following disclaimer in the
-# documentation and/or other materials provided with the distribution.
-#
-# 3. The names of the contributors may not be used to endorse or promote
-# products derived from this software without specific prior written
-# permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
-# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
-# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
-# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
 
 # This package is used from an environment when CilConfig.pm has been loaded
 package Ciltut;
 
     # Filter out -include options
     for ($i = 0; $i <= $#args; $i++) {
-	$_ = $args[$i];
-	if (/^-include/) {
-	    $i++;
-	}
-	else {
-	    push @newargs, $_;
-	}
+      $_ = $args[$i];
+      if (/^-include/) {
+        $i++;
+      }
+      else {
+        push @newargs, $_;
+      }
     }
     push @newargs, "$self->{INCARG}$::ciltuthome/include";
     return $self->SUPER::compile_cil($src, $dest, \@newargs, $ccargs);
     my @libs = @{$ldargs};
     my @cargs = @{$ccargs};
     if ($self->{DARWIN}) {
-	push @libs, "-Wl,-multiply_defined", "-Wl,suppress";
+        push @libs, "-Wl,-multiply_defined", "-Wl,suppress";
     }
     if (scalar @srcs == 0) {
         print STDERR "ciltutcc: no input files\n";

File src/tut13.ml

-(* tut13.ml *)
 
-(*
 
-  \chapter{Whole-program Analysis}
 
-  \cil{} has a simple mechanism for allowing whole-program analysis. This
-  mechanism is invoked when the \ttt{-{}-merge} switch is passed to
-  \program{ciltutcc}. First, in the compilation phase, instead of compiling
-  source code to an object file with the back-end compiler, the emitted
-  \file{.o} file will contain the pre-processed source. Then, in the link
-  stage, \program{ciltutcc} parses the \ttt{.o} files, and uses the \cil{}
-  [Mergecil] module to combine the separate source files into a single
-  [Cil.file]. More details of this process can be found in the official
-  \cil{} documentation.
-
-
-  \section{tut13.ml}
-
-  In this tutorial, we'll see how to compute a whole-program call graph.
-  The code in thie module is very simple since there is already a pretty good
-  module for computing call graphs in \file{cil/src/ext/callgraph.ml}.
-  Additionally, we'll use the \program{ocamlgraph} library to output a
-  \file{.dot} file, which can be used to generate an image of the call-graph.
-*)
 open Cil
 open Tututil
 module H = Hashtbl
 module CG = Callgraph
 
-(*
-  First we'll need to define a module for the graph using a functor from
-  the graph library
-*)
+
 module SG = Graph.Imperative.Digraph.ConcreteBidirectional(struct
-(*i*)
+
   type t = CG.callnode
   let hash n = H.hash n.CG.cnid
   let compare n1 n2 =
     compare n1.CG.cnid n2.CG.cnid
   let equal n1 n2 = 
     n1.CG.cnid = n2.CG.cnid
-(*i*)
-(* ... *)
+
+
 end)
 
-(*
-  We'll also need to define a module that extends the graph module above with
-  functions to define properties of vertices used by Dot to draw the graph.
-  We'll just use the defaults. These functions can be modified to add more
-  information to the graph.
-*)
+
 module D = Graph.Graphviz.Dot(struct
-(*i*)
+
   type t = SG.t
   module V = SG.V
   module E = SG.E
   let get_subgraph v = None
   let default_edge_attributes g = []
   let edge_attributes e = []
-(*i*)
-(* ... *)
+
+
 end)
 
-(*
-  The [graph_of_callgraph] functions converts a \cil{} call-graph into an
-  \program{ocamlgraph} graph that we can use to generate the \file{.dot} file.
-*)
+
 let graph_of_callgraph (cg : CG.callgraph) : SG.t =
   let g = SG.create () in
   H.iter (fun s n -> SG.add_vertex g n) cg;
   ) cg;
   g
 
-(*
-  Now, we'll compute the call-graph, convert it to a graph for the graph
-  library, and pass it to the graph library function that produces the
-  \file{.dot} file.
-*)
+
 let tut13 (f : file) : unit =
   let o = open_out !Ciltutoptions.tut13out in
   f |> CG.computeGraph |> graph_of_callgraph |> D.output_graph o;
   close_out o
 
-(*
 
-  \section{Example}
 
-  The difficult part of arranging for whole-program analysis is the more
-  complicated compilation process. Here are two source files that we'll use
-  to generate one call-graph:
-
-  In the first file, we'll declare an \ttt{extern} function \ttt{bar} and
-  define a function \ttt{foo} that calls it.
-
-  \input{../test/tut13a}
-  \normalfont\normalsize
-
-  In the second file, we'll make an \ttt{extern} declaration for the function
-  \ttt{foo}, and define the function \ttt{bar} that in turn calls \ttt{foo}. The
-  \ttt{main} function simply calls \ttt{bar}. (Obviously, this program is a
-  nonsense example.)
-
-  \input{../test/tut13b}
-  \normalfont\normalsize
-
-  Now we can build this program with the whole program analysis by executing
-  the following commands:
-
-  \commands{\$~ciltutcc -{}-merge -o tut13a.o -c test/tut13a.c\\
-            \$~ciltutcc -{}-merge -o tut13b.o -c test/tut13b.c\\
-            \$~ciltutcc -{}-merge -{}-enable-tut13 -{}-tut13-out tut13.dot -o tut13 tut13a.o tut13b.o}
-
-  Then, we can generate a graph from the \file{.dot} file as follows:
-
-  \commands{\$~dot -Tpdf tut13.dot -o tut13.pdf}
-
-  to produce the graph in Figure~\ref{fig:callgraph}. Which is the call-graph
-  for the whole program.
-
-  \begin{figure}
-  \begin{center}
-  \includegraphics{callgraph.pdf}
-  \caption{The call-graph for \file{tut13a.c} and \file{tut13b.c}}
-  \label{fig:callgraph}
-  \end{center}
-  \end{figure}
-*)
-

File test/tut10.c

 
 #define DATA_SIZE 50000000
 #define TIMES 5
+#define STEP  1
 
 struct array {
   uint64_t *ptr;
   cache_report {
     for (j = 0; j < TIMES; j++) {
       ptr[0] = 0x50505050;
-      for (i = 1; i < size; i++)
+      for (i = 1; i < size; i+=STEP)
         ptr[i] *= ptr[i-1];
     }
   }

File test/tut13a.c

-//@highlight \hlbegincode{}
+
 
 extern int bar(int x);
 
 {
   return bar(x);
 }
-//@highlight \hlendcode{}
+

File test/tut13b.c

-//@highlight \hlbegincode{}
+
 
 extern int foo(int x);
 
   bar(1);
   return 0;
 }
-//@highlight \hlendcode{}
+