5590 improper use of NULL in tools/protocmp
[unleashed.git] / usr / src / tools / stabs / genassym.c
blobb318545804870da772d765ef61563241f38a3222
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <unistd.h>
30 #include <math.h>
31 #include "stabs.h"
33 void genassym_do_sou(struct tdesc *tdp, struct node *np);
34 void genassym_do_enum(struct tdesc *tdp, struct node *np);
35 void genassym_do_intrinsic(struct tdesc *tdp, struct node *np);
37 static void switch_on_type(struct mlist *mlp, struct tdesc *tdp,
38 char *format, int level);
40 static void print_intrinsic(struct mlist *mlp, struct tdesc *tdp,
41 char *format, int level);
42 static void print_forward(struct mlist *mlp, struct tdesc *tdp,
43 char *format, int level);
44 static void print_pointer(struct mlist *mlp, struct tdesc *tdp,
45 char *format, int level);
46 static void print_array(struct mlist *mlp, struct tdesc *tdp,
47 char *format, int level);
48 static void print_function(struct mlist *mlp, struct tdesc *tdp,
49 char *format, int level);
50 static void print_union(struct mlist *mlp, struct tdesc *tdp,
51 char *format, int level);
52 static void print_enum(struct mlist *mlp, struct tdesc *tdp,
53 char *format, int level);
54 static void print_forward(struct mlist *mlp, struct tdesc *tdp,
55 char *format, int level);
56 static void print_typeof(struct mlist *mlp, struct tdesc *tdp,
57 char *format, int level);
58 static void print_struct(struct mlist *mlp, struct tdesc *tdp,
59 char *format, int level);
60 static void print_volatile(struct mlist *mlp, struct tdesc *tdp,
61 char *format, int level);
62 static int stabs_log2(unsigned int value);
64 void
65 genassym_do_intrinsic(struct tdesc *tdp, struct node *np)
67 if (np->format != NULL) {
68 char *upper = uc(np->format);
70 printf("#define\t%s 0x%x\n", upper, tdp->size);
72 free(upper);
77 void
78 genassym_do_sou(struct tdesc *tdp, struct node *np)
80 struct mlist *mlp;
81 struct child *chp;
82 char *format;
84 if (np->format != NULL) {
85 char *upper = uc(np->format);
86 int l;
88 printf("#define\t%s 0x%x\n", upper, tdp->size);
90 if ((np->format2 != NULL) &&
91 (l = stabs_log2(tdp->size)) != -1) {
92 printf("#define\t%s 0x%x\n", np->format2, l);
95 free(upper);
99 * Run thru all the fields of a struct and print them out
101 for (mlp = tdp->data.members.forw; mlp != NULL; mlp = mlp->next) {
103 * If there's a child list, only print those members.
105 if (np->child != NULL) {
106 if (mlp->name == NULL)
107 continue;
108 chp = find_child(np, mlp->name);
109 if (chp == NULL)
110 continue;
111 format = uc(chp->format);
112 } else {
113 format = NULL;
115 if (mlp->fdesc == NULL)
116 continue;
117 switch_on_type(mlp, mlp->fdesc, format, 0);
118 if (format != NULL)
119 free(format);
123 void
124 genassym_do_enum(struct tdesc *tdp, struct node *np)
126 int nelem = 0;
127 struct elist *elp;
129 printf("\n");
130 for (elp = tdp->data.emem; elp != NULL; elp = elp->next) {
131 printf("#define\tENUM_%s 0x%x\n", elp->name, elp->number);
132 nelem++;
134 printf("%x c-enum .%s\n", nelem, np->name);
137 static void
138 switch_on_type(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
140 boolean_t allocated = B_FALSE;
142 if (format == NULL) {
143 allocated = B_TRUE;
144 format = uc(mlp->name);
147 switch (tdp->type) {
148 case INTRINSIC:
149 print_intrinsic(mlp, tdp, format, level);
150 break;
151 case POINTER:
152 print_pointer(mlp, tdp, format, level);
153 break;
154 case ARRAY:
155 print_array(mlp, tdp, format, level);
156 break;
157 case FUNCTION:
158 print_function(mlp, tdp, format, level);
159 break;
160 case UNION:
161 print_union(mlp, tdp, format, level);
162 break;
163 case ENUM:
164 print_enum(mlp, tdp, format, level);
165 break;
166 case FORWARD:
167 print_forward(mlp, tdp, format, level);
168 break;
169 case TYPEOF:
170 print_typeof(mlp, tdp, format, level);
171 break;
172 case STRUCT:
173 print_struct(mlp, tdp, format, level);
174 break;
175 case VOLATILE:
176 print_volatile(mlp, tdp, format, level);
177 break;
178 default:
179 fprintf(stderr, "Switch to Unknown type\n");
180 error = B_TRUE;
181 break;
183 if (allocated)
184 free(format);
188 static void
189 print_forward(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
191 fprintf(stderr, "%s never defined\n", mlp->name);
192 error = B_TRUE;
195 static void
196 print_typeof(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
198 switch_on_type(mlp, tdp->data.tdesc, format, level);
201 static void
202 print_volatile(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
204 switch_on_type(mlp, tdp->data.tdesc, format, level);
207 static void
208 print_intrinsic(struct mlist *mlp, struct tdesc *tdp,
209 char *format, int level)
211 if (level != 0) {
212 switch (tdp->size) {
213 case 1:
214 printf("/* ' c@ ' %s */", format);
215 break;
216 case 2:
217 printf("/* ' w@ ' %s */", format);
218 break;
219 case 4:
220 printf("/* ' l@ ' %s */", format);
221 break;
222 case 8:
223 printf("/* ' x@ ' %s */", format);
224 break;
227 * Check for bit field.
229 } else if (mlp->size != 0 &&
230 ((mlp->size % 8) != 0 || (mlp->offset % mlp->size) != 0)) {
231 int offset, shift, mask;
233 offset = (mlp->offset / 32) * 4;
234 shift = 32 - ((mlp->offset % 32) + mlp->size);
235 mask = ((int)pow(2, mlp->size) - 1) << shift;
237 printf("#define\t%s_SHIFT 0x%x\n", format, shift);
238 printf("#define\t%s_MASK 0x%x\n", format, mask);
239 printf("#define\t%s_OFFSET 0x%x\n", format, offset);
240 } else if (mlp->name != NULL) {
241 printf("#define\t%s 0x%x\n", format, mlp->offset / 8);
245 static void
246 print_pointer(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
248 if (level != 0) {
249 switch (tdp->size) {
250 case 1:
251 printf("/* ' c@ ' %s */", format);
252 break;
253 case 2:
254 printf("/* ' w@ ' %s */", format);
255 break;
256 case 4:
257 printf("/* ' l@ ' %s */", format);
258 break;
259 case 8:
260 printf("/* ' x@ ' %s */", format);
261 break;
263 } else {
264 printf("#define\t%s 0x%x\n", format, mlp->offset / 8);
268 static void
269 print_array(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
271 struct ardef *ap = tdp->data.ardef;
272 int items, inc;
274 if (level == 0) {
275 items = ap->indices->range_end - ap->indices->range_start + 1;
276 inc = (mlp->size / items) / 8;
277 printf("#define\t%s 0x%x\n", format, mlp->offset / 8);
278 printf("#define\t%s_INCR 0x%x\n", format, inc);
282 static void
283 print_function(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
285 fprintf(stderr, "function in struct %s\n", tdp->name);
286 error = B_TRUE;
289 static void
290 print_struct(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
292 if (level != 0)
293 printf("/* ' noop ' %s */", format);
294 else
295 printf("#define\t%s 0x%x\n", format, mlp->offset / 8);
298 static void
299 print_union(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
301 if (level != 0)
302 printf("/* ' noop ' %s */", format);
303 else
304 printf("#define\t%s 0x%x\n", format, mlp->offset / 8);
307 static void
308 print_enum(struct mlist *mlp, struct tdesc *tdp, char *format, int level)
310 if (level != 0)
311 printf("/* ' l@ ' %s */", format);
312 else
313 printf("#define\t%s 0x%x\n", format, mlp->offset / 8);
316 static int
317 stabs_log2(unsigned int value)
319 int log = 1;
320 int i;
322 for (i = 0; i < sizeof (value) * 8; i++) {
323 if ((log << i) == value)
324 return (i);
326 return (-1);