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.
9 * Licensed under the Open Software License version 1.1
23 #include "expression.h"
25 static void emit_blob(struct symbol
*sym
)
27 int size
= sym
->bit_size
;
28 int alignment
= sym
->ctype
.alignment
;
29 const char *name
= show_ident(sym
->ident
);
32 warning(sym
->pos
, "emitting insized symbol");
36 warning(sym
->pos
, "emitting symbol of size %d bits\n", size
);
40 if (!(size
& (alignment
-1))) {
43 printf("unsigned char %s[%d];\n", name
, size
);
46 printf("unsigned short %s[%d];\n", name
, (size
+1) >> 1);
49 printf("unsigned int %s[%d];\n", name
, (size
+3) >> 2);
53 printf("unsigned char %s[%d] __attribute__((aligned(%d)));\n",
54 name
, size
, alignment
);
58 static void emit_fn(struct symbol
*sym
)
60 const char *name
= show_ident(sym
->ident
);
61 printf("%s();\n", name
);
64 void emit_symbol(struct symbol
*sym
)
69 if (sym
->type
!= SYM_NODE
) {
70 warning(sym
->pos
, "I really want to emit nodes, not pure types!");
74 ctype
= sym
->ctype
.base_type
;
77 switch (ctype
->type
) {
90 warning(sym
->pos
, "what kind of strange node do you want me to emit again?");
95 static void emit_symbol_list(struct symbol_list
*list
)
99 FOR_EACH_PTR(list
, sym
) {
101 } END_FOR_EACH_PTR(sym
);
104 int main(int argc
, char **argv
)
107 char *filename
= argv
[1];
109 struct symbol_list
*list
= NULL
;
111 // Initialize symbol stream first, so that we can add defines etc
114 fd
= open(filename
, O_RDONLY
);
116 die("No such file: %s", filename
);
118 // Initialize type system
121 // Tokenize the input stream
122 token
= tokenize(filename
, fd
, NULL
, includepath
);
125 // Pre-process the stream
126 token
= preprocess(token
);
128 // Parse the resulting C code
129 translation_unit(token
, &list
);
131 // Do type evaluation and simplify
132 emit_symbol_list(list
);