xref: /illumos-gate/usr/src/tools/smatch/src/compile.c (revision c85f09cc)
11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Example trivial client program that uses the sparse library
31f5207b7SJohn Levon  * and x86 backend.
41f5207b7SJohn Levon  *
51f5207b7SJohn Levon  * Copyright (C) 2003 Transmeta Corp.
61f5207b7SJohn Levon  *               2003 Linus Torvalds
71f5207b7SJohn Levon  * Copyright 2003 Jeff Garzik
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
101f5207b7SJohn Levon  * of this software and associated documentation files (the "Software"), to deal
111f5207b7SJohn Levon  * in the Software without restriction, including without limitation the rights
121f5207b7SJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
131f5207b7SJohn Levon  * copies of the Software, and to permit persons to whom the Software is
141f5207b7SJohn Levon  * furnished to do so, subject to the following conditions:
151f5207b7SJohn Levon  *
161f5207b7SJohn Levon  * The above copyright notice and this permission notice shall be included in
171f5207b7SJohn Levon  * all copies or substantial portions of the Software.
181f5207b7SJohn Levon  *
191f5207b7SJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
201f5207b7SJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
211f5207b7SJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
221f5207b7SJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
231f5207b7SJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
241f5207b7SJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
251f5207b7SJohn Levon  * THE SOFTWARE.
261f5207b7SJohn Levon  *
271f5207b7SJohn Levon  */
281f5207b7SJohn Levon #include <stdarg.h>
291f5207b7SJohn Levon #include <stdlib.h>
301f5207b7SJohn Levon #include <stdio.h>
311f5207b7SJohn Levon #include <string.h>
321f5207b7SJohn Levon #include <ctype.h>
331f5207b7SJohn Levon #include <unistd.h>
341f5207b7SJohn Levon #include <fcntl.h>
351f5207b7SJohn Levon 
361f5207b7SJohn Levon #include "lib.h"
371f5207b7SJohn Levon #include "allocate.h"
381f5207b7SJohn Levon #include "token.h"
391f5207b7SJohn Levon #include "parse.h"
401f5207b7SJohn Levon #include "symbol.h"
411f5207b7SJohn Levon #include "expression.h"
421f5207b7SJohn Levon #include "compile.h"
431f5207b7SJohn Levon 
clean_up_symbols(struct symbol_list * list)441f5207b7SJohn Levon static void clean_up_symbols(struct symbol_list *list)
451f5207b7SJohn Levon {
461f5207b7SJohn Levon 	struct symbol *sym;
471f5207b7SJohn Levon 
481f5207b7SJohn Levon 	FOR_EACH_PTR(list, sym) {
491f5207b7SJohn Levon 		expand_symbol(sym);
501f5207b7SJohn Levon 		emit_one_symbol(sym);
511f5207b7SJohn Levon 	} END_FOR_EACH_PTR(sym);
521f5207b7SJohn Levon }
531f5207b7SJohn Levon 
main(int argc,char ** argv)541f5207b7SJohn Levon int main(int argc, char **argv)
551f5207b7SJohn Levon {
561f5207b7SJohn Levon 	char *file;
571f5207b7SJohn Levon 	struct string_list *filelist = NULL;
581f5207b7SJohn Levon 
591f5207b7SJohn Levon 	bits_in_bool = 8;
601f5207b7SJohn Levon 
611f5207b7SJohn Levon 	clean_up_symbols(sparse_initialize(argc, argv, &filelist));
62*c85f09ccSJohn Levon 	FOR_EACH_PTR(filelist, file) {
631f5207b7SJohn Levon 		struct symbol_list *list;
641f5207b7SJohn Levon 		const char *basename = strrchr(file, '/');
651f5207b7SJohn Levon 		basename = basename ?  basename+1 : file;
661f5207b7SJohn Levon 
671f5207b7SJohn Levon 		list = sparse(file);
681f5207b7SJohn Levon 
691f5207b7SJohn Levon 		// Do type evaluation and simplification
701f5207b7SJohn Levon 		emit_unit_begin(basename);
711f5207b7SJohn Levon 		clean_up_symbols(list);
721f5207b7SJohn Levon 		emit_unit_end();
73*c85f09ccSJohn Levon 	} END_FOR_EACH_PTR(file);
741f5207b7SJohn Levon 
751f5207b7SJohn Levon #if 0
761f5207b7SJohn Levon 	// And show the allocation statistics
771f5207b7SJohn Levon 	show_ident_alloc();
781f5207b7SJohn Levon 	show_token_alloc();
791f5207b7SJohn Levon 	show_symbol_alloc();
801f5207b7SJohn Levon 	show_expression_alloc();
811f5207b7SJohn Levon 	show_statement_alloc();
821f5207b7SJohn Levon 	show_string_alloc();
831f5207b7SJohn Levon 	show_bytes_alloc();
841f5207b7SJohn Levon #endif
851f5207b7SJohn Levon 	return 0;
861f5207b7SJohn Levon }
87