1 /* cond.c - conditional assembly pseudo-ops, and .include
2 Copyright 1990, 1991, 1992, 1993, 1995, 1997, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 /* This is allocated to grow and shrink as .ifdef/.endif pairs are
29 struct obstack cond_obstack;
36 /* We push one of these structures for each .if, and pop it at the
39 struct conditional_frame {
40 /* The source file & line number of the "if". */
41 struct file_line if_file_line;
42 /* The source file & line of the "else". */
43 struct file_line else_file_line;
44 /* The previous conditional. */
45 struct conditional_frame *previous_cframe;
46 /* Have we seen an else yet? */
48 /* Whether we are currently ignoring input. */
50 /* Whether a conditional at a higher level is ignoring input.
51 Set also when a branch of an "if .. elseif .." tree has matched
52 to prevent further matches. */
54 /* Macro nesting level at which this conditional was created. */
58 static void initialize_cframe PARAMS ((struct conditional_frame *cframe));
59 static char *get_mri_string PARAMS ((int, int *));
61 static struct conditional_frame *current_cframe = NULL;
63 /* Performs the .ifdef (test_defined == 1) and
64 the .ifndef (test_defined == 0) pseudo op. */
67 s_ifdef (test_defined)
70 /* Points to name of symbol. */
72 /* Points to symbol. */
74 struct conditional_frame cframe;
77 /* Leading whitespace is part of operand. */
79 name = input_line_pointer;
81 if (!is_name_beginner (*name))
83 as_bad (_("invalid identifier for \".ifdef\""));
84 obstack_1grow (&cond_obstack, 0);
85 ignore_rest_of_line ();
89 c = get_symbol_end ();
90 symbolP = symbol_find (name);
91 *input_line_pointer = c;
93 initialize_cframe (&cframe);
101 /* Use the same definition of 'defined' as .equiv so that a symbol
102 which has been referenced but not yet given a value/address is
103 considered to be undefined. */
106 && S_IS_DEFINED (symbolP)
107 && S_GET_SEGMENT (symbolP) != reg_section;
109 cframe.ignoring = ! (test_defined ^ is_defined);
112 current_cframe = ((struct conditional_frame *)
113 obstack_copy (&cond_obstack, &cframe,
116 if (LISTING_SKIP_COND ()
118 && (cframe.previous_cframe == NULL
119 || ! cframe.previous_cframe->ignoring))
122 demand_empty_rest_of_line ();
130 struct conditional_frame cframe;
136 stop = mri_comment_field (&stopc);
138 /* Leading whitespace is part of operand. */
141 if (current_cframe != NULL && current_cframe->ignoring)
143 operand.X_add_number = 0;
144 while (! is_end_of_line[(unsigned char) *input_line_pointer])
145 ++input_line_pointer;
149 expression (&operand);
150 if (operand.X_op != O_constant)
151 as_bad (_("non-constant expression in \".if\" statement"));
154 switch ((operatorT) arg)
156 case O_eq: t = operand.X_add_number == 0; break;
157 case O_ne: t = operand.X_add_number != 0; break;
158 case O_lt: t = operand.X_add_number < 0; break;
159 case O_le: t = operand.X_add_number <= 0; break;
160 case O_ge: t = operand.X_add_number >= 0; break;
161 case O_gt: t = operand.X_add_number > 0; break;
167 /* If the above error is signaled, this will dispatch
168 using an undefined result. No big deal. */
169 initialize_cframe (&cframe);
170 cframe.ignoring = cframe.dead_tree || ! t;
171 current_cframe = ((struct conditional_frame *)
172 obstack_copy (&cond_obstack, &cframe, sizeof (cframe)));
174 if (LISTING_SKIP_COND ()
176 && (cframe.previous_cframe == NULL
177 || ! cframe.previous_cframe->ignoring))
181 mri_comment_end (stop, stopc);
183 demand_empty_rest_of_line ();
186 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
189 get_mri_string (terminator, len)
197 s = ret = input_line_pointer;
198 if (*input_line_pointer == '\'')
201 ++input_line_pointer;
202 while (! is_end_of_line[(unsigned char) *input_line_pointer])
204 *s++ = *input_line_pointer++;
207 if (*input_line_pointer != '\'')
209 ++input_line_pointer;
216 while (*input_line_pointer != terminator
217 && ! is_end_of_line[(unsigned char) *input_line_pointer])
218 ++input_line_pointer;
219 s = input_line_pointer;
220 while (s > ret && (s[-1] == ' ' || s[-1] == '\t'))
228 /* The MRI IFC and IFNC pseudo-ops. */
239 struct conditional_frame cframe;
242 stop = mri_comment_field (&stopc);
244 s1 = get_mri_string (',', &len1);
246 if (*input_line_pointer != ',')
247 as_bad (_("bad format for ifc or ifnc"));
249 ++input_line_pointer;
251 s2 = get_mri_string (';', &len2);
253 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
255 initialize_cframe (&cframe);
256 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
257 current_cframe = ((struct conditional_frame *)
258 obstack_copy (&cond_obstack, &cframe, sizeof (cframe)));
260 if (LISTING_SKIP_COND ()
262 && (cframe.previous_cframe == NULL
263 || ! cframe.previous_cframe->ignoring))
267 mri_comment_end (stop, stopc);
269 demand_empty_rest_of_line ();
276 if (current_cframe == NULL)
278 as_bad (_("\".elseif\" without matching \".if\""));
280 else if (current_cframe->else_seen)
282 as_bad (_("\".elseif\" after \".else\""));
283 as_bad_where (current_cframe->else_file_line.file,
284 current_cframe->else_file_line.line,
285 _("here is the previous \"else\""));
286 as_bad_where (current_cframe->if_file_line.file,
287 current_cframe->if_file_line.line,
288 _("here is the previous \"if\""));
292 as_where (¤t_cframe->else_file_line.file,
293 ¤t_cframe->else_file_line.line);
295 current_cframe->dead_tree |= !current_cframe->ignoring;
296 current_cframe->ignoring = current_cframe->dead_tree;
299 if (current_cframe == NULL || current_cframe->ignoring)
301 while (! is_end_of_line[(unsigned char) *input_line_pointer])
302 ++input_line_pointer;
304 if (current_cframe == NULL)
312 /* Leading whitespace is part of operand. */
315 expression (&operand);
316 if (operand.X_op != O_constant)
317 as_bad (_("non-constant expression in \".elseif\" statement"));
319 switch ((operatorT) arg)
321 case O_eq: t = operand.X_add_number == 0; break;
322 case O_ne: t = operand.X_add_number != 0; break;
323 case O_lt: t = operand.X_add_number < 0; break;
324 case O_le: t = operand.X_add_number <= 0; break;
325 case O_ge: t = operand.X_add_number >= 0; break;
326 case O_gt: t = operand.X_add_number > 0; break;
332 current_cframe->ignoring = current_cframe->dead_tree || ! t;
335 if (LISTING_SKIP_COND ()
336 && (current_cframe->previous_cframe == NULL
337 || ! current_cframe->previous_cframe->ignoring))
339 if (! current_cframe->ignoring)
345 demand_empty_rest_of_line ();
350 int arg ATTRIBUTE_UNUSED;
352 struct conditional_frame *hold;
354 if (current_cframe == NULL)
356 as_bad (_("\".endif\" without \".if\""));
360 if (LISTING_SKIP_COND ()
361 && current_cframe->ignoring
362 && (current_cframe->previous_cframe == NULL
363 || ! current_cframe->previous_cframe->ignoring))
366 hold = current_cframe;
367 current_cframe = current_cframe->previous_cframe;
368 obstack_free (&cond_obstack, hold);
369 } /* if one pop too many */
373 while (! is_end_of_line[(unsigned char) *input_line_pointer])
374 ++input_line_pointer;
377 demand_empty_rest_of_line ();
382 int arg ATTRIBUTE_UNUSED;
384 if (current_cframe == NULL)
386 as_bad (_("\".else\" without matching \".if\""));
388 else if (current_cframe->else_seen)
390 as_bad (_("duplicate \"else\""));
391 as_bad_where (current_cframe->else_file_line.file,
392 current_cframe->else_file_line.line,
393 _("here is the previous \"else\""));
394 as_bad_where (current_cframe->if_file_line.file,
395 current_cframe->if_file_line.line,
396 _("here is the previous \"if\""));
400 as_where (¤t_cframe->else_file_line.file,
401 ¤t_cframe->else_file_line.line);
403 current_cframe->ignoring =
404 current_cframe->dead_tree | !current_cframe->ignoring;
406 if (LISTING_SKIP_COND ()
407 && (current_cframe->previous_cframe == NULL
408 || ! current_cframe->previous_cframe->ignoring))
410 if (! current_cframe->ignoring)
416 current_cframe->else_seen = 1;
421 while (! is_end_of_line[(unsigned char) *input_line_pointer])
422 ++input_line_pointer;
425 demand_empty_rest_of_line ();
435 struct conditional_frame cframe;
437 s1 = demand_copy_C_string (&len1);
440 if (*input_line_pointer != ',')
442 as_bad (_(".ifeqs syntax error"));
443 ignore_rest_of_line ();
447 ++input_line_pointer;
449 s2 = demand_copy_C_string (&len2);
451 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
453 initialize_cframe (&cframe);
454 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
455 current_cframe = ((struct conditional_frame *)
456 obstack_copy (&cond_obstack, &cframe, sizeof (cframe)));
458 if (LISTING_SKIP_COND ()
460 && (cframe.previous_cframe == NULL
461 || ! cframe.previous_cframe->ignoring))
464 demand_empty_rest_of_line ();
472 s = input_line_pointer;
474 if (NO_PSEUDO_DOT || flag_m68k_mri)
482 return (current_cframe != NULL) && (current_cframe->ignoring);
485 /* We cannot ignore certain pseudo ops. */
488 && (!strncasecmp (s, "if", 2)
489 || !strncasecmp (s, "ifdef", 5)
490 || !strncasecmp (s, "ifndef", 6)))
493 && (!strncasecmp (s, "else", 4)
494 || !strncasecmp (s, "endif", 5)
495 || !strncasecmp (s, "endc", 4))))
498 return (current_cframe != NULL) && (current_cframe->ignoring);
502 initialize_cframe (cframe)
503 struct conditional_frame *cframe;
505 memset (cframe, 0, sizeof (*cframe));
506 as_where (&cframe->if_file_line.file,
507 &cframe->if_file_line.line);
508 cframe->previous_cframe = current_cframe;
509 cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring;
510 cframe->macro_nest = macro_nest;
513 /* Give an error if a conditional is unterminated inside a macro or
514 the assembly as a whole. If NEST is non negative, we are being
515 called because of the end of a macro expansion. If NEST is
516 negative, we are being called at the of the input files. */
519 cond_finish_check (nest)
522 if (current_cframe != NULL && current_cframe->macro_nest >= nest)
525 as_bad (_("end of macro inside conditional"));
527 as_bad (_("end of file inside conditional"));
528 as_bad_where (current_cframe->if_file_line.file,
529 current_cframe->if_file_line.line,
530 _("here is the start of the unterminated conditional"));
531 if (current_cframe->else_seen)
532 as_bad_where (current_cframe->else_file_line.file,
533 current_cframe->else_file_line.line,
534 _("here is the \"else\" of the unterminated conditional"));
538 /* This function is called when we exit out of a macro. We assume
539 that any conditionals which began within the macro are correctly
540 nested, and just pop them off the stack. */
543 cond_exit_macro (nest)
546 while (current_cframe != NULL && current_cframe->macro_nest >= nest)
548 struct conditional_frame *hold;
550 hold = current_cframe;
551 current_cframe = current_cframe->previous_cframe;
552 obstack_free (&cond_obstack, hold);