2 * Example trivial client program that uses the sparse library
3 * to tokenize, pre-process and parse a C file, and prints out
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
23 #include "expression.h"
24 #include "linearize.h"
26 static int context_increase(struct basic_block
*bb
)
29 struct instruction
*insn
;
31 FOR_EACH_PTR(bb
->insns
, insn
) {
32 if (insn
->opcode
== OP_CONTEXT
)
33 sum
+= insn
->increment
;
34 } END_FOR_EACH_PTR(insn
);
38 static int imbalance(struct entrypoint
*ep
, struct basic_block
*bb
, int entry
, int exit
, const char *why
)
41 struct symbol
*sym
= ep
->name
;
42 warning(bb
->pos
, "context imbalance in '%s' - %s", show_ident(sym
->ident
), why
);
47 static int check_bb_context(struct entrypoint
*ep
, struct basic_block
*bb
, int entry
, int exit
);
49 static int check_children(struct entrypoint
*ep
, struct basic_block
*bb
, int entry
, int exit
)
51 struct instruction
*insn
;
52 struct basic_block
*child
;
54 insn
= last_instruction(bb
->insns
);
57 if (insn
->opcode
== OP_RET
)
58 return entry
!= exit
? imbalance(ep
, bb
, entry
, exit
, "wrong count at exit") : 0;
60 FOR_EACH_PTR(bb
->children
, child
) {
61 if (check_bb_context(ep
, child
, entry
, exit
))
63 } END_FOR_EACH_PTR(child
);
67 static int check_bb_context(struct entrypoint
*ep
, struct basic_block
*bb
, int entry
, int exit
)
71 if (bb
->context
== entry
)
74 /* Now that's not good.. */
76 return imbalance(ep
, bb
, entry
, bb
->context
, "different lock contexts for basic block");
79 entry
+= context_increase(bb
);
81 return imbalance(ep
, bb
, entry
, exit
, "unexpected unlock");
83 return check_children(ep
, bb
, entry
, exit
);
86 static void check_context(struct entrypoint
*ep
)
88 struct symbol
*sym
= ep
->name
;
90 check_bb_context(ep
, ep
->entry
, sym
->ctype
.in_context
, sym
->ctype
.out_context
);
93 static void check_symbols(struct symbol_list
*list
)
97 FOR_EACH_PTR(list
, sym
) {
98 struct entrypoint
*ep
;
101 ep
= linearize_symbol(sym
);
104 } END_FOR_EACH_PTR(sym
);
107 int main(int argc
, char **argv
)
109 // Expand, linearize and show it.
110 check_symbols(sparse(argc
, argv
));