xref: /illumos-gate/usr/src/tools/smatch/src/compile.c (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Example trivial client program that uses the sparse library
3*1f5207b7SJohn Levon  * and x86 backend.
4*1f5207b7SJohn Levon  *
5*1f5207b7SJohn Levon  * Copyright (C) 2003 Transmeta Corp.
6*1f5207b7SJohn Levon  *               2003 Linus Torvalds
7*1f5207b7SJohn Levon  * Copyright 2003 Jeff Garzik
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
10*1f5207b7SJohn Levon  * of this software and associated documentation files (the "Software"), to deal
11*1f5207b7SJohn Levon  * in the Software without restriction, including without limitation the rights
12*1f5207b7SJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13*1f5207b7SJohn Levon  * copies of the Software, and to permit persons to whom the Software is
14*1f5207b7SJohn Levon  * furnished to do so, subject to the following conditions:
15*1f5207b7SJohn Levon  *
16*1f5207b7SJohn Levon  * The above copyright notice and this permission notice shall be included in
17*1f5207b7SJohn Levon  * all copies or substantial portions of the Software.
18*1f5207b7SJohn Levon  *
19*1f5207b7SJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*1f5207b7SJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*1f5207b7SJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22*1f5207b7SJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23*1f5207b7SJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24*1f5207b7SJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25*1f5207b7SJohn Levon  * THE SOFTWARE.
26*1f5207b7SJohn Levon  *
27*1f5207b7SJohn Levon  */
28*1f5207b7SJohn Levon #include <stdarg.h>
29*1f5207b7SJohn Levon #include <stdlib.h>
30*1f5207b7SJohn Levon #include <stdio.h>
31*1f5207b7SJohn Levon #include <string.h>
32*1f5207b7SJohn Levon #include <ctype.h>
33*1f5207b7SJohn Levon #include <unistd.h>
34*1f5207b7SJohn Levon #include <fcntl.h>
35*1f5207b7SJohn Levon 
36*1f5207b7SJohn Levon #include "lib.h"
37*1f5207b7SJohn Levon #include "allocate.h"
38*1f5207b7SJohn Levon #include "token.h"
39*1f5207b7SJohn Levon #include "parse.h"
40*1f5207b7SJohn Levon #include "symbol.h"
41*1f5207b7SJohn Levon #include "expression.h"
42*1f5207b7SJohn Levon #include "compile.h"
43*1f5207b7SJohn Levon 
44*1f5207b7SJohn Levon static void clean_up_symbols(struct symbol_list *list)
45*1f5207b7SJohn Levon {
46*1f5207b7SJohn Levon 	struct symbol *sym;
47*1f5207b7SJohn Levon 
48*1f5207b7SJohn Levon 	FOR_EACH_PTR(list, sym) {
49*1f5207b7SJohn Levon 		expand_symbol(sym);
50*1f5207b7SJohn Levon 		emit_one_symbol(sym);
51*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(sym);
52*1f5207b7SJohn Levon }
53*1f5207b7SJohn Levon 
54*1f5207b7SJohn Levon int main(int argc, char **argv)
55*1f5207b7SJohn Levon {
56*1f5207b7SJohn Levon 	char *file;
57*1f5207b7SJohn Levon 	struct string_list *filelist = NULL;
58*1f5207b7SJohn Levon 
59*1f5207b7SJohn Levon 	bits_in_bool = 8;
60*1f5207b7SJohn Levon 
61*1f5207b7SJohn Levon 	clean_up_symbols(sparse_initialize(argc, argv, &filelist));
62*1f5207b7SJohn Levon 	FOR_EACH_PTR_NOTAG(filelist, file) {
63*1f5207b7SJohn Levon 		struct symbol_list *list;
64*1f5207b7SJohn Levon 		const char *basename = strrchr(file, '/');
65*1f5207b7SJohn Levon 		basename = basename ?  basename+1 : file;
66*1f5207b7SJohn Levon 
67*1f5207b7SJohn Levon 		list = sparse(file);
68*1f5207b7SJohn Levon 
69*1f5207b7SJohn Levon 		// Do type evaluation and simplification
70*1f5207b7SJohn Levon 		emit_unit_begin(basename);
71*1f5207b7SJohn Levon 		clean_up_symbols(list);
72*1f5207b7SJohn Levon 		emit_unit_end();
73*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR_NOTAG(file);
74*1f5207b7SJohn Levon 
75*1f5207b7SJohn Levon #if 0
76*1f5207b7SJohn Levon 	// And show the allocation statistics
77*1f5207b7SJohn Levon 	show_ident_alloc();
78*1f5207b7SJohn Levon 	show_token_alloc();
79*1f5207b7SJohn Levon 	show_symbol_alloc();
80*1f5207b7SJohn Levon 	show_expression_alloc();
81*1f5207b7SJohn Levon 	show_statement_alloc();
82*1f5207b7SJohn Levon 	show_string_alloc();
83*1f5207b7SJohn Levon 	show_bytes_alloc();
84*1f5207b7SJohn Levon #endif
85*1f5207b7SJohn Levon 	return 0;
86*1f5207b7SJohn Levon }
87