Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / gcc / config / arm / driver-arm.c
1 /* Subroutines for the gcc driver.
2    Copyright (C) 2011-2013 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
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC 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 GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "configargs.h"
25
26 struct vendor_cpu {
27   const char *part_no;
28   const char *arch_name;
29   const char *cpu_name;
30 };
31
32 static struct vendor_cpu arm_cpu_table[] = {
33     {"0x926", "armv5te", "arm926ej-s"},
34     {"0xa26", "armv5te", "arm1026ej-s"},
35     {"0xb02", "armv6k", "mpcore"},
36     {"0xb36", "armv6j", "arm1136j-s"},
37     {"0xb56", "armv6t2", "arm1156t2-s"},
38     {"0xb76", "armv6zk", "arm1176jz-s"},
39     {"0xc05", "armv7-a", "cortex-a5"},
40     {"0xc07", "armv7-a", "cortex-a7"},
41     {"0xc08", "armv7-a", "cortex-a8"},
42     {"0xc09", "armv7-a", "cortex-a9"},
43     {"0xc0f", "armv7-a", "cortex-a15"},
44     {"0xc14", "armv7-r", "cortex-r4"},
45     {"0xc15", "armv7-r", "cortex-r5"},
46     {"0xc20", "armv6-m", "cortex-m0"},
47     {"0xc21", "armv6-m", "cortex-m1"},
48     {"0xc23", "armv7-m", "cortex-m3"},
49     {"0xc24", "armv7e-m", "cortex-m4"},
50     {NULL, NULL, NULL}
51 };
52
53 static struct {
54   const char *vendor_no;
55   const struct vendor_cpu *vendor_parts;
56 } vendors[] = {
57     {"0x41", arm_cpu_table},
58     {NULL, NULL}
59 };
60
61 /* This will be called by the spec parser in gcc.c when it sees
62    a %:local_cpu_detect(args) construct.  Currently it will be called
63    with either "arch", "cpu" or "tune" as argument depending on if
64    -march=native, -mcpu=native or -mtune=native is to be substituted.
65
66    It returns a string containing new command line parameters to be
67    put at the place of the above two options, depending on what CPU
68    this is executed.  E.g. "-march=armv7-a" on a Cortex-A8 for
69    -march=native.  If the routine can't detect a known processor,
70    the -march or -mtune option is discarded.
71
72    ARGC and ARGV are set depending on the actual arguments given
73    in the spec.  */
74 const char *
75 host_detect_local_cpu (int argc, const char **argv)
76 {
77   const char *val = NULL;
78   char buf[128];
79   FILE *f = NULL;
80   bool arch;
81   const struct vendor_cpu *cpu_table = NULL;
82
83   if (argc < 1)
84     goto not_found;
85
86   arch = strcmp (argv[0], "arch") == 0;
87   if (!arch && strcmp (argv[0], "cpu") != 0 && strcmp (argv[0], "tune"))
88     goto not_found;
89
90   f = fopen ("/proc/cpuinfo", "r");
91   if (f == NULL)
92     goto not_found;
93
94   while (fgets (buf, sizeof (buf), f) != NULL)
95     {
96       /* Ensure that CPU implementer is ARM (0x41).  */
97       if (strncmp (buf, "CPU implementer", sizeof ("CPU implementer") - 1) == 0)
98         {
99           int i;
100           for (i = 0; vendors[i].vendor_no != NULL; i++)
101             if (strstr (buf, vendors[i].vendor_no) != NULL)
102               {
103                 cpu_table = vendors[i].vendor_parts;
104                 break;
105               }
106         }
107
108       /* Detect arch/cpu.  */
109       if (strncmp (buf, "CPU part", sizeof ("CPU part") - 1) == 0)
110         {
111           int i;
112
113           if (cpu_table == NULL)
114             goto not_found;
115
116           for (i = 0; cpu_table[i].part_no != NULL; i++)
117             if (strstr (buf, cpu_table[i].part_no) != NULL)
118               {
119                 val = arch ? cpu_table[i].arch_name : cpu_table[i].cpu_name;
120                 break;
121               }
122           break;
123         }
124     }
125
126   fclose (f);
127
128   if (val == NULL)
129     goto not_found;
130
131   return concat ("-m", argv[0], "=", val, NULL);
132
133 not_found:
134   {
135     unsigned int i;
136     unsigned int opt;
137     const char *search[] = {NULL, "arch"};
138
139     if (f)
140       fclose (f);
141
142     search[0] = argv[0];
143     for (opt = 0; opt < ARRAY_SIZE (search); opt++)
144       for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
145         if (strcmp (configure_default_options[i].name, search[opt]) == 0)
146           return concat ("-m", search[opt], "=",
147                          configure_default_options[i].value, NULL);
148     return NULL;
149   }
150 }