OSDN Git Service

Workaround for Itanium A/B step errata
[pf3gnuchains/gcc-fork.git] / gcc / c-pragma.c
1 /* Handle #pragma, system V.4 style.  Supports #pragma weak and #pragma pack.
2    Copyright (C) 1992, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "function.h"
26 #include "defaults.h"
27 #include "cpplib.h"
28 #include "c-pragma.h"
29 #include "flags.h"
30 #include "toplev.h"
31 #include "ggc.h"
32 #include "c-lex.h"
33 #include "tm_p.h"
34
35 #if USE_CPPLIB
36 extern cpp_reader parse_in;
37 #else
38 struct pragma_entry;
39 static struct pragma_entry *pragmas;
40
41 void cpp_register_pragma PARAMS ((cpp_reader *, const char *, const char *,
42                                   void (*) PARAMS ((cpp_reader *)) ));
43 void cpp_register_pragma_space PARAMS ((cpp_reader *, const char *));
44 #endif
45
46 #define BAD(msgid) do { warning (msgid); return; } while (0)
47 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
48
49 #ifdef HANDLE_PRAGMA_PACK
50 static void handle_pragma_pack PARAMS ((cpp_reader *));
51
52 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
53 typedef struct align_stack
54 {
55   int                  alignment;
56   unsigned int         num_pushes;
57   tree                 id;
58   struct align_stack * prev;
59 } align_stack;
60
61 static struct align_stack * alignment_stack = NULL;
62
63 /* If we have a "global" #pragma pack(<n>) in effect when the first
64    #pragma pack(push,<n>) is encountered, this stores the value of 
65    maximum_field_alignment in effect.  When the final pop_alignment() 
66    happens, we restore the value to this, not to a value of 0 for
67    maximum_field_alignment.  Value is in bits. */
68 static int  default_alignment;
69 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
70 (default_alignment = maximum_field_alignment = (ALIGN))
71
72 static void push_alignment PARAMS ((int, tree));
73 static void pop_alignment  PARAMS ((tree));
74 static void mark_align_stack PARAMS ((void *));
75
76 /* Push an alignment value onto the stack.  */
77 static void
78 push_alignment (alignment, id)
79      int alignment;
80      tree id;
81 {
82   
83   if (alignment_stack == NULL
84       || alignment_stack->alignment != alignment
85       || id != NULL_TREE)
86     {
87       align_stack * entry;
88
89       entry = (align_stack *) xmalloc (sizeof (* entry));
90
91       entry->alignment  = alignment;
92       entry->num_pushes = 1;
93       entry->id         = id;
94       entry->prev       = alignment_stack;
95       
96       /* The current value of maximum_field_alignment is not necessarily 
97          0 since there may be a #pragma pack(<n>) in effect; remember it 
98          so that we can restore it after the final #pragma pop(). */
99       if (alignment_stack == NULL)
100         default_alignment = maximum_field_alignment;
101       
102       alignment_stack = entry;
103
104       maximum_field_alignment = alignment;
105     }
106   else
107     alignment_stack->num_pushes ++;
108 }
109
110 /* Undo a push of an alignment onto the stack.  */
111 static void
112 pop_alignment (id)
113      tree id;
114 {
115   align_stack * entry;
116       
117   if (alignment_stack == NULL)
118     {
119       warning ("\
120 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
121                );
122       return;
123     }
124
125   /* If we got an identifier, strip away everything above the target
126      entry so that the next step will restore the state just below it.  */
127   if (id)
128     {
129       for (entry = alignment_stack; entry; entry = entry->prev)
130         if (entry->id == id)
131           {
132             entry->num_pushes = 1;
133             alignment_stack = entry;
134             break;
135           }
136       if (entry == NULL)
137         warning ("\
138 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
139                  , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
140     }
141
142   if (-- alignment_stack->num_pushes == 0)
143     {
144       entry = alignment_stack->prev;
145
146       if (entry == NULL)
147         maximum_field_alignment = default_alignment;
148       else
149         maximum_field_alignment = entry->alignment;
150
151       free (alignment_stack);
152
153       alignment_stack = entry;
154     }
155 }
156
157 static void
158 mark_align_stack (p)
159     void *p;
160 {
161   align_stack *a = *(align_stack **) p;
162
163   while (a)
164     {
165       ggc_mark_tree (a->id);
166       a = a->prev;
167     }
168 }
169 #else  /* not HANDLE_PRAGMA_PACK_PUSH_POP */
170 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
171 #define push_alignment(ID, N) \
172     BAD("#pragma pack(push[, id], <n>) is not supported on this target")
173 #define pop_alignment(ID) \
174     BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
175 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
176
177 /* #pragma pack ()
178    #pragma pack (N)
179    
180    #pragma pack (push, N)
181    #pragma pack (push, ID, N)
182    #pragma pack (pop)
183    #pragma pack (pop, ID) */
184 static void
185 handle_pragma_pack (dummy)
186      cpp_reader *dummy ATTRIBUTE_UNUSED;
187 {
188   tree x, id = 0;
189   int align = -1;
190   enum cpp_ttype token;
191   enum { set, push, pop } action;
192
193   if (c_lex (&x) != CPP_OPEN_PAREN)
194     BAD ("missing '(' after '#pragma pack' - ignored");
195
196   token = c_lex (&x);
197   if (token == CPP_CLOSE_PAREN)
198     {
199       action = set;
200       align = 0;
201     }
202   else if (token == CPP_NUMBER)
203     {
204       align = TREE_INT_CST_LOW (x);
205       action = set;
206       if (c_lex (&x) != CPP_CLOSE_PAREN)
207         BAD ("malformed '#pragma pack' - ignored");
208     }
209   else if (token == CPP_NAME)
210     {
211 #define BAD_ACTION do { if (action == push) \
212           BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
213         else \
214           BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
215         } while (0)
216
217       const char *op = IDENTIFIER_POINTER (x);
218       if (!strcmp (op, "push"))
219         action = push;
220       else if (!strcmp (op, "pop"))
221         action = pop;
222       else
223         BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
224
225       token = c_lex (&x);
226       if (token != CPP_COMMA && action == push)
227         BAD_ACTION;
228
229       if (token == CPP_COMMA)
230         {
231           token = c_lex (&x);
232           if (token == CPP_NAME)
233             {
234               id = x;
235               if (action == push && c_lex (&x) != CPP_COMMA)
236                 BAD_ACTION;
237               token = c_lex (&x);
238             }
239
240           if (action == push)
241             {
242               if (token == CPP_NUMBER)
243                 {
244                   align = TREE_INT_CST_LOW (x);
245                   token = c_lex (&x);
246                 }
247               else
248                 BAD_ACTION;
249             }
250         }
251
252       if (token != CPP_CLOSE_PAREN)
253         BAD_ACTION;
254 #undef BAD_ACTION
255     }
256   else
257     BAD ("malformed '#pragma pack' - ignored");
258
259   if (c_lex (&x) != CPP_EOF)
260     warning ("junk at end of '#pragma pack'");
261
262   if (action != pop)
263     switch (align)
264       {
265       case 0:
266       case 1:
267       case 2:
268       case 4:
269       case 8:
270       case 16:
271         align *= BITS_PER_UNIT;
272         break;
273       default:
274         BAD2 ("alignment must be a small power of two, not %d", align);
275       }
276
277   switch (action)
278     {
279     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
280     case push:  push_alignment (align, id);    break;
281     case pop:   pop_alignment (id);            break;
282     }
283 }
284 #endif  /* HANDLE_PRAGMA_PACK */
285
286 #ifdef HANDLE_PRAGMA_WEAK
287 static void handle_pragma_weak PARAMS ((cpp_reader *));
288
289 /* #pragma weak name [= value] */
290 static void
291 handle_pragma_weak (dummy)
292      cpp_reader *dummy ATTRIBUTE_UNUSED;
293 {
294   tree name, value, x;
295   enum cpp_ttype t;
296
297   value = 0;
298
299   if (c_lex (&name) != CPP_NAME)
300     BAD ("malformed #pragma weak, ignored");
301   t = c_lex (&x);
302   if (t == CPP_EQ)
303     {
304       if (c_lex (&value) != CPP_NAME)
305         BAD ("malformed #pragma weak, ignored");
306       t = c_lex (&x);
307     }
308   if (t != CPP_EOF)
309     warning ("junk at end of #pragma weak");
310
311   add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
312 }
313 #endif
314
315 #if !USE_CPPLIB
316 /* Glue version of cpplib's pragma registration and dispatch system.  */
317 struct pragma_entry
318 {
319   struct pragma_entry *next;
320   const char *name;
321   size_t len;
322   int isnspace;
323   union {
324     void (*handler) PARAMS ((cpp_reader *));
325     struct pragma_entry *space;
326   } u;
327 };
328
329 void
330 cpp_register_pragma_space (pfile, space)
331      cpp_reader *pfile ATTRIBUTE_UNUSED;
332      const char *space;
333 {
334   struct pragma_entry *new;
335   const struct pragma_entry *p = pragmas;
336   size_t len = strlen (space);
337
338   while (p)
339     {
340       if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
341         return;
342       p = p->next;
343     }
344
345   new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
346   new->name = space;
347   new->len = len;
348   new->isnspace = 1;
349   new->u.space = 0;
350
351   new->next = pragmas;
352   pragmas = new;
353 }
354
355 void
356 cpp_register_pragma (pfile, space, name, handler)
357      cpp_reader *pfile ATTRIBUTE_UNUSED;
358      const char *space;
359      const char *name;
360      void (*handler) PARAMS ((cpp_reader *));
361 {
362   struct pragma_entry **x, *new;
363   size_t len;
364
365   x = &pragmas;
366   if (space)
367     {
368       struct pragma_entry *p = pragmas;
369       len = strlen (space);
370       while (p)
371         {
372           if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
373             {
374               x = &p->u.space;
375               goto found;
376             }
377           p = p->next;
378         }
379       abort ();
380     }
381
382  found:
383   new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
384   new->name = name;
385   new->len = strlen (name);
386   new->isnspace = 0;
387   new->u.handler = handler;
388
389   new->next = *x;
390   *x = new;
391 }
392
393 /* Called from process_directive() for #pragma lines.  */
394 void
395 dispatch_pragma ()
396 {
397   enum cpp_ttype t;
398   tree x;
399   const struct pragma_entry *p;
400   const char *name, *space = 0;
401   size_t len;
402
403   p = pragmas;
404
405  new_space:
406   t = c_lex (&x);
407   if (t == CPP_EOF)
408     return;
409
410   if (t != CPP_NAME)
411     {
412       warning ("malformed #pragma directive");
413       return;
414     }
415
416   name = IDENTIFIER_POINTER (x);
417   len = IDENTIFIER_LENGTH (x);
418   while (p)
419     {
420       if (strlen (p->name) == len && !memcmp (p->name, name, len))
421         {
422           if (p->isnspace)
423             {
424               space = p->name;
425               p = p->u.space;
426               goto new_space;
427             }
428           else
429             {
430               (*p->u.handler) (0);
431               return;
432             }
433         }
434       p = p->next;
435     }
436
437   /* Issue a warning message if we have been asked to do so.  Ignore
438      unknown pragmas in system headers unless an explicit
439      -Wunknown-pragmas has been given. */
440   if (warn_unknown_pragmas > in_system_header)
441     {
442       if (space)
443         warning ("ignoring #pragma %s %s", space, name);
444       else
445         warning ("ignoring #pragma %s", name);
446     }
447 }
448
449 #endif
450
451 void
452 init_pragma ()
453 {
454   cpp_reader *pfile ATTRIBUTE_UNUSED;
455 #if !USE_CPPLIB
456   pfile = 0;
457 #else
458   pfile = &parse_in;
459 #endif
460
461 #ifdef HANDLE_PRAGMA_PACK
462   cpp_register_pragma (pfile, 0, "pack", handle_pragma_pack);
463 #endif
464 #ifdef HANDLE_PRAGMA_WEAK
465   cpp_register_pragma (pfile, 0, "weak", handle_pragma_weak);
466 #endif
467 #ifdef REGISTER_TARGET_PRAGMAS
468   REGISTER_TARGET_PRAGMAS (pfile);
469 #endif
470
471 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
472   ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
473                 mark_align_stack);
474 #endif
475 }