8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / ctf / stabs / common / genassym.c
blob7c1aa73b7aa71225d16138b72175f8914adb0fd6
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * In this mode, we generate header files containg various #defines which can
28 * be used to access members of various structures, and to walk through arrays.
29 * The input template specifies the structures and members for whom #defines
30 * are to be generated.
32 * The template has the following elements
34 * 1. Given the name of a structure or union, #defines can be generated that
35 * describe the type. If requested, #defines that give the size and the
36 * log2 (shift) of the structure will be generated. The latter can only
37 * be requested for structures whose size is a power of two.
39 * Per-member #defines are also generated. The value of these defines will
40 * be the offsets necessary to access the members they describe. By
41 * default, the name of the #define will be the name of the member, in upper
42 * case, but a user-supplied version can be used instead. If the member is
43 * an array, an extra #define will be generated that will give the increment
44 * needed to access individual array elements. The name of the increment
45 * #define will be identical to that of the member #define, but with an
46 * "_INCR" suffix.
48 * 2. Literal cpp directives
50 * Lines beginning with "\#" are copied directly to the output file.
52 * 3. Comments
54 * Lines beginning with backslashes (excluding the literal cpp directives
55 * described above) are ignored.
57 * Example input:
59 * \ Dump the `foo' structure, creating a size #define called FOO_SIZE, and a
60 * \ shift #define called FOO_SHIFT. `foo' has one member called `mem'.
61 * foo FOO_SIZE FOO_SHIFT
63 * \ Dump the `a' and `b' members of the `bar' structure. the offset
64 * \ #defines for these members should be `FRED' and `BOB', respectively.
65 * \ Both members are of type `char'
66 * bar
67 * a FRED
68 * b BOB
70 * Example output:
72 * #define FOO_SIZE 0x4
73 * #define FOO_SHIFT 0x2
74 * #define FRED 0x0
75 * #define FRED_INCR 0x1
76 * #define BOB 0x4
79 #include <string.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <ctype.h>
83 #include <sys/types.h>
85 #include "ctf_headers.h"
86 #include "utils.h"
87 #include "ctfstabs.h"
89 static int
90 ga_parse_tokens(char *line, int max, char ***wret)
92 char *c = line;
93 char *word;
94 int n;
96 while (isspace(*c))
97 c++;
99 for (n = 1, word = strtok(line, " \t"); word != NULL;
100 word = strtok(NULL, " \t"), n++) {
101 if (n > max)
102 return (-1);
104 *(wret[n - 1]) = word;
107 return (n - 1);
110 static int
111 ga_parse_common(char *line, int min, int max, char **w1, char **w2, char **w3)
113 char **wret[3];
114 int nread;
116 wret[0] = w1;
117 wret[1] = w2;
118 wret[2] = w3;
120 if ((nread = ga_parse_tokens(line, max, wret)) < min)
121 return (-1);
123 if (nread < 3 && wret[2] != NULL)
124 *wret[2] = (char *)NULL;
125 if (nread < 2 && wret[1] != NULL)
126 *wret[1] = (char *)NULL;
127 if (nread < 1 && wret[0] != NULL)
128 *wret[0] = (char *)NULL;
130 return (nread);
134 * Valid format: typename [sizedefname [shiftdefname]]
136 static int
137 ga_parse_name(char *line, char **cnp, char **szdp, char **shdp)
139 return (ga_parse_common(line, 1, 3, cnp, szdp, shdp));
143 * Valid format: memname [offdefname]
145 static int
146 ga_parse_member(char *line, char **mnp, char **offp)
148 return (ga_parse_common(line, 1, 2, mnp, offp, NULL));
152 * Used to begin a new structure/union block, and to print the optional size
153 * and optional shift constants.
155 static int
156 ga_process_name(char *line)
158 char *curname, *sizedef, *shdef;
159 ctf_id_t curtype;
160 ssize_t sz, shift;
162 if (ga_parse_name(line, &curname, &sizedef, &shdef) < 0)
163 return (parse_warn("Couldn't parse name"));
165 if ((curtype = find_type(curname)) == CTF_ERR)
166 return (parse_warn("Couldn't find type %s", curname));
168 if (sizedef != NULL) {
169 if ((sz = ctf_type_size(ctf, curtype)) < 0) {
170 return (parse_warn("Couldn't get size for type %s",
171 curname));
172 } else if (sz == 0) {
173 return (parse_warn("Invalid type size 0 for %s",
174 curname));
177 (void) fprintf(out, "#define\t%s\t0x%x\n", sizedef, sz);
180 if (shdef != NULL) {
181 ssize_t tsz;
183 for (shift = -1, tsz = sz; tsz > 0; tsz >>= 1, shift++)
185 if (shift < 0 || 1 << shift != sz) {
186 return (parse_warn("Can't make shift #define: %s size "
187 "(%d) isn't a power of 2", curname, sz));
190 (void) fprintf(out, "#define\t%s\t0x%x\n", shdef, shift);
193 return (curtype);
197 * ga_process_member() and ga_member_cb() are used to print the offset and
198 * possibly array increment values for a given structure member. A specific
199 * member is requested via ga_process_member(), and ga_member_cb() is used
200 * to iterate through the members of the current structure type, looking for
201 * that member. This is not the most efficient way to do things, but the
202 * lists involved are generally short.
204 typedef struct ga_member_cb_data {
205 char *gmcb_memname;
206 char *gmcb_submem;
207 char *gmcb_offdef;
208 size_t gmcb_off;
209 } ga_member_cb_data_t;
211 static int ga_member_find(ctf_id_t, ga_member_cb_data_t *);
213 static int
214 ga_member_cb(const char *name, ctf_id_t type, ulong_t off, void *arg)
216 ga_member_cb_data_t *md = arg;
217 ctf_arinfo_t arinfo;
218 char *label;
220 if (strcmp(name, md->gmcb_memname) != 0)
221 return (0);
223 md->gmcb_off += off / 8; /* off is in bits */
225 if (md->gmcb_submem != NULL) {
227 * The user requested foo.bar. We've found foo, and now need to
228 * recurse down to bar.
230 ga_member_cb_data_t smd;
232 smd.gmcb_memname = md->gmcb_submem;
233 smd.gmcb_submem = NULL;
234 smd.gmcb_offdef = md->gmcb_offdef;
235 smd.gmcb_off = md->gmcb_off;
237 return (ga_member_find(type, &smd));
240 if (md->gmcb_offdef == NULL) {
241 int i;
243 label = md->gmcb_memname;
244 for (i = 0; i < strlen(label); i++)
245 label[i] = toupper(label[i]);
246 } else
247 label = md->gmcb_offdef;
249 /* offsets are in bits - we need bytes */
250 (void) fprintf(out, "#define\t%s\t0x%lx\n", label,
251 (ulong_t)md->gmcb_off);
253 if ((type = ctf_type_resolve(ctf, type)) == CTF_ERR)
254 return (parse_warn("Couldn't resolve type %s", name));
256 if (ctf_array_info(ctf, type, &arinfo) == 0) {
257 ssize_t sz;
259 if ((sz = ctf_type_size(ctf, arinfo.ctr_contents)) < 0)
260 return (parse_warn("Couldn't get array elem size"));
262 (void) fprintf(out, "#define\t%s_INCR\t0x%x\n", label, sz);
265 return (1);
268 static int
269 ga_member_find(ctf_id_t curtype, ga_member_cb_data_t *md)
271 char *c;
272 int rc;
274 if ((c = strchr(md->gmcb_memname, '.')) != NULL)
275 *c++ = '\0';
276 md->gmcb_submem = c;
278 if ((rc = ctf_member_iter(ctf, curtype, ga_member_cb, md)) == 0) {
279 return (parse_warn("Couldn't find member named %s",
280 md->gmcb_memname));
281 } else if (rc != 1)
282 return (parse_warn("Can't parse"));
284 return (1);
287 static int
288 ga_process_member(ctf_id_t curtype, char *line)
290 ga_member_cb_data_t md = { 0 };
292 if (ga_parse_member(line, &md.gmcb_memname, &md.gmcb_offdef) < 0)
293 return (parse_warn("Couldn't parse member"));
295 return (ga_member_find(curtype, &md));
298 static int
299 ga_process_line(char *line)
301 static int curtype = -1;
302 static int blanks = 0;
304 if (strlen(line) == 0) {
305 blanks++;
306 return (1);
307 } else if (blanks) {
308 if (!isspace(line[0]))
309 curtype = -1;
310 blanks = 0;
313 if (line[0] == '\\') {
314 if (line[1] == '#') {
315 /* dump, verbatim, lines that begin with "\#" */
316 (void) fprintf(out, "%s\n", line + 1);
318 return (1);
320 } else if (line[0] == '#') {
322 * This is a comment of some sort; is it a line number
323 * comment? Those look like '# 53 "filename.c"'. GCC
324 * sometimes inserts them and removes all other vertical
325 * whitespace, so they should be treated as a "type
326 * terminator" like a blank line is.
328 if (isdigit(line[2])) {
329 /* line number, terminate type */
330 curtype = -1;
332 return (1);
334 if (curtype == -1)
335 return ((curtype = ga_process_name(line)));
336 else
337 return (ga_process_member(curtype, line));
340 proc_ops_t ga_ops = {
341 NULL,
342 ga_process_line,
343 NULL