Debian Codegraph @

gcc/config/mips/driver-native.cc

raw
1 /* Subroutines for the gcc driver.
2 Copyright (C) 2008-2025 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 #define IN_TARGET_CODE 1
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #ifdef HAVE_SYS_AUXV_H
27 #include <sys/auxv.h>
28 #endif
29
30 /* This will be called by the spec parser in gcc.cc when it sees
31 a %:local_cpu_detect(args) construct. Currently it will be called
32 with either "arch" or "tune" as argument depending on if -march=native
33 or -mtune=native is to be substituted.
34
35 It returns a string containing new command line parameters to be
36 put at the place of the above two options, depending on what CPU
37 this is executed. E.g. "-march=loongson2f" on a Loongson 2F for
38 -march=native. If the routine can't detect a known processor,
39 the -march or -mtune option is discarded.
40
41 ARGC and ARGV are set depending on the actual arguments given
42 in the spec. */
43 const char *
44 host_detect_local_cpu (int argc, const char **argv)
45 {
46 const char *cpu = NULL;
47 /* Don't assigne any static string to ret. If you need to do so,
48 use concat. */
49 char *ret = NULL;
50 char buf[128];
51 FILE *f;
52 bool arch;
53
54 if (argc < 1)
55 return NULL;
56
57 arch = strcmp (argv[0], "arch") == 0;
58 if (!arch && strcmp (argv[0], "tune"))
59 return NULL;
60
61 f = fopen ("/proc/cpuinfo", "r");
62 if (f == NULL)
63 goto fallback_cpu;
64
65 while (fgets (buf, sizeof (buf), f) != NULL)
66 if (startswith (buf, "cpu model"))
67 {
68 if (strstr (buf, "Godson2 V0.2") != NULL
69 || strstr (buf, "Loongson-2 V0.2") != NULL
70 || strstr (buf, "Loongson-2E") != NULL)
71 cpu = "loongson2e";
72 else if (strstr (buf, "Godson2 V0.3") != NULL
73 || strstr (buf, "Loongson-2 V0.3") != NULL
74 || strstr (buf, "Loongson-2F") != NULL)
75 cpu = "loongson2f";
76 else if (strstr (buf, "Godson3 V0.5") != NULL
77 || strstr (buf, "Loongson-3 V0.5") != NULL
78 || strstr (buf, "Loongson-3A") != NULL)
79 cpu = "loongson3a";
80 else if (strstr (buf, "SiByte SB1") != NULL)
81 cpu = "sb1";
82 else if (strstr (buf, "R5000") != NULL)
83 cpu = "r5000";
84 else if (strstr (buf, "Octeon II") != NULL)
85 cpu = "octeon2";
86 else if (strstr (buf, "Octeon") != NULL)
87 cpu = "octeon";
88 break;
89 }
90
91 fclose (f);
92
93 fallback_cpu:
94 #if defined (__mips_nan2008)
95 /* Put the ret to the end of list, since it may be NULL. */
96 if (arch)
97 ret = reconcat (ret, " -mnan=2008 ", ret, NULL);
98 #endif
99
100 #ifdef HAVE_GETAUXVAL
101 if (cpu == NULL)
102 cpu = (const char *) getauxval (AT_BASE_PLATFORM);
103 #endif
104
105 #if defined (_MIPS_ARCH)
106 if (cpu == NULL)
107 cpu = _MIPS_ARCH;
108 #endif
109
110 if (cpu)
111 ret = reconcat (ret, " -m", argv[0], "=", cpu, ret, NULL);
112
113 return ret;
114 }