1 /* Common hooks for ARM.
2    Copyright (C) 1991-2021 Free Software Foundation, Inc.
3 
4    This file is part of GCC.
5 
6    GCC is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published
8    by the Free Software Foundation; either version 3, or (at your
9    option) any later version.
10 
11    GCC is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14    License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19 
20 #define INCLUDE_LIST
21 #define INCLUDE_VECTOR
22 #define INCLUDE_ALGORITHM
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "memmodel.h"
28 #include "tm_p.h"
29 #include "common/common-target.h"
30 #include "common/common-target-def.h"
31 #include "opts.h"
32 #include "flags.h"
33 #include "sbitmap.h"
34 #include "diagnostic.h"
35 
36 #include "configargs.h"
37 
38 /* Set default optimization options.  */
39 static const struct default_options arm_option_optimization_table[] =
40   {
41     /* Enable section anchors by default at -O1 or higher.  */
42     { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 },
43     { OPT_LEVELS_1_PLUS, OPT_fsched_pressure, NULL, 1 },
44     { OPT_LEVELS_NONE, 0, NULL, 0 }
45   };
46 
47 /* Implement TARGET_EXCEPT_UNWIND_INFO.  */
48 
49 enum unwind_info_type
arm_except_unwind_info(struct gcc_options * opts)50 arm_except_unwind_info (struct gcc_options *opts)
51 {
52   /* Honor the --enable-sjlj-exceptions configure switch.  */
53 #ifdef CONFIG_SJLJ_EXCEPTIONS
54   if (CONFIG_SJLJ_EXCEPTIONS)
55     return UI_SJLJ;
56 #endif
57 
58   /* If not using ARM EABI unwind tables... */
59   if (ARM_UNWIND_INFO)
60     {
61       /* For simplicity elsewhere in this file, indicate that all unwind
62 	 info is disabled if we're not emitting unwind tables.  */
63       if (!opts->x_flag_exceptions && !opts->x_flag_unwind_tables)
64 	return UI_NONE;
65       else
66 	return UI_TARGET;
67     }
68 
69   /* ... honor target configurations requesting DWARF2 EH...  */
70 #ifdef DWARF2_UNWIND_INFO
71   if (DWARF2_UNWIND_INFO)
72     return UI_DWARF2;
73 #endif
74 
75   /* ... or fallback to sjlj exceptions for backwards compatibility.  */
76   return UI_SJLJ;
77 }
78 
79 #define ARM_CPU_NAME_LENGTH 20
80 
81 /* Truncate NAME at the first '.' or '+' character seen, or return
82    NAME unmodified.  */
83 
84 const char *
arm_rewrite_selected_cpu(const char * name)85 arm_rewrite_selected_cpu (const char *name)
86 {
87   static char output_buf[ARM_CPU_NAME_LENGTH + 1] = {0};
88   char *arg_pos;
89 
90   strncpy (output_buf, name, ARM_CPU_NAME_LENGTH);
91   output_buf[ARM_CPU_NAME_LENGTH] = 0;
92 
93   arg_pos = strchr (output_buf, '.');
94 
95   /* If we found a '.' truncate the entry at that point.  */
96   if (arg_pos)
97     *arg_pos = '\0';
98 
99   arg_pos = strchr (output_buf, '+');
100 
101   /* If we found a '+' truncate the entry at that point.  */
102   if (arg_pos)
103     *arg_pos = '\0';
104 
105   return output_buf;
106 }
107 
108 /* Called by the driver to rewrite a name passed to the -mcpu
109    argument in preparation to be passed to the assembler.  The
110    names passed from the command line will be in ARGV, we want
111    to use the right-most argument, which should be in
112    ARGV[ARGC - 1].  ARGC should always be greater than 0.  */
113 
114 const char *
arm_rewrite_mcpu(int argc,const char ** argv)115 arm_rewrite_mcpu (int argc, const char **argv)
116 {
117   gcc_assert (argc);
118 
119 #ifdef HAVE_GAS_ARM_EXTENDED_ARCH
120   return argv[argc - 1];
121 #else
122   return arm_rewrite_selected_cpu (argv[argc - 1]);
123 #endif
124 }
125 
126 /* Comparator for arm_rewrite_selected_arch.  Compare the two arch extension
127    strings FIRST and SECOND and return TRUE if FIRST is less than SECOND
128    alphabetically.  */
129 
130 static bool
compare_opt_names(const char * first,const char * second)131 compare_opt_names (const char *first, const char *second)
132 {
133   return strcmp (first, second) <= 0;
134 }
135 
136 /* Rewrite the architecture string for passing to the assembler.
137    Although the syntax is similar we cannot assume that it supports
138    the newer FP related options.  So strip any option that only
139    defines features in the standard -mfpu options out.  We'll generate
140    a suitable -mfpu option elsewhere to carry that information.  NAME
141    should already have been canonicalized, so we do not expect to
142    encounter +no.. options that remove features.  A final problem is
143    that the assembler expects the feature extensions to be listed
144    alphabetically, so we build a list of required options and then
145    sort them into canonical order in the resulting string.  */
146 const char *
arm_rewrite_selected_arch(const char * name)147 arm_rewrite_selected_arch (const char *name)
148 {
149   /* The result we return needs to be semi persistent, so handle being
150      re-invoked.  */
151   static char *asm_arch = NULL;
152 
153   if (asm_arch)
154     {
155       free (asm_arch);
156       asm_arch = NULL;
157     }
158 
159   const char *arg_pos = strchr (name, '+');
160 
161   /* No extension options? just return the original string.  */
162   if (arg_pos == NULL)
163     return name;
164 
165   const arch_option *arch_opt
166     = arm_parse_arch_option_name (all_architectures, "-march", name);
167 
168   auto_sbitmap fpu_bits (isa_num_bits);
169   static const enum isa_feature fpu_bitlist[]
170     = { ISA_ALL_FPU_INTERNAL, isa_nobit };
171 
172   arm_initialize_isa (fpu_bits, fpu_bitlist);
173 
174   auto_sbitmap opt_bits (isa_num_bits);
175 
176   /* Ensure that the resulting string is large enough for the result.  We
177      never add options, so using strdup here will ensure that.  */
178   asm_arch = xstrdup (name);
179   asm_arch[arg_pos - name] = '\0';
180 
181   std::vector<const char *>optlist;
182 
183   while (arg_pos)
184     {
185       const char *end = strchr (arg_pos + 1, '+');
186       size_t len = end ? end - arg_pos : strlen (arg_pos);
187 
188       for (const cpu_arch_extension *entry = arch_opt->common.extensions;
189 	   entry->name != NULL;
190 	   entry++)
191 	{
192 	  if (strncmp (entry->name, arg_pos + 1, len - 1) == 0
193 	      && entry->name[len - 1] == '\0')
194 	    {
195 	      /* Don't expect removal options.  */
196 	      gcc_assert (!entry->remove);
197 	      arm_initialize_isa (opt_bits, entry->isa_bits);
198 	      if (!bitmap_subset_p (opt_bits, fpu_bits))
199 		optlist.push_back (entry->name);
200 	      bitmap_clear (opt_bits);
201 	      break;
202 	    }
203 	}
204 
205       arg_pos = end;
206     }
207 
208   std::sort (optlist.begin (), optlist.end (), compare_opt_names);
209 
210   for (std::vector<const char *>::iterator opt_iter = optlist.begin ();
211        opt_iter != optlist.end ();
212        ++opt_iter)
213     {
214       strcat (asm_arch, "+");
215       strcat (asm_arch, (*opt_iter));
216     }
217 
218   return asm_arch;
219 }
220 
221 /* Called by the driver to rewrite a name passed to the -march
222    argument in preparation to be passed to the assembler.  The
223    names passed from the command line will be in ARGV, we want
224    to use the right-most argument, which should be in
225    ARGV[ARGC - 1].  ARGC should always be greater than 0.  */
226 
227 const char *
arm_rewrite_march(int argc,const char ** argv)228 arm_rewrite_march (int argc, const char **argv)
229 {
230   gcc_assert (argc);
231 
232 #ifdef HAVE_GAS_ARM_EXTENDED_ARCH
233   return argv[argc - 1];
234 #else
235   return arm_rewrite_selected_arch (argv[argc - 1]);
236 #endif
237 }
238 
239 #include "arm-cpu-cdata.h"
240 
241 /* Scan over a raw feature array BITS checking for BIT being present.
242    This is slower than the normal bitmask checks, but we would spend longer
243    initializing that than doing the check this way.  Returns true iff
244    BIT is found.  */
245 static bool
check_isa_bits_for(const enum isa_feature * bits,enum isa_feature bit)246 check_isa_bits_for (const enum isa_feature* bits, enum isa_feature bit)
247 {
248   while (*bits != isa_nobit)
249     if (*bits++ == bit)
250       return true;
251 
252   return false;
253 }
254 
255 /* Look up NAME in the configuration defaults for this build of the
256    the compiler.  Return the value associated with that name, or NULL
257    if no value is found.  */
258 static const char *
arm_config_default(const char * name)259 arm_config_default (const char *name)
260 {
261   unsigned i;
262 
263   if (configure_default_options[0].name == NULL)
264     return NULL;
265 
266   for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
267     if (strcmp (configure_default_options[i].name, name) == 0)
268       return configure_default_options[i].value;
269 
270   return NULL;
271 }
272 
273 /* Called by the driver to check whether the target denoted by current
274    command line options is a Thumb-only, or ARM-only, target.  ARGV is
275    an array of tupples (normally only one) where the first element of
276    the tupple is 'cpu' or 'arch' and the second is the option passed
277    to the compiler for that.  An architecture tupple is always taken
278    in preference to a cpu tupple and the last of each type always
279    overrides any earlier setting.  */
280 
281 const char *
arm_target_mode(int argc,const char ** argv)282 arm_target_mode (int argc, const char **argv)
283 {
284   const char *arch = NULL;
285   const char *cpu = NULL;
286 
287   if (argc % 2 != 0)
288     fatal_error (input_location,
289 		 "%%:target_mode_check takes an even number of parameters");
290 
291   while (argc)
292     {
293       if (strcmp (argv[0], "arch") == 0)
294 	arch = argv[1];
295       else if (strcmp (argv[0], "cpu") == 0)
296 	cpu = argv[1];
297       else
298 	fatal_error (input_location,
299 		     "unrecognized option passed to %%:target_mode_check");
300       argc -= 2;
301       argv += 2;
302     }
303 
304   /* No architecture, or CPU, has option extensions that change
305      whether or not we have a Thumb-only device, so there is no need
306      to scan any option extensions specified.  */
307 
308   /* If the architecture is specified, that overrides any CPU setting.  */
309   if (arch)
310     {
311       const arch_option *arch_opt
312 	= arm_parse_arch_option_name (all_architectures, "-march", arch,
313 				      false);
314 
315       if (arch_opt && !check_isa_bits_for (arch_opt->common.isa_bits,
316 					   isa_bit_notm))
317 	return "-mthumb";
318       if (arch_opt && !check_isa_bits_for (arch_opt->common.isa_bits,
319 					   isa_bit_thumb))
320 	return "-marm";
321     }
322   else if (cpu)
323     {
324       const cpu_option *cpu_opt
325 	= arm_parse_cpu_option_name (all_cores, "-mcpu", cpu, false);
326 
327       if (cpu_opt && !check_isa_bits_for (cpu_opt->common.isa_bits,
328 					  isa_bit_notm))
329 	return "-mthumb";
330       if (cpu_opt && !check_isa_bits_for (cpu_opt->common.isa_bits,
331 					   isa_bit_thumb))
332 	return "-marm";
333     }
334 
335   const char *default_mode = arm_config_default ("mode");
336   if (default_mode)
337     {
338       if (strcmp (default_mode, "thumb") == 0)
339 	return "-mthumb";
340       else if (strcmp (default_mode, "arm") == 0)
341 	return "-marm";
342       else
343 	gcc_unreachable ();
344     }
345 
346   /* Compiler hasn't been configured with a default, and the CPU
347      doesn't require Thumb, so default to ARM.  */
348   return "-marm";
349 }
350 
351 /* List the permitted CPU option names.  If TARGET is a near miss for an
352    entry, print out the suggested alternative.  */
353 static void
arm_print_hint_for_cpu_option(const char * target,const cpu_option * list)354 arm_print_hint_for_cpu_option (const char *target,
355 			       const cpu_option *list)
356 {
357   auto_vec<const char*> candidates;
358   for (; list->common.name != NULL; list++)
359     {
360       candidates.safe_push (list->common.name);
361       if (list->aliases)
362 	{
363 	  for (const cpu_alias *alias = list->aliases; alias->name != NULL;
364 	       alias++)
365 	    if (alias->visible)
366 	      candidates.safe_push (alias->name);
367 	}
368     }
369 
370 #ifdef HAVE_LOCAL_CPU_DETECT
371   /* Add also "native" as possible value.  */
372   candidates.safe_push ("native");
373 #endif
374 
375   char *s;
376   const char *hint = candidates_list_and_hint (target, s, candidates);
377   if (hint)
378     inform (input_location, "valid arguments are: %s; did you mean %qs?",
379 	    s, hint);
380   else
381     inform (input_location, "valid arguments are: %s", s);
382 
383   XDELETEVEC (s);
384 }
385 
386 /* Parse the base component of a CPU selection in LIST.  Return a
387    pointer to the entry in the architecture table.  OPTNAME is the
388    name of the option we are parsing and can be used if a diagnostic
389    is needed.  If COMPLAIN is true (the default) emit error
390    messages and hints on invalid input.  */
391 const cpu_option *
arm_parse_cpu_option_name(const cpu_option * list,const char * optname,const char * target,bool complain)392 arm_parse_cpu_option_name (const cpu_option *list, const char *optname,
393 			   const char *target, bool complain)
394 {
395   const cpu_option *entry;
396   const char *end  = strchr (target, '+');
397   size_t len = end ? end - target : strlen (target);
398 
399   for (entry = list; entry->common.name != NULL; entry++)
400     {
401       if (strncmp (entry->common.name, target, len) == 0
402 	  && entry->common.name[len] == '\0')
403 	return entry;
404 
405       /* Match against any legal alias for this CPU candidate.  */
406       if (entry->aliases)
407 	{
408 	  for (const cpu_alias *alias = entry->aliases; alias->name != NULL;
409 	       alias++)
410 	    if (strncmp (alias->name, target, len) == 0
411 		&& alias->name[len] == '\0')
412 	      return entry;
413 	}
414     }
415 
416   if (complain)
417     {
418       error_at (input_location, "unrecognized %s target: %s", optname, target);
419       arm_print_hint_for_cpu_option (target, list);
420     }
421   return NULL;
422 }
423 
424 /* List the permitted architecture option names.  If TARGET is a near
425    miss for an entry, print out the suggested alternative.  */
426 static void
arm_print_hint_for_arch_option(const char * target,const arch_option * list)427 arm_print_hint_for_arch_option (const char *target,
428 			       const arch_option *list)
429 {
430   auto_vec<const char*> candidates;
431   for (; list->common.name != NULL; list++)
432     candidates.safe_push (list->common.name);
433 
434 #ifdef HAVE_LOCAL_CPU_DETECT
435   /* Add also "native" as possible value.  */
436   candidates.safe_push ("native");
437 #endif
438 
439   char *s;
440   const char *hint = candidates_list_and_hint (target, s, candidates);
441   if (hint)
442     inform (input_location, "valid arguments are: %s; did you mean %qs?",
443 	    s, hint);
444   else
445     inform (input_location, "valid arguments are: %s", s);
446 
447   XDELETEVEC (s);
448 }
449 
450 /* Parse the base component of a CPU or architecture selection in
451    LIST.  Return a pointer to the entry in the architecture table.
452    OPTNAME is the name of the option we are parsing and can be used if
453    a diagnostic is needed.  If COMPLAIN is true (the default) emit error
454    messages and hints on invalid input.  */
455 const arch_option *
arm_parse_arch_option_name(const arch_option * list,const char * optname,const char * target,bool complain)456 arm_parse_arch_option_name (const arch_option *list, const char *optname,
457 			    const char *target, bool complain)
458 {
459   const arch_option *entry;
460   const char *end  = strchr (target, '+');
461   size_t len = end ? end - target : strlen (target);
462 
463   for (entry = list; entry->common.name != NULL; entry++)
464     {
465       if (strncmp (entry->common.name, target, len) == 0
466 	  && entry->common.name[len] == '\0')
467 	return entry;
468     }
469 
470   if (complain)
471     {
472       error_at (input_location, "unrecognized %s target: %s", optname, target);
473       arm_print_hint_for_arch_option (target, list);
474     }
475   return NULL;
476 }
477 
478 /* List the permitted architecture option names.  If TARGET is a near
479    miss for an entry, print out the suggested alternative.  */
480 static void
arm_print_hint_for_fpu_option(const char * target)481 arm_print_hint_for_fpu_option (const char *target)
482 {
483   auto_vec<const char*> candidates;
484   for (int i = 0; i < TARGET_FPU_auto; i++)
485     candidates.safe_push (all_fpus[i].name);
486   char *s;
487   const char *hint = candidates_list_and_hint (target, s, candidates);
488   if (hint)
489     inform (input_location, "valid arguments are: %s; did you mean %qs?",
490 	    s, hint);
491   else
492     inform (input_location, "valid arguments are: %s", s);
493 
494   XDELETEVEC (s);
495 }
496 
497 static const arm_fpu_desc *
arm_parse_fpu_option(const char * opt)498 arm_parse_fpu_option (const char *opt)
499 {
500   int i;
501 
502   for (i = 0; i < TARGET_FPU_auto; i++)
503     {
504       if (strcmp (all_fpus[i].name, opt) == 0)
505 	return all_fpus + i;
506     }
507 
508   error_at (input_location, "unrecognized %<-mfpu%> target: %s", opt);
509   arm_print_hint_for_fpu_option (opt);
510   return NULL;
511 }
512 
513 /* Convert a static initializer array of feature bits to sbitmap
514    representation.  */
515 void
arm_initialize_isa(sbitmap isa,const enum isa_feature * isa_bits)516 arm_initialize_isa (sbitmap isa, const enum isa_feature *isa_bits)
517 {
518   bitmap_clear (isa);
519   while (*isa_bits != isa_nobit)
520     bitmap_set_bit (isa, *(isa_bits++));
521 }
522 
523 /* OPT isn't a recognized feature.  Print a suitable error message and
524    suggest a possible value.  Always print the list of permitted
525    values.  */
526 static void
arm_unrecognized_feature(const char * opt,size_t len,const cpu_arch_option * target)527 arm_unrecognized_feature (const char *opt, size_t len,
528 			  const cpu_arch_option *target)
529 {
530   char *this_opt = XALLOCAVEC (char, len+1);
531   auto_vec<const char*> candidates;
532 
533   strncpy (this_opt, opt, len);
534   this_opt[len] = 0;
535 
536   error_at (input_location, "%qs does not support feature %qs", target->name,
537 	    this_opt);
538   for (const cpu_arch_extension *list = target->extensions;
539        list->name != NULL;
540        list++)
541     candidates.safe_push (list->name);
542 
543   char *s;
544   const char *hint = candidates_list_and_hint (this_opt, s, candidates);
545 
546   if (hint)
547     inform (input_location, "valid feature names are: %s; did you mean %qs?",
548 	    s, hint);
549   else
550     inform (input_location, "valid feature names are: %s", s);
551 
552   XDELETEVEC (s);
553 }
554 
555 /* Parse any feature extensions to add to (or remove from) the
556    permitted ISA selection.  */
557 void
arm_parse_option_features(sbitmap isa,const cpu_arch_option * target,const char * opts_in)558 arm_parse_option_features (sbitmap isa, const cpu_arch_option *target,
559 			   const char *opts_in)
560 {
561   const char *opts = opts_in;
562 
563   if (!opts)
564     return;
565 
566   if (!target->extensions)
567     {
568       error_at (input_location, "%s does not take any feature options",
569 		target->name);
570       return;
571     }
572 
573   while (opts)
574     {
575       gcc_assert (*opts == '+');
576       const struct cpu_arch_extension *entry;
577       const char *end = strchr (++opts, '+');
578       size_t len = end ? end - opts : strlen (opts);
579       bool matched = false;
580 
581       for (entry = target->extensions;
582 	   !matched && entry->name != NULL;
583 	   entry++)
584 	{
585 	  if (strncmp (entry->name, opts, len) == 0
586 	      && entry->name[len] == '\0')
587 	    {
588 	      if (isa)
589 		{
590 		  const enum isa_feature *f = entry->isa_bits;
591 		  if (entry->remove)
592 		    {
593 		      while (*f != isa_nobit)
594 			bitmap_clear_bit (isa, *(f++));
595 		    }
596 		  else
597 		    {
598 		      while (*f != isa_nobit)
599 			bitmap_set_bit (isa, *(f++));
600 		    }
601 		}
602 	      matched = true;
603 	    }
604 	}
605 
606       if (!matched)
607 	arm_unrecognized_feature (opts, len, target);
608 
609       opts = end;
610     }
611 }
612 
613 class candidate_extension
614 {
615 public:
616   const cpu_arch_extension *extension;
617   sbitmap isa_bits;
618   bool required;
619 
candidate_extension(const cpu_arch_extension * ext,sbitmap bits)620   candidate_extension (const cpu_arch_extension *ext, sbitmap bits)
621     : extension (ext), isa_bits (bits), required (true)
622     {}
~candidate_extension()623   ~candidate_extension ()
624     {
625       sbitmap_free (isa_bits);
626     }
627 };
628 
629 /* Generate a canonical representation of the -march option from the
630    current -march string (if given) and other options on the command
631    line that might affect the architecture.  This aids multilib selection
632    by ensuring that:
633    a) the option is always present
634    b) only the minimal set of options are used
635    c) when there are multiple extensions, they are in a consistent order.
636 
637    The options array consists of couplets of information where the
638    first item in each couplet is the string describing which option
639    name was selected (arch, cpu, fpu) and the second is the value
640    passed for that option.
641 
642    arch_for_multilib is boolean variable taking value true or false.
643    arch_for_multilib is false when the canonical representation is for -march
644    option and it is true when canonical representation is for -mlibarch option.
645    On passing arch_for_multilib true the canonical string generated will be
646    without the compiler options which are not required for multilib linking.  */
647 static const char *
arm_canon_arch_option_1(int argc,const char ** argv,bool arch_for_multilib)648 arm_canon_arch_option_1 (int argc, const char **argv, bool arch_for_multilib)
649 {
650   const char *arch = NULL;
651   const char *cpu = NULL;
652   const char *fpu = NULL;
653   const char *abi = NULL;
654   static char *canonical_arch = NULL;
655 
656   /* Just in case we're called more than once.  */
657   if (canonical_arch)
658     {
659       free (canonical_arch);
660       canonical_arch = NULL;
661     }
662 
663   if (argc & 1)
664     fatal_error (input_location,
665 		 "%%:canon_for_mlib takes 1 or more pairs of parameters");
666 
667   while (argc)
668     {
669       if (strcmp (argv[0], "arch") == 0)
670 	arch = argv[1];
671       else if (strcmp (argv[0], "cpu") == 0)
672 	cpu = argv[1];
673       else if (strcmp (argv[0], "fpu") == 0)
674 	fpu = argv[1];
675       else if (strcmp (argv[0], "abi") == 0)
676 	abi = argv[1];
677       else
678 	fatal_error (input_location,
679 		     "unrecognized operand to %%:canon_for_mlib");
680 
681       argc -= 2;
682       argv += 2;
683     }
684 
685   auto_sbitmap target_isa (isa_num_bits);
686   auto_sbitmap base_isa (isa_num_bits);
687   auto_sbitmap fpu_isa (isa_num_bits);
688 
689   bitmap_clear (fpu_isa);
690 
691   const arch_option *selected_arch = NULL;
692 
693   /* At least one of these must be defined by either the specs or the
694      user.  */
695   gcc_assert (cpu || arch);
696 
697   if (!fpu)
698     fpu = FPUTYPE_AUTO;
699 
700   if (!abi)
701     {
702       if (TARGET_DEFAULT_FLOAT_ABI == ARM_FLOAT_ABI_SOFT)
703 	abi = "soft";
704       else if (TARGET_DEFAULT_FLOAT_ABI == ARM_FLOAT_ABI_SOFTFP)
705 	abi = "softfp";
706       else if (TARGET_DEFAULT_FLOAT_ABI == ARM_FLOAT_ABI_HARD)
707 	abi = "hard";
708     }
709 
710   /* First build up a bitmap describing the target architecture.  */
711   if (arch)
712     {
713       selected_arch = arm_parse_arch_option_name (all_architectures, "-march",
714 						  arch, !arch_for_multilib);
715 
716       if (selected_arch == NULL)
717 	return "";
718 
719       arm_initialize_isa (target_isa, selected_arch->common.isa_bits);
720       arm_parse_option_features (target_isa, &selected_arch->common,
721 				 strchr (arch, '+'));
722       if (arch_for_multilib)
723 	{
724 	  const enum isa_feature removable_bits[] = {ISA_IGNORE_FOR_MULTILIB,
725 						     isa_nobit};
726 	  sbitmap isa_bits = sbitmap_alloc (isa_num_bits);
727 	  arm_initialize_isa (isa_bits, removable_bits);
728 	  bitmap_and_compl (target_isa, target_isa, isa_bits);
729 	}
730 
731       if (fpu && strcmp (fpu, "auto") != 0)
732 	{
733 	  /* We assume that architectures do not have any FPU bits
734 	     enabled by default.  If they did, we would need to strip
735 	     these out first.  */
736 	  const arm_fpu_desc *target_fpu = arm_parse_fpu_option (fpu);
737 	  if (target_fpu == NULL)
738 	    return "";
739 
740 	  arm_initialize_isa (fpu_isa, target_fpu->isa_bits);
741 	  bitmap_ior (target_isa, target_isa, fpu_isa);
742 	}
743     }
744   else if (cpu)
745     {
746       const cpu_option *selected_cpu
747 	= arm_parse_cpu_option_name (all_cores, "-mcpu", cpu,
748 				     !arch_for_multilib);
749 
750       if (selected_cpu == NULL)
751 	return "";
752 
753       arm_initialize_isa (target_isa, selected_cpu->common.isa_bits);
754       arm_parse_option_features (target_isa, &selected_cpu->common,
755 				 strchr (cpu, '+'));
756       if (fpu && strcmp (fpu, "auto") != 0)
757 	{
758 	  /* The easiest and safest way to remove the default fpu
759 	     capabilities is to look for a '+no..' option that removes
760 	     the base FPU bit (isa_bit_vfpv2).  If that doesn't exist
761 	     then the best we can do is strip out all the bits that
762 	     might be part of the most capable FPU we know about,
763 	     which is "crypto-neon-fp-armv8".  */
764 	  bool default_fpu_found = false;
765 	  if (selected_cpu->common.extensions)
766 	    {
767 	      const cpu_arch_extension *ext;
768 	      for (ext = selected_cpu->common.extensions; ext->name != NULL;
769 		   ++ext)
770 		{
771 		  if (ext->remove
772 		      && check_isa_bits_for (ext->isa_bits, isa_bit_vfpv2))
773 		    {
774 		      arm_initialize_isa (fpu_isa, ext->isa_bits);
775 		      bitmap_and_compl (target_isa, target_isa, fpu_isa);
776 		      default_fpu_found = true;
777 		    }
778 		}
779 
780 	    }
781 
782 	  if (!default_fpu_found)
783 	    {
784 	      arm_initialize_isa
785 		(fpu_isa,
786 		 all_fpus[TARGET_FPU_crypto_neon_fp_armv8].isa_bits);
787 	      bitmap_and_compl (target_isa, target_isa, fpu_isa);
788 	    }
789 
790 	  const arm_fpu_desc *target_fpu = arm_parse_fpu_option (fpu);
791 	  if (target_fpu == NULL)
792 	    return "";
793 
794 	  arm_initialize_isa (fpu_isa, target_fpu->isa_bits);
795 	  bitmap_ior (target_isa, target_isa, fpu_isa);
796 	}
797 
798       selected_arch = all_architectures + selected_cpu->arch;
799     }
800 
801   /* If we have a soft-float ABI, disable the FPU.  */
802   if (abi && strcmp (abi, "soft") == 0)
803     {
804       /* Clearing the VFPv2 bit is sufficient to stop any extention that
805 	 builds on the FPU from matching.  */
806       bitmap_clear_bit (target_isa, isa_bit_vfpv2);
807     }
808 
809   /* If we don't have a selected architecture by now, something's
810      badly wrong.  */
811   gcc_assert (selected_arch);
812 
813   arm_initialize_isa (base_isa, selected_arch->common.isa_bits);
814 
815   /* Architecture has no extension options, so just return the canonical
816      architecture name.  */
817   if (selected_arch->common.extensions == NULL)
818     return selected_arch->common.name;
819 
820   /* We're only interested in extension bits.  */
821   bitmap_and_compl (target_isa, target_isa, base_isa);
822 
823   /* There are no extensions needed.  Just return the canonical architecture
824      name.  */
825   if (bitmap_empty_p (target_isa))
826     return selected_arch->common.name;
827 
828   /* What is left is the architecture that the compiler will target.  We
829      now need to map that back into a suitable option+features list.
830 
831      The list is built in two passes.  First we scan every additive
832      option feature supported by the architecture.  If the option
833      provides a subset of the features we need we add it to the list
834      of candidates.  We then scan backwards over the list of
835      candidates and if we find a feature that adds nothing to one that
836      was later in the list we mark it as redundant.  The result is a
837      minimal list of required features for the target
838      architecture.  */
839 
840   std::list<candidate_extension *> extensions;
841 
842   auto_sbitmap target_isa_unsatisfied (isa_num_bits);
843   bitmap_copy (target_isa_unsatisfied, target_isa);
844 
845   sbitmap isa_bits = NULL;
846   for (const cpu_arch_extension *cand = selected_arch->common.extensions;
847        cand->name != NULL;
848        cand++)
849     {
850       if (cand->remove || cand->alias)
851 	continue;
852 
853       if (isa_bits == NULL)
854 	isa_bits = sbitmap_alloc (isa_num_bits);
855 
856       arm_initialize_isa (isa_bits, cand->isa_bits);
857       if (bitmap_subset_p (isa_bits, target_isa))
858 	{
859 	  extensions.push_back (new candidate_extension (cand, isa_bits));
860 	  bitmap_and_compl (target_isa_unsatisfied, target_isa_unsatisfied,
861 			    isa_bits);
862 	  isa_bits = NULL;
863 	}
864     }
865 
866   /* There's one extra case to consider, which is that the user has
867      specified an FPU that is less capable than this architecture
868      supports.  In that case the code above will fail to find a
869      suitable feature.  We handle this by scanning the list of options
870      again, matching the first option that provides an FPU that is
871      more capable than the selected FPU.
872 
873      Note that the other case (user specified a more capable FPU than
874      this architecture supports) should end up selecting the most
875      capable FPU variant that we do support.  This is sufficient for
876      multilib selection.  */
877 
878   if (bitmap_bit_p (target_isa_unsatisfied, isa_bit_vfpv2)
879       && bitmap_bit_p (fpu_isa, isa_bit_vfpv2))
880     {
881       std::list<candidate_extension *>::iterator ipoint = extensions.begin ();
882 
883       for (const cpu_arch_extension *cand = selected_arch->common.extensions;
884 	   cand->name != NULL;
885 	   cand++)
886 	{
887 	  if (cand->remove || cand->alias)
888 	    continue;
889 
890 	  if (isa_bits == NULL)
891 	    isa_bits = sbitmap_alloc (isa_num_bits);
892 
893 	  /* We need to keep the features in canonical order, so move the
894 	     insertion point if this feature is a candidate.  */
895 	  if (ipoint != extensions.end ()
896 	      && (*ipoint)->extension == cand)
897 	    ++ipoint;
898 
899 	  arm_initialize_isa (isa_bits, cand->isa_bits);
900 	  if (bitmap_subset_p (fpu_isa, isa_bits))
901 	    {
902 	      extensions.insert (ipoint,
903 				 new candidate_extension (cand, isa_bits));
904 	      isa_bits = NULL;
905 	      break;
906 	    }
907 	}
908     }
909 
910   if (isa_bits)
911     sbitmap_free (isa_bits);
912 
913   bitmap_clear (target_isa);
914   size_t len = 1;
915   for (std::list<candidate_extension *>::reverse_iterator riter
916 	 = extensions.rbegin ();
917        riter != extensions.rend (); ++riter)
918     {
919       if (bitmap_subset_p ((*riter)->isa_bits, target_isa))
920 	(*riter)->required = false;
921       else
922 	{
923 	  bitmap_ior (target_isa, target_isa, (*riter)->isa_bits);
924 	  len += strlen ((*riter)->extension->name) + 1;
925 	}
926     }
927 
928   canonical_arch
929     = (char *) xmalloc (len + strlen (selected_arch->common.name));
930 
931   strcpy (canonical_arch, selected_arch->common.name);
932 
933   for (std::list<candidate_extension *>::iterator iter = extensions.begin ();
934        iter != extensions.end (); ++iter)
935     {
936       if ((*iter)->required)
937 	{
938 	  strcat (canonical_arch, "+");
939 	  strcat (canonical_arch, (*iter)->extension->name);
940 	}
941       delete (*iter);
942     }
943 
944   return canonical_arch;
945 }
946 
947 /* If building big-endian on a BE8 target generate a --be8 option for
948    the linker.  Takes four types of option: "little" - little-endian;
949    "big" - big-endian; "be8" - force be8 iff big-endian; and "arch"
950    "<arch-name>" (two arguments) - the target architecture.  The
951    parameter names are generated by the driver from the command-line
952    options.  */
953 const char *
arm_be8_option(int argc,const char ** argv)954 arm_be8_option (int argc, const char **argv)
955 {
956   int endian = TARGET_ENDIAN_DEFAULT;
957   const char *arch = NULL;
958   int arg;
959   bool force = false;
960 
961   for (arg = 0; arg < argc; arg++)
962     {
963       if (strcmp (argv[arg], "little") == 0)
964 	endian = 0;
965       else if (strcmp (argv[arg], "big") == 0)
966 	endian = 1;
967       else if (strcmp (argv[arg], "be8") == 0)
968 	force = true;
969       else if (strcmp (argv[arg], "arch") == 0)
970 	{
971 	  arg++;
972 	  gcc_assert (arg < argc);
973 	  arch = argv[arg];
974 	}
975       else
976 	gcc_unreachable ();
977     }
978 
979   /* Little endian - no be8 option.  */
980   if (!endian)
981     return "";
982 
983   if (force)
984     return "--be8";
985 
986   /* Arch might not be set iff arm_canon_arch (above) detected an
987      error.  Do nothing in that case.  */
988   if (!arch)
989     return "";
990 
991   const arch_option *selected_arch
992     = arm_parse_arch_option_name (all_architectures, "-march", arch);
993 
994   /* Similarly if the given arch option was itself invalid.  */
995   if (!selected_arch)
996     return "";
997 
998   if (check_isa_bits_for (selected_arch->common.isa_bits, isa_bit_be8))
999     return "--be8";
1000 
1001   return "";
1002 }
1003 
1004 /* Generate a -mfpu= option for passing to the assembler.  This is
1005    only called when -mfpu was set (possibly defaulted) to auto and is
1006    needed to ensure that the assembler knows the correct FPU to use.
1007    It wouldn't really be needed except that the compiler can be used
1008    to invoke the assembler directly on hand-written files that lack
1009    the necessary internal .fpu directives.  We assume that the architecture
1010    canonicalization calls have already been made so that we have a final
1011    -march= option to derive the fpu from.  */
1012 const char*
arm_asm_auto_mfpu(int argc,const char ** argv)1013 arm_asm_auto_mfpu (int argc, const char **argv)
1014 {
1015   static char *auto_fpu = NULL;
1016   const char *arch = NULL;
1017   static const enum isa_feature fpu_bitlist[]
1018     = { ISA_ALL_FPU_INTERNAL, isa_nobit };
1019   const arch_option *selected_arch;
1020   static const char* fpuname = "softvfp";
1021 
1022   /* Handle multiple calls to this routine.  */
1023   if (auto_fpu)
1024     {
1025       free (auto_fpu);
1026       auto_fpu = NULL;
1027     }
1028 
1029   while (argc)
1030     {
1031       if (strcmp (argv[0], "arch") == 0)
1032 	arch = argv[1];
1033       else
1034 	fatal_error (input_location,
1035 		     "unrecognized operand to %%:asm_auto_mfpu");
1036       argc -= 2;
1037       argv += 2;
1038     }
1039 
1040   auto_sbitmap target_isa (isa_num_bits);
1041   auto_sbitmap fpubits (isa_num_bits);
1042 
1043   gcc_assert (arch != NULL);
1044   selected_arch = arm_parse_arch_option_name (all_architectures,
1045 					      "-march", arch);
1046   if (selected_arch == NULL)
1047     return "";
1048 
1049   arm_initialize_isa (target_isa, selected_arch->common.isa_bits);
1050   arm_parse_option_features (target_isa, &selected_arch->common,
1051 			     strchr (arch, '+'));
1052   arm_initialize_isa (fpubits, fpu_bitlist);
1053 
1054   bitmap_and (fpubits, fpubits, target_isa);
1055 
1056   /* The logic below is essentially identical to that in
1057      arm.c:arm_identify_fpu_from_isa(), but that only works in the main
1058      part of the compiler.  */
1059 
1060   /* If there are no FPU capability bits, we just pass -mfpu=softvfp.  */
1061   if (!bitmap_empty_p (fpubits))
1062     {
1063       unsigned int i;
1064       auto_sbitmap cand_fpubits (isa_num_bits);
1065       for (i = 0; i < TARGET_FPU_auto; i++)
1066 	{
1067 	  arm_initialize_isa (cand_fpubits, all_fpus[i].isa_bits);
1068 	  if (bitmap_equal_p (fpubits, cand_fpubits))
1069 	    {
1070 	      fpuname = all_fpus[i].name;
1071 	      break;
1072 	    }
1073 	}
1074 
1075       gcc_assert (i != TARGET_FPU_auto
1076 		  || bitmap_bit_p (target_isa, isa_bit_vfp_base));
1077     }
1078 
1079   auto_fpu = (char *) xmalloc (strlen (fpuname) + sizeof ("-mfpu="));
1080   strcpy (auto_fpu, "-mfpu=");
1081   strcat (auto_fpu, fpuname);
1082   return auto_fpu;
1083 }
1084 
1085 #undef ARM_CPU_NAME_LENGTH
1086 
1087 
1088 #undef  TARGET_DEFAULT_TARGET_FLAGS
1089 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
1090 
1091 #undef  TARGET_OPTION_OPTIMIZATION_TABLE
1092 #define TARGET_OPTION_OPTIMIZATION_TABLE arm_option_optimization_table
1093 
1094 #undef TARGET_EXCEPT_UNWIND_INFO
1095 #define TARGET_EXCEPT_UNWIND_INFO  arm_except_unwind_info
1096 
1097 struct gcc_targetm_common targetm_common = TARGETM_COMMON_INITIALIZER;
1098 
1099 /* Returns a canonical representation of the -march option from the current
1100    -march string (if given) and other options on the command line that might
1101    affect the architecture.  */
1102 const char *
arm_canon_arch_option(int argc,const char ** argv)1103 arm_canon_arch_option (int argc, const char **argv)
1104 {
1105   return arm_canon_arch_option_1 (argc, argv, false);
1106 }
1107 
1108 /* Returns a canonical representation of the -mlibarch option from the current
1109    -march string (if given) and other options on the command line that might
1110    affect the architecture after removing the compiler extension options which
1111    are not required for multilib linking.  */
1112 const char *
arm_canon_arch_multilib_option(int argc,const char ** argv)1113 arm_canon_arch_multilib_option (int argc, const char **argv)
1114 {
1115   return arm_canon_arch_option_1 (argc, argv, true);
1116 }
1117