xref: /titanic_51/usr/src/cmd/adbgen/common/adbsub.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983-1998 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  * All rights reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Subroutines to be called by adbgen2.c, the C program generated
31*7c478bd9Sstevel@tonic-gate  * by adbgen1.c.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate off_t last_off;
39*7c478bd9Sstevel@tonic-gate int warnings = 1;
40*7c478bd9Sstevel@tonic-gate int warns = 0;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * User claims offset is ok.
44*7c478bd9Sstevel@tonic-gate  * This usually follows call to another script, which we cannot handle.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate void
47*7c478bd9Sstevel@tonic-gate offsetok(void)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	last_off = -1;
50*7c478bd9Sstevel@tonic-gate }
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Get adb.s dot to the right offset.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate void
56*7c478bd9Sstevel@tonic-gate offset(off_t off)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	off_t off_diff;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if (last_off == -1) {
61*7c478bd9Sstevel@tonic-gate 		last_off = off;
62*7c478bd9Sstevel@tonic-gate 		return;
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 	off_diff = off - last_off;
65*7c478bd9Sstevel@tonic-gate 	if (off_diff) {
66*7c478bd9Sstevel@tonic-gate 		if (off_diff > 0) {
67*7c478bd9Sstevel@tonic-gate 			if (off_diff > 1) {
68*7c478bd9Sstevel@tonic-gate 				printf("%ld", off_diff);
69*7c478bd9Sstevel@tonic-gate 			}
70*7c478bd9Sstevel@tonic-gate 			printf("+");
71*7c478bd9Sstevel@tonic-gate 		}
72*7c478bd9Sstevel@tonic-gate 		if (off_diff < 0) {
73*7c478bd9Sstevel@tonic-gate 			if (off_diff < -1) {
74*7c478bd9Sstevel@tonic-gate 				printf("%ld", -off_diff);
75*7c478bd9Sstevel@tonic-gate 			}
76*7c478bd9Sstevel@tonic-gate 			printf("-");
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	last_off = off;
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * Emit the format command, return the size.
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate int
86*7c478bd9Sstevel@tonic-gate do_fmt(char *acp)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	int rcount, width, sum, i;
89*7c478bd9Sstevel@tonic-gate 	char *cp;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	cp = acp;
92*7c478bd9Sstevel@tonic-gate 	sum = rcount = 0;
93*7c478bd9Sstevel@tonic-gate 	do {
94*7c478bd9Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9') {
95*7c478bd9Sstevel@tonic-gate 			rcount = rcount * 10 + *cp++ - '0';
96*7c478bd9Sstevel@tonic-gate 		}
97*7c478bd9Sstevel@tonic-gate 		if (rcount == 0) {
98*7c478bd9Sstevel@tonic-gate 			rcount = 1;
99*7c478bd9Sstevel@tonic-gate 		}
100*7c478bd9Sstevel@tonic-gate 		switch (*cp) {
101*7c478bd9Sstevel@tonic-gate 		case 'e':
102*7c478bd9Sstevel@tonic-gate 		case 'E':
103*7c478bd9Sstevel@tonic-gate 		case 'F':
104*7c478bd9Sstevel@tonic-gate 		case 'g':
105*7c478bd9Sstevel@tonic-gate 		case 'G':
106*7c478bd9Sstevel@tonic-gate 		case 'J':
107*7c478bd9Sstevel@tonic-gate 			width = 8;
108*7c478bd9Sstevel@tonic-gate 			break;
109*7c478bd9Sstevel@tonic-gate 		case 'K':
110*7c478bd9Sstevel@tonic-gate #ifdef	_LP64
111*7c478bd9Sstevel@tonic-gate 			width = 8;
112*7c478bd9Sstevel@tonic-gate #else	/* _LP64 */
113*7c478bd9Sstevel@tonic-gate 			width = 4;
114*7c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
115*7c478bd9Sstevel@tonic-gate 			break;
116*7c478bd9Sstevel@tonic-gate 		case 'X':
117*7c478bd9Sstevel@tonic-gate 		case 'O':
118*7c478bd9Sstevel@tonic-gate 		case 'Q':
119*7c478bd9Sstevel@tonic-gate 		case 'D':
120*7c478bd9Sstevel@tonic-gate 		case 'U':
121*7c478bd9Sstevel@tonic-gate 		case 'f':
122*7c478bd9Sstevel@tonic-gate 		case 'Y':
123*7c478bd9Sstevel@tonic-gate 		case 'p':
124*7c478bd9Sstevel@tonic-gate 		case 'P':
125*7c478bd9Sstevel@tonic-gate 			width = 4;
126*7c478bd9Sstevel@tonic-gate 			break;
127*7c478bd9Sstevel@tonic-gate 		case 'x':
128*7c478bd9Sstevel@tonic-gate 		case 'o':
129*7c478bd9Sstevel@tonic-gate 		case 'q':
130*7c478bd9Sstevel@tonic-gate 		case 'd':
131*7c478bd9Sstevel@tonic-gate 		case 'u':
132*7c478bd9Sstevel@tonic-gate 			width = 2;
133*7c478bd9Sstevel@tonic-gate 			break;
134*7c478bd9Sstevel@tonic-gate 		case 'v':
135*7c478bd9Sstevel@tonic-gate 		case 'V':
136*7c478bd9Sstevel@tonic-gate 		case 'b':
137*7c478bd9Sstevel@tonic-gate 		case 'B':
138*7c478bd9Sstevel@tonic-gate 		case 'c':
139*7c478bd9Sstevel@tonic-gate 		case 'C':
140*7c478bd9Sstevel@tonic-gate 		case '+':
141*7c478bd9Sstevel@tonic-gate 			width = 1;
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 		case 'I':
144*7c478bd9Sstevel@tonic-gate 		case 'a':
145*7c478bd9Sstevel@tonic-gate 		case 'A':
146*7c478bd9Sstevel@tonic-gate 		case 't':
147*7c478bd9Sstevel@tonic-gate 		case 'r':
148*7c478bd9Sstevel@tonic-gate 		case 'n':
149*7c478bd9Sstevel@tonic-gate 			width = 0;
150*7c478bd9Sstevel@tonic-gate 			break;
151*7c478bd9Sstevel@tonic-gate 		case '-':
152*7c478bd9Sstevel@tonic-gate 			width = -1;
153*7c478bd9Sstevel@tonic-gate 			break;
154*7c478bd9Sstevel@tonic-gate 		case 's':
155*7c478bd9Sstevel@tonic-gate 		case 'S':
156*7c478bd9Sstevel@tonic-gate 		case 'i':
157*7c478bd9Sstevel@tonic-gate 			if (warnings) {
158*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
159*7c478bd9Sstevel@tonic-gate 				"Unknown format size \"%s\", assuming zero\n",
160*7c478bd9Sstevel@tonic-gate 				acp);
161*7c478bd9Sstevel@tonic-gate 				warns++;
162*7c478bd9Sstevel@tonic-gate 			}
163*7c478bd9Sstevel@tonic-gate 			width = 0;
164*7c478bd9Sstevel@tonic-gate 			break;
165*7c478bd9Sstevel@tonic-gate 		default:
166*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Unknown format size: %s\n", acp);
167*7c478bd9Sstevel@tonic-gate 			exit(1);
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < rcount; i++) {
170*7c478bd9Sstevel@tonic-gate 			putchar(*cp);
171*7c478bd9Sstevel@tonic-gate 		}
172*7c478bd9Sstevel@tonic-gate 		cp++;
173*7c478bd9Sstevel@tonic-gate 		sum += width * rcount;
174*7c478bd9Sstevel@tonic-gate 	} while (*cp);
175*7c478bd9Sstevel@tonic-gate 	return (sum);
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate /*
179*7c478bd9Sstevel@tonic-gate  * Format struct member, checking size.
180*7c478bd9Sstevel@tonic-gate  */
181*7c478bd9Sstevel@tonic-gate void
182*7c478bd9Sstevel@tonic-gate format(char *name, size_t size, char *fmt)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 	int fs;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	fs = do_fmt(fmt);
187*7c478bd9Sstevel@tonic-gate 	if (fs != size && warnings) {
188*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
189*7c478bd9Sstevel@tonic-gate 			"warning: \"%s\" size is %ld, \"%s\" width is %d\n",
190*7c478bd9Sstevel@tonic-gate 			name, size, fmt, fs);
191*7c478bd9Sstevel@tonic-gate 		warns++;
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	last_off += fs;
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate /*
197*7c478bd9Sstevel@tonic-gate  * Get the value at offset based on base.
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate void
200*7c478bd9Sstevel@tonic-gate indirect(off_t offset, size_t size, char *base, char *member)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	if (size == 8 || size == 4) {
203*7c478bd9Sstevel@tonic-gate 		if (offset == 0) {
204*7c478bd9Sstevel@tonic-gate 			printf("*%s", base);
205*7c478bd9Sstevel@tonic-gate 		} else {
206*7c478bd9Sstevel@tonic-gate 			printf("*(%s+0t%ld)", base, offset);
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 	} else if (size == 2) {
209*7c478bd9Sstevel@tonic-gate 		if (offset == 2) {
210*7c478bd9Sstevel@tonic-gate 			printf("(*%s&0xffff)", base);
211*7c478bd9Sstevel@tonic-gate 		} else {
212*7c478bd9Sstevel@tonic-gate 			printf("(*(%s+0t%ld)&0xffff)", base, offset - 2);
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate 	} else if (size == 1) {
215*7c478bd9Sstevel@tonic-gate 		if (offset == 3) {
216*7c478bd9Sstevel@tonic-gate 			printf("(*%s&0xff)", base);
217*7c478bd9Sstevel@tonic-gate 		} else {
218*7c478bd9Sstevel@tonic-gate 			if ((offset & 0x1) == 0x1) {
219*7c478bd9Sstevel@tonic-gate 				printf("(*(%s+0t%ld)&0xff)", base, offset - 3);
220*7c478bd9Sstevel@tonic-gate 			} else {
221*7c478bd9Sstevel@tonic-gate 				printf("((*(%s+0t%ld)&0xff00)/0x100)",
222*7c478bd9Sstevel@tonic-gate 				    base, offset - 2);
223*7c478bd9Sstevel@tonic-gate 			}
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 	} else {
226*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Indirect size %ld not 1, 2, or 4: %s\n",
227*7c478bd9Sstevel@tonic-gate 		    size, member);
228*7c478bd9Sstevel@tonic-gate 		exit(1);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate }
231