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