1 /* cond.c - conditional assembly pseudo-ops, and .include
2 Copyright (C) 1990, 91, 92, 93, 95, 96, 97, 98, 1999
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 scanned. */
28 struct obstack cond_obstack
;
36 /* We push one of these structures for each .if, and pop it at the
39 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. */
53 /* Macro nesting level at which this conditional was created. */
57 static void initialize_cframe
PARAMS ((struct conditional_frame
*cframe
));
58 static char *get_mri_string
PARAMS ((int, int *));
60 static struct conditional_frame
*current_cframe
= NULL
;
66 register char *name
; /* points to name of symbol */
67 register symbolS
*symbolP
; /* Points to symbol */
68 struct conditional_frame cframe
;
70 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
71 name
= input_line_pointer
;
73 if (!is_name_beginner (*name
))
75 as_bad (_("invalid identifier for \".ifdef\""));
76 obstack_1grow (&cond_obstack
, 0);
77 ignore_rest_of_line ();
83 c
= get_symbol_end ();
84 symbolP
= symbol_find (name
);
85 *input_line_pointer
= c
;
87 initialize_cframe (&cframe
);
88 cframe
.ignoring
= cframe
.dead_tree
|| !((symbolP
!= 0) ^ arg
);
89 current_cframe
= ((struct conditional_frame
*)
90 obstack_copy (&cond_obstack
, &cframe
,
93 if (LISTING_SKIP_COND ()
95 && (cframe
.previous_cframe
== NULL
96 || ! cframe
.previous_cframe
->ignoring
))
99 demand_empty_rest_of_line ();
100 } /* if a valid identifyer name */
108 struct conditional_frame cframe
;
114 stop
= mri_comment_field (&stopc
);
116 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
118 if (current_cframe
!= NULL
&& current_cframe
->ignoring
)
120 operand
.X_add_number
= 0;
121 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
122 ++input_line_pointer
;
126 expression (&operand
);
127 if (operand
.X_op
!= O_constant
)
128 as_bad (_("non-constant expression in \".if\" statement"));
131 switch ((operatorT
) arg
)
133 case O_eq
: t
= operand
.X_add_number
== 0; break;
134 case O_ne
: t
= operand
.X_add_number
!= 0; break;
135 case O_lt
: t
= operand
.X_add_number
< 0; break;
136 case O_le
: t
= operand
.X_add_number
<= 0; break;
137 case O_ge
: t
= operand
.X_add_number
>= 0; break;
138 case O_gt
: t
= operand
.X_add_number
> 0; break;
144 /* If the above error is signaled, this will dispatch
145 using an undefined result. No big deal. */
146 initialize_cframe (&cframe
);
147 cframe
.ignoring
= cframe
.dead_tree
|| ! t
;
148 current_cframe
= ((struct conditional_frame
*)
149 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
151 if (LISTING_SKIP_COND ()
153 && (cframe
.previous_cframe
== NULL
154 || ! cframe
.previous_cframe
->ignoring
))
158 mri_comment_end (stop
, stopc
);
160 demand_empty_rest_of_line ();
163 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
166 get_mri_string (terminator
, len
)
174 s
= ret
= input_line_pointer
;
175 if (*input_line_pointer
== '\'')
178 ++input_line_pointer
;
179 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
181 *s
++ = *input_line_pointer
++;
184 if (*input_line_pointer
!= '\'')
186 ++input_line_pointer
;
193 while (*input_line_pointer
!= terminator
194 && ! is_end_of_line
[(unsigned char) *input_line_pointer
])
195 ++input_line_pointer
;
196 s
= input_line_pointer
;
197 while (s
> ret
&& (s
[-1] == ' ' || s
[-1] == '\t'))
205 /* The MRI IFC and IFNC pseudo-ops. */
216 struct conditional_frame cframe
;
219 stop
= mri_comment_field (&stopc
);
221 s1
= get_mri_string (',', &len1
);
223 if (*input_line_pointer
!= ',')
224 as_bad (_("bad format for ifc or ifnc"));
226 ++input_line_pointer
;
228 s2
= get_mri_string (';', &len2
);
230 res
= len1
== len2
&& strncmp (s1
, s2
, len1
) == 0;
232 initialize_cframe (&cframe
);
233 cframe
.ignoring
= cframe
.dead_tree
|| ! (res
^ arg
);
234 current_cframe
= ((struct conditional_frame
*)
235 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
237 if (LISTING_SKIP_COND ()
239 && (cframe
.previous_cframe
== NULL
240 || ! cframe
.previous_cframe
->ignoring
))
244 mri_comment_end (stop
, stopc
);
246 demand_empty_rest_of_line ();
251 int arg ATTRIBUTE_UNUSED
;
253 struct conditional_frame
*hold
;
255 if (current_cframe
== NULL
)
257 as_bad (_("\".endif\" without \".if\""));
261 if (LISTING_SKIP_COND ()
262 && current_cframe
->ignoring
263 && (current_cframe
->previous_cframe
== NULL
264 || ! current_cframe
->previous_cframe
->ignoring
))
267 hold
= current_cframe
;
268 current_cframe
= current_cframe
->previous_cframe
;
269 obstack_free (&cond_obstack
, hold
);
270 } /* if one pop too many */
274 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
275 ++input_line_pointer
;
278 demand_empty_rest_of_line ();
283 int arg ATTRIBUTE_UNUSED
;
285 if (current_cframe
== NULL
)
287 as_bad (_(".else without matching .if - ignored"));
290 else if (current_cframe
->else_seen
)
292 as_bad (_("duplicate \"else\" - ignored"));
293 as_bad_where (current_cframe
->else_file_line
.file
,
294 current_cframe
->else_file_line
.line
,
295 _("here is the previous \"else\""));
296 as_bad_where (current_cframe
->if_file_line
.file
,
297 current_cframe
->if_file_line
.line
,
298 _("here is the previous \"if\""));
302 as_where (¤t_cframe
->else_file_line
.file
,
303 ¤t_cframe
->else_file_line
.line
);
305 if (!current_cframe
->dead_tree
)
307 current_cframe
->ignoring
= !current_cframe
->ignoring
;
308 if (LISTING_SKIP_COND ())
310 if (! current_cframe
->ignoring
)
315 } /* if not a dead tree */
317 current_cframe
->else_seen
= 1;
318 } /* if error else do it */
322 while (! is_end_of_line
[(unsigned char) *input_line_pointer
])
323 ++input_line_pointer
;
326 demand_empty_rest_of_line ();
336 struct conditional_frame cframe
;
338 s1
= demand_copy_C_string (&len1
);
341 if (*input_line_pointer
!= ',')
343 as_bad (_(".ifeqs syntax error"));
344 ignore_rest_of_line ();
348 ++input_line_pointer
;
350 s2
= demand_copy_C_string (&len2
);
352 res
= len1
== len2
&& strncmp (s1
, s2
, len1
) == 0;
354 initialize_cframe (&cframe
);
355 cframe
.ignoring
= cframe
.dead_tree
|| ! (res
^ arg
);
356 current_cframe
= ((struct conditional_frame
*)
357 obstack_copy (&cond_obstack
, &cframe
, sizeof (cframe
)));
359 if (LISTING_SKIP_COND ()
361 && (cframe
.previous_cframe
== NULL
362 || ! cframe
.previous_cframe
->ignoring
))
365 demand_empty_rest_of_line ();
373 s
= input_line_pointer
;
387 return (current_cframe
!= NULL
) && (current_cframe
->ignoring
);
390 /* We cannot ignore certain pseudo ops. */
393 && (!strncasecmp (s
, "if", 2)
394 || !strncasecmp (s
, "ifdef", 5)
395 || !strncasecmp (s
, "ifndef", 6)))
398 && (!strncasecmp (s
, "else", 4)
399 || !strncasecmp (s
, "endif", 5)
400 || !strncasecmp (s
, "endc", 4))))
403 return (current_cframe
!= NULL
) && (current_cframe
->ignoring
);
404 } /* ignore_input() */
407 initialize_cframe (cframe
)
408 struct conditional_frame
*cframe
;
410 memset (cframe
, 0, sizeof (*cframe
));
411 as_where (&cframe
->if_file_line
.file
,
412 &cframe
->if_file_line
.line
);
413 cframe
->previous_cframe
= current_cframe
;
414 cframe
->dead_tree
= current_cframe
!= NULL
&& current_cframe
->ignoring
;
415 cframe
->macro_nest
= macro_nest
;
418 /* Give an error if a conditional is unterminated inside a macro or
419 the assembly as a whole. If NEST is non negative, we are being
420 called because of the end of a macro expansion. If NEST is
421 negative, we are being called at the of the input files. */
424 cond_finish_check (nest
)
427 if (current_cframe
!= NULL
&& current_cframe
->macro_nest
>= nest
)
430 as_bad (_("end of macro inside conditional"));
432 as_bad (_("end of file inside conditional"));
433 as_bad_where (current_cframe
->if_file_line
.file
,
434 current_cframe
->if_file_line
.line
,
435 _("here is the start of the unterminated conditional"));
436 if (current_cframe
->else_seen
)
437 as_bad_where (current_cframe
->else_file_line
.file
,
438 current_cframe
->else_file_line
.line
,
439 _("here is the \"else\" of the unterminated conditional"));
443 /* This function is called when we exit out of a macro. We assume
444 that any conditionals which began within the macro are correctly
445 nested, and just pop them off the stack. */
448 cond_exit_macro (nest
)
451 while (current_cframe
!= NULL
&& current_cframe
->macro_nest
>= nest
)
453 struct conditional_frame
*hold
;
455 hold
= current_cframe
;
456 current_cframe
= current_cframe
->previous_cframe
;
457 obstack_free (&cond_obstack
, hold
);