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.
8 * Licensed under the Open Software License version 1.1
22 #include "expression.h"
24 static void emit_blob(struct symbol
*sym
)
26 int size
= sym
->bit_size
;
27 int alignment
= sym
->ctype
.alignment
;
28 const char *name
= show_ident(sym
->ident
);
31 warn(sym
->pos
, "emitting insized symbol");
35 warn(sym
->pos
, "emitting symbol of size %d bits\n", size
);
39 if (!(size
& (alignment
-1))) {
42 printf("unsigned char %s[%d];\n", name
, size
);
45 printf("unsigned short %s[%d];\n", name
, (size
+1) >> 1);
48 printf("unsigned int %s[%d];\n", name
, (size
+3) >> 2);
52 printf("unsigned char %s[%d] __attribute__((aligned(%d)));\n",
53 name
, size
, alignment
);
57 static void emit_fn(struct symbol
*sym
)
59 const char *name
= show_ident(sym
->ident
);
60 printf("%s();\n", name
);
63 void emit_symbol(struct symbol
*sym
, void *_parent
, int flags
)
68 if (sym
->type
!= SYM_NODE
) {
69 warn(sym
->pos
, "I really want to emit nodes, not pure types!");
73 ctype
= sym
->ctype
.base_type
;
76 switch (ctype
->type
) {
89 warn(sym
->pos
, "what kind of strange node do you want me to emit again?");
94 int main(int argc
, char **argv
)
97 char *filename
= argv
[1];
99 struct symbol_list
*list
= NULL
;
101 // Initialize symbol stream first, so that we can add defines etc
104 fd
= open(filename
, O_RDONLY
);
106 die("No such file: %s", argv
[1]);
108 // Tokenize the input stream
109 token
= tokenize(filename
, fd
, NULL
);
112 // Pre-process the stream
113 token
= preprocess(token
);
115 // Parse the resulting C code
116 translation_unit(token
, &list
);
118 // Do type evaluation and simplify
119 symbol_iterate(list
, emit_symbol
, NULL
);