Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* cond.c - conditional assembly pseudo-ops, and .include |
abd63a32 | 2 | Copyright (C) 1990, 91, 92, 93, 95, 96, 97, 98, 99, 2000 |
252b5132 RH |
3 | Free Software Foundation, Inc. |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
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) | |
10 | any later version. | |
11 | ||
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. | |
16 | ||
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 | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | #include "as.h" | |
23 | #include "macro.h" | |
24 | ||
25 | #include "obstack.h" | |
26 | ||
76b0a8c0 KH |
27 | /* This is allocated to grow and shrink as .ifdef/.endif pairs are |
28 | scanned. */ | |
252b5132 RH |
29 | struct obstack cond_obstack; |
30 | ||
30a2b4ef | 31 | struct file_line { |
252b5132 RH |
32 | char *file; |
33 | unsigned int line; | |
34 | }; | |
35 | ||
36 | /* We push one of these structures for each .if, and pop it at the | |
37 | .endif. */ | |
38 | ||
30a2b4ef | 39 | struct conditional_frame { |
252b5132 RH |
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? */ | |
47 | int else_seen; | |
48 | /* Whether we are currently ignoring input. */ | |
49 | int ignoring; | |
50 | /* Whether a conditional at a higher level is ignoring input. */ | |
51 | int dead_tree; | |
52 | /* Macro nesting level at which this conditional was created. */ | |
53 | int macro_nest; | |
54 | }; | |
55 | ||
56 | static void initialize_cframe PARAMS ((struct conditional_frame *cframe)); | |
57 | static char *get_mri_string PARAMS ((int, int *)); | |
58 | ||
59 | static struct conditional_frame *current_cframe = NULL; | |
60 | ||
76b0a8c0 | 61 | void |
252b5132 RH |
62 | s_ifdef (arg) |
63 | int arg; | |
64 | { | |
76b0a8c0 KH |
65 | /* Points to name of symbol. */ |
66 | register char *name; | |
67 | /* Points to symbol. */ | |
68 | register symbolS *symbolP; | |
252b5132 RH |
69 | struct conditional_frame cframe; |
70 | ||
76b0a8c0 KH |
71 | /* Leading whitespace is part of operand. */ |
72 | SKIP_WHITESPACE (); | |
252b5132 RH |
73 | name = input_line_pointer; |
74 | ||
75 | if (!is_name_beginner (*name)) | |
76 | { | |
77 | as_bad (_("invalid identifier for \".ifdef\"")); | |
78 | obstack_1grow (&cond_obstack, 0); | |
79 | ignore_rest_of_line (); | |
80 | } | |
81 | else | |
82 | { | |
83 | char c; | |
84 | ||
85 | c = get_symbol_end (); | |
86 | symbolP = symbol_find (name); | |
87 | *input_line_pointer = c; | |
88 | ||
89 | initialize_cframe (&cframe); | |
90 | cframe.ignoring = cframe.dead_tree || !((symbolP != 0) ^ arg); | |
91 | current_cframe = ((struct conditional_frame *) | |
92 | obstack_copy (&cond_obstack, &cframe, | |
93 | sizeof (cframe))); | |
94 | ||
95 | if (LISTING_SKIP_COND () | |
96 | && cframe.ignoring | |
97 | && (cframe.previous_cframe == NULL | |
98 | || ! cframe.previous_cframe->ignoring)) | |
99 | listing_list (2); | |
100 | ||
101 | demand_empty_rest_of_line (); | |
102 | } /* if a valid identifyer name */ | |
76b0a8c0 | 103 | } |
252b5132 | 104 | |
76b0a8c0 | 105 | void |
252b5132 RH |
106 | s_if (arg) |
107 | int arg; | |
108 | { | |
109 | expressionS operand; | |
110 | struct conditional_frame cframe; | |
111 | int t; | |
112 | char *stop = NULL; | |
113 | char stopc; | |
114 | ||
115 | if (flag_mri) | |
116 | stop = mri_comment_field (&stopc); | |
117 | ||
76b0a8c0 KH |
118 | /* Leading whitespace is part of operand. */ |
119 | SKIP_WHITESPACE (); | |
252b5132 RH |
120 | |
121 | if (current_cframe != NULL && current_cframe->ignoring) | |
122 | { | |
123 | operand.X_add_number = 0; | |
124 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
125 | ++input_line_pointer; | |
126 | } | |
127 | else | |
128 | { | |
129 | expression (&operand); | |
130 | if (operand.X_op != O_constant) | |
131 | as_bad (_("non-constant expression in \".if\" statement")); | |
132 | } | |
133 | ||
134 | switch ((operatorT) arg) | |
135 | { | |
136 | case O_eq: t = operand.X_add_number == 0; break; | |
137 | case O_ne: t = operand.X_add_number != 0; break; | |
138 | case O_lt: t = operand.X_add_number < 0; break; | |
139 | case O_le: t = operand.X_add_number <= 0; break; | |
140 | case O_ge: t = operand.X_add_number >= 0; break; | |
141 | case O_gt: t = operand.X_add_number > 0; break; | |
142 | default: | |
143 | abort (); | |
144 | return; | |
145 | } | |
146 | ||
147 | /* If the above error is signaled, this will dispatch | |
148 | using an undefined result. No big deal. */ | |
149 | initialize_cframe (&cframe); | |
150 | cframe.ignoring = cframe.dead_tree || ! t; | |
151 | current_cframe = ((struct conditional_frame *) | |
152 | obstack_copy (&cond_obstack, &cframe, sizeof (cframe))); | |
153 | ||
154 | if (LISTING_SKIP_COND () | |
155 | && cframe.ignoring | |
156 | && (cframe.previous_cframe == NULL | |
157 | || ! cframe.previous_cframe->ignoring)) | |
158 | listing_list (2); | |
159 | ||
160 | if (flag_mri) | |
161 | mri_comment_end (stop, stopc); | |
162 | ||
163 | demand_empty_rest_of_line (); | |
76b0a8c0 | 164 | } |
252b5132 RH |
165 | |
166 | /* Get a string for the MRI IFC or IFNC pseudo-ops. */ | |
167 | ||
168 | static char * | |
169 | get_mri_string (terminator, len) | |
170 | int terminator; | |
171 | int *len; | |
172 | { | |
173 | char *ret; | |
174 | char *s; | |
175 | ||
176 | SKIP_WHITESPACE (); | |
177 | s = ret = input_line_pointer; | |
178 | if (*input_line_pointer == '\'') | |
179 | { | |
180 | ++s; | |
181 | ++input_line_pointer; | |
182 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
183 | { | |
184 | *s++ = *input_line_pointer++; | |
185 | if (s[-1] == '\'') | |
186 | { | |
187 | if (*input_line_pointer != '\'') | |
188 | break; | |
189 | ++input_line_pointer; | |
190 | } | |
191 | } | |
192 | SKIP_WHITESPACE (); | |
193 | } | |
194 | else | |
195 | { | |
196 | while (*input_line_pointer != terminator | |
197 | && ! is_end_of_line[(unsigned char) *input_line_pointer]) | |
198 | ++input_line_pointer; | |
199 | s = input_line_pointer; | |
200 | while (s > ret && (s[-1] == ' ' || s[-1] == '\t')) | |
201 | --s; | |
202 | } | |
203 | ||
204 | *len = s - ret; | |
205 | return ret; | |
206 | } | |
207 | ||
208 | /* The MRI IFC and IFNC pseudo-ops. */ | |
209 | ||
210 | void | |
211 | s_ifc (arg) | |
212 | int arg; | |
213 | { | |
214 | char *stop = NULL; | |
215 | char stopc; | |
216 | char *s1, *s2; | |
217 | int len1, len2; | |
218 | int res; | |
219 | struct conditional_frame cframe; | |
220 | ||
221 | if (flag_mri) | |
222 | stop = mri_comment_field (&stopc); | |
223 | ||
224 | s1 = get_mri_string (',', &len1); | |
225 | ||
226 | if (*input_line_pointer != ',') | |
227 | as_bad (_("bad format for ifc or ifnc")); | |
228 | else | |
229 | ++input_line_pointer; | |
230 | ||
231 | s2 = get_mri_string (';', &len2); | |
232 | ||
233 | res = len1 == len2 && strncmp (s1, s2, len1) == 0; | |
234 | ||
235 | initialize_cframe (&cframe); | |
236 | cframe.ignoring = cframe.dead_tree || ! (res ^ arg); | |
237 | current_cframe = ((struct conditional_frame *) | |
238 | obstack_copy (&cond_obstack, &cframe, sizeof (cframe))); | |
239 | ||
240 | if (LISTING_SKIP_COND () | |
241 | && cframe.ignoring | |
242 | && (cframe.previous_cframe == NULL | |
243 | || ! cframe.previous_cframe->ignoring)) | |
244 | listing_list (2); | |
245 | ||
246 | if (flag_mri) | |
247 | mri_comment_end (stop, stopc); | |
248 | ||
249 | demand_empty_rest_of_line (); | |
250 | } | |
251 | ||
76b0a8c0 | 252 | void |
3fd9f047 TW |
253 | s_elseif (arg) |
254 | int arg; | |
255 | { | |
256 | expressionS operand; | |
257 | int t; | |
258 | ||
259 | if (current_cframe == NULL) | |
260 | { | |
261 | as_bad (_("\".elseif\" without matching \".if\" - ignored")); | |
262 | ||
263 | } | |
264 | else if (current_cframe->else_seen) | |
265 | { | |
266 | as_bad (_("\".elseif\" after \".else\" - ignored")); | |
267 | as_bad_where (current_cframe->else_file_line.file, | |
268 | current_cframe->else_file_line.line, | |
269 | _("here is the previous \"else\"")); | |
270 | as_bad_where (current_cframe->if_file_line.file, | |
271 | current_cframe->if_file_line.line, | |
272 | _("here is the previous \"if\"")); | |
273 | } | |
274 | else | |
275 | { | |
276 | as_where (¤t_cframe->else_file_line.file, | |
277 | ¤t_cframe->else_file_line.line); | |
278 | ||
279 | if (!current_cframe->dead_tree) | |
280 | { | |
281 | current_cframe->ignoring = !current_cframe->ignoring; | |
282 | if (LISTING_SKIP_COND ()) | |
283 | { | |
284 | if (! current_cframe->ignoring) | |
285 | listing_list (1); | |
286 | else | |
287 | listing_list (2); | |
288 | } | |
289 | } /* if not a dead tree */ | |
290 | } /* if error else do it */ | |
291 | ||
76b0a8c0 KH |
292 | /* Leading whitespace is part of operand. */ |
293 | SKIP_WHITESPACE (); | |
3fd9f047 TW |
294 | |
295 | if (current_cframe != NULL && current_cframe->ignoring) | |
296 | { | |
297 | operand.X_add_number = 0; | |
298 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
299 | ++input_line_pointer; | |
300 | } | |
301 | else | |
302 | { | |
303 | expression (&operand); | |
304 | if (operand.X_op != O_constant) | |
305 | as_bad (_("non-constant expression in \".elseif\" statement")); | |
306 | } | |
76b0a8c0 | 307 | |
3fd9f047 TW |
308 | switch ((operatorT) arg) |
309 | { | |
310 | case O_eq: t = operand.X_add_number == 0; break; | |
311 | case O_ne: t = operand.X_add_number != 0; break; | |
312 | case O_lt: t = operand.X_add_number < 0; break; | |
313 | case O_le: t = operand.X_add_number <= 0; break; | |
314 | case O_ge: t = operand.X_add_number >= 0; break; | |
315 | case O_gt: t = operand.X_add_number > 0; break; | |
316 | default: | |
317 | abort (); | |
318 | return; | |
319 | } | |
320 | ||
321 | current_cframe->ignoring = current_cframe->dead_tree || ! t; | |
322 | ||
323 | if (LISTING_SKIP_COND () | |
324 | && current_cframe->ignoring | |
325 | && (current_cframe->previous_cframe == NULL | |
326 | || ! current_cframe->previous_cframe->ignoring)) | |
327 | listing_list (2); | |
328 | ||
329 | demand_empty_rest_of_line (); | |
330 | } | |
331 | ||
76b0a8c0 | 332 | void |
252b5132 | 333 | s_endif (arg) |
ab9da554 | 334 | int arg ATTRIBUTE_UNUSED; |
252b5132 RH |
335 | { |
336 | struct conditional_frame *hold; | |
337 | ||
338 | if (current_cframe == NULL) | |
339 | { | |
340 | as_bad (_("\".endif\" without \".if\"")); | |
341 | } | |
342 | else | |
343 | { | |
344 | if (LISTING_SKIP_COND () | |
345 | && current_cframe->ignoring | |
346 | && (current_cframe->previous_cframe == NULL | |
347 | || ! current_cframe->previous_cframe->ignoring)) | |
348 | listing_list (1); | |
349 | ||
350 | hold = current_cframe; | |
351 | current_cframe = current_cframe->previous_cframe; | |
352 | obstack_free (&cond_obstack, hold); | |
353 | } /* if one pop too many */ | |
354 | ||
355 | if (flag_mri) | |
356 | { | |
357 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
358 | ++input_line_pointer; | |
359 | } | |
360 | ||
361 | demand_empty_rest_of_line (); | |
76b0a8c0 | 362 | } |
252b5132 | 363 | |
76b0a8c0 | 364 | void |
252b5132 | 365 | s_else (arg) |
ab9da554 | 366 | int arg ATTRIBUTE_UNUSED; |
252b5132 RH |
367 | { |
368 | if (current_cframe == NULL) | |
369 | { | |
370 | as_bad (_(".else without matching .if - ignored")); | |
371 | ||
372 | } | |
373 | else if (current_cframe->else_seen) | |
374 | { | |
375 | as_bad (_("duplicate \"else\" - ignored")); | |
376 | as_bad_where (current_cframe->else_file_line.file, | |
377 | current_cframe->else_file_line.line, | |
378 | _("here is the previous \"else\"")); | |
379 | as_bad_where (current_cframe->if_file_line.file, | |
380 | current_cframe->if_file_line.line, | |
381 | _("here is the previous \"if\"")); | |
382 | } | |
383 | else | |
384 | { | |
385 | as_where (¤t_cframe->else_file_line.file, | |
386 | ¤t_cframe->else_file_line.line); | |
387 | ||
388 | if (!current_cframe->dead_tree) | |
389 | { | |
390 | current_cframe->ignoring = !current_cframe->ignoring; | |
391 | if (LISTING_SKIP_COND ()) | |
392 | { | |
393 | if (! current_cframe->ignoring) | |
394 | listing_list (1); | |
395 | else | |
396 | listing_list (2); | |
397 | } | |
398 | } /* if not a dead tree */ | |
399 | ||
400 | current_cframe->else_seen = 1; | |
401 | } /* if error else do it */ | |
402 | ||
403 | if (flag_mri) | |
404 | { | |
405 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
406 | ++input_line_pointer; | |
407 | } | |
408 | ||
409 | demand_empty_rest_of_line (); | |
76b0a8c0 | 410 | } |
252b5132 | 411 | |
76b0a8c0 | 412 | void |
252b5132 RH |
413 | s_ifeqs (arg) |
414 | int arg; | |
415 | { | |
416 | char *s1, *s2; | |
417 | int len1, len2; | |
418 | int res; | |
419 | struct conditional_frame cframe; | |
420 | ||
421 | s1 = demand_copy_C_string (&len1); | |
422 | ||
423 | SKIP_WHITESPACE (); | |
424 | if (*input_line_pointer != ',') | |
425 | { | |
426 | as_bad (_(".ifeqs syntax error")); | |
427 | ignore_rest_of_line (); | |
428 | return; | |
429 | } | |
430 | ||
431 | ++input_line_pointer; | |
432 | ||
433 | s2 = demand_copy_C_string (&len2); | |
434 | ||
435 | res = len1 == len2 && strncmp (s1, s2, len1) == 0; | |
436 | ||
437 | initialize_cframe (&cframe); | |
438 | cframe.ignoring = cframe.dead_tree || ! (res ^ arg); | |
439 | current_cframe = ((struct conditional_frame *) | |
440 | obstack_copy (&cond_obstack, &cframe, sizeof (cframe))); | |
441 | ||
442 | if (LISTING_SKIP_COND () | |
443 | && cframe.ignoring | |
444 | && (cframe.previous_cframe == NULL | |
445 | || ! cframe.previous_cframe->ignoring)) | |
446 | listing_list (2); | |
447 | ||
448 | demand_empty_rest_of_line (); | |
76b0a8c0 | 449 | } |
252b5132 | 450 | |
76b0a8c0 | 451 | int |
252b5132 RH |
452 | ignore_input () |
453 | { | |
454 | char *s; | |
455 | ||
456 | s = input_line_pointer; | |
457 | ||
abd63a32 | 458 | if (NO_PSEUDO_DOT || flag_m68k_mri) |
252b5132 RH |
459 | { |
460 | if (s[-1] != '.') | |
461 | --s; | |
462 | } | |
463 | else | |
464 | { | |
465 | if (s[-1] != '.') | |
466 | return (current_cframe != NULL) && (current_cframe->ignoring); | |
467 | } | |
468 | ||
469 | /* We cannot ignore certain pseudo ops. */ | |
470 | if (((s[0] == 'i' | |
471 | || s[0] == 'I') | |
472 | && (!strncasecmp (s, "if", 2) | |
473 | || !strncasecmp (s, "ifdef", 5) | |
474 | || !strncasecmp (s, "ifndef", 6))) | |
475 | || ((s[0] == 'e' | |
476 | || s[0] == 'E') | |
477 | && (!strncasecmp (s, "else", 4) | |
478 | || !strncasecmp (s, "endif", 5) | |
479 | || !strncasecmp (s, "endc", 4)))) | |
480 | return 0; | |
481 | ||
482 | return (current_cframe != NULL) && (current_cframe->ignoring); | |
76b0a8c0 | 483 | } |
252b5132 | 484 | |
76b0a8c0 | 485 | static void |
252b5132 RH |
486 | initialize_cframe (cframe) |
487 | struct conditional_frame *cframe; | |
488 | { | |
489 | memset (cframe, 0, sizeof (*cframe)); | |
490 | as_where (&cframe->if_file_line.file, | |
491 | &cframe->if_file_line.line); | |
492 | cframe->previous_cframe = current_cframe; | |
493 | cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring; | |
494 | cframe->macro_nest = macro_nest; | |
495 | } | |
496 | ||
497 | /* Give an error if a conditional is unterminated inside a macro or | |
498 | the assembly as a whole. If NEST is non negative, we are being | |
499 | called because of the end of a macro expansion. If NEST is | |
500 | negative, we are being called at the of the input files. */ | |
501 | ||
502 | void | |
503 | cond_finish_check (nest) | |
504 | int nest; | |
505 | { | |
506 | if (current_cframe != NULL && current_cframe->macro_nest >= nest) | |
507 | { | |
508 | if (nest >= 0) | |
509 | as_bad (_("end of macro inside conditional")); | |
510 | else | |
511 | as_bad (_("end of file inside conditional")); | |
512 | as_bad_where (current_cframe->if_file_line.file, | |
513 | current_cframe->if_file_line.line, | |
514 | _("here is the start of the unterminated conditional")); | |
515 | if (current_cframe->else_seen) | |
516 | as_bad_where (current_cframe->else_file_line.file, | |
517 | current_cframe->else_file_line.line, | |
518 | _("here is the \"else\" of the unterminated conditional")); | |
519 | } | |
520 | } | |
521 | ||
522 | /* This function is called when we exit out of a macro. We assume | |
523 | that any conditionals which began within the macro are correctly | |
524 | nested, and just pop them off the stack. */ | |
525 | ||
526 | void | |
527 | cond_exit_macro (nest) | |
528 | int nest; | |
529 | { | |
530 | while (current_cframe != NULL && current_cframe->macro_nest >= nest) | |
531 | { | |
532 | struct conditional_frame *hold; | |
533 | ||
534 | hold = current_cframe; | |
535 | current_cframe = current_cframe->previous_cframe; | |
536 | obstack_free (&cond_obstack, hold); | |
537 | } | |
538 | } |