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.
4 This file is part of GNU CC.
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)
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.
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. */
35 extern cpp_reader parse_in;
37 #define BAD(msgid) do { warning (msgid); return; } while (0)
38 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
40 #ifdef HANDLE_PRAGMA_PACK
41 static void handle_pragma_pack PARAMS ((cpp_reader *));
43 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
44 typedef struct align_stack
47 unsigned int num_pushes;
49 struct align_stack * prev;
52 static struct align_stack * alignment_stack = NULL;
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))
63 static void push_alignment PARAMS ((int, tree));
64 static void pop_alignment PARAMS ((tree));
65 static void mark_align_stack PARAMS ((void *));
67 /* Push an alignment value onto the stack. */
69 push_alignment (alignment, id)
74 if (alignment_stack == NULL
75 || alignment_stack->alignment != alignment
80 entry = (align_stack *) xmalloc (sizeof (* entry));
82 entry->alignment = alignment;
83 entry->num_pushes = 1;
85 entry->prev = alignment_stack;
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;
93 alignment_stack = entry;
95 maximum_field_alignment = alignment;
98 alignment_stack->num_pushes ++;
101 /* Undo a push of an alignment onto the stack. */
108 if (alignment_stack == NULL)
111 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
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. */
120 for (entry = alignment_stack; entry; entry = entry->prev)
123 entry->num_pushes = 1;
124 alignment_stack = entry;
129 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
130 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
133 if (-- alignment_stack->num_pushes == 0)
135 entry = alignment_stack->prev;
138 maximum_field_alignment = default_alignment;
140 maximum_field_alignment = entry->alignment;
142 free (alignment_stack);
144 alignment_stack = entry;
152 align_stack *a = *(align_stack **) p;
156 ggc_mark_tree (a->id);
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 */
171 #pragma pack (push, N)
172 #pragma pack (push, ID, N)
174 #pragma pack (pop, ID) */
176 handle_pragma_pack (dummy)
177 cpp_reader *dummy ATTRIBUTE_UNUSED;
181 enum cpp_ttype token;
182 enum { set, push, pop } action;
184 if (c_lex (&x) != CPP_OPEN_PAREN)
185 BAD ("missing '(' after '#pragma pack' - ignored");
188 if (token == CPP_CLOSE_PAREN)
193 else if (token == CPP_NUMBER)
195 align = TREE_INT_CST_LOW (x);
197 if (c_lex (&x) != CPP_CLOSE_PAREN)
198 BAD ("malformed '#pragma pack' - ignored");
200 else if (token == CPP_NAME)
202 #define BAD_ACTION do { if (action == push) \
203 BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
205 BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
208 const char *op = IDENTIFIER_POINTER (x);
209 if (!strcmp (op, "push"))
211 else if (!strcmp (op, "pop"))
214 BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
217 if (token != CPP_COMMA && action == push)
220 if (token == CPP_COMMA)
223 if (token == CPP_NAME)
226 if (action == push && c_lex (&x) != CPP_COMMA)
233 if (token == CPP_NUMBER)
235 align = TREE_INT_CST_LOW (x);
243 if (token != CPP_CLOSE_PAREN)
248 BAD ("malformed '#pragma pack' - ignored");
250 if (c_lex (&x) != CPP_EOF)
251 warning ("junk at end of '#pragma pack'");
262 align *= BITS_PER_UNIT;
265 BAD2 ("alignment must be a small power of two, not %d", align);
270 case set: SET_GLOBAL_ALIGNMENT (align); break;
271 case push: push_alignment (align, id); break;
272 case pop: pop_alignment (id); break;
275 #endif /* HANDLE_PRAGMA_PACK */
277 #ifdef HANDLE_PRAGMA_WEAK
278 static void handle_pragma_weak PARAMS ((cpp_reader *));
280 /* #pragma weak name [= value] */
282 handle_pragma_weak (dummy)
283 cpp_reader *dummy ATTRIBUTE_UNUSED;
290 if (c_lex (&name) != CPP_NAME)
291 BAD ("malformed #pragma weak, ignored");
295 if (c_lex (&value) != CPP_NAME)
296 BAD ("malformed #pragma weak, ignored");
300 warning ("junk at end of #pragma weak");
302 add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
309 cpp_reader *pfile ATTRIBUTE_UNUSED;
312 #ifdef HANDLE_PRAGMA_PACK
313 cpp_register_pragma (pfile, 0, "pack", handle_pragma_pack);
315 #ifdef HANDLE_PRAGMA_WEAK
316 cpp_register_pragma (pfile, 0, "weak", handle_pragma_weak);
318 #ifdef REGISTER_TARGET_PRAGMAS
319 REGISTER_TARGET_PRAGMAS (pfile);
322 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
323 ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),