1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/CodeGen/CommandFlags.h"
#include <optional>
#include <string>
#include <utility>
namespace rcc {
namespace {
[[nodiscard]] Expected<std::optional<std::string_view>>
takeValue(std::span<const std::string_view> Arguments, std::size_t &Index, std::string_view Name) {
const std::string_view Argument = Arguments[Index];
const std::string LongName =
Name.starts_with('-') && !Name.starts_with("--") ? "-" + std::string(Name) : std::string();
if (Argument == Name || (!LongName.empty() && Argument == LongName)) {
if (Index + 1U >= Arguments.size()) {
return makeUnexpected(ErrorCode::InvalidInvocation,
"missing value for command-line option '" + std::string(Name) + "'");
}
return Arguments[++Index];
}
std::string Prefix(Name);
Prefix.push_back('=');
if (Argument.starts_with(Prefix))
return Argument.substr(Prefix.size());
if (!LongName.empty()) {
std::string LongPrefix(LongName);
LongPrefix.push_back('=');
if (Argument.starts_with(LongPrefix))
return Argument.substr(LongPrefix.size());
}
return std::nullopt;
}
template <typename ValueT, typename ParserT>
[[nodiscard]] Expected<bool> parseValueFlag(std::span<const std::string_view> Arguments,
std::size_t &Index, std::string_view Name,
std::optional<ValueT> &Destination, ParserT Parser) {
auto Value = takeValue(Arguments, Index, Name);
if (!Value)
return std::unexpected(Value.error());
if (!*Value)
return false;
auto Parsed = Parser(**Value);
if (!Parsed)
return std::unexpected(Parsed.error());
Destination = std::move(*Parsed);
return true;
}
} // namespace
std::string_view spelling(RelocationModel Value) noexcept {
switch (Value) {
case RelocationModel::Static:
return "static";
case RelocationModel::PositionIndependent:
return "pic";
}
return "invalid";
}
std::string_view spelling(CodeModel Value) noexcept {
switch (Value) {
case CodeModel::Small:
return "small";
case CodeModel::Medium:
return "medium";
case CodeModel::Large:
return "large";
}
return "invalid";
}
std::string_view spelling(OptimizationLevel Value) noexcept {
switch (Value) {
case OptimizationLevel::O0:
return "O0";
case OptimizationLevel::O1:
return "O1";
case OptimizationLevel::O2:
return "O2";
case OptimizationLevel::O3:
return "O3";
case OptimizationLevel::Os:
return "Os";
case OptimizationLevel::Oz:
return "Oz";
}
return "invalid";
}
std::string_view spelling(CodeGenFileType Value) noexcept {
switch (Value) {
case CodeGenFileType::AssemblyFile:
return "asm";
case CodeGenFileType::ObjectFile:
return "obj";
case CodeGenFileType::Null:
return "null";
}
return "invalid";
}
Expected<RelocationModel> parseRelocationModel(std::string_view Value) {
if (Value == "static")
return RelocationModel::Static;
if (Value == "pic")
return RelocationModel::PositionIndependent;
return makeUnexpected(ErrorCode::InvalidInvocation, "unknown relocation model");
}
Expected<CodeModel> parseCodeModel(std::string_view Value) {
if (Value == "small")
return CodeModel::Small;
if (Value == "medium")
return CodeModel::Medium;
if (Value == "large")
return CodeModel::Large;
return makeUnexpected(ErrorCode::InvalidInvocation, "unknown code model");
}
Expected<OptimizationLevel> parseOptimizationLevel(std::string_view Value) {
if (Value == "O0")
return OptimizationLevel::O0;
if (Value == "O1")
return OptimizationLevel::O1;
if (Value == "O2")
return OptimizationLevel::O2;
if (Value == "O3")
return OptimizationLevel::O3;
if (Value == "Os")
return OptimizationLevel::Os;
if (Value == "Oz")
return OptimizationLevel::Oz;
return makeUnexpected(ErrorCode::InvalidInvocation, "unknown optimization level");
}
Expected<CodeGenFileType> parseCodeGenFileType(std::string_view Value) {
if (Value == "asm")
return CodeGenFileType::AssemblyFile;
if (Value == "obj")
return CodeGenFileType::ObjectFile;
if (Value == "null")
return CodeGenFileType::Null;
return makeUnexpected(ErrorCode::InvalidInvocation, "unknown file type");
}
Expected<bool> parseCodeGenCommandFlag(std::span<const std::string_view> Arguments,
std::size_t &Index, CodeGenCommandFlags &Flags) {
const std::string_view Argument = Arguments[Index];
const std::string_view Optimization = Argument.starts_with("--") ? Argument.substr(2)
: Argument.starts_with('-') ? Argument.substr(1)
: std::string_view{};
if ((Argument.starts_with('-') && !Argument.starts_with("---")) &&
(Optimization == "O0" || Optimization == "O1" || Optimization == "O2" ||
Optimization == "O3" || Optimization == "Os" || Optimization == "Oz")) {
auto Level = parseOptimizationLevel(Optimization);
if (!Level)
return std::unexpected(Level.error());
Flags.Optimization = *Level;
return true;
}
const auto ParseString = [&](std::string_view Name,
std::optional<std::string> &Destination) -> Expected<bool> {
auto Value = takeValue(Arguments, Index, Name);
if (!Value)
return std::unexpected(Value.error());
if (!*Value)
return false;
Destination = std::string(**Value);
return true;
};
for (const auto &[Name, Destination] :
{std::pair{"-mtriple", &Flags.Triple}, std::pair{"-mcpu", &Flags.CPU},
std::pair{"-mtune", &Flags.TuneCPU}, std::pair{"-target-abi", &Flags.ABI}}) {
auto Parsed = ParseString(Name, *Destination);
if (!Parsed)
return std::unexpected(Parsed.error());
if (*Parsed)
return true;
}
auto Features = takeValue(Arguments, Index, "-mattr");
if (!Features)
return std::unexpected(Features.error());
if (*Features) {
std::size_t Begin = 0;
while (Begin <= (**Features).size()) {
const std::size_t Comma = (**Features).find(',', Begin);
const std::size_t End = Comma == std::string_view::npos ? (**Features).size() : Comma;
if (End == Begin) {
return makeUnexpected(ErrorCode::InvalidInvocation,
"target feature list contains an empty element");
}
Flags.FeatureOverrides.emplace_back((**Features).substr(Begin, End - Begin));
if (Comma == std::string_view::npos)
break;
Begin = Comma + 1U;
}
return true;
}
auto Relocation =
parseValueFlag(Arguments, Index, "-relocation-model", Flags.Relocation, parseRelocationModel);
if (!Relocation)
return std::unexpected(Relocation.error());
if (*Relocation)
return true;
auto Code = parseValueFlag(Arguments, Index, "-code-model", Flags.Code, parseCodeModel);
if (!Code)
return std::unexpected(Code.error());
if (*Code)
return true;
auto FileType = takeValue(Arguments, Index, "-filetype");
if (!FileType)
return std::unexpected(FileType.error());
if (*FileType) {
auto Parsed = parseCodeGenFileType(**FileType);
if (!Parsed)
return std::unexpected(Parsed.error());
Flags.FileType = *Parsed;
Flags.FileTypeWasSpecified = true;
return true;
}
return false;
}
} // namespace rcc
|