1 /* cond.c - conditional assembly pseudo-ops, and .include
2 Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS 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 GAS 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 GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * $FreeBSD: src/gnu/usr.bin/as/cond.c,v 1.6 1999/08/27 23:34:13 peter Exp $
22 * $DragonFly: src/gnu/usr.bin/as/Attic/cond.c,v 1.2 2003/06/17 04:25:44 dillon Exp $
28 /* This is allocated to grow and shrink as .ifdef/.endif pairs are scanned. */
29 struct obstack cond_obstack;
37 /* This is what we push and pop. */
38 struct conditional_frame
40 struct file_line if_file_line; /* the source file & line number of the "if" */
41 struct file_line else_file_line; /* the source file & line of the "else" */
42 struct conditional_frame *previous_cframe;
43 int else_seen; /* have we seen an else yet? */
44 int ignoring; /* if we are currently ignoring input. */
45 int dead_tree; /* if a conditional at a higher level is ignoring input. */
46 }; /* conditional_frame */
48 static void initialize_cframe PARAMS ((struct conditional_frame *cframe));
50 static struct conditional_frame *current_cframe = NULL;
56 register char *name; /* points to name of symbol */
57 register struct symbol *symbolP; /* Points to symbol */
58 struct conditional_frame cframe;
60 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
61 name = input_line_pointer;
63 if (!is_name_beginner (*name))
65 as_bad ("invalid identifier for \".ifdef\"");
66 obstack_1grow (&cond_obstack, 0);
72 symbolP = symbol_find (name);
74 initialize_cframe (&cframe);
75 cframe.ignoring = cframe.dead_tree || !((symbolP != 0) ^ arg);
76 current_cframe = (struct conditional_frame *) obstack_copy (&cond_obstack, &cframe, sizeof (cframe));
77 } /* if a valid identifyer name */
87 struct conditional_frame cframe;
89 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
90 expression (&operand);
93 if (operand.X_op != O_constant)
94 as_bad ("non-constant expression in \".if\" statement");
96 if (operand.X_add_symbol != NULL || operand.X_subtract_symbol != NULL) {
97 as_bad("non-constant expression in \".if\" statement");
101 /* If the above error is signaled, this will dispatch
102 using an undefined result. No big deal. */
103 initialize_cframe (&cframe);
104 cframe.ignoring = cframe.dead_tree || !((operand.X_add_number != 0) ^ arg);
105 current_cframe = (struct conditional_frame *) obstack_copy (&cond_obstack, &cframe, sizeof (cframe));
113 struct conditional_frame *hold;
115 if (current_cframe == NULL)
117 as_bad ("\".endif\" without \".if\"");
121 hold = current_cframe;
122 current_cframe = current_cframe->previous_cframe;
123 obstack_free (&cond_obstack, hold);
124 } /* if one pop too many */
133 if (current_cframe == NULL)
135 as_bad (".else without matching .if - ignored");
138 else if (current_cframe->else_seen)
140 as_bad ("duplicate \"else\" - ignored");
141 as_bad_where (current_cframe->else_file_line.file,
142 current_cframe->else_file_line.line,
143 "here is the previous \"else\"");
144 as_bad_where (current_cframe->if_file_line.file,
145 current_cframe->if_file_line.line,
146 "here is the previous \"if\"");
150 as_where (¤t_cframe->else_file_line.file,
151 ¤t_cframe->else_file_line.line);
153 if (!current_cframe->dead_tree)
155 current_cframe->ignoring = !current_cframe->ignoring;
156 } /* if not a dead tree */
158 current_cframe->else_seen = 1;
159 } /* if error else do it */
168 as_bad ("ifeqs not implemented.");
183 /* We cannot ignore certain pseudo ops. */
184 if (input_line_pointer[-1] == '.'
185 && ((input_line_pointer[0] == 'i'
186 && (!strncmp (input_line_pointer, "if", 2)
187 || !strncmp (input_line_pointer, "ifdef", 5)
188 || !strncmp (input_line_pointer, "ifndef", 6)))
189 || (input_line_pointer[0] == 'e'
190 && (!strncmp (input_line_pointer, "else", 4)
191 || !strncmp (input_line_pointer, "endif", 5)))))
196 return ((current_cframe != NULL) && (current_cframe->ignoring));
197 } /* ignore_input() */
200 initialize_cframe (cframe)
201 struct conditional_frame *cframe;
203 memset (cframe, 0, sizeof (*cframe));
204 as_where (&cframe->if_file_line.file,
205 &cframe->if_file_line.line);
206 cframe->previous_cframe = current_cframe;
207 cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring;
210 } /* initialize_cframe() */