gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gas / cond.c
CommitLineData
252b5132 1/* cond.c - conditional assembly pseudo-ops, and .include
b3adc24a 2 Copyright (C) 1990-2020 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
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
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
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.
15
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
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132
RH
20
21#include "as.h"
ebd1c875 22#include "sb.h"
252b5132
RH
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
29struct obstack cond_obstack;
30
c77852c8
NC
31struct file_line
32{
3b4dbbbf 33 const char *file;
252b5132
RH
34 unsigned int line;
35};
36
37/* We push one of these structures for each .if, and pop it at the
38 .endif. */
39
c77852c8
NC
40struct conditional_frame
41{
252b5132
RH
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;
cdbc6895
AM
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. */
252b5132
RH
55 int dead_tree;
56 /* Macro nesting level at which this conditional was created. */
57 int macro_nest;
58};
59
73ee5e4c
KH
60static void initialize_cframe (struct conditional_frame *cframe);
61static char *get_mri_string (int, int *);
252b5132
RH
62
63static struct conditional_frame *current_cframe = NULL;
64
8dfa0188
NC
65/* Performs the .ifdef (test_defined == 1) and
66 the .ifndef (test_defined == 0) pseudo op. */
67
76b0a8c0 68void
73ee5e4c 69s_ifdef (int test_defined)
252b5132 70{
76b0a8c0 71 /* Points to name of symbol. */
8dfa0188 72 char *name;
76b0a8c0 73 /* Points to symbol. */
8dfa0188 74 symbolS *symbolP;
252b5132 75 struct conditional_frame cframe;
8dfa0188 76 char c;
252b5132 77
76b0a8c0
KH
78 /* Leading whitespace is part of operand. */
79 SKIP_WHITESPACE ();
252b5132
RH
80 name = input_line_pointer;
81
d02603dc 82 if (!is_name_beginner (*name) && *name != '"')
252b5132
RH
83 {
84 as_bad (_("invalid identifier for \".ifdef\""));
85 obstack_1grow (&cond_obstack, 0);
86 ignore_rest_of_line ();
8dfa0188 87 return;
252b5132 88 }
8dfa0188 89
d02603dc 90 c = get_symbol_name (& name);
8dfa0188 91 symbolP = symbol_find (name);
d02603dc 92 (void) restore_line_pointer (c);
8dfa0188
NC
93
94 initialize_cframe (&cframe);
34bca508 95
8dfa0188
NC
96 if (cframe.dead_tree)
97 cframe.ignoring = 1;
252b5132
RH
98 else
99 {
8dfa0188
NC
100 int is_defined;
101
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
92757bc9 107 && (S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
8dfa0188
NC
108 && S_GET_SEGMENT (symbolP) != reg_section;
109
110 cframe.ignoring = ! (test_defined ^ is_defined);
111 }
112
c77852c8
NC
113 current_cframe =
114 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
115 memcpy (current_cframe, &cframe, sizeof cframe);
8dfa0188
NC
116
117 if (LISTING_SKIP_COND ()
118 && cframe.ignoring
119 && (cframe.previous_cframe == NULL
120 || ! cframe.previous_cframe->ignoring))
121 listing_list (2);
122
123 demand_empty_rest_of_line ();
76b0a8c0 124}
252b5132 125
76b0a8c0 126void
73ee5e4c 127s_if (int arg)
252b5132
RH
128{
129 expressionS operand;
130 struct conditional_frame cframe;
131 int t;
132 char *stop = NULL;
da7119c9 133 char stopc = 0;
252b5132
RH
134
135 if (flag_mri)
136 stop = mri_comment_field (&stopc);
137
76b0a8c0
KH
138 /* Leading whitespace is part of operand. */
139 SKIP_WHITESPACE ();
252b5132
RH
140
141 if (current_cframe != NULL && current_cframe->ignoring)
142 {
143 operand.X_add_number = 0;
144 while (! is_end_of_line[(unsigned char) *input_line_pointer])
145 ++input_line_pointer;
146 }
147 else
148 {
9497f5ac 149 expression_and_evaluate (&operand);
252b5132
RH
150 if (operand.X_op != O_constant)
151 as_bad (_("non-constant expression in \".if\" statement"));
152 }
153
154 switch ((operatorT) arg)
155 {
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;
165 }
166
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;
c77852c8
NC
171 current_cframe =
172 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
173 memcpy (current_cframe, & cframe, sizeof cframe);
252b5132
RH
174
175 if (LISTING_SKIP_COND ()
176 && cframe.ignoring
177 && (cframe.previous_cframe == NULL
178 || ! cframe.previous_cframe->ignoring))
179 listing_list (2);
180
181 if (flag_mri)
182 mri_comment_end (stop, stopc);
183
184 demand_empty_rest_of_line ();
76b0a8c0 185}
252b5132 186
26aca5f6
JB
187/* Performs the .ifb (test_blank == 1) and
188 the .ifnb (test_blank == 0) pseudo op. */
189
190void
191s_ifb (int test_blank)
192{
193 struct conditional_frame cframe;
194
195 initialize_cframe (&cframe);
34bca508 196
26aca5f6
JB
197 if (cframe.dead_tree)
198 cframe.ignoring = 1;
199 else
200 {
201 int is_eol;
202
203 SKIP_WHITESPACE ();
204 is_eol = is_end_of_line[(unsigned char) *input_line_pointer];
205 cframe.ignoring = (test_blank == !is_eol);
206 }
207
c77852c8
NC
208 current_cframe =
209 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
210 memcpy (current_cframe, &cframe, sizeof cframe);
26aca5f6
JB
211
212 if (LISTING_SKIP_COND ()
213 && cframe.ignoring
214 && (cframe.previous_cframe == NULL
215 || ! cframe.previous_cframe->ignoring))
216 listing_list (2);
217
218 ignore_rest_of_line ();
219}
220
252b5132
RH
221/* Get a string for the MRI IFC or IFNC pseudo-ops. */
222
223static char *
73ee5e4c 224get_mri_string (int terminator, int *len)
252b5132
RH
225{
226 char *ret;
227 char *s;
228
229 SKIP_WHITESPACE ();
230 s = ret = input_line_pointer;
231 if (*input_line_pointer == '\'')
232 {
233 ++s;
234 ++input_line_pointer;
235 while (! is_end_of_line[(unsigned char) *input_line_pointer])
236 {
237 *s++ = *input_line_pointer++;
238 if (s[-1] == '\'')
239 {
240 if (*input_line_pointer != '\'')
241 break;
242 ++input_line_pointer;
243 }
244 }
245 SKIP_WHITESPACE ();
246 }
247 else
248 {
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;
255 }
256
257 *len = s - ret;
258 return ret;
259}
260
261/* The MRI IFC and IFNC pseudo-ops. */
262
263void
73ee5e4c 264s_ifc (int arg)
252b5132
RH
265{
266 char *stop = NULL;
da7119c9 267 char stopc = 0;
252b5132
RH
268 char *s1, *s2;
269 int len1, len2;
270 int res;
271 struct conditional_frame cframe;
272
273 if (flag_mri)
274 stop = mri_comment_field (&stopc);
275
276 s1 = get_mri_string (',', &len1);
277
278 if (*input_line_pointer != ',')
279 as_bad (_("bad format for ifc or ifnc"));
280 else
281 ++input_line_pointer;
282
283 s2 = get_mri_string (';', &len2);
284
285 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
286
287 initialize_cframe (&cframe);
288 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
c77852c8
NC
289 current_cframe =
290 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
291 memcpy (current_cframe, &cframe, sizeof cframe);
292
293 if (LISTING_SKIP_COND ()
252b5132
RH
294 && cframe.ignoring
295 && (cframe.previous_cframe == NULL
296 || ! cframe.previous_cframe->ignoring))
297 listing_list (2);
298
299 if (flag_mri)
300 mri_comment_end (stop, stopc);
301
302 demand_empty_rest_of_line ();
303}
304
76b0a8c0 305void
73ee5e4c 306s_elseif (int arg)
3fd9f047 307{
3fd9f047
TW
308 if (current_cframe == NULL)
309 {
0e389e77 310 as_bad (_("\".elseif\" without matching \".if\""));
3fd9f047
TW
311 }
312 else if (current_cframe->else_seen)
313 {
0e389e77 314 as_bad (_("\".elseif\" after \".else\""));
3fd9f047
TW
315 as_bad_where (current_cframe->else_file_line.file,
316 current_cframe->else_file_line.line,
9fd07943 317 _("here is the previous \".else\""));
3fd9f047
TW
318 as_bad_where (current_cframe->if_file_line.file,
319 current_cframe->if_file_line.line,
9fd07943 320 _("here is the previous \".if\""));
3fd9f047
TW
321 }
322 else
323 {
3b4dbbbf
TS
324 current_cframe->else_file_line.file
325 = as_where (&current_cframe->else_file_line.line);
3fd9f047 326
61b96bb4
AM
327 current_cframe->dead_tree |= !current_cframe->ignoring;
328 current_cframe->ignoring = current_cframe->dead_tree;
329 }
3fd9f047 330
cdbc6895 331 if (current_cframe == NULL || current_cframe->ignoring)
3fd9f047 332 {
3fd9f047
TW
333 while (! is_end_of_line[(unsigned char) *input_line_pointer])
334 ++input_line_pointer;
61b96bb4
AM
335
336 if (current_cframe == NULL)
337 return;
3fd9f047 338 }
61b96bb4
AM
339 else
340 {
341 expressionS operand;
342 int t;
cdbc6895 343
61b96bb4
AM
344 /* Leading whitespace is part of operand. */
345 SKIP_WHITESPACE ();
cdbc6895 346
9497f5ac 347 expression_and_evaluate (&operand);
61b96bb4
AM
348 if (operand.X_op != O_constant)
349 as_bad (_("non-constant expression in \".elseif\" statement"));
76b0a8c0 350
61b96bb4
AM
351 switch ((operatorT) arg)
352 {
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;
362 }
3fd9f047 363
61b96bb4
AM
364 current_cframe->ignoring = current_cframe->dead_tree || ! t;
365 }
3fd9f047
TW
366
367 if (LISTING_SKIP_COND ()
3fd9f047
TW
368 && (current_cframe->previous_cframe == NULL
369 || ! current_cframe->previous_cframe->ignoring))
61b96bb4
AM
370 {
371 if (! current_cframe->ignoring)
372 listing_list (1);
373 else
374 listing_list (2);
375 }
3fd9f047
TW
376
377 demand_empty_rest_of_line ();
378}
379
76b0a8c0 380void
73ee5e4c 381s_endif (int arg ATTRIBUTE_UNUSED)
252b5132
RH
382{
383 struct conditional_frame *hold;
384
385 if (current_cframe == NULL)
386 {
387 as_bad (_("\".endif\" without \".if\""));
388 }
389 else
390 {
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);
396
397 hold = current_cframe;
398 current_cframe = current_cframe->previous_cframe;
399 obstack_free (&cond_obstack, hold);
400 } /* if one pop too many */
401
402 if (flag_mri)
403 {
404 while (! is_end_of_line[(unsigned char) *input_line_pointer])
405 ++input_line_pointer;
406 }
407
408 demand_empty_rest_of_line ();
76b0a8c0 409}
252b5132 410
76b0a8c0 411void
73ee5e4c 412s_else (int arg ATTRIBUTE_UNUSED)
252b5132
RH
413{
414 if (current_cframe == NULL)
415 {
0e389e77 416 as_bad (_("\".else\" without matching \".if\""));
252b5132
RH
417 }
418 else if (current_cframe->else_seen)
419 {
9fd07943 420 as_bad (_("duplicate \".else\""));
252b5132
RH
421 as_bad_where (current_cframe->else_file_line.file,
422 current_cframe->else_file_line.line,
9fd07943 423 _("here is the previous \".else\""));
252b5132
RH
424 as_bad_where (current_cframe->if_file_line.file,
425 current_cframe->if_file_line.line,
9fd07943 426 _("here is the previous \".if\""));
252b5132
RH
427 }
428 else
429 {
3b4dbbbf
TS
430 current_cframe->else_file_line.file
431 = as_where (&current_cframe->else_file_line.line);
252b5132 432
61b96bb4
AM
433 current_cframe->ignoring =
434 current_cframe->dead_tree | !current_cframe->ignoring;
435
436 if (LISTING_SKIP_COND ()
437 && (current_cframe->previous_cframe == NULL
438 || ! current_cframe->previous_cframe->ignoring))
252b5132 439 {
61b96bb4
AM
440 if (! current_cframe->ignoring)
441 listing_list (1);
442 else
443 listing_list (2);
444 }
252b5132
RH
445
446 current_cframe->else_seen = 1;
61b96bb4 447 }
252b5132
RH
448
449 if (flag_mri)
450 {
451 while (! is_end_of_line[(unsigned char) *input_line_pointer])
452 ++input_line_pointer;
453 }
454
455 demand_empty_rest_of_line ();
76b0a8c0 456}
252b5132 457
76b0a8c0 458void
73ee5e4c 459s_ifeqs (int arg)
252b5132
RH
460{
461 char *s1, *s2;
462 int len1, len2;
463 int res;
464 struct conditional_frame cframe;
465
466 s1 = demand_copy_C_string (&len1);
467
468 SKIP_WHITESPACE ();
469 if (*input_line_pointer != ',')
470 {
471 as_bad (_(".ifeqs syntax error"));
472 ignore_rest_of_line ();
473 return;
474 }
475
476 ++input_line_pointer;
477
478 s2 = demand_copy_C_string (&len2);
479
480 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
481
482 initialize_cframe (&cframe);
483 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
c77852c8
NC
484 current_cframe =
485 (struct conditional_frame *) obstack_alloc (&cond_obstack, sizeof cframe);
486 memcpy (current_cframe, &cframe, sizeof cframe);
252b5132
RH
487
488 if (LISTING_SKIP_COND ()
489 && cframe.ignoring
490 && (cframe.previous_cframe == NULL
491 || ! cframe.previous_cframe->ignoring))
492 listing_list (2);
493
494 demand_empty_rest_of_line ();
76b0a8c0 495}
252b5132 496
76b0a8c0 497int
73ee5e4c 498ignore_input (void)
252b5132
RH
499{
500 char *s;
501
502 s = input_line_pointer;
503
abd63a32 504 if (NO_PSEUDO_DOT || flag_m68k_mri)
252b5132
RH
505 {
506 if (s[-1] != '.')
507 --s;
508 }
509 else
510 {
511 if (s[-1] != '.')
512 return (current_cframe != NULL) && (current_cframe->ignoring);
513 }
514
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;
527
528 return (current_cframe != NULL) && (current_cframe->ignoring);
76b0a8c0 529}
252b5132 530
76b0a8c0 531static void
73ee5e4c 532initialize_cframe (struct conditional_frame *cframe)
252b5132
RH
533{
534 memset (cframe, 0, sizeof (*cframe));
3b4dbbbf
TS
535 cframe->if_file_line.file
536 = as_where (&cframe->if_file_line.line);
252b5132
RH
537 cframe->previous_cframe = current_cframe;
538 cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring;
539 cframe->macro_nest = macro_nest;
540}
541
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. */
546
547void
73ee5e4c 548cond_finish_check (int nest)
252b5132
RH
549{
550 if (current_cframe != NULL && current_cframe->macro_nest >= nest)
551 {
552 if (nest >= 0)
553 as_bad (_("end of macro inside conditional"));
554 else
555 as_bad (_("end of file inside conditional"));
c77852c8 556
252b5132
RH
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"));
564 }
565}
566
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. */
570
571void
73ee5e4c 572cond_exit_macro (int nest)
252b5132
RH
573{
574 while (current_cframe != NULL && current_cframe->macro_nest >= nest)
575 {
576 struct conditional_frame *hold;
577
578 hold = current_cframe;
579 current_cframe = current_cframe->previous_cframe;
580 obstack_free (&cond_obstack, hold);
581 }
582}
This page took 0.912892 seconds and 4 git commands to generate.