PR sanitizer/78106
[official-gcc.git] / gcc / config / sparc / driver-sparc.c
blobea174bf3dc5ccb8ab05e28956b600bbd36cfe761
1 /* Subroutines for the gcc driver.
2 Copyright (C) 2011-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 static const struct cpu_names {
26 const char *const name;
27 const char *const cpu;
28 } cpu_names[] = {
29 #if defined __sun__ && defined __svr4__
30 { "TMS390S10", "supersparc" }, /* Texas Instruments microSPARC I */
31 { "TMS390Z50", "supersparc" }, /* Texas Instruments SuperSPARC I */
32 { "TMS390Z55", "supersparc" }, /* Texas Instruments
33 SuperSPARC I with SuperCache */
34 { "MB86904", "supersparc" }, /* Fujitsu microSPARC II */
35 { "MB86907", "supersparc" }, /* Fujitsu TurboSPARC */
36 { "RT623", "hypersparc" }, /* Ross hyperSPARC */
37 { "RT625", "hypersparc" },
38 { "RT626", "hypersparc" },
39 { "UltraSPARC-I", "ultrasparc" },
40 { "UltraSPARC-II", "ultrasparc" },
41 { "UltraSPARC-IIe", "ultrasparc" },
42 { "UltraSPARC-IIi", "ultrasparc" },
43 { "SPARC64-III", "ultrasparc" },
44 { "SPARC64-IV", "ultrasparc" },
45 { "UltraSPARC-III", "ultrasparc3" },
46 { "UltraSPARC-III+", "ultrasparc3" },
47 { "UltraSPARC-IIIi", "ultrasparc3" },
48 { "UltraSPARC-IIIi+", "ultrasparc3" },
49 { "UltraSPARC-IV", "ultrasparc3" },
50 { "UltraSPARC-IV+", "ultrasparc3" },
51 { "SPARC64-V", "ultrasparc3" },
52 { "SPARC64-VI", "ultrasparc3" },
53 { "SPARC64-VII", "ultrasparc3" },
54 { "UltraSPARC-T1", "niagara" },
55 { "UltraSPARC-T2", "niagara2" },
56 { "UltraSPARC-T2", "niagara2" },
57 { "UltraSPARC-T2+", "niagara2" },
58 { "SPARC-T3", "niagara3" },
59 { "SPARC-T4", "niagara4" },
60 #else
61 { "SuperSparc", "supersparc" },
62 { "HyperSparc", "hypersparc" },
63 { "SpitFire", "ultrasparc" },
64 { "BlackBird", "ultrasparc" },
65 { "Sabre", "ultrasparc" },
66 { "Hummingbird", "ultrasparc" },
67 { "Cheetah", "ultrasparc3" },
68 { "Jalapeno", "ultrasparc3" },
69 { "Jaguar", "ultrasparc3" },
70 { "Panther", "ultrasparc3" },
71 { "Serrano", "ultrasparc3" },
72 { "UltraSparc T1", "niagara" },
73 { "UltraSparc T2", "niagara2" },
74 { "UltraSparc T3", "niagara3" },
75 { "UltraSparc T4", "niagara4" },
76 { "LEON", "leon3" },
77 #endif
78 { "SPARC-M7", "niagara7" },
79 { "SPARC-S7", "niagara7" },
80 { NULL, NULL }
83 #if defined __sun__ && defined __svr4__
84 #include <kstat.h>
85 #endif
87 /* This will be called by the spec parser in gcc.c when it sees
88 a %:local_cpu_detect(args) construct. Currently it will be called
89 with either "cpu" or "tune" as argument depending on if -mcpu=native
90 or -mtune=native is to be substituted.
92 It returns a string containing new command line parameters to be
93 put at the place of the above two options, depending on what CPU
94 this is executed. E.g. "-mcpu=ultrasparc3" on an UltraSPARC III for
95 -mcpu=native. If the routine can't detect a known processor,
96 the -mcpu or -mtune option is discarded.
98 ARGC and ARGV are set depending on the actual arguments given
99 in the spec. */
100 const char *
101 host_detect_local_cpu (int argc, const char **argv)
103 const char *cpu = NULL;
104 #if defined __sun__ && defined __svr4__
105 char *buf = NULL;
106 kstat_ctl_t *kc;
107 kstat_t *ksp;
108 kstat_named_t *brand = NULL;
109 #else
110 char buf[128];
111 FILE *f;
112 #endif
113 int i;
115 if (argc < 1)
116 return NULL;
118 if (strcmp (argv[0], "cpu") && strcmp (argv[0], "tune"))
119 return NULL;
121 #if defined __sun__ && defined __svr4__
122 kc = kstat_open ();
123 if (kc != NULL)
125 ksp = kstat_lookup (kc, CONST_CAST2 (char *, const char *, "cpu_info"),
126 -1, NULL);
127 if (ksp != NULL
128 && kstat_read (kc, ksp, NULL) != -1
129 && ksp->ks_type == KSTAT_TYPE_NAMED)
130 brand = (kstat_named_t *)
131 kstat_data_lookup (ksp, CONST_CAST2 (char *, const char *, "brand"));
132 /* "brand" was only introduced in Solaris 10. */
133 if (brand == NULL)
134 brand = (kstat_named_t *)
135 kstat_data_lookup (ksp, CONST_CAST2 (char *, const char *,
136 "implementation"));
137 /* KSTAT_DATA_STRING was introduced in Solaris 9. */
138 #ifdef KSTAT_DATA_STRING
139 if (brand != NULL && brand->data_type == KSTAT_DATA_STRING)
140 buf = KSTAT_NAMED_STR_PTR (brand);
141 #else
142 if (brand != NULL && brand->data_type == KSTAT_DATA_CHAR)
143 buf = brand->value.c;
144 #endif
146 kstat_close (kc);
148 for (i = 0; cpu_names[i].name != NULL; i++)
149 if (strcmp (buf, cpu_names[i].name) == 0)
150 cpu = cpu_names[i].cpu;
151 #else
152 f = fopen ("/proc/cpuinfo", "r");
153 if (f == NULL)
154 return NULL;
156 while (fgets (buf, sizeof (buf), f) != NULL)
157 if (strncmp (buf, "cpu\t\t:", sizeof ("cpu\t\t:") - 1) == 0)
159 for (i = 0; cpu_names [i].name; i++)
160 if (strstr (buf, cpu_names [i].name) != NULL)
162 cpu = cpu_names [i].cpu;
163 break;
165 break;
168 fclose (f);
169 #endif
171 if (cpu == NULL)
172 return NULL;
174 return concat ("-m", argv[0], "=", cpu, NULL);