A lot of work to the manual for 0.3.
[gazelle.git] / utilities / bitcode_dump.c
blobcd6cbb2efab0cfd8b869ed12ff8a6e3a79e2b6c3
1 /*********************************************************************
3 Gazelle: a system for building fast, reusable parsers
5 bitcode_dump.c
7 This is a very simple utility for dumping a Bitcode file literally,
8 without any recognition or processing of any of the data. It could
9 be extended to dump only selected parts, or to dump it in other,
10 more useful formats.
12 Copyright (c) 2007 Joshua Haberman. See LICENSE for details.
14 *********************************************************************/
16 #include "bc_read_stream.h"
18 #include <stdio.h>
19 #include <string.h>
21 void check_error(struct bc_read_stream *s)
23 if(bc_rs_get_error(s))
25 int err = bc_rs_get_error(s);
26 fprintf(stderr, "There were stream errors!\n");
27 if(err & BITCODE_ERR_VALUE_TOO_LARGE)
28 fprintf(stderr, " Value too large.\n");
29 if(err & BITCODE_ERR_NO_SUCH_VALUE)
30 fprintf(stderr, " No such value.\n");
31 if(err & BITCODE_ERR_IO)
32 fprintf(stderr, " IO error.\n");
33 if(err & BITCODE_ERR_CORRUPT_INPUT)
34 fprintf(stderr, " Corrupt input.\n");
35 if(err & BITCODE_ERR_INTERNAL)
36 fprintf(stderr, " Internal error.\n");
40 void usage()
42 printf("bitcode_dump: dumps all of the records in a bitcode file\n");
43 printf("Usage: bitcode_dump <bitcode file>\n");
46 int main(int argc, char *argv[0])
48 int nesting = 0;
50 if(argc < 2 || strcmp(argv[1], "--help") == 0)
52 usage();
53 return 1;
56 struct bc_read_stream *s = bc_rs_open_file(argv[1]);
57 if(!s)
59 printf("Failed to open bitcode file %s\n", argv[1]);
60 return 1;
63 while(1)
65 struct record_info ri = bc_rs_next_data_record(s);
66 if(ri.record_type == DataRecord)
68 for(int i = 0; i < nesting; i++)
69 printf(" ");
71 printf("%u: ", ri.id);
72 for(int i = 0; i < bc_rs_get_record_size(s); i++)
73 printf("%llu ", bc_rs_read_64(s, i));
74 printf("\n");
76 else if(ri.record_type == StartBlock)
78 for(int i = 0; i < nesting; i++)
79 printf(" ");
80 printf("-- (id=%u)\n", ri.id);
81 nesting++;
83 else if(ri.record_type == EndBlock)
85 nesting--;
87 else if(ri.record_type == Eof)
89 bc_rs_close_stream(s);
90 return 0;
92 else if(ri.record_type == Err)
94 fprintf(stderr, "Hit an error. :(\n");
95 check_error(s);
96 return 1;
98 check_error(s);
103 * Local Variables:
104 * c-file-style: "bsd"
105 * c-basic-offset: 4
106 * indent-tabs-mode: nil
107 * End:
108 * vim:et:sts=4:sw=4