forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize.c
333 lines (288 loc) · 8.21 KB
/
size.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*ident "@(#)cls4:src/size.c 1.4" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
size.c:
initialize alignment and sizeof "constants"
**********************************************************************/
#include "cfront.h"
#include "size.h"
int BI_IN_WORD = DBI_IN_WORD;
int BI_IN_BYTE = DBI_IN_BYTE;
int SZ_CHAR = DSZ_CHAR;
int AL_CHAR = DAL_CHAR;
int SZ_SHORT = DSZ_SHORT;
int AL_SHORT = DAL_SHORT;
int SZ_INT = DSZ_INT;
int AL_INT = DAL_INT;
int SZ_LONG = DSZ_LONG;
int AL_LONG = DAL_LONG;
int SZ_FLOAT = DSZ_FLOAT;
int AL_FLOAT = DAL_FLOAT;
int SZ_DOUBLE = DSZ_DOUBLE;
int AL_DOUBLE = DAL_DOUBLE;
int SZ_LDOUBLE = DSZ_LDOUBLE;
int AL_LDOUBLE = DAL_LDOUBLE;
int SZ_STRUCT = DSZ_STRUCT;
int AL_STRUCT = DAL_STRUCT;
// int SZ_FRAME = DSZ_FRAME;
// int AL_FRAME = DAL_FRAME;
int SZ_WORD = DSZ_WORD;
int SZ_WPTR = DSZ_WPTR;
int AL_WPTR = DAL_WPTR;
int SZ_BPTR = DSZ_BPTR;
int AL_BPTR = DAL_BPTR;
// int SZ_TOP = DSZ_TOP;
// int SZ_BOTTOM = DSZ_BOTTOM;
char *LARGEST_INT = DLARGEST_INT;
int F_SENSITIVE = DF_SENSITIVE;
int F_OPTIMIZED = DF_OPTIMIZED;
static int arg1 = 0;
int get_line(FILE *fp) {
char s[32];
char s2[32];
if (fscanf(fp, "%s %s", s2, s) == EOF)
return 0;
if (strcmp("DLARGEST_INT", s) == 0) {
if (fscanf(fp, " %s", s2) == EOF)
return 0;
//// get rid of quotes
for (int i = 0; i <= strlen(s2) + 1; i++)
if (s2[i] == '"') {
for (int j = i; j <= strlen(s2) + 1; j++)
s2[j] = s2[j + 1];
}
} else if (fscanf(fp, "%d ", &arg1) == EOF)
return 0;
if (strcmp("DSZ_CHAR", s) == 0) {
SZ_CHAR = arg1;
return 1;
}
if (strcmp("DAL_CHAR", s) == 0) {
AL_CHAR = arg1;
return 1;
}
if (strcmp("DSZ_SHORT", s) == 0) {
SZ_SHORT = arg1;
return 1;
}
if (strcmp("DAL_SHORT", s) == 0) {
AL_SHORT = arg1;
return 1;
}
if (strcmp("DSZ_INT", s) == 0) {
SZ_INT = arg1;
return 1;
}
if (strcmp("DAL_INT", s) == 0) {
AL_INT = arg1;
return 1;
}
if (strcmp("DLARGEST_INT", s) == 0) {
LARGEST_INT = new char[strlen(s2) + 1];
strcpy(LARGEST_INT, s2);
return 1;
}
if (strcmp("DSZ_LONG", s) == 0) {
SZ_LONG = arg1;
return 1;
}
if (strcmp("DAL_LONG", s) == 0) {
AL_LONG = arg1;
return 1;
}
if (strcmp("DSZ_FLOAT", s) == 0) {
SZ_FLOAT = arg1;
return 1;
}
if (strcmp("DAL_FLOAT", s) == 0) {
AL_FLOAT = arg1;
return 1;
}
if (strcmp("DSZ_DOUBLE", s) == 0) {
SZ_DOUBLE = arg1;
return 1;
}
if (strcmp("DAL_DOUBLE", s) == 0) {
AL_DOUBLE = arg1;
return 1;
}
if (strcmp("DSZ_LDOUBLE", s) == 0) {
SZ_LDOUBLE = arg1;
return 1;
}
if (strcmp("DAL_LDOUBLE", s) == 0) {
AL_LDOUBLE = arg1;
return 1;
}
if (strcmp("DBI_IN_BYTE", s) == 0) {
BI_IN_BYTE = arg1;
return 1;
}
if (strcmp("DBI_IN_WORD", s) == 0) {
BI_IN_WORD = arg1;
return 1;
}
if (strcmp("DSZ_STRUCT", s) == 0) {
SZ_STRUCT = arg1;
return 1;
}
if (strcmp("DAL_STRUCT", s) == 0) {
AL_STRUCT = arg1;
return 1;
}
if (strcmp("DF_SENSITIVE", s) == 0) {
F_SENSITIVE = arg1;
return 1;
}
if (strcmp("DF_OPTIMIZED", s) == 0) {
F_OPTIMIZED = arg1;
return 1;
}
if (strcmp("frame", s) == 0) {
// SZ_FRAME = arg1;
// AL_FRAME = arg2;
return 1;
}
if (strcmp("DSZ_WORD", s) == 0) {
SZ_WORD = arg1;
return 1;
}
if (strcmp("DSZ_WPTR", s) == 0) {
SZ_WPTR = arg1;
return 1;
}
if (strcmp("DAL_WPTR", s) == 0) {
AL_WPTR = arg1;
return 1;
}
if (strcmp("DSZ_BPTR", s) == 0) {
SZ_BPTR = arg1;
return 1;
}
if (strcmp("DAL_BPTR", s) == 0) {
AL_BPTR = arg1;
return 1;
}
if (strcmp("top", s) == 0) {
// SZ_TOP = arg1;
// SZ_BOTTOM = arg2;
return 1;
}
return 0;
}
int read_align(char *f) {
char *p = f;
if (*p == 0) {
fprintf(stderr, "size/align file missing\n");
ext(1);
}
FILE *fp = fopen(f, "r");
if (fp == 0)
return 1;
while (get_line(fp))
;
return 0;
}
/*
print_align(char* s)
{
fprintf(stderr,"%s sizes and alignments\n\n",s);
fprintf(stderr," size align largest\n");
fprintf(stderr,"char %d %d\n",SZ_CHAR,AL_CHAR);
fprintf(stderr,"short %d %d\n",SZ_SHORT,AL_SHORT);
fprintf(stderr,"int %d %d %s\n",SZ_INT,AL_INT,LARGEST_INT);
fprintf(stderr,"long %d %d\n",SZ_LONG,AL_LONG);
fprintf(stderr,"float %d %d\n",SZ_FLOAT,AL_FLOAT);
fprintf(stderr,"double %d %d\n",SZ_DOUBLE,AL_DOUBLE);
fprintf(stderr,"ldouble %d %d\n",SZ_LDOUBLE,AL_LDOUBLE);
fprintf(stderr,"bptr %d %d\n",SZ_BPTR,AL_BPTR);
fprintf(stderr,"wptr %d %d\n",SZ_WPTR,AL_WPTR);
fprintf(stderr,"struct %d %d\n",SZ_STRUCT,AL_STRUCT);
fprintf(stderr,"struct2 %d %d\n",F_SENSITIVE,F_OPTIMIZED);
// fprintf(stderr,"frame %d %d\n",SZ_FRAME,AL_FRAME);
fprintf(stderr,"%d bits in a byte, %d bits in a word, %d bytes in a word\n",
BI_IN_BYTE, BI_IN_WORD, SZ_WORD);
return 1;
}
*/
int c_strlen(const char *s)
/*
return sizeof(s) with escapes processed
sizeof("") == 1 the terminating 0
sizeof("a") == 2
sizeof("\0x") == 3 0 x 0
sizeof("\012") == 2 '\012'
sizeof("\01") '\001'
sizeof("\x") == 2 \ ignored
*/
{
int i = 1;
for (const char *p = s; *p; i++, p++) {
if (*p == '\\') { // '\?
switch (*++p) {
case '0':
switch (p[1]) { // '\01' or '\012'
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
break;
default:
continue; // '\0'
}
/* no break */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7': // '\123'
switch (*++p) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
switch (*++p) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
break;
default:
--p;
}
break;
default:
--p;
break;
}
break;
case '\n': // \newline doesn't count
i--;
break;
}
}
}
return i;
}