]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/riscv/multilib-generator
Update copyright years.
[thirdparty/gcc.git] / gcc / config / riscv / multilib-generator
1 #!/usr/bin/env python
2
3 # RISC-V multilib list generator.
4 # Copyright (C) 2011-2022 Free Software Foundation, Inc.
5 # Contributed by Andrew Waterman (andrew@sifive.com).
6 #
7 # This file is part of GCC.
8 #
9 # GCC is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
12 # any later version.
13 #
14 # GCC is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with GCC; see the file COPYING3. If not see
21 # <http://www.gnu.org/licenses/>.
22
23 # Each argument to this script is of the form
24 # <primary arch>-<abi>-<additional arches>-<extensions>
25 # Example 1:
26 # rv32imafd-ilp32d-rv32g-c,v
27 # means that, in addition to rv32imafd, these configurations can also use the
28 # rv32imafd-ilp32d libraries: rv32imafdc, rv32imafdv, rv32g, rv32gc, rv32gv
29 #
30 # Example 2:
31 # rv32imafd-ilp32d--c*b
32 # means that, in addition to rv32imafd, these configurations can also use the
33 # rv32imafd-ilp32d libraries: rv32imafdc-ilp32d, rv32imafdb-ilp32d,
34 # rv32imafdcb-ilp32d
35
36 from __future__ import print_function
37 import sys
38 import os
39 import collections
40 import itertools
41 from functools import reduce
42 import subprocess
43 import argparse
44
45 #
46 # TODO: Add test for this script.
47 #
48
49 arches = collections.OrderedDict()
50 abis = collections.OrderedDict()
51 required = []
52 reuse = []
53
54 def arch_canonicalize(arch):
55 this_file = os.path.abspath(os.path.join( __file__))
56 arch_can_script = \
57 os.path.join(os.path.dirname(this_file), "arch-canonicalize")
58 proc = subprocess.Popen([sys.executable, arch_can_script, arch],
59 stdout=subprocess.PIPE)
60 out, err = proc.communicate()
61 return out.decode().strip()
62
63 #
64 # Handle expansion operation.
65 #
66 # e.g. "a*b" -> [("a",), ("b",), ("a", "b")]
67 # "a" -> [("a",)]
68 #
69 def _expand_combination(ext):
70 exts = list(ext.split("*"))
71
72 # Add underline to every extension.
73 # e.g.
74 # _b * zvamo => _b * _zvamo
75 exts = list(map(lambda x: '_' + x, exts))
76
77 # No need to expand if there is no `*`.
78 if len(exts) == 1:
79 return [(exts[0],)]
80
81 # Generate combination!
82 ext_combs = []
83 for comb_len in range(1, len(exts)+1):
84 for ext_comb in itertools.combinations(exts, comb_len):
85 ext_combs.append(ext_comb)
86
87 return ext_combs
88
89 #
90 # Input a list and drop duplicated entry.
91 # e.g.
92 # ["a", "b", "ab", "a"] -> ["a", "b", "ab"]
93 #
94 def unique(x):
95 #
96 # Drop duplicated entry.
97 # Convert list to set and then convert back to list.
98 #
99 # Add sorted to prevent non-deterministic results in different env.
100 #
101 return list(sorted(list(set(x))))
102
103 #
104 # Expand EXT string if there is any expansion operator (*).
105 # e.g.
106 # "a*b,c" -> ["a", "b", "ab", "c"]
107 #
108 def expand_combination(ext):
109 ext = list(filter(None, ext.split(',')))
110
111 # Expand combination for EXT, got lots of list.
112 # e.g.
113 # a * b => [[("a",), ("b",)], [("a", "b")]]
114 ext_combs = list(map(_expand_combination, ext))
115
116 # Then fold to single list.
117 # e.g.
118 # [[("a",), ("b",)], [("a", "b")]] => [("a",), ("b",), ("a", "b")]
119 ext = list(reduce(lambda x, y: x + y, ext_combs, []))
120
121 # Fold the tuple to string.
122 # e.g.
123 # [("a",), ("b",), ("a", "b")] => ["a", "b", "ab"]
124 ext = map(lambda e : reduce(lambda x, y: x + y, e), ext)
125
126 # Drop duplicated entry.
127 ext = unique(ext)
128
129 return ext
130
131 multilib_cfgs = filter(lambda x:not x.startswith("--"), sys.argv[1:])
132 options = filter(lambda x:x.startswith("--"), sys.argv[1:])
133
134 parser = argparse.ArgumentParser()
135 parser.add_argument("--cmodel", type=str)
136 parser.add_argument("cfgs", type=str, nargs='*')
137 args = parser.parse_args()
138
139 if args.cmodel:
140 cmodels = [None] + args.cmodel.split(",")
141 else:
142 cmodels = [None]
143
144 cmodel_options = '/'.join(['mcmodel=%s' % x for x in cmodels[1:]])
145 cmodel_dirnames = ' \\\n'.join(cmodels[1:])
146
147 for cmodel in cmodels:
148 for cfg in args.cfgs:
149 try:
150 (arch, abi, extra, ext) = cfg.split('-')
151 except:
152 print ("Invalid configure string %s, <arch>-<abi>-<extra>-<extensions>\n"
153 "<extra> and <extensions> can be empty, "
154 "e.g. rv32imafd-ilp32--" % cfg)
155 sys.exit(1)
156
157 # Compact code model only support rv64.
158 if cmodel == "compact" and arch.startswith("rv32"):
159 continue
160
161 arch = arch_canonicalize (arch)
162 arches[arch] = 1
163 abis[abi] = 1
164 extra = list(filter(None, extra.split(',')))
165 ext_combs = expand_combination(ext)
166 alts = sum([[x] + [x + y for y in ext_combs] for x in [arch] + extra], [])
167 alts = list(map(arch_canonicalize, alts))
168
169 # Drop duplicated entry.
170 alts = unique(alts)
171
172 for alt in alts[1:]:
173 if alt == arch:
174 continue
175 arches[alt] = 1
176 reuse.append('march.%s/mabi.%s=march.%s/mabi.%s' % (arch, abi, alt, abi))
177
178 if cmodel:
179 required.append('march=%s/mabi=%s/mcmodel=%s' % (arch, abi, cmodel))
180 else:
181 required.append('march=%s/mabi=%s' % (arch, abi))
182
183 arch_options = '/'.join(['march=%s' % x for x in arches.keys()])
184 arch_dirnames = ' \\\n'.join(arches.keys())
185
186 abi_options = '/'.join(['mabi=%s' % x for x in abis.keys()])
187 abi_dirnames = ' \\\n'.join(abis.keys())
188
189 prog = sys.argv[0].split('/')[-1]
190 print('# This file was generated by %s with the command:' % prog)
191 print('# %s' % ' '.join(sys.argv))
192
193 print('MULTILIB_OPTIONS = %s %s %s' % (arch_options, abi_options, cmodel_options))
194 print('MULTILIB_DIRNAMES = %s %s %s' % (arch_dirnames, abi_dirnames, cmodel_dirnames))
195 print('MULTILIB_REQUIRED = %s' % ' \\\n'.join(required))
196 print('MULTILIB_REUSE = %s' % ' \\\n'.join(reuse))