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