1 /* cond.c - conditional assembly pseudo-ops, and .include
2 Copyright 1990, 1991, 1992, 1993, 1995, 1997, 1998, 2000, 2001, 2002,
3 2003, 2006 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
28 /* This is allocated to grow and shrink as .ifdef/.endif pairs are
30 struct obstack cond_obstack
;
37 /* We push one of these structures for each .if, and pop it at the
40 struct conditional_frame
{
41 /* The source file & line number of the "if". */
42 struct file_line if_file_line
;
43 /* The source file & line of the "else". */
44 struct file_line else_file_line
;
45 /* The previous conditional. */
46 struct conditional_frame
*previous_cframe
;
47 /* Have we seen an else yet? */
49 /* Whether we are currently ignoring input. */
51 /* Whether a conditional at a higher level is ignoring input.
52 Set also when a branch of an "if .. elseif .." tree has matched
53 to prevent further matches. */
55 /* Macro nesting level at which this conditional was created. */
59 static void initialize_cframe (struct conditional_frame
*cframe
);
60 static char *get_mri_string (int, int *);
62 static struct conditional_frame
*current_cframe
= NULL
;
64 /* Performs the .ifdef (test_defined == 1) and
65 the .ifndef (test_defined == 0) pseudo op. */
68 s_ifdef (int 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
) || symbol_equated_p (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 ();
129 struct conditional_frame cframe
;
135 stop
= mri_comment_field (&stopc
);
137 /* Leading whitespace is part of operand. */
140 if (current_cframe
!= NULL
&& current_cframe
->ignoring
)
142 operand
.X_add_number
= 0;
143 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
144 ++input_line_pointer
;
148 expression_and_evaluate (&operand
);
149 if (operand
.X_op
!= O_constant
)
150 as_bad (_("non-constant expression in \".if\" statement"));
153 switch ((operatorT
) arg
)
155 case O_eq
: t
= operand
.X_add_number
== 0; break;
156 case O_ne
: t
= operand
.X_add_number
!= 0; break;
157 case O_lt
: t
= operand
.X_add_number
< 0; break;
158 case O_le
: t
= operand
.X_add_number
<= 0; break;
159 case O_ge
: t
= operand
.X_add_number
>= 0; break;
160 case O_gt
: t
= operand
.X_add_number
> 0; break;
166 /* If the above error is signaled, this will dispatch
167 using an undefined result. No big deal. */
168 initialize_cframe (&cframe
);
169 cframe
.ignoring
= cframe
.dead_tree
|| ! t
;
170 current_cframe
= ((struct conditional_frame
*)
171 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
173 if (LISTING_SKIP_COND ()
175 && (cframe
.previous_cframe
== NULL
176 || ! cframe
.previous_cframe
->ignoring
))
180 mri_comment_end (stop
, stopc
);
182 demand_empty_rest_of_line ();
185 /* Performs the .ifb (test_blank == 1) and
186 the .ifnb (test_blank == 0) pseudo op. */
189 s_ifb (int test_blank
)
191 struct conditional_frame cframe
;
193 initialize_cframe (&cframe
);
195 if (cframe
.dead_tree
)
202 is_eol
= is_end_of_line
[(unsigned char) *input_line_pointer
];
203 cframe
.ignoring
= (test_blank
== !is_eol
);
206 current_cframe
= ((struct conditional_frame
*)
207 obstack_copy (&cond_obstack
, &cframe
,
210 if (LISTING_SKIP_COND ()
212 && (cframe
.previous_cframe
== NULL
213 || ! cframe
.previous_cframe
->ignoring
))
216 ignore_rest_of_line ();
219 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
222 get_mri_string (int terminator
, int *len
)
228 s
= ret
= input_line_pointer
;
229 if (*input_line_pointer
== '\'')
232 ++input_line_pointer
;
233 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
235 *s
++ = *input_line_pointer
++;
238 if (*input_line_pointer
!= '\'')
240 ++input_line_pointer
;
247 while (*input_line_pointer
!= terminator
248 && ! is_end_of_line
[(unsigned char) *input_line_pointer
])
249 ++input_line_pointer
;
250 s
= input_line_pointer
;
251 while (s
> ret
&& (s
[-1] == ' ' || s
[-1] == '\t'))
259 /* The MRI IFC and IFNC pseudo-ops. */
269 struct conditional_frame cframe
;
272 stop
= mri_comment_field (&stopc
);
274 s1
= get_mri_string (',', &len1
);
276 if (*input_line_pointer
!= ',')
277 as_bad (_("bad format for ifc or ifnc"));
279 ++input_line_pointer
;
281 s2
= get_mri_string (';', &len2
);
283 res
= len1
== len2
&& strncmp (s1
, s2
, len1
) == 0;
285 initialize_cframe (&cframe
);
286 cframe
.ignoring
= cframe
.dead_tree
|| ! (res
^ arg
);
287 current_cframe
= ((struct conditional_frame
*)
288 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
290 if (LISTING_SKIP_COND ()
292 && (cframe
.previous_cframe
== NULL
293 || ! cframe
.previous_cframe
->ignoring
))
297 mri_comment_end (stop
, stopc
);
299 demand_empty_rest_of_line ();
305 if (current_cframe
== NULL
)
307 as_bad (_("\".elseif\" without matching \".if\""));
309 else if (current_cframe
->else_seen
)
311 as_bad (_("\".elseif\" after \".else\""));
312 as_bad_where (current_cframe
->else_file_line
.file
,
313 current_cframe
->else_file_line
.line
,
314 _("here is the previous \"else\""));
315 as_bad_where (current_cframe
->if_file_line
.file
,
316 current_cframe
->if_file_line
.line
,
317 _("here is the previous \"if\""));
321 as_where (¤t_cframe
->else_file_line
.file
,
322 ¤t_cframe
->else_file_line
.line
);
324 current_cframe
->dead_tree
|= !current_cframe
->ignoring
;
325 current_cframe
->ignoring
= current_cframe
->dead_tree
;
328 if (current_cframe
== NULL
|| current_cframe
->ignoring
)
330 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
331 ++input_line_pointer
;
333 if (current_cframe
== NULL
)
341 /* Leading whitespace is part of operand. */
344 expression_and_evaluate (&operand
);
345 if (operand
.X_op
!= O_constant
)
346 as_bad (_("non-constant expression in \".elseif\" statement"));
348 switch ((operatorT
) arg
)
350 case O_eq
: t
= operand
.X_add_number
== 0; break;
351 case O_ne
: t
= operand
.X_add_number
!= 0; break;
352 case O_lt
: t
= operand
.X_add_number
< 0; break;
353 case O_le
: t
= operand
.X_add_number
<= 0; break;
354 case O_ge
: t
= operand
.X_add_number
>= 0; break;
355 case O_gt
: t
= operand
.X_add_number
> 0; break;
361 current_cframe
->ignoring
= current_cframe
->dead_tree
|| ! t
;
364 if (LISTING_SKIP_COND ()
365 && (current_cframe
->previous_cframe
== NULL
366 || ! current_cframe
->previous_cframe
->ignoring
))
368 if (! current_cframe
->ignoring
)
374 demand_empty_rest_of_line ();
378 s_endif (int arg ATTRIBUTE_UNUSED
)
380 struct conditional_frame
*hold
;
382 if (current_cframe
== NULL
)
384 as_bad (_("\".endif\" without \".if\""));
388 if (LISTING_SKIP_COND ()
389 && current_cframe
->ignoring
390 && (current_cframe
->previous_cframe
== NULL
391 || ! current_cframe
->previous_cframe
->ignoring
))
394 hold
= current_cframe
;
395 current_cframe
= current_cframe
->previous_cframe
;
396 obstack_free (&cond_obstack
, hold
);
397 } /* if one pop too many */
401 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
402 ++input_line_pointer
;
405 demand_empty_rest_of_line ();
409 s_else (int arg ATTRIBUTE_UNUSED
)
411 if (current_cframe
== NULL
)
413 as_bad (_("\".else\" without matching \".if\""));
415 else if (current_cframe
->else_seen
)
417 as_bad (_("duplicate \"else\""));
418 as_bad_where (current_cframe
->else_file_line
.file
,
419 current_cframe
->else_file_line
.line
,
420 _("here is the previous \"else\""));
421 as_bad_where (current_cframe
->if_file_line
.file
,
422 current_cframe
->if_file_line
.line
,
423 _("here is the previous \"if\""));
427 as_where (¤t_cframe
->else_file_line
.file
,
428 ¤t_cframe
->else_file_line
.line
);
430 current_cframe
->ignoring
=
431 current_cframe
->dead_tree
| !current_cframe
->ignoring
;
433 if (LISTING_SKIP_COND ()
434 && (current_cframe
->previous_cframe
== NULL
435 || ! current_cframe
->previous_cframe
->ignoring
))
437 if (! current_cframe
->ignoring
)
443 current_cframe
->else_seen
= 1;
448 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
449 ++input_line_pointer
;
452 demand_empty_rest_of_line ();
461 struct conditional_frame cframe
;
463 s1
= demand_copy_C_string (&len1
);
466 if (*input_line_pointer
!= ',')
468 as_bad (_(".ifeqs syntax error"));
469 ignore_rest_of_line ();
473 ++input_line_pointer
;
475 s2
= demand_copy_C_string (&len2
);
477 res
= len1
== len2
&& strncmp (s1
, s2
, len1
) == 0;
479 initialize_cframe (&cframe
);
480 cframe
.ignoring
= cframe
.dead_tree
|| ! (res
^ arg
);
481 current_cframe
= ((struct conditional_frame
*)
482 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
484 if (LISTING_SKIP_COND ()
486 && (cframe
.previous_cframe
== NULL
487 || ! cframe
.previous_cframe
->ignoring
))
490 demand_empty_rest_of_line ();
498 s
= input_line_pointer
;
500 if (NO_PSEUDO_DOT
|| flag_m68k_mri
)
508 return (current_cframe
!= NULL
) && (current_cframe
->ignoring
);
511 /* We cannot ignore certain pseudo ops. */
514 && (!strncasecmp (s
, "if", 2)
515 || !strncasecmp (s
, "ifdef", 5)
516 || !strncasecmp (s
, "ifndef", 6)))
519 && (!strncasecmp (s
, "else", 4)
520 || !strncasecmp (s
, "endif", 5)
521 || !strncasecmp (s
, "endc", 4))))
524 return (current_cframe
!= NULL
) && (current_cframe
->ignoring
);
528 initialize_cframe (struct conditional_frame
*cframe
)
530 memset (cframe
, 0, sizeof (*cframe
));
531 as_where (&cframe
->if_file_line
.file
,
532 &cframe
->if_file_line
.line
);
533 cframe
->previous_cframe
= current_cframe
;
534 cframe
->dead_tree
= current_cframe
!= NULL
&& current_cframe
->ignoring
;
535 cframe
->macro_nest
= macro_nest
;
538 /* Give an error if a conditional is unterminated inside a macro or
539 the assembly as a whole. If NEST is non negative, we are being
540 called because of the end of a macro expansion. If NEST is
541 negative, we are being called at the of the input files. */
544 cond_finish_check (int nest
)
546 if (current_cframe
!= NULL
&& current_cframe
->macro_nest
>= nest
)
549 as_bad (_("end of macro inside conditional"));
551 as_bad (_("end of file inside conditional"));
552 as_bad_where (current_cframe
->if_file_line
.file
,
553 current_cframe
->if_file_line
.line
,
554 _("here is the start of the unterminated conditional"));
555 if (current_cframe
->else_seen
)
556 as_bad_where (current_cframe
->else_file_line
.file
,
557 current_cframe
->else_file_line
.line
,
558 _("here is the \"else\" of the unterminated conditional"));
562 /* This function is called when we exit out of a macro. We assume
563 that any conditionals which began within the macro are correctly
564 nested, and just pop them off the stack. */
567 cond_exit_macro (int nest
)
569 while (current_cframe
!= NULL
&& current_cframe
->macro_nest
>= nest
)
571 struct conditional_frame
*hold
;
573 hold
= current_cframe
;
574 current_cframe
= current_cframe
->previous_cframe
;
575 obstack_free (&cond_obstack
, hold
);