1 /* tag: Tom Lord Tue Dec  4 14:41:35 2001 (bits-print.c)
2  */
3 /* bits-print.c -
4  *
5  ****************************************************************
6  * Copyright (C) 2000 Tom Lord
7  *
8  * See the file "COPYING" for further information about
9  * the copyright and warranty status of this work.
10  */
11 
12 
13 #include "hackerlab/bugs/panic.h"
14 #include "hackerlab/char/str.h"
15 #include "hackerlab/vu/safe.h"
16 #include "hackerlab/bitsets/bitset-tree-print.h"
17 #include "hackerlab/bitsets/bits-print.h"
18 
19 
20 /************************************************************************
21  *(h2 "Printing Shared Bitset Trees"
22  *    :includes ("hackerlab/bitsets/bits-print.h"))
23  *
24  * |bitset printing (shared bitset trees)|
25  * |printing (shared bitset trees)|
26  *
27  *
28  */
29 
30 
31 /*(c bits_print)
32  * void bits_print (int fd,
33  *                  bits set,
34  *                  t_uchar * name,
35  *                  t_uchar * stub,
36  *                  int is_static,
37  *                  int decls_only,
38  *                  int as_macro);
39  *
40  * Print C code for an initialized shared bitset tree.
41  *
42  * `fd' -- print output on descriptor `fd'.
43  *
44  * `set' -- the bitset to print.
45  *
46  * `name' -- the variable name for the bitset.
47  *
48  * `stub' -- a variable name prefix for components of the bitset.
49  *
50  * `is_static' -- print a static declaration.
51  *
52  * `decls_only' -- print only a declaration, not the bitset itself.
53  *
54  * `as_macro' -- define `name' as a pre-processor name, not a variable.
55  *
56  * This function calls `panic' if an error occurs.
57  */
58 void
bits_print(int fd,bits set,t_uchar * name,t_uchar * stub,int is_static,int decls_only,int as_macro)59 bits_print (int fd,
60 	    bits set,
61 	    t_uchar * name,
62 	    t_uchar * stub,
63 	    int is_static,
64 	    int decls_only,
65 	    int as_macro)
66 {
67 #define MAX_NAME 2048
68   int name_len;
69   t_uchar struct_name[2 * MAX_NAME];
70   t_uchar rule_name[2 * MAX_NAME];
71   t_uchar stree_name[2 * MAX_NAME];
72 
73   name_len = str_length (name);
74   if (name_len > MAX_NAME)
75     panic ("name too long in bits_tree_print");
76 
77   if (decls_only)
78     {
79       safe_printfmt (fd, "%s bits %s;\n", (is_static ? "static" : "extern"),  name);
80       return;
81     }
82 
83   bits_compact (set);
84 
85   bits_tree_print (fd, set->lim, set->rule, set->stree->tree, stub, stub, 1, 0, 1);
86 
87   str_cpy (rule_name, stub);
88   str_cat (rule_name, "_rule");
89   safe_printfmt (fd, "static struct bits_tree_rule %s[] =\n", rule_name);
90   safe_printfmt (fd, "{\n");
91   {
92     int x;
93     x = 0;
94     do
95       {
96 	safe_printfmt (fd, "  {%d, %lu, %lu, %lu},\n",
97 		       set->rule[x].fanout,
98 		       (unsigned long)set->rule[x].subset_size,
99 		       (unsigned long)set->rule[x].subset_shift,
100 		       (unsigned long)set->rule[x].subset_mask);
101       }
102     while (set->rule[x++].fanout);
103   }
104   safe_printfmt (fd, "};\n\n");
105 
106   str_cpy (stree_name, stub);
107   str_cat (stree_name, "_stree");
108   safe_printfmt (fd, "static struct bits_tree_shared %s = { %d, (bits_tree)&%s };\n\n", stree_name, 1, stub);
109 
110   str_cpy (struct_name, stub);
111   str_cat (struct_name, "_bits");
112   safe_printfmt (fd, "static struct bits %s = \n", struct_name);
113   safe_printfmt (fd, "{\n");
114   safe_printfmt (fd, "  0,\n");
115   safe_printfmt (fd, "  %s,\n", rule_name);
116   safe_printfmt (fd, "  &%s\n", stree_name);
117   safe_printfmt (fd, "};\n\n");
118   if (!as_macro)
119     safe_printfmt (fd, "%sbits %s = &%s;\n\n", (is_static ? "static " : ""), name, struct_name);
120   else
121     safe_printfmt (fd, "#define %s  (&%s)\n\n", name, struct_name);
122 }
123 
124