OSDN Git Service

2bb0bc57e2bdc2ccfff6221dcce461c58a5bf29a
[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 extern cpp_reader parse_in;
36
37 #define BAD(msgid) do { warning (msgid); return; } while (0)
38 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
39
40 #ifdef HANDLE_PRAGMA_PACK
41 static void handle_pragma_pack PARAMS ((cpp_reader *));
42
43 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
44 typedef struct align_stack
45 {
46   int                  alignment;
47   unsigned int         num_pushes;
48   tree                 id;
49   struct align_stack * prev;
50 } align_stack;
51
52 static struct align_stack * alignment_stack = NULL;
53
54 /* If we have a "global" #pragma pack(<n>) in effect when the first
55    #pragma pack(push,<n>) is encountered, this stores the value of 
56    maximum_field_alignment in effect.  When the final pop_alignment() 
57    happens, we restore the value to this, not to a value of 0 for
58    maximum_field_alignment.  Value is in bits. */
59 static int  default_alignment;
60 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
61 (default_alignment = maximum_field_alignment = (ALIGN))
62
63 static void push_alignment PARAMS ((int, tree));
64 static void pop_alignment  PARAMS ((tree));
65 static void mark_align_stack PARAMS ((void *));
66
67 /* Push an alignment value onto the stack.  */
68 static void
69 push_alignment (alignment, id)
70      int alignment;
71      tree id;
72 {
73   
74   if (alignment_stack == NULL
75       || alignment_stack->alignment != alignment
76       || id != NULL_TREE)
77     {
78       align_stack * entry;
79
80       entry = (align_stack *) xmalloc (sizeof (* entry));
81
82       entry->alignment  = alignment;
83       entry->num_pushes = 1;
84       entry->id         = id;
85       entry->prev       = alignment_stack;
86       
87       /* The current value of maximum_field_alignment is not necessarily 
88          0 since there may be a #pragma pack(<n>) in effect; remember it 
89          so that we can restore it after the final #pragma pop(). */
90       if (alignment_stack == NULL)
91         default_alignment = maximum_field_alignment;
92       
93       alignment_stack = entry;
94
95       maximum_field_alignment = alignment;
96     }
97   else
98     alignment_stack->num_pushes ++;
99 }
100
101 /* Undo a push of an alignment onto the stack.  */
102 static void
103 pop_alignment (id)
104      tree id;
105 {
106   align_stack * entry;
107       
108   if (alignment_stack == NULL)
109     {
110       warning ("\
111 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
112                );
113       return;
114     }
115
116   /* If we got an identifier, strip away everything above the target
117      entry so that the next step will restore the state just below it.  */
118   if (id)
119     {
120       for (entry = alignment_stack; entry; entry = entry->prev)
121         if (entry->id == id)
122           {
123             entry->num_pushes = 1;
124             alignment_stack = entry;
125             break;
126           }
127       if (entry == NULL)
128         warning ("\
129 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
130                  , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
131     }
132
133   if (-- alignment_stack->num_pushes == 0)
134     {
135       entry = alignment_stack->prev;
136
137       if (entry == NULL)
138         maximum_field_alignment = default_alignment;
139       else
140         maximum_field_alignment = entry->alignment;
141
142       free (alignment_stack);
143
144       alignment_stack = entry;
145     }
146 }
147
148 static void
149 mark_align_stack (p)
150     void *p;
151 {
152   align_stack *a = *(align_stack **) p;
153
154   while (a)
155     {
156       ggc_mark_tree (a->id);
157       a = a->prev;
158     }
159 }
160 #else  /* not HANDLE_PRAGMA_PACK_PUSH_POP */
161 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
162 #define push_alignment(ID, N) \
163     BAD("#pragma pack(push[, id], <n>) is not supported on this target")
164 #define pop_alignment(ID) \
165     BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
166 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
167
168 /* #pragma pack ()
169    #pragma pack (N)
170    
171    #pragma pack (push, N)
172    #pragma pack (push, ID, N)
173    #pragma pack (pop)
174    #pragma pack (pop, ID) */
175 static void
176 handle_pragma_pack (dummy)
177      cpp_reader *dummy ATTRIBUTE_UNUSED;
178 {
179   tree x, id = 0;
180   int align = -1;
181   enum cpp_ttype token;
182   enum { set, push, pop } action;
183
184   if (c_lex (&x) != CPP_OPEN_PAREN)
185     BAD ("missing '(' after '#pragma pack' - ignored");
186
187   token = c_lex (&x);
188   if (token == CPP_CLOSE_PAREN)
189     {
190       action = set;
191       align = 0;
192     }
193   else if (token == CPP_NUMBER)
194     {
195       align = TREE_INT_CST_LOW (x);
196       action = set;
197       if (c_lex (&x) != CPP_CLOSE_PAREN)
198         BAD ("malformed '#pragma pack' - ignored");
199     }
200   else if (token == CPP_NAME)
201     {
202 #define BAD_ACTION do { if (action == push) \
203           BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
204         else \
205           BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
206         } while (0)
207
208       const char *op = IDENTIFIER_POINTER (x);
209       if (!strcmp (op, "push"))
210         action = push;
211       else if (!strcmp (op, "pop"))
212         action = pop;
213       else
214         BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
215
216       token = c_lex (&x);
217       if (token != CPP_COMMA && action == push)
218         BAD_ACTION;
219
220       if (token == CPP_COMMA)
221         {
222           token = c_lex (&x);
223           if (token == CPP_NAME)
224             {
225               id = x;
226               if (action == push && c_lex (&x) != CPP_COMMA)
227                 BAD_ACTION;
228               token = c_lex (&x);
229             }
230
231           if (action == push)
232             {
233               if (token == CPP_NUMBER)
234                 {
235                   align = TREE_INT_CST_LOW (x);
236                   token = c_lex (&x);
237                 }
238               else
239                 BAD_ACTION;
240             }
241         }
242
243       if (token != CPP_CLOSE_PAREN)
244         BAD_ACTION;
245 #undef BAD_ACTION
246     }
247   else
248     BAD ("malformed '#pragma pack' - ignored");
249
250   if (c_lex (&x) != CPP_EOF)
251     warning ("junk at end of '#pragma pack'");
252
253   if (action != pop)
254     switch (align)
255       {
256       case 0:
257       case 1:
258       case 2:
259       case 4:
260       case 8:
261       case 16:
262         align *= BITS_PER_UNIT;
263         break;
264       default:
265         BAD2 ("alignment must be a small power of two, not %d", align);
266       }
267
268   switch (action)
269     {
270     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
271     case push:  push_alignment (align, id);    break;
272     case pop:   pop_alignment (id);            break;
273     }
274 }
275 #endif  /* HANDLE_PRAGMA_PACK */
276
277 #ifdef HANDLE_PRAGMA_WEAK
278 static void handle_pragma_weak PARAMS ((cpp_reader *));
279
280 /* #pragma weak name [= value] */
281 static void
282 handle_pragma_weak (dummy)
283      cpp_reader *dummy ATTRIBUTE_UNUSED;
284 {
285   tree name, value, x;
286   enum cpp_ttype t;
287
288   value = 0;
289
290   if (c_lex (&name) != CPP_NAME)
291     BAD ("malformed #pragma weak, ignored");
292   t = c_lex (&x);
293   if (t == CPP_EQ)
294     {
295       if (c_lex (&value) != CPP_NAME)
296         BAD ("malformed #pragma weak, ignored");
297       t = c_lex (&x);
298     }
299   if (t != CPP_EOF)
300     warning ("junk at end of #pragma weak");
301
302   add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
303 }
304 #endif
305
306 void
307 init_pragma ()
308 {
309   cpp_reader *pfile ATTRIBUTE_UNUSED;
310   pfile = &parse_in;
311
312 #ifdef HANDLE_PRAGMA_PACK
313   cpp_register_pragma (pfile, 0, "pack", handle_pragma_pack);
314 #endif
315 #ifdef HANDLE_PRAGMA_WEAK
316   cpp_register_pragma (pfile, 0, "weak", handle_pragma_weak);
317 #endif
318 #ifdef REGISTER_TARGET_PRAGMAS
319   REGISTER_TARGET_PRAGMAS (pfile);
320 #endif
321
322 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
323   ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
324                 mark_align_stack);
325 #endif
326 }