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