-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsavenl.c
401 lines (352 loc) · 9.51 KB
/
savenl.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*-
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if !defined(lint) && defined(sccs)
static char sccsid[] = "@(#)savenl.c 8.2 (Berkeley) 5/24/94";
#endif /* not lint */
/*
* savenl - routines for saving namelist and line number information
*
* This module contains the routines that make pi dump a namelist
* at the end of the object file. We do this by first creating
* four temporary files in "startnlfile". One temp file contains
* the string table, one the symbol table, one the file name
* information and one the line number information.
*
* Prior to generation of the code for a statement the "lineno"
* routine is called to dump the line number and current object
* address. At the end of each block "savenl" is called to dump
* the strings and symbol structures.
*
* At the end of execution "copynlfile" is called and it copies
* the temp files onto the end of the obj file.
*
* In case of error, "removenlfile" is called to destroy the temp files.
*
* The only other changes to pi are in calling these routines from
*
* "main" (main.c)
* "yymain" (yymain.c)
* "funcend" (fend.c)
* "yyget" (yyget.c)
* "putline" (stat.c)
*/
#include "whoami.h"
#ifdef OBJ
/*
* pdx header files
*/
#include "../pdx/defs.h"
#include "../pdx/object.h"
#include "../pdx/object/objsym.rep"
#include "../pdx/mappings.h"
#include "../pdx/mappings/filetab.h"
#include "0.h"
#include "objfmt.h"
struct nlhdr nlhdr = {0};
LOCAL char symname[] = "/tmp/obj.symXXXXXX";
LOCAL char strname[] = "/tmp/obj.strXXXXXX";
LOCAL char filesname[] = "/tmp/obj.filesXXXXXX";
LOCAL char linesname[] = "/tmp/obj.linesXXXXXX";
#if defined(WIN32)
LOCAL char t_symname[_MAX_PATH] = {0};
LOCAL char t_strname[_MAX_PATH] = {0};
LOCAL char t_filesname[_MAX_PATH] = {0};
LOCAL char t_linesname[_MAX_PATH] = {0};
#endif
LOCAL FILE *symfp;
LOCAL FILE *strfp;
LOCAL FILE *filesfp;
LOCAL FILE *linesfp;
LOCAL long nlsize;
LOCAL void putblock(char *s);
LOCAL void putstring(char *s);
/*
* create temporary files for the namelist info
*/
void
startnlfile()
{
nlsize = 0;
#if defined(WIN32)
xmktemp(symname, t_symname, sizeof(t_symname));
xmktemp(strname, t_strname, sizeof(t_strname));
xmktemp(filesname, t_filesname, sizeof(t_filesname));
xmktemp(linesname, t_linesname, sizeof(t_linesname));
symfp = fopen(t_symname, "wb");
strfp = fopen(t_strname, "wb");
filesfp = fopen(t_filesname, "wb");
linesfp = fopen(t_linesname, "wb");
#else
(void) mktemp(symname);
(void) mktemp(strname);
(void) mktemp(filesname);
(void) mktemp(linesname);
symfp = fopen(symname, "wb");
strfp = fopen(strname, "wb");
filesfp = fopen(filesname, "wb");
linesfp = fopen(linesname, "wb");
#endif
if (symfp==NULL || strfp==NULL || filesfp==NULL || linesfp==NULL) {
fprintf(stderr, "can't create /tmp/obj");
pexit(NOSTART);
}
newfile(filename, 1);
}
/*
* now copy the temp files back to obj; strings, symbols, file names, and lines
*
* There's some efficiency garbage here that uses straight system
* calls rather than standard I/O library calls.
*/
void
copynlfile()
{
register int n;
int symfd, strfd, filesfd, linesfd;
char buff[BUFSIZ];
(void) fclose((FILE *) symfp);
(void) fclose((FILE *) strfp);
(void) fclose((FILE *) filesfp);
(void) fclose((FILE *) linesfp);
if (!opt('g')) {
removenlfile();
return;
}
symfd = open(symname, O_RDONLY | O_BINARY);
strfd = open(strname, O_RDONLY | O_BINARY);
filesfd = open(filesname, O_RDONLY | O_BINARY);
linesfd = open(linesname, O_RDONLY | O_BINARY);
if (symfd < 0 || strfd < 0 || filesfd < 0 || linesfd < 0) {
fprintf(stderr, "sync error on /tmp/obj");
pexit(ERRS);
}
(void) lseek(ofil, 0L, 2);
#ifdef DEBUG
if (opt('x'))
dumphex(tell(ofil), &nlhdr, sizeof(nlhdr));
#endif
write(ofil, (char *)(&nlhdr), sizeof(nlhdr));
n = read(strfd, buff, BUFSIZ - sizeof(nlhdr));
#ifdef DEBUG
if (opt('x'))
dumphex(tell(ofil), buff, n);
#endif
write(ofil, buff, n);
cat(strfd);
cat(symfd);
cat(filesfd);
cat(linesfd);
removenlfile();
}
void
cat(fd)
int fd;
{
register int n;
char buff[BUFSIZ];
while ((n = read(fd, buff, BUFSIZ)) > 0) {
#ifdef DEBUG
if (opt('x'))
dumphex(tell(ofil), buff, n);
#endif
write(ofil, buff, n);
}
(void) close(fd);
}
void
removenlfile()
{
#if defined(WIN32)
unlink(t_symname);
unlink(t_strname);
unlink(t_filesname);
unlink(t_linesname);
#else
unlink(symname);
unlink(strname);
unlink(filesname);
unlink(linesname);
#endif
}
int
nlhdrsize()
{
int r;
if (!opt('g')) {
r = 0;
} else {
r = nlsize + sizeof(nlhdr);
}
return r;
}
#define isblock(s) (s->class == FUNC || s->class == PROC)
#define isbuiltin(s) ((s->nl_block&037) == 0 && isblock(s))
#define symno(p) (p==NULL ? 0 : nloff(p))
struct nls {
struct nl *nls_low;
struct nl *nls_high;
};
extern struct nl nl[], *nlp, ntab[], *nlact;
/*VARARGS*/
void
savenl(to, rout)
struct nl *to;
{
register struct nl *p;
register OBJSYM *s;
OBJSYM tmpsym;
struct nls *nlsp;
if (to != NIL) {
putblock((char *) rout);
} else {
putblock("main program");
}
nlsp = (struct nls *) nlact;
s = &tmpsym;
for (p = nlp; p != to;) {
if (p == nlsp->nls_low) {
if (nlsp == ((struct nls *) &ntab[0]))
break;
nlsp--;
p = nlsp->nls_high;
}
p--;
if (isbuiltin(p) || symno(p) == 0) {
continue;
}
nlhdr.nsyms++;
nlsize += sizeof(OBJSYM) + sizeof(int);
(void) putw(symno(p), symfp);
if (p->symbol != NULL) {
s->strindex = nlhdr.stringsize;
putstring(p->symbol);
} else {
s->strindex = 0;
}
s->oclass = p->class;
s->oblkno = (p->nl_block&037);
s->typno = symno(p->type);
s->chno = symno(p->chain);
s->osymvalue.orangev.lower = p->range[0];
s->osymvalue.orangev.upper = p->range[1];
if (isblock(p)) {
s->osymvalue.ofuncv.codeloc = p->value[NL_ENTLOC];
} else if (p->class == RECORD || p->class == VARNT) {
s->osymvalue.ovarnt.vtorecno = symno(p->ptr[2]);
s->osymvalue.ovarnt.vtagno = symno(p->ptr[3]);
}
fwrite((char *) s, sizeof(*s), 1, symfp);
}
}
/*
* Dump a line number and the current object location counter.
*
* To save space the difference from the previous line number and offset
* (one byte each) is dumped.
*/
LOCAL int oline = 0;
LOCAL int olc = HEADER_BYTES;
void
lineno(line)
int line;
{
OBJLINE info;
if (line != oline) {
nlhdr.nlines++;
nlsize += sizeof(OBJLINE);
info.separate.lineincr = line - oline;
info.separate.addrincr = ((unsigned short) (lc - olc));
(void) putw((int) info.together, linesfp);
oline = line;
olc = (int) lc;
}
}
/*
* put out a file name entry, including:
*
* the current line number for the new file
* the current location counter
* the string table address of the file name
* an index into the current line number information
*/
void
newfile(s, line)
char *s;
int line;
{
FILETAB ft;
nlhdr.nfiles++;
nlsize += sizeof(FILETAB);
ft.line = line;
oline = line;
if (lc == 0) {
ft.addr = 0;
} else {
ft.addr = ((LINENO) lc - HEADER_BYTES );
}
ft.filename = (char *) nlhdr.stringsize;
putstring(s);
ft.lineindex = nlhdr.nlines;
fwrite((char *) (&ft), sizeof(ft), 1, filesfp);
}
/*
* put out a dummy symbol at the beginning of a block
*/
LOCAL void
putblock(s)
char *s;
{
static OBJSYM zerosym;
nlhdr.nsyms++;
nlsize += sizeof(OBJSYM) + sizeof(int);
(void) putw(0, symfp);
zerosym.strindex = nlhdr.stringsize;
putstring(s);
fwrite((char *) (&zerosym), sizeof(zerosym), 1, symfp);
}
/*
* put out a string to the string table file
*/
LOCAL void
putstring(s)
char *s;
{
register char *p;
for (p = s; *p != '\0'; p++) {
putc(*p, strfp);
}
nlhdr.stringsize += (p - s + 1);
nlsize += (p - s + 1);
putc('\0', strfp);
}
#endif /*OBJ*/