Update copyright year in gdbarch.sh doc/gdb.texinfo and doc/refcard.tex
[binutils-gdb.git] / gas / cond.c
blob5996d2a59418e4f0424e77d54a0cb97517fc8981
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)
9 any later version.
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
19 02110-1301, USA. */
21 #include "as.h"
22 #include "sb.h"
23 #include "macro.h"
25 #include "obstack.h"
27 /* This is allocated to grow and shrink as .ifdef/.endif pairs are
28 scanned. */
29 struct obstack cond_obstack;
31 struct file_line
33 const char *file;
34 unsigned int line;
37 /* We push one of these structures for each .if, and pop it at the
38 .endif. */
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? */
49 int else_seen;
50 /* Whether we are currently ignoring input. */
51 int ignoring;
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. */
55 int dead_tree;
56 /* Macro nesting level at which this conditional was created. */
57 int macro_nest;
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. */
68 void
69 s_ifdef (int test_defined)
71 /* Points to name of symbol. */
72 char *name;
73 /* Points to symbol. */
74 symbolS *symbolP;
75 struct conditional_frame cframe;
76 char c;
78 /* Leading whitespace is part of operand. */
79 SKIP_WHITESPACE ();
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 ();
87 return;
90 c = get_symbol_name (& name);
91 symbolP = symbol_find (name);
92 (void) restore_line_pointer (c);
94 initialize_cframe (&cframe);
96 if (cframe.dead_tree)
97 cframe.ignoring = 1;
98 else
100 int is_defined;
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. */
105 is_defined =
106 symbolP != NULL
107 && (S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
108 && S_GET_SEGMENT (symbolP) != reg_section;
110 cframe.ignoring = ! (test_defined ^ is_defined);
113 current_cframe =
114 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
115 memcpy (current_cframe, &cframe, sizeof cframe);
117 if (LISTING_SKIP_COND ()
118 && cframe.ignoring
119 && (cframe.previous_cframe == NULL
120 || ! cframe.previous_cframe->ignoring))
121 listing_list (2);
123 demand_empty_rest_of_line ();
126 void
127 s_if (int arg)
129 expressionS operand;
130 struct conditional_frame cframe;
131 int t;
132 char *stop = NULL;
133 char stopc = 0;
135 if (flag_mri)
136 stop = mri_comment_field (&stopc);
138 /* Leading whitespace is part of operand. */
139 SKIP_WHITESPACE ();
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;
147 else
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;
162 default:
163 abort ();
164 return;
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 =
172 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
173 memcpy (current_cframe, & cframe, sizeof cframe);
175 if (LISTING_SKIP_COND ()
176 && cframe.ignoring
177 && (cframe.previous_cframe == NULL
178 || ! cframe.previous_cframe->ignoring))
179 listing_list (2);
181 if (flag_mri)
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. */
190 void
191 s_ifb (int test_blank)
193 struct conditional_frame cframe;
195 initialize_cframe (&cframe);
197 if (cframe.dead_tree)
198 cframe.ignoring = 1;
199 else
201 int is_eol;
203 SKIP_WHITESPACE ();
204 is_eol = is_end_of_line[(unsigned char) *input_line_pointer];
205 cframe.ignoring = (test_blank == !is_eol);
208 current_cframe =
209 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
210 memcpy (current_cframe, &cframe, sizeof cframe);
212 if (LISTING_SKIP_COND ()
213 && cframe.ignoring
214 && (cframe.previous_cframe == NULL
215 || ! cframe.previous_cframe->ignoring))
216 listing_list (2);
218 ignore_rest_of_line ();
221 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
223 static char *
224 get_mri_string (int terminator, int *len)
226 char *ret;
227 char *s;
229 SKIP_WHITESPACE ();
230 s = ret = input_line_pointer;
231 if (*input_line_pointer == '\'')
233 ++s;
234 ++input_line_pointer;
235 while (! is_end_of_line[(unsigned char) *input_line_pointer])
237 *s++ = *input_line_pointer++;
238 if (s[-1] == '\'')
240 if (*input_line_pointer != '\'')
241 break;
242 ++input_line_pointer;
245 SKIP_WHITESPACE ();
247 else
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'))
254 --s;
257 *len = s - ret;
258 return ret;
261 /* The MRI IFC and IFNC pseudo-ops. */
263 void
264 s_ifc (int arg)
266 char *stop = NULL;
267 char stopc = 0;
268 char *s1, *s2;
269 int len1, len2;
270 int res;
271 struct conditional_frame cframe;
273 if (flag_mri)
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"));
280 else
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);
289 current_cframe =
290 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
291 memcpy (current_cframe, &cframe, sizeof cframe);
293 if (LISTING_SKIP_COND ()
294 && cframe.ignoring
295 && (cframe.previous_cframe == NULL
296 || ! cframe.previous_cframe->ignoring))
297 listing_list (2);
299 if (flag_mri)
300 mri_comment_end (stop, stopc);
302 demand_empty_rest_of_line ();
305 void
306 s_elseif (int arg)
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\""));
322 else
324 current_cframe->else_file_line.file
325 = as_where (&current_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)
337 return;
339 else
341 expressionS operand;
342 int t;
344 /* Leading whitespace is part of operand. */
345 SKIP_WHITESPACE ();
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;
359 default:
360 abort ();
361 return;
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)
372 listing_list (1);
373 else
374 listing_list (2);
377 demand_empty_rest_of_line ();
380 void
381 s_endif (int arg ATTRIBUTE_UNUSED)
383 struct conditional_frame *hold;
385 if (current_cframe == NULL)
387 as_bad (_("\".endif\" without \".if\""));
389 else
391 if (LISTING_SKIP_COND ()
392 && current_cframe->ignoring
393 && (current_cframe->previous_cframe == NULL
394 || ! current_cframe->previous_cframe->ignoring))
395 listing_list (1);
397 hold = current_cframe;
398 current_cframe = current_cframe->previous_cframe;
399 obstack_free (&cond_obstack, hold);
400 } /* if one pop too many */
402 if (flag_mri)
404 while (! is_end_of_line[(unsigned char) *input_line_pointer])
405 ++input_line_pointer;
408 demand_empty_rest_of_line ();
411 void
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\""));
428 else
430 current_cframe->else_file_line.file
431 = as_where (&current_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)
441 listing_list (1);
442 else
443 listing_list (2);
446 current_cframe->else_seen = 1;
449 if (flag_mri)
451 while (! is_end_of_line[(unsigned char) *input_line_pointer])
452 ++input_line_pointer;
455 demand_empty_rest_of_line ();
458 void
459 s_ifeqs (int arg)
461 char *s1, *s2;
462 int len1, len2;
463 int res;
464 struct conditional_frame cframe;
466 s1 = demand_copy_C_string (&len1);
468 SKIP_WHITESPACE ();
469 if (*input_line_pointer != ',')
471 as_bad (_(".ifeqs syntax error"));
472 ignore_rest_of_line ();
473 return;
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);
484 current_cframe =
485 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
486 memcpy (current_cframe, &cframe, sizeof cframe);
488 if (LISTING_SKIP_COND ()
489 && cframe.ignoring
490 && (cframe.previous_cframe == NULL
491 || ! cframe.previous_cframe->ignoring))
492 listing_list (2);
494 demand_empty_rest_of_line ();
498 ignore_input (void)
500 char *s;
502 s = input_line_pointer;
504 if (NO_PSEUDO_DOT || flag_m68k_mri)
506 if (s[-1] != '.')
507 --s;
509 else
511 if (s[-1] != '.')
512 return (current_cframe != NULL) && (current_cframe->ignoring);
515 /* We cannot ignore certain pseudo ops. */
516 if (((s[0] == 'i'
517 || s[0] == 'I')
518 && (!strncasecmp (s, "if", 2)
519 || !strncasecmp (s, "ifdef", 5)
520 || !strncasecmp (s, "ifndef", 6)))
521 || ((s[0] == 'e'
522 || s[0] == 'E')
523 && (!strncasecmp (s, "else", 4)
524 || !strncasecmp (s, "endif", 5)
525 || !strncasecmp (s, "endc", 4))))
526 return 0;
528 return (current_cframe != NULL) && (current_cframe->ignoring);
531 static void
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. */
547 void
548 cond_finish_check (int nest)
550 if (current_cframe != NULL && current_cframe->macro_nest >= nest)
552 if (nest >= 0)
553 as_bad (_("end of macro inside conditional"));
554 else
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. */
571 void
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);