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, all rights reserved.
20 #include "expression.h"
22 static void emit_blob(struct symbol
*sym
)
24 int size
= sym
->bit_size
;
25 int alignment
= sym
->ctype
.alignment
;
26 const char *name
= show_ident(sym
->ident
);
29 warn(sym
->pos
, "emitting insized symbol");
33 warn(sym
->pos
, "emitting symbol of size %d bits\n", size
);
37 if (!(size
& (alignment
-1))) {
40 printf("unsigned char %s[%d];\n", name
, size
);
43 printf("unsigned short %s[%d];\n", name
, (size
+1) >> 1);
46 printf("unsigned int %s[%d];\n", name
, (size
+3) >> 2);
50 printf("unsigned char %s[%d] __attribute__((aligned(%d)));\n",
51 name
, size
, alignment
);
55 static void emit_fn(struct symbol
*sym
)
57 const char *name
= show_ident(sym
->ident
);
58 printf("%s();\n", name
);
61 void emit_symbol(struct symbol
*sym
, void *_parent
, int flags
)
66 if (sym
->type
!= SYM_NODE
) {
67 warn(sym
->pos
, "I really want to emit nodes, not pure types!");
71 ctype
= sym
->ctype
.base_type
;
74 switch (ctype
->type
) {
87 warn(sym
->pos
, "what kind of strange node do you want me to emit again?");
92 int main(int argc
, char **argv
)
95 char *filename
= argv
[1];
97 struct symbol_list
*list
= NULL
;
99 // Initialize symbol stream first, so that we can add defines etc
102 fd
= open(filename
, O_RDONLY
);
104 die("No such file: %s", argv
[1]);
106 // Tokenize the input stream
107 token
= tokenize(filename
, fd
, NULL
);
110 // Pre-process the stream
111 token
= preprocess(token
);
113 // Parse the resulting C code
114 translation_unit(token
, &list
);
116 // Do type evaluation and simplify
117 symbol_iterate(list
, emit_symbol
, NULL
);