* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / c-pragma.c
blob0d1caaff4864b3c694965620c7ad6cec9fc27cd2
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "function.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 "output.h"
34 #include "tm_p.h"
36 #define BAD(msgid) do { warning (msgid); return; } while (0)
37 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
39 #ifdef HANDLE_PRAGMA_PACK
40 static void handle_pragma_pack PARAMS ((cpp_reader *));
42 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
43 typedef struct align_stack
45 int alignment;
46 unsigned int num_pushes;
47 tree id;
48 struct align_stack * prev;
49 } align_stack;
51 static struct align_stack * alignment_stack = NULL;
53 /* If we have a "global" #pragma pack(<n>) in effect when the first
54 #pragma pack(push,<n>) is encountered, this stores the value of
55 maximum_field_alignment in effect. When the final pop_alignment()
56 happens, we restore the value to this, not to a value of 0 for
57 maximum_field_alignment. Value is in bits. */
58 static int default_alignment;
59 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
60 (default_alignment = maximum_field_alignment = (ALIGN))
62 static void push_alignment PARAMS ((int, tree));
63 static void pop_alignment PARAMS ((tree));
64 static void mark_align_stack PARAMS ((void *));
66 /* Push an alignment value onto the stack. */
67 static void
68 push_alignment (alignment, id)
69 int alignment;
70 tree id;
73 if (alignment_stack == NULL
74 || alignment_stack->alignment != alignment
75 || id != NULL_TREE)
77 align_stack * entry;
79 entry = (align_stack *) xmalloc (sizeof (* entry));
81 entry->alignment = alignment;
82 entry->num_pushes = 1;
83 entry->id = id;
84 entry->prev = alignment_stack;
86 /* The current value of maximum_field_alignment is not necessarily
87 0 since there may be a #pragma pack(<n>) in effect; remember it
88 so that we can restore it after the final #pragma pop(). */
89 if (alignment_stack == NULL)
90 default_alignment = maximum_field_alignment;
92 alignment_stack = entry;
94 maximum_field_alignment = alignment;
96 else
97 alignment_stack->num_pushes ++;
100 /* Undo a push of an alignment onto the stack. */
101 static void
102 pop_alignment (id)
103 tree id;
105 align_stack * entry;
107 if (alignment_stack == NULL)
109 warning ("\
110 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
112 return;
115 /* If we got an identifier, strip away everything above the target
116 entry so that the next step will restore the state just below it. */
117 if (id)
119 for (entry = alignment_stack; entry; entry = entry->prev)
120 if (entry->id == id)
122 entry->num_pushes = 1;
123 alignment_stack = entry;
124 break;
126 if (entry == NULL)
127 warning ("\
128 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
129 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
132 if (-- alignment_stack->num_pushes == 0)
134 entry = alignment_stack->prev;
136 if (entry == NULL)
137 maximum_field_alignment = default_alignment;
138 else
139 maximum_field_alignment = entry->alignment;
141 free (alignment_stack);
143 alignment_stack = entry;
147 static void
148 mark_align_stack (p)
149 void *p;
151 align_stack *a = *(align_stack **) p;
153 while (a)
155 ggc_mark_tree (a->id);
156 a = a->prev;
159 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
160 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
161 #define push_alignment(ID, N) \
162 BAD("#pragma pack(push[, id], <n>) is not supported on this target")
163 #define pop_alignment(ID) \
164 BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
165 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
167 /* #pragma pack ()
168 #pragma pack (N)
170 #pragma pack (push, N)
171 #pragma pack (push, ID, N)
172 #pragma pack (pop)
173 #pragma pack (pop, ID) */
174 static void
175 handle_pragma_pack (dummy)
176 cpp_reader *dummy ATTRIBUTE_UNUSED;
178 tree x, id = 0;
179 int align = -1;
180 enum cpp_ttype token;
181 enum { set, push, pop } action;
183 if (c_lex (&x) != CPP_OPEN_PAREN)
184 BAD ("missing '(' after '#pragma pack' - ignored");
186 token = c_lex (&x);
187 if (token == CPP_CLOSE_PAREN)
189 action = set;
190 align = 0;
192 else if (token == CPP_NUMBER)
194 align = TREE_INT_CST_LOW (x);
195 action = set;
196 if (c_lex (&x) != CPP_CLOSE_PAREN)
197 BAD ("malformed '#pragma pack' - ignored");
199 else if (token == CPP_NAME)
201 #define BAD_ACTION do { if (action == push) \
202 BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
203 else \
204 BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
205 } while (0)
207 const char *op = IDENTIFIER_POINTER (x);
208 if (!strcmp (op, "push"))
209 action = push;
210 else if (!strcmp (op, "pop"))
211 action = pop;
212 else
213 BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
215 token = c_lex (&x);
216 if (token != CPP_COMMA && action == push)
217 BAD_ACTION;
219 if (token == CPP_COMMA)
221 token = c_lex (&x);
222 if (token == CPP_NAME)
224 id = x;
225 if (action == push && c_lex (&x) != CPP_COMMA)
226 BAD_ACTION;
227 token = c_lex (&x);
230 if (action == push)
232 if (token == CPP_NUMBER)
234 align = TREE_INT_CST_LOW (x);
235 token = c_lex (&x);
237 else
238 BAD_ACTION;
242 if (token != CPP_CLOSE_PAREN)
243 BAD_ACTION;
244 #undef BAD_ACTION
246 else
247 BAD ("malformed '#pragma pack' - ignored");
249 if (c_lex (&x) != CPP_EOF)
250 warning ("junk at end of '#pragma pack'");
252 if (action != pop)
253 switch (align)
255 case 0:
256 case 1:
257 case 2:
258 case 4:
259 case 8:
260 case 16:
261 align *= BITS_PER_UNIT;
262 break;
263 default:
264 BAD2 ("alignment must be a small power of two, not %d", align);
267 switch (action)
269 case set: SET_GLOBAL_ALIGNMENT (align); break;
270 case push: push_alignment (align, id); break;
271 case pop: pop_alignment (id); break;
274 #endif /* HANDLE_PRAGMA_PACK */
276 #ifdef HANDLE_PRAGMA_WEAK
277 static void handle_pragma_weak PARAMS ((cpp_reader *));
279 /* #pragma weak name [= value] */
280 static void
281 handle_pragma_weak (dummy)
282 cpp_reader *dummy ATTRIBUTE_UNUSED;
284 tree name, value, x;
285 enum cpp_ttype t;
287 value = 0;
289 if (c_lex (&name) != CPP_NAME)
290 BAD ("malformed #pragma weak, ignored");
291 t = c_lex (&x);
292 if (t == CPP_EQ)
294 if (c_lex (&value) != CPP_NAME)
295 BAD ("malformed #pragma weak, ignored");
296 t = c_lex (&x);
298 if (t != CPP_EOF)
299 warning ("junk at end of #pragma weak");
301 add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
303 #endif
305 void
306 init_pragma ()
308 #ifdef HANDLE_PRAGMA_PACK
309 cpp_register_pragma (parse_in, 0, "pack", handle_pragma_pack);
310 #endif
311 #ifdef HANDLE_PRAGMA_WEAK
312 cpp_register_pragma (parse_in, 0, "weak", handle_pragma_weak);
313 #endif
314 #ifdef REGISTER_TARGET_PRAGMAS
315 REGISTER_TARGET_PRAGMAS (parse_in);
316 #endif
318 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
319 ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
320 mark_align_stack);
321 #endif