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 warn(sym
->pos
, "emitting insized symbol");
36 warn(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
, void *_parent
, int flags
)
69 if (sym
->type
!= SYM_NODE
) {
70 warn(sym
->pos
, "I really want to emit nodes, not pure types!");
74 ctype
= sym
->ctype
.base_type
;
77 switch (ctype
->type
) {
90 warn(sym
->pos
, "what kind of strange node do you want me to emit again?");
95 int main(int argc
, char **argv
)
98 char *filename
= argv
[1];
100 struct symbol_list
*list
= NULL
;
102 // Initialize symbol stream first, so that we can add defines etc
105 fd
= open(filename
, O_RDONLY
);
107 die("No such file: %s", argv
[1]);
109 // Tokenize the input stream
110 token
= tokenize(filename
, fd
, NULL
);
113 // Pre-process the stream
114 token
= preprocess(token
);
116 // Parse the resulting C code
117 translation_unit(token
, &list
);
119 // Do type evaluation and simplify
120 symbol_iterate(list
, emit_symbol
, NULL
);