]> git.saurik.com Git - apple/libc.git/blob - ppc/sys/genassym.c
Libc-262.2.12.tar.gz
[apple/libc.git] / ppc / sys / genassym.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #import <stdio.h>
26 #import <ctype.h>
27 #import <libc.h>
28 #import <ansi/string.h>
29
30 #import "genassym.h"
31
32 #define NAME_LEN 30
33
34 char *progname;
35
36 unsigned bit_num(char *reg_type, char *field, unsigned bits)
37 {
38 unsigned bit;
39 unsigned mask;
40
41 for (bit = 0, mask = 0x1;
42 (mask & bits) == 0 && mask;
43 mask <<= 1, bit += 1)
44 continue;
45 if (mask)
46 return bit;
47 fprintf(stderr, "%s: Bad BIT_POS for %s.%s\n", progname,
48 reg_type, field);
49 exit(1);
50 }
51
52 unsigned field_width(char *reg_type, char *field, unsigned bits)
53 {
54 unsigned width;
55
56 while (bits && (bits & 0x1) == 0)
57 bits >>= 1;
58 for (width = 0; (bits & 0x1) == 1; bits >>= 1, width += 1)
59 continue;
60 if (bits == 0 && width)
61 return width;
62 fprintf(stderr, "%s: Bad BIT_FIELD for %s.%s\n", progname,
63 reg_type, field);
64 exit(1);
65 }
66
67 unsigned log2(unsigned val, char *type)
68 {
69 unsigned l2 = 0;
70
71 if (val == 0) {
72 fprintf(stderr, "log2: sizeof(%s) is zero!\n", type);
73 exit(1);
74 }
75 while ((val & 0x1) == 0) {
76 l2 += 1;
77 val >>= 1;
78 }
79 if (val != 0x1) {
80 fprintf(stderr, "log2: sizeof(%s) is not power of two!\n",
81 type);
82 exit(1);
83 }
84 return l2;
85 }
86
87 const char *skip_white(const char *cp)
88 {
89 while (*cp && isspace(*cp))
90 cp += 1;
91 return cp;
92 }
93
94 const char *strip_prefix(const char *cp, const char *prefix)
95 {
96 int len;
97
98 cp = skip_white(cp);
99 len = strlen(prefix);
100 if (strncmp(cp, prefix, len) == 0 && isspace(*(cp+len)))
101 cp += len;
102 return cp;
103 }
104
105 void
106 print_define(const char *prefix, const char *type_name, const char *field)
107 {
108 const char *cp;
109 int col = 0;
110
111 printf("#define\t");
112 if (prefix != NULL && *prefix != '\0') {
113 printf("%s", prefix);
114 col += strlen(prefix);
115 }
116 if (type_name != NULL && *type_name != '\0') {
117 cp = strip_prefix(type_name, "struct");
118 cp = strip_prefix(cp, "enum");
119 cp = skip_white(cp);
120 if (*cp != '\0' && col != 0) {
121 putchar('_');
122 col += 1;
123 }
124 for (; *cp != '\0'; cp++) {
125 if (isspace(*cp))
126 break;
127 if (*cp == '*')
128 break;
129 if (strncmp(cp, "_t", 2) == 0 && !isalnum(cp[2]))
130 break;
131 putchar(isalpha(*cp) ? toupper(*cp) : *cp);
132 col += 1;
133
134 }
135 }
136 if (field != NULL && *field != '\0') {
137 if (col != 0) {
138 putchar('_');
139 col++;
140 }
141 for (cp = field; *cp != 0; cp++) {
142 if (*cp == '.')
143 putchar('_');
144 else if (*cp == '[')
145 putchar('_');
146 else if (*cp == ']')
147 continue;
148 else if (!isspace(*cp))
149 putchar(isalpha(*cp) ? toupper(*cp) : *cp);
150 col++;
151 }
152 }
153 if (col == 0) {
154 fprintf(stderr, "%s: Bad call to print_define\n", progname);
155 exit(1);
156 }
157 do {
158 putchar(' ');
159 col += 1;
160 } while (col < NAME_LEN);
161 }
162
163 void print_dec(int val)
164 {
165 printf("%d\n", val);
166 }
167
168 void print_hex(unsigned val)
169 {
170 printf("%#010x\n", val);
171 }
172
173 void print_str(const char *str)
174 {
175 printf("%s\n", str);
176 }
177
178 void comment(cmt_level_t level, const char *cmt)
179 {
180 switch (level) {
181 case MAJOR:
182 printf("\n\n");
183 printf("/*\n");
184 printf(" * %s\n", cmt);
185 printf(" */\n");
186 break;
187 case MINOR:
188 printf("\n");
189 printf("/* %s */\n", cmt);
190 printf("\n");
191 break;
192 default:
193 fprintf(stderr, "%s: Bad comment level\n", progname);
194 exit(1);
195 }
196 }
197
198 void main(int argc, char **argv)
199 {
200 progname = argv[0];
201
202 printf("/* assym.h -- generated by genassym */\n");
203 printf("/* DON'T EDIT THIS -- change assymdefs.c */\n");
204
205 assymdefs();
206
207 exit(0);
208 }