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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* This is allocated to grow and shrink as .ifdef/.endif pairs are scanned. */
25 struct obstack cond_obstack;
33 /* This is what we push and pop. */
34 struct conditional_frame
36 struct file_line if_file_line; /* the source file & line number of the "if" */
37 struct file_line else_file_line; /* the source file & line of the "else" */
38 struct conditional_frame *previous_cframe;
39 int else_seen; /* have we seen an else yet? */
40 int ignoring; /* if we are currently ignoring input. */
41 int dead_tree; /* if a conditional at a higher level is ignoring input. */
42 }; /* conditional_frame */
44 static void initialize_cframe PARAMS ((struct conditional_frame *cframe));
45 static char *get_mri_string PARAMS ((int, int *));
47 static struct conditional_frame *current_cframe = NULL;
53 register char *name; /* points to name of symbol */
54 register struct symbol *symbolP; /* Points to symbol */
55 struct conditional_frame cframe;
57 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
58 name = input_line_pointer;
60 if (!is_name_beginner (*name))
62 as_bad ("invalid identifier for \".ifdef\"");
63 obstack_1grow (&cond_obstack, 0);
64 ignore_rest_of_line ();
70 c = get_symbol_end ();
71 symbolP = symbol_find (name);
72 *input_line_pointer = c;
74 initialize_cframe (&cframe);
75 cframe.ignoring = cframe.dead_tree || !((symbolP != 0) ^ arg);
76 current_cframe = ((struct conditional_frame *)
77 obstack_copy (&cond_obstack, &cframe,
79 demand_empty_rest_of_line ();
80 } /* if a valid identifyer name */
88 struct conditional_frame cframe;
91 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
93 if (current_cframe != NULL && current_cframe->ignoring)
95 operand.X_add_number = 0;
96 while (! is_end_of_line[(unsigned char) *input_line_pointer])
101 expression (&operand);
102 if (operand.X_op != O_constant)
103 as_bad ("non-constant expression in \".if\" statement");
106 switch ((operatorT) arg)
108 case O_eq: t = operand.X_add_number == 0; break;
109 case O_ne: t = operand.X_add_number != 0; break;
110 case O_lt: t = operand.X_add_number < 0; break;
111 case O_le: t = operand.X_add_number <= 0; break;
112 case O_ge: t = operand.X_add_number >= 0; break;
113 case O_gt: t = operand.X_add_number > 0; break;
118 /* If the above error is signaled, this will dispatch
119 using an undefined result. No big deal. */
120 initialize_cframe (&cframe);
121 cframe.ignoring = cframe.dead_tree || ! t;
122 current_cframe = ((struct conditional_frame *)
123 obstack_copy (&cond_obstack, &cframe, sizeof (cframe)));
125 demand_empty_rest_of_line ();
128 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
131 get_mri_string (terminator, len)
139 s = ret = input_line_pointer;
140 if (*input_line_pointer == '\'')
143 ++input_line_pointer;
144 while (! is_end_of_line[(unsigned char) *input_line_pointer])
146 *s++ = *input_line_pointer++;
149 if (*input_line_pointer != '\'')
151 ++input_line_pointer;
158 while (*input_line_pointer != terminator
159 && ! is_end_of_line[(unsigned char) *input_line_pointer])
160 ++input_line_pointer;
161 s = input_line_pointer;
162 while (s > ret && (s[-1] == ' ' || s[-1] == '\t'))
170 /* The MRI IFC and IFNC pseudo-ops. */
179 struct conditional_frame cframe;
181 s1 = get_mri_string (',', &len1);
183 if (*input_line_pointer != ',')
184 as_bad ("bad format for ifc or ifnc");
186 ++input_line_pointer;
188 s2 = get_mri_string (';', &len2);
190 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
192 initialize_cframe (&cframe);
193 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
194 current_cframe = ((struct conditional_frame *)
195 obstack_copy (&cond_obstack, &cframe, sizeof (cframe)));
202 struct conditional_frame *hold;
204 if (current_cframe == NULL)
206 as_bad ("\".endif\" without \".if\"");
210 hold = current_cframe;
211 current_cframe = current_cframe->previous_cframe;
212 obstack_free (&cond_obstack, hold);
213 } /* if one pop too many */
222 if (current_cframe == NULL)
224 as_bad (".else without matching .if - ignored");
227 else if (current_cframe->else_seen)
229 as_bad ("duplicate \"else\" - ignored");
230 as_bad_where (current_cframe->else_file_line.file,
231 current_cframe->else_file_line.line,
232 "here is the previous \"else\"");
233 as_bad_where (current_cframe->if_file_line.file,
234 current_cframe->if_file_line.line,
235 "here is the previous \"if\"");
239 as_where (¤t_cframe->else_file_line.file,
240 ¤t_cframe->else_file_line.line);
242 if (!current_cframe->dead_tree)
244 current_cframe->ignoring = !current_cframe->ignoring;
245 } /* if not a dead tree */
247 current_cframe->else_seen = 1;
248 } /* if error else do it */
257 as_bad ("ifeqs not implemented.");
267 s = input_line_pointer;
281 return (current_cframe != NULL) && (current_cframe->ignoring);
284 /* We cannot ignore certain pseudo ops. */
287 && (!strncasecmp (s, "if", 2)
288 || !strncasecmp (s, "ifdef", 5)
289 || !strncasecmp (s, "ifndef", 6)))
292 && (!strncasecmp (s, "else", 4)
293 || !strncasecmp (s, "endif", 5)
294 || !strncasecmp (s, "endc", 4))))
297 return (current_cframe != NULL) && (current_cframe->ignoring);
298 } /* ignore_input() */
301 initialize_cframe (cframe)
302 struct conditional_frame *cframe;
304 memset (cframe, 0, sizeof (*cframe));
305 as_where (&cframe->if_file_line.file,
306 &cframe->if_file_line.line);
307 cframe->previous_cframe = current_cframe;
308 cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring;
311 } /* initialize_cframe() */