1 /* cond.c - conditional assembly pseudo-ops, and .include
2 Copyright 1990, 1991, 1992, 1993, 1995, 1997, 1998, 2000, 2001, 2002,
3 2003 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, 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
;
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 (struct conditional_frame
*cframe
);
59 static char *get_mri_string (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 (int test_defined
)
69 /* Points to name of symbol. */
71 /* Points to symbol. */
73 struct conditional_frame cframe
;
76 /* Leading whitespace is part of operand. */
78 name
= input_line_pointer
;
80 if (!is_name_beginner (*name
))
82 as_bad (_("invalid identifier for \".ifdef\""));
83 obstack_1grow (&cond_obstack
, 0);
84 ignore_rest_of_line ();
88 c
= get_symbol_end ();
89 symbolP
= symbol_find (name
);
90 *input_line_pointer
= c
;
92 initialize_cframe (&cframe
);
100 /* Use the same definition of 'defined' as .equiv so that a symbol
101 which has been referenced but not yet given a value/address is
102 considered to be undefined. */
105 && (S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
106 && S_GET_SEGMENT (symbolP
) != reg_section
;
108 cframe
.ignoring
= ! (test_defined
^ is_defined
);
111 current_cframe
= ((struct conditional_frame
*)
112 obstack_copy (&cond_obstack
, &cframe
,
115 if (LISTING_SKIP_COND ()
117 && (cframe
.previous_cframe
== NULL
118 || ! cframe
.previous_cframe
->ignoring
))
121 demand_empty_rest_of_line ();
128 struct conditional_frame cframe
;
134 stop
= mri_comment_field (&stopc
);
136 /* Leading whitespace is part of operand. */
139 if (current_cframe
!= NULL
&& current_cframe
->ignoring
)
141 operand
.X_add_number
= 0;
142 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
143 ++input_line_pointer
;
147 expression_and_evaluate (&operand
);
148 if (operand
.X_op
!= O_constant
)
149 as_bad (_("non-constant expression in \".if\" statement"));
152 switch ((operatorT
) arg
)
154 case O_eq
: t
= operand
.X_add_number
== 0; break;
155 case O_ne
: t
= operand
.X_add_number
!= 0; break;
156 case O_lt
: t
= operand
.X_add_number
< 0; break;
157 case O_le
: t
= operand
.X_add_number
<= 0; break;
158 case O_ge
: t
= operand
.X_add_number
>= 0; break;
159 case O_gt
: t
= operand
.X_add_number
> 0; break;
165 /* If the above error is signaled, this will dispatch
166 using an undefined result. No big deal. */
167 initialize_cframe (&cframe
);
168 cframe
.ignoring
= cframe
.dead_tree
|| ! t
;
169 current_cframe
= ((struct conditional_frame
*)
170 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
172 if (LISTING_SKIP_COND ()
174 && (cframe
.previous_cframe
== NULL
175 || ! cframe
.previous_cframe
->ignoring
))
179 mri_comment_end (stop
, stopc
);
181 demand_empty_rest_of_line ();
184 /* Performs the .ifb (test_blank == 1) and
185 the .ifnb (test_blank == 0) pseudo op. */
188 s_ifb (int test_blank
)
190 struct conditional_frame cframe
;
192 initialize_cframe (&cframe
);
194 if (cframe
.dead_tree
)
201 is_eol
= is_end_of_line
[(unsigned char) *input_line_pointer
];
202 cframe
.ignoring
= (test_blank
== !is_eol
);
205 current_cframe
= ((struct conditional_frame
*)
206 obstack_copy (&cond_obstack
, &cframe
,
209 if (LISTING_SKIP_COND ()
211 && (cframe
.previous_cframe
== NULL
212 || ! cframe
.previous_cframe
->ignoring
))
215 ignore_rest_of_line ();
218 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
221 get_mri_string (int terminator
, int *len
)
227 s
= ret
= input_line_pointer
;
228 if (*input_line_pointer
== '\'')
231 ++input_line_pointer
;
232 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
234 *s
++ = *input_line_pointer
++;
237 if (*input_line_pointer
!= '\'')
239 ++input_line_pointer
;
246 while (*input_line_pointer
!= terminator
247 && ! is_end_of_line
[(unsigned char) *input_line_pointer
])
248 ++input_line_pointer
;
249 s
= input_line_pointer
;
250 while (s
> ret
&& (s
[-1] == ' ' || s
[-1] == '\t'))
258 /* The MRI IFC and IFNC pseudo-ops. */
268 struct conditional_frame cframe
;
271 stop
= mri_comment_field (&stopc
);
273 s1
= get_mri_string (',', &len1
);
275 if (*input_line_pointer
!= ',')
276 as_bad (_("bad format for ifc or ifnc"));
278 ++input_line_pointer
;
280 s2
= get_mri_string (';', &len2
);
282 res
= len1
== len2
&& strncmp (s1
, s2
, len1
) == 0;
284 initialize_cframe (&cframe
);
285 cframe
.ignoring
= cframe
.dead_tree
|| ! (res
^ arg
);
286 current_cframe
= ((struct conditional_frame
*)
287 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
289 if (LISTING_SKIP_COND ()
291 && (cframe
.previous_cframe
== NULL
292 || ! cframe
.previous_cframe
->ignoring
))
296 mri_comment_end (stop
, stopc
);
298 demand_empty_rest_of_line ();
304 if (current_cframe
== NULL
)
306 as_bad (_("\".elseif\" without matching \".if\""));
308 else if (current_cframe
->else_seen
)
310 as_bad (_("\".elseif\" after \".else\""));
311 as_bad_where (current_cframe
->else_file_line
.file
,
312 current_cframe
->else_file_line
.line
,
313 _("here is the previous \"else\""));
314 as_bad_where (current_cframe
->if_file_line
.file
,
315 current_cframe
->if_file_line
.line
,
316 _("here is the previous \"if\""));
320 as_where (¤t_cframe
->else_file_line
.file
,
321 ¤t_cframe
->else_file_line
.line
);
323 current_cframe
->dead_tree
|= !current_cframe
->ignoring
;
324 current_cframe
->ignoring
= current_cframe
->dead_tree
;
327 if (current_cframe
== NULL
|| current_cframe
->ignoring
)
329 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
330 ++input_line_pointer
;
332 if (current_cframe
== NULL
)
340 /* Leading whitespace is part of operand. */
343 expression_and_evaluate (&operand
);
344 if (operand
.X_op
!= O_constant
)
345 as_bad (_("non-constant expression in \".elseif\" statement"));
347 switch ((operatorT
) arg
)
349 case O_eq
: t
= operand
.X_add_number
== 0; break;
350 case O_ne
: t
= operand
.X_add_number
!= 0; break;
351 case O_lt
: t
= operand
.X_add_number
< 0; break;
352 case O_le
: t
= operand
.X_add_number
<= 0; break;
353 case O_ge
: t
= operand
.X_add_number
>= 0; break;
354 case O_gt
: t
= operand
.X_add_number
> 0; break;
360 current_cframe
->ignoring
= current_cframe
->dead_tree
|| ! t
;
363 if (LISTING_SKIP_COND ()
364 && (current_cframe
->previous_cframe
== NULL
365 || ! current_cframe
->previous_cframe
->ignoring
))
367 if (! current_cframe
->ignoring
)
373 demand_empty_rest_of_line ();
377 s_endif (int arg ATTRIBUTE_UNUSED
)
379 struct conditional_frame
*hold
;
381 if (current_cframe
== NULL
)
383 as_bad (_("\".endif\" without \".if\""));
387 if (LISTING_SKIP_COND ()
388 && current_cframe
->ignoring
389 && (current_cframe
->previous_cframe
== NULL
390 || ! current_cframe
->previous_cframe
->ignoring
))
393 hold
= current_cframe
;
394 current_cframe
= current_cframe
->previous_cframe
;
395 obstack_free (&cond_obstack
, hold
);
396 } /* if one pop too many */
400 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
401 ++input_line_pointer
;
404 demand_empty_rest_of_line ();
408 s_else (int arg ATTRIBUTE_UNUSED
)
410 if (current_cframe
== NULL
)
412 as_bad (_("\".else\" without matching \".if\""));
414 else if (current_cframe
->else_seen
)
416 as_bad (_("duplicate \"else\""));
417 as_bad_where (current_cframe
->else_file_line
.file
,
418 current_cframe
->else_file_line
.line
,
419 _("here is the previous \"else\""));
420 as_bad_where (current_cframe
->if_file_line
.file
,
421 current_cframe
->if_file_line
.line
,
422 _("here is the previous \"if\""));
426 as_where (¤t_cframe
->else_file_line
.file
,
427 ¤t_cframe
->else_file_line
.line
);
429 current_cframe
->ignoring
=
430 current_cframe
->dead_tree
| !current_cframe
->ignoring
;
432 if (LISTING_SKIP_COND ()
433 && (current_cframe
->previous_cframe
== NULL
434 || ! current_cframe
->previous_cframe
->ignoring
))
436 if (! current_cframe
->ignoring
)
442 current_cframe
->else_seen
= 1;
447 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
448 ++input_line_pointer
;
451 demand_empty_rest_of_line ();
460 struct conditional_frame cframe
;
462 s1
= demand_copy_C_string (&len1
);
465 if (*input_line_pointer
!= ',')
467 as_bad (_(".ifeqs syntax error"));
468 ignore_rest_of_line ();
472 ++input_line_pointer
;
474 s2
= demand_copy_C_string (&len2
);
476 res
= len1
== len2
&& strncmp (s1
, s2
, len1
) == 0;
478 initialize_cframe (&cframe
);
479 cframe
.ignoring
= cframe
.dead_tree
|| ! (res
^ arg
);
480 current_cframe
= ((struct conditional_frame
*)
481 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
483 if (LISTING_SKIP_COND ()
485 && (cframe
.previous_cframe
== NULL
486 || ! cframe
.previous_cframe
->ignoring
))
489 demand_empty_rest_of_line ();
497 s
= input_line_pointer
;
499 if (NO_PSEUDO_DOT
|| flag_m68k_mri
)
507 return (current_cframe
!= NULL
) && (current_cframe
->ignoring
);
510 /* We cannot ignore certain pseudo ops. */
513 && (!strncasecmp (s
, "if", 2)
514 || !strncasecmp (s
, "ifdef", 5)
515 || !strncasecmp (s
, "ifndef", 6)))
518 && (!strncasecmp (s
, "else", 4)
519 || !strncasecmp (s
, "endif", 5)
520 || !strncasecmp (s
, "endc", 4))))
523 return (current_cframe
!= NULL
) && (current_cframe
->ignoring
);
527 initialize_cframe (struct conditional_frame
*cframe
)
529 memset (cframe
, 0, sizeof (*cframe
));
530 as_where (&cframe
->if_file_line
.file
,
531 &cframe
->if_file_line
.line
);
532 cframe
->previous_cframe
= current_cframe
;
533 cframe
->dead_tree
= current_cframe
!= NULL
&& current_cframe
->ignoring
;
534 cframe
->macro_nest
= macro_nest
;
537 /* Give an error if a conditional is unterminated inside a macro or
538 the assembly as a whole. If NEST is non negative, we are being
539 called because of the end of a macro expansion. If NEST is
540 negative, we are being called at the of the input files. */
543 cond_finish_check (int nest
)
545 if (current_cframe
!= NULL
&& current_cframe
->macro_nest
>= nest
)
548 as_bad (_("end of macro inside conditional"));
550 as_bad (_("end of file inside conditional"));
551 as_bad_where (current_cframe
->if_file_line
.file
,
552 current_cframe
->if_file_line
.line
,
553 _("here is the start of the unterminated conditional"));
554 if (current_cframe
->else_seen
)
555 as_bad_where (current_cframe
->else_file_line
.file
,
556 current_cframe
->else_file_line
.line
,
557 _("here is the \"else\" of the unterminated conditional"));
561 /* This function is called when we exit out of a macro. We assume
562 that any conditionals which began within the macro are correctly
563 nested, and just pop them off the stack. */
566 cond_exit_macro (int nest
)
568 while (current_cframe
!= NULL
&& current_cframe
->macro_nest
>= nest
)
570 struct conditional_frame
*hold
;
572 hold
= current_cframe
;
573 current_cframe
= current_cframe
->previous_cframe
;
574 obstack_free (&cond_obstack
, hold
);