* arm.c (arm_compute_initial_elimination_offset): If optimizing for
[official-gcc.git] / gcc / cpppch.c
blobe7f0c91adfdcd2e863a2a6d2d3ab245dfb45f97e
1 /* Part of CPP library. (Precompiled header reading/writing.)
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "cpplib.h"
22 #include "cpphash.h"
23 #include "intl.h"
24 #include "hashtab.h"
25 #include "mkdeps.h"
27 static int write_macdef PARAMS ((cpp_reader *, cpp_hashnode *, void *));
28 static int save_idents PARAMS ((cpp_reader *, cpp_hashnode *, void *));
29 static hashval_t hashmem PARAMS ((const void *, size_t));
30 static hashval_t cpp_string_hash PARAMS ((const void *));
31 static int cpp_string_eq PARAMS ((const void *, const void *));
32 static int count_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
33 static int write_defs PARAMS ((cpp_reader *, cpp_hashnode *, void *));
34 static int save_macros PARAMS ((cpp_reader *, cpp_hashnode *, void *));
35 static int reset_ht PARAMS ((cpp_reader *, cpp_hashnode *, void *));
37 /* This structure represents a macro definition on disk. */
38 struct macrodef_struct
40 unsigned int definition_length;
41 unsigned short name_length;
42 unsigned short flags;
45 /* This is how we write out a macro definition.
46 Suitable for being called by cpp_forall_identifiers. */
48 static int
49 write_macdef (pfile, hn, file_p)
50 cpp_reader *pfile;
51 cpp_hashnode *hn;
52 void *file_p;
54 FILE *f = (FILE *) file_p;
55 switch (hn->type)
57 case NT_VOID:
58 if (! (hn->flags & NODE_POISONED))
59 return 1;
61 case NT_MACRO:
62 if ((hn->flags & NODE_BUILTIN))
63 return 1;
66 struct macrodef_struct s;
67 const unsigned char *defn;
69 s.name_length = NODE_LEN (hn);
70 s.flags = hn->flags & NODE_POISONED;
72 if (hn->type == NT_MACRO)
74 defn = cpp_macro_definition (pfile, hn);
75 s.definition_length = ustrlen (defn);
77 else
79 defn = NODE_NAME (hn);
80 s.definition_length = s.name_length;
83 if (fwrite (&s, sizeof (s), 1, f) != 1
84 || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
86 cpp_errno (pfile, DL_ERROR, "while writing precompiled header");
87 return 0;
90 return 1;
92 case NT_ASSERTION:
93 /* Not currently implemented. */
94 return 1;
96 default:
97 abort ();
101 /* This structure records the names of the defined macros.
102 It's also used as a callback structure for size_initial_idents
103 and save_idents. */
105 struct cpp_savedstate
107 /* A hash table of the defined identifiers. */
108 htab_t definedhash;
109 /* The size of the definitions of those identifiers (the size of
110 'definedstrs'). */
111 size_t hashsize;
112 /* Space for the next definition. Definitions are null-terminated
113 strings. */
114 unsigned char *definedstrs;
117 /* Save this identifier into the state: put it in the hash table,
118 put the definition in 'definedstrs'. */
120 static int
121 save_idents (pfile, hn, ss_p)
122 cpp_reader *pfile ATTRIBUTE_UNUSED;
123 cpp_hashnode *hn;
124 void *ss_p;
126 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
128 if (hn->type != NT_VOID)
130 struct cpp_string news;
131 void **slot;
133 news.len = NODE_LEN (hn);
134 news.text= NODE_NAME (hn);
135 slot = htab_find_slot (ss->definedhash, &news, INSERT);
136 if (*slot == NULL)
138 struct cpp_string *sp;
139 unsigned char *text;
141 sp = xmalloc (sizeof (struct cpp_string));
142 *slot = sp;
144 sp->len = NODE_LEN (hn);
145 sp->text = text = xmalloc (NODE_LEN (hn));
146 memcpy (text, NODE_NAME (hn), NODE_LEN (hn));
150 return 1;
153 /* Hash some memory in a generic way. */
155 static hashval_t
156 hashmem (p_p, sz)
157 const void *p_p;
158 size_t sz;
160 const unsigned char *p = (const unsigned char *)p_p;
161 size_t i;
162 hashval_t h;
164 h = 0;
165 for (i = 0; i < sz; i++)
166 h = h * 67 - (*p++ - 113);
167 return h;
170 /* Hash a cpp string for the hashtable machinery. */
172 static hashval_t
173 cpp_string_hash (a_p)
174 const void *a_p;
176 const struct cpp_string *a = (const struct cpp_string *) a_p;
177 return hashmem (a->text, a->len);
180 /* Compare two cpp strings for the hashtable machinery. */
182 static int
183 cpp_string_eq (a_p, b_p)
184 const void *a_p;
185 const void *b_p;
187 const struct cpp_string *a = (const struct cpp_string *) a_p;
188 const struct cpp_string *b = (const struct cpp_string *) b_p;
189 return (a->len == b->len
190 && memcmp (a->text, b->text, a->len) == 0);
193 /* Save the current definitions of the cpp_reader for dependency
194 checking purposes. When writing a precompiled header, this should
195 be called at the same point in the compilation as cpp_valid_state
196 would be called when reading the precompiled header back in. */
199 cpp_save_state (r, f)
200 cpp_reader *r;
201 FILE *f;
203 /* Save the list of non-void identifiers for the dependency checking. */
204 r->savedstate = xmalloc (sizeof (struct cpp_savedstate));
205 r->savedstate->definedhash = htab_create (100, cpp_string_hash,
206 cpp_string_eq, NULL);
207 cpp_forall_identifiers (r, save_idents, r->savedstate);
209 /* Write out the list of defined identifiers. */
210 cpp_forall_identifiers (r, write_macdef, f);
212 return 0;
215 /* Calculate the 'hashsize' field of the saved state. */
217 static int
218 count_defs (pfile, hn, ss_p)
219 cpp_reader *pfile ATTRIBUTE_UNUSED;
220 cpp_hashnode *hn;
221 void *ss_p;
223 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
225 switch (hn->type)
227 case NT_MACRO:
228 if (hn->flags & NODE_BUILTIN)
229 return 1;
231 /* else fall through. */
233 case NT_VOID:
235 struct cpp_string news;
236 void **slot;
238 news.len = NODE_LEN (hn);
239 news.text = NODE_NAME (hn);
240 slot = htab_find (ss->definedhash, &news);
241 if (slot == NULL)
242 ss->hashsize += NODE_LEN (hn) + 1;
244 return 1;
246 case NT_ASSERTION:
247 /* Not currently implemented. */
248 return 1;
250 default:
251 abort ();
255 /* Write the identifiers into 'definedstrs' of the state. */
257 static int
258 write_defs (pfile, hn, ss_p)
259 cpp_reader *pfile ATTRIBUTE_UNUSED;
260 cpp_hashnode *hn;
261 void *ss_p;
263 struct cpp_savedstate *const ss = (struct cpp_savedstate *)ss_p;
265 switch (hn->type)
267 case NT_MACRO:
268 if (hn->flags & NODE_BUILTIN)
269 return 1;
271 /* else fall through. */
273 case NT_VOID:
275 struct cpp_string news;
276 void **slot;
278 news.len = NODE_LEN (hn);
279 news.text = NODE_NAME (hn);
280 slot = htab_find (ss->definedhash, &news);
281 if (slot == NULL)
283 memcpy (ss->definedstrs, NODE_NAME (hn), NODE_LEN (hn));
284 ss->definedstrs[NODE_LEN (hn)] = 0;
285 ss->definedstrs += NODE_LEN (hn) + 1;
288 return 1;
290 case NT_ASSERTION:
291 /* Not currently implemented. */
292 return 1;
294 default:
295 abort ();
299 /* Write out the remainder of the dependency information. This should be
300 called after the PCH is ready to be saved. */
303 cpp_write_pch_deps (r, f)
304 cpp_reader *r;
305 FILE *f;
307 struct macrodef_struct z;
308 struct cpp_savedstate *const ss = r->savedstate;
309 unsigned char *definedstrs;
311 ss->hashsize = 0;
313 /* Write out the list of identifiers which have been seen and
314 weren't defined to anything previously. */
315 cpp_forall_identifiers (r, count_defs, ss);
316 definedstrs = ss->definedstrs = xmalloc (ss->hashsize);
317 cpp_forall_identifiers (r, write_defs, ss);
318 memset (&z, 0, sizeof (z));
319 z.definition_length = ss->hashsize;
320 if (fwrite (&z, sizeof (z), 1, f) != 1
321 || fwrite (definedstrs, ss->hashsize, 1, f) != 1)
323 cpp_errno (r, DL_ERROR, "while writing precompiled header");
324 return -1;
326 free (definedstrs);
328 /* Free the saved state. */
329 free (ss);
330 r->savedstate = NULL;
331 return 0;
334 /* Write out the definitions of the preprocessor, in a form suitable for
335 cpp_read_state. */
338 cpp_write_pch_state (r, f)
339 cpp_reader *r;
340 FILE *f;
342 struct macrodef_struct z;
344 /* Write out the list of defined identifiers. */
345 cpp_forall_identifiers (r, write_macdef, f);
346 memset (&z, 0, sizeof (z));
347 if (fwrite (&z, sizeof (z), 1, f) != 1)
349 cpp_errno (r, DL_ERROR, "while writing precompiled header");
350 return -1;
353 if (!r->deps)
354 r->deps = deps_init ();
356 if (deps_save (r->deps, f) != 0)
358 cpp_errno (r, DL_ERROR, "while writing precompiled header");
359 return -1;
362 return 0;
365 /* Return nonzero if FD is a precompiled header which is consistent
366 with the preprocessor's current definitions. It will be consistent
367 when:
369 - anything that was defined just before the PCH was generated
370 is defined the same way now; and
371 - anything that was not defined then, but is defined now, was not
372 used by the PCH.
374 NAME is used to print warnings if `warn_invalid_pch' is set in the
375 reader's flags.
379 cpp_valid_state (r, name, fd)
380 cpp_reader *r;
381 const char *name;
382 int fd;
384 struct macrodef_struct m;
385 size_t namebufsz = 256;
386 unsigned char *namebuf = xmalloc (namebufsz);
387 unsigned char *undeftab = NULL;
388 unsigned int i;
390 /* Read in the list of identifiers that must be defined
391 Check that they are defined in the same way. */
392 for (;;)
394 cpp_hashnode *h;
395 const unsigned char *newdefn;
397 if (read (fd, &m, sizeof (m)) != sizeof (m))
398 goto error;
400 if (m.name_length == 0)
401 break;
403 if (m.definition_length > namebufsz)
405 free (namebuf);
406 namebufsz = m.definition_length + 256;
407 namebuf = xmalloc (namebufsz);
410 if ((size_t)read (fd, namebuf, m.definition_length)
411 != m.definition_length)
412 goto error;
414 h = cpp_lookup (r, namebuf, m.name_length);
415 if (m.flags & NODE_POISONED
416 || h->type != NT_MACRO
417 || h->flags & NODE_POISONED)
419 if (CPP_OPTION (r, warn_invalid_pch))
420 cpp_error (r, DL_WARNING,
421 "%s: not used because `%.*s' not defined",
422 name, m.name_length, namebuf);
423 goto fail;
426 newdefn = cpp_macro_definition (r, h);
428 if (m.definition_length != ustrlen (newdefn)
429 || memcmp (namebuf, newdefn, m.definition_length) != 0)
431 if (CPP_OPTION (r, warn_invalid_pch))
432 cpp_error (r, DL_WARNING,
433 "%s: not used because `%.*s' defined as `%s' not `%.*s'",
434 name, m.name_length, namebuf, newdefn + m.name_length,
435 m.definition_length - m.name_length,
436 namebuf + m.name_length);
437 goto fail;
440 free (namebuf);
441 namebuf = NULL;
443 /* Read in the list of identifiers that must not be defined.
444 Check that they really aren't. */
445 undeftab = xmalloc (m.definition_length);
446 if ((size_t) read (fd, undeftab, m.definition_length) != m.definition_length)
447 goto error;
448 for (i = 0; i < m.definition_length; )
450 int l = ustrlen (undeftab + i);
451 cpp_hashnode *h;
452 h = cpp_lookup (r, undeftab + i, l);
453 if (h->type != NT_VOID
454 || h->flags & NODE_POISONED)
456 if (CPP_OPTION (r, warn_invalid_pch))
457 cpp_error (r, DL_WARNING, "%s: not used because `%s' is defined",
458 name, undeftab + i);
459 goto fail;
461 i += l + 1;
463 free (undeftab);
465 /* We win! */
466 return 0;
468 error:
469 cpp_errno (r, DL_ERROR, "while reading precompiled header");
470 return -1;
472 fail:
473 if (namebuf != NULL)
474 free (namebuf);
475 if (undeftab != NULL)
476 free (undeftab);
477 return 1;
480 /* Save all the existing macros and assertions.
481 This code assumes that there might be hundreds, but not thousands of
482 existing definitions. */
484 struct save_macro_item {
485 struct save_macro_item *next;
486 struct cpp_hashnode macs[64];
489 struct save_macro_data
491 struct save_macro_item *macros;
492 size_t count;
493 char **saved_pragmas;
496 /* Save the definition of a single macro, so that it will persist across
497 a PCH restore. */
499 static int
500 save_macros (r, h, data_p)
501 cpp_reader *r ATTRIBUTE_UNUSED;
502 cpp_hashnode *h;
503 void *data_p;
505 struct save_macro_data *data = (struct save_macro_data *)data_p;
506 if (h->type != NT_VOID
507 && (h->flags & NODE_BUILTIN) == 0)
509 cpp_hashnode *save;
510 if (data->count == ARRAY_SIZE (data->macros->macs))
512 struct save_macro_item *d = data->macros;
513 data->macros = xmalloc (sizeof (struct save_macro_item));
514 data->macros->next = d;
515 data->count = 0;
517 save = data->macros->macs + data->count;
518 data->count++;
519 memcpy (save, h, sizeof (struct cpp_hashnode));
520 HT_STR (&save->ident) = xmemdup (HT_STR (HT_NODE (save)),
521 HT_LEN (HT_NODE (save)),
522 HT_LEN (HT_NODE (save)) + 1);
524 return 1;
527 /* Prepare to restore the state, by saving the currently-defined
528 macros in 'data'. */
530 void
531 cpp_prepare_state (r, data)
532 cpp_reader *r;
533 struct save_macro_data **data;
535 struct save_macro_data *d = xmalloc (sizeof (struct save_macro_data));
537 d->macros = NULL;
538 d->count = ARRAY_SIZE (d->macros->macs);
539 cpp_forall_identifiers (r, save_macros, d);
540 d->saved_pragmas = _cpp_save_pragma_names (r);
541 *data = d;
544 /* Erase all the existing macros and assertions. */
546 static int
547 reset_ht (r, h, unused)
548 cpp_reader *r ATTRIBUTE_UNUSED;
549 cpp_hashnode *h;
550 void *unused ATTRIBUTE_UNUSED;
552 if (h->type != NT_VOID
553 && (h->flags & NODE_BUILTIN) == 0)
555 h->type = NT_VOID;
556 memset (&h->value, 0, sizeof (h->value));
558 return 1;
561 /* Given a precompiled header that was previously determined to be valid,
562 apply all its definitions (and undefinitions) to the current state.
563 DEPNAME is passed to deps_restore. */
566 cpp_read_state (r, name, f, data)
567 cpp_reader *r;
568 const char *name;
569 FILE *f;
570 struct save_macro_data *data;
572 struct macrodef_struct m;
573 size_t defnlen = 256;
574 unsigned char *defn = xmalloc (defnlen);
575 struct lexer_state old_state;
576 struct save_macro_item *d;
577 size_t i, mac_count;
578 int saved_line = r->line;
580 /* Erase all the existing hashtable entries for macros. At this
581 point, they're all from the PCH file, and their pointers won't be
582 valid. */
583 cpp_forall_identifiers (r, reset_ht, NULL);
585 /* Restore spec_nodes, which will be full of references to the old
586 hashtable entries and so will now be invalid. */
588 struct spec_nodes *s = &r->spec_nodes;
589 s->n_defined = cpp_lookup (r, DSC("defined"));
590 s->n_true = cpp_lookup (r, DSC("true"));
591 s->n_false = cpp_lookup (r, DSC("false"));
592 s->n__VA_ARGS__ = cpp_lookup (r, DSC("__VA_ARGS__"));
595 /* Run through the carefully-saved macros, insert them. */
596 d = data->macros;
597 mac_count = data->count;
598 while (d)
600 struct save_macro_item *nextd;
601 for (i = 0; i < mac_count; i++)
603 cpp_hashnode *h;
605 h = cpp_lookup (r, HT_STR (HT_NODE (&d->macs[i])),
606 HT_LEN (HT_NODE (&d->macs[i])));
607 h->type = d->macs[i].type;
608 h->flags = d->macs[i].flags;
609 h->value = d->macs[i].value;
610 free ((void *)HT_STR (HT_NODE (&d->macs[i])));
612 nextd = d->next;
613 free (d);
614 d = nextd;
615 mac_count = ARRAY_SIZE (d->macs);
618 _cpp_restore_pragma_names (r, data->saved_pragmas);
620 free (data);
622 old_state = r->state;
624 r->state.in_directive = 1;
625 r->state.prevent_expansion = 1;
626 r->state.angled_headers = 0;
628 /* Read in the identifiers that must be defined. */
629 for (;;)
631 cpp_hashnode *h;
633 if (fread (&m, sizeof (m), 1, f) != 1)
634 goto error;
636 if (m.name_length == 0)
637 break;
639 if (defnlen < m.definition_length + 1)
641 defnlen = m.definition_length + 256;
642 defn = xrealloc (defn, defnlen);
645 if (fread (defn, 1, m.definition_length, f) != m.definition_length)
646 goto error;
647 defn[m.definition_length] = '\0';
649 h = cpp_lookup (r, defn, m.name_length);
651 if (h->type == NT_MACRO)
652 _cpp_free_definition (h);
653 if (m.flags & NODE_POISONED)
654 h->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
655 else if (m.name_length != m.definition_length)
657 if (cpp_push_buffer (r, defn + m.name_length,
658 m.definition_length - m.name_length,
659 true, 1) != NULL)
661 if (!_cpp_create_definition (r, h))
662 abort ();
663 _cpp_pop_buffer (r);
665 else
666 abort ();
670 r->state = old_state;
671 r->line = saved_line;
672 free (defn);
673 defn = NULL;
675 if (deps_restore (r->deps, f, CPP_OPTION (r, restore_pch_deps) ? name : NULL)
676 != 0)
677 goto error;
679 return 0;
681 error:
682 cpp_errno (r, DL_ERROR, "while reading precompiled header");
683 return -1;