1 /* cond.c - conditional assembly pseudo-ops, and .include
2 Copyright (C) 1990-2019 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 3, 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 the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
27 /* This is allocated to grow and shrink as .ifdef/.endif pairs are
29 struct obstack cond_obstack;
37 /* We push one of these structures for each .if, and pop it at the
40 struct conditional_frame
42 /* The source file & line number of the "if". */
43 struct file_line if_file_line;
44 /* The source file & line of the "else". */
45 struct file_line else_file_line;
46 /* The previous conditional. */
47 struct conditional_frame *previous_cframe;
48 /* Have we seen an else yet? */
50 /* Whether we are currently ignoring input. */
52 /* Whether a conditional at a higher level is ignoring input.
53 Set also when a branch of an "if .. elseif .." tree has matched
54 to prevent further matches. */
56 /* Macro nesting level at which this conditional was created. */
60 static void initialize_cframe (struct conditional_frame *cframe);
61 static char *get_mri_string (int, int *);
63 static struct conditional_frame *current_cframe = NULL;
65 /* Performs the .ifdef (test_defined == 1) and
66 the .ifndef (test_defined == 0) pseudo op. */
69 s_ifdef (int test_defined)
71 /* Points to name of symbol. */
73 /* Points to symbol. */
75 struct conditional_frame cframe;
78 /* Leading whitespace is part of operand. */
80 name = input_line_pointer;
82 if (!is_name_beginner (*name) && *name != '"')
84 as_bad (_("invalid identifier for \".ifdef\""));
85 obstack_1grow (&cond_obstack, 0);
86 ignore_rest_of_line ();
90 c = get_symbol_name (& name);
91 symbolP = symbol_find (name);
92 (void) restore_line_pointer (c);
94 initialize_cframe (&cframe);
102 /* Use the same definition of 'defined' as .equiv so that a symbol
103 which has been referenced but not yet given a value/address is
104 considered to be undefined. */
107 && (S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
108 && S_GET_SEGMENT (symbolP) != reg_section;
110 cframe.ignoring = ! (test_defined ^ is_defined);
114 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
115 memcpy (current_cframe, &cframe, sizeof cframe);
117 if (LISTING_SKIP_COND ()
119 && (cframe.previous_cframe == NULL
120 || ! cframe.previous_cframe->ignoring))
123 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_and_evaluate (&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;
172 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
173 memcpy (current_cframe, & cframe, sizeof cframe);
175 if (LISTING_SKIP_COND ()
177 && (cframe.previous_cframe == NULL
178 || ! cframe.previous_cframe->ignoring))
182 mri_comment_end (stop, stopc);
184 demand_empty_rest_of_line ();
187 /* Performs the .ifb (test_blank == 1) and
188 the .ifnb (test_blank == 0) pseudo op. */
191 s_ifb (int test_blank)
193 struct conditional_frame cframe;
195 initialize_cframe (&cframe);
197 if (cframe.dead_tree)
204 is_eol = is_end_of_line[(unsigned char) *input_line_pointer];
205 cframe.ignoring = (test_blank == !is_eol);
209 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
210 memcpy (current_cframe, &cframe, sizeof cframe);
212 if (LISTING_SKIP_COND ()
214 && (cframe.previous_cframe == NULL
215 || ! cframe.previous_cframe->ignoring))
218 ignore_rest_of_line ();
221 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
224 get_mri_string (int terminator, int *len)
230 s = ret = input_line_pointer;
231 if (*input_line_pointer == '\'')
234 ++input_line_pointer;
235 while (! is_end_of_line[(unsigned char) *input_line_pointer])
237 *s++ = *input_line_pointer++;
240 if (*input_line_pointer != '\'')
242 ++input_line_pointer;
249 while (*input_line_pointer != terminator
250 && ! is_end_of_line[(unsigned char) *input_line_pointer])
251 ++input_line_pointer;
252 s = input_line_pointer;
253 while (s > ret && (s[-1] == ' ' || s[-1] == '\t'))
261 /* The MRI IFC and IFNC pseudo-ops. */
271 struct conditional_frame cframe;
274 stop = mri_comment_field (&stopc);
276 s1 = get_mri_string (',', &len1);
278 if (*input_line_pointer != ',')
279 as_bad (_("bad format for ifc or ifnc"));
281 ++input_line_pointer;
283 s2 = get_mri_string (';', &len2);
285 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
287 initialize_cframe (&cframe);
288 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
290 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
291 memcpy (current_cframe, &cframe, sizeof cframe);
293 if (LISTING_SKIP_COND ()
295 && (cframe.previous_cframe == NULL
296 || ! cframe.previous_cframe->ignoring))
300 mri_comment_end (stop, stopc);
302 demand_empty_rest_of_line ();
308 if (current_cframe == NULL)
310 as_bad (_("\".elseif\" without matching \".if\""));
312 else if (current_cframe->else_seen)
314 as_bad (_("\".elseif\" after \".else\""));
315 as_bad_where (current_cframe->else_file_line.file,
316 current_cframe->else_file_line.line,
317 _("here is the previous \".else\""));
318 as_bad_where (current_cframe->if_file_line.file,
319 current_cframe->if_file_line.line,
320 _("here is the previous \".if\""));
324 current_cframe->else_file_line.file
325 = as_where (¤t_cframe->else_file_line.line);
327 current_cframe->dead_tree |= !current_cframe->ignoring;
328 current_cframe->ignoring = current_cframe->dead_tree;
331 if (current_cframe == NULL || current_cframe->ignoring)
333 while (! is_end_of_line[(unsigned char) *input_line_pointer])
334 ++input_line_pointer;
336 if (current_cframe == NULL)
344 /* Leading whitespace is part of operand. */
347 expression_and_evaluate (&operand);
348 if (operand.X_op != O_constant)
349 as_bad (_("non-constant expression in \".elseif\" statement"));
351 switch ((operatorT) arg)
353 case O_eq: t = operand.X_add_number == 0; break;
354 case O_ne: t = operand.X_add_number != 0; break;
355 case O_lt: t = operand.X_add_number < 0; break;
356 case O_le: t = operand.X_add_number <= 0; break;
357 case O_ge: t = operand.X_add_number >= 0; break;
358 case O_gt: t = operand.X_add_number > 0; break;
364 current_cframe->ignoring = current_cframe->dead_tree || ! t;
367 if (LISTING_SKIP_COND ()
368 && (current_cframe->previous_cframe == NULL
369 || ! current_cframe->previous_cframe->ignoring))
371 if (! current_cframe->ignoring)
377 demand_empty_rest_of_line ();
381 s_endif (int arg ATTRIBUTE_UNUSED)
383 struct conditional_frame *hold;
385 if (current_cframe == NULL)
387 as_bad (_("\".endif\" without \".if\""));
391 if (LISTING_SKIP_COND ()
392 && current_cframe->ignoring
393 && (current_cframe->previous_cframe == NULL
394 || ! current_cframe->previous_cframe->ignoring))
397 hold = current_cframe;
398 current_cframe = current_cframe->previous_cframe;
399 obstack_free (&cond_obstack, hold);
400 } /* if one pop too many */
404 while (! is_end_of_line[(unsigned char) *input_line_pointer])
405 ++input_line_pointer;
408 demand_empty_rest_of_line ();
412 s_else (int arg ATTRIBUTE_UNUSED)
414 if (current_cframe == NULL)
416 as_bad (_("\".else\" without matching \".if\""));
418 else if (current_cframe->else_seen)
420 as_bad (_("duplicate \".else\""));
421 as_bad_where (current_cframe->else_file_line.file,
422 current_cframe->else_file_line.line,
423 _("here is the previous \".else\""));
424 as_bad_where (current_cframe->if_file_line.file,
425 current_cframe->if_file_line.line,
426 _("here is the previous \".if\""));
430 current_cframe->else_file_line.file
431 = as_where (¤t_cframe->else_file_line.line);
433 current_cframe->ignoring =
434 current_cframe->dead_tree | !current_cframe->ignoring;
436 if (LISTING_SKIP_COND ()
437 && (current_cframe->previous_cframe == NULL
438 || ! current_cframe->previous_cframe->ignoring))
440 if (! current_cframe->ignoring)
446 current_cframe->else_seen = 1;
451 while (! is_end_of_line[(unsigned char) *input_line_pointer])
452 ++input_line_pointer;
455 demand_empty_rest_of_line ();
464 struct conditional_frame cframe;
466 s1 = demand_copy_C_string (&len1);
469 if (*input_line_pointer != ',')
471 as_bad (_(".ifeqs syntax error"));
472 ignore_rest_of_line ();
476 ++input_line_pointer;
478 s2 = demand_copy_C_string (&len2);
480 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
482 initialize_cframe (&cframe);
483 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
485 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
486 memcpy (current_cframe, &cframe, sizeof cframe);
488 if (LISTING_SKIP_COND ()
490 && (cframe.previous_cframe == NULL
491 || ! cframe.previous_cframe->ignoring))
494 demand_empty_rest_of_line ();
502 s = input_line_pointer;
504 if (NO_PSEUDO_DOT || flag_m68k_mri)
512 return (current_cframe != NULL) && (current_cframe->ignoring);
515 /* We cannot ignore certain pseudo ops. */
518 && (!strncasecmp (s, "if", 2)
519 || !strncasecmp (s, "ifdef", 5)
520 || !strncasecmp (s, "ifndef", 6)))
523 && (!strncasecmp (s, "else", 4)
524 || !strncasecmp (s, "endif", 5)
525 || !strncasecmp (s, "endc", 4))))
528 return (current_cframe != NULL) && (current_cframe->ignoring);
532 initialize_cframe (struct conditional_frame *cframe)
534 memset (cframe, 0, sizeof (*cframe));
535 cframe->if_file_line.file
536 = as_where (&cframe->if_file_line.line);
537 cframe->previous_cframe = current_cframe;
538 cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring;
539 cframe->macro_nest = macro_nest;
542 /* Give an error if a conditional is unterminated inside a macro or
543 the assembly as a whole. If NEST is non negative, we are being
544 called because of the end of a macro expansion. If NEST is
545 negative, we are being called at the of the input files. */
548 cond_finish_check (int nest)
550 if (current_cframe != NULL && current_cframe->macro_nest >= nest)
553 as_bad (_("end of macro inside conditional"));
555 as_bad (_("end of file inside conditional"));
557 as_bad_where (current_cframe->if_file_line.file,
558 current_cframe->if_file_line.line,
559 _("here is the start of the unterminated conditional"));
560 if (current_cframe->else_seen)
561 as_bad_where (current_cframe->else_file_line.file,
562 current_cframe->else_file_line.line,
563 _("here is the \"else\" of the unterminated conditional"));
567 /* This function is called when we exit out of a macro. We assume
568 that any conditionals which began within the macro are correctly
569 nested, and just pop them off the stack. */
572 cond_exit_macro (int nest)
574 while (current_cframe != NULL && current_cframe->macro_nest >= nest)
576 struct conditional_frame *hold;
578 hold = current_cframe;
579 current_cframe = current_cframe->previous_cframe;
580 obstack_free (&cond_obstack, hold);