extra: type bug handling asm expressions
[smatch.git] / Documentation / data-structures.txt
blob3745b54cc8fad72c9ad2dad0fe23d38368ac0446
2 <JoshTriplett> As far as the parsing structures go...
3 <JoshTriplett> The C parser exists in two main files: parse.c, which parses statements, and expression.c, which parses expressions.
4 <JoshTriplett> parse.h contains the definition of struct statement, which represents a C statement.
5 <JoshTriplett> That includes only those things which can't appear as an expression, which primarily includes control flow statements such as if, loops, switch/case, and goto.
6 <JoshTriplett> expression.h contains the definition of struct expression, which represents a C expression.  That has a lot more content, since most C constructs can appear in expressions.
7 <JoshTriplett> A series of statements forms a compound statement (STMT_COMPOUND).
8 <JoshTriplett> That appears as another struct statement which has a statement_list member.
9 <JoshTriplett> A function body consists of a compound statement.
10 <JoshTriplett> When you look at a loop body, if or else body, or case body, you'll notice that they just have a struct statement, not a statement_list; they can have multiple statements by using a compound statement.
11 <JoshTriplett> Also note that all loops get turned into a single "iterator" statement.
12 <JoshTriplett> for, while, and do-while.
13 <JoshTriplett> A symbol, then, represents a name in a C file.  A symbol might represent a variable, a function, a label, or various other things.
14 <JoshTriplett> See symbol.h.
15 <JoshTriplett> "struct symbol" represents one symbol.
16 <JoshTriplett> As with the various other structures, it has some common data and a union of sub-structures for the parts that differ between different types.
17 <JoshTriplett> Most of the interesting bits come in the NS_SYMBOL case.
18 <JoshTriplett> Among other things, it has a struct statement for the body of a function (if any), a list of symbols for the arguments, an expression for a variable initializer, and so on.
19 <JoshTriplett> Together, struct symbol, struct statement, and struct expression represent most of the abstract syntax tree for C.
20 <JoshTriplett> So, that represents most of the "front-end" of Sparse: parsing C and generating that abstract syntax tree.
21 <JoshTriplett> That much occurs in pretty much any program using the Sparse frontend.
22 <JoshTriplett> The backend varies among programs.
23 <JoshTriplett> For instance, the c2xml backend goes that far, then outputs XML.
24 <JoshTriplett> The sparse static analysis backend has a few steps: it generates linearized bytecode, does some evaluation on that, and outputs some warnings.
25 <JoshTriplett> Several other backends run that linearized bytecode stage.
26 <JoshTriplett> The linearized bytecode itself has a set of nested structures.
27 <JoshTriplett> linearize.h defines all of them.
28 <JoshTriplett> At the top level, it has struct entrypoint.
29 <JoshTriplett> That represents an entrypoint to the code, which would normally mean a function.
30 <JoshTriplett> An entrypoint has a list of basic blocks.
31 <JoshTriplett> struct basic_block.
32 <JoshTriplett> A basic block represents a series of instructions with no branches.
33 <JoshTriplett> Straight-line code.
34 <JoshTriplett> A branch only occurs at the end of a basic block, and branches can only target the beginning of a basic block.
35 <JoshTriplett> Typically, a conditional will consist of a basic block leading up to the branch, a basic block for the true case, a basic block for the false case, and a basic block where the two paths merge back together.
36 <JoshTriplett> Either the true or the false case may not exist.
37 <JoshTriplett> A loop will normally have a basic block for the loop body, which can branch to the top at the end or continue to the next basic block.
38 <JoshTriplett> So basic blocks represent a node in the control flow graph.
39 <JoshTriplett> The edges in that graph lead from one basic block to a basic block which can follow it in the execution of the program.
40 <JoshTriplett> Each basic block has a series of instructions, "struct instruction".
41 <JoshTriplett> "enum opcode" lists all the instructions.
42 <JoshTriplett> Fairly high-level instruction set, corresponding directly to bits of C.
43 <JoshTriplett> So you have an entrypoint, which has a graph of basic blocks, each of which has a list of instructions.
44 <JoshTriplett> An entrypoint also has a pointer to the first instruction.
45 <JoshTriplett> One last bit of trickiness: struct pseudo.
46 <JoshTriplett> Have you ever heard of "static single assignment" or SSA form?
47 <JoshTriplett> struct pseudo represents one of those single-assignment variables.
48 <JoshTriplett> Each one has a pointer to the symbol it represents (which may have many pseudos referencing it).
49 <JoshTriplett> Each one also has a pointer to the instruction that defines it.
50 <JoshTriplett> That covers most of the major data structures in Sparse.
51 <JoshTriplett> Now, given all that, some of the top-level stuff in sparse.c may make more sense.
52 <JoshTriplett> For instance, the context checking works in terms of basic blocks.
53 <JoshTriplett> Hopefully some of that helped you understand Sparse better.