forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverilog_preprocessor.cc
308 lines (279 loc) · 11.7 KB
/
verilog_preprocessor.cc
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2017-2022 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/flags/usage.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "common/text/token_stream_view.h"
#include "common/util/file_util.h"
#include "common/util/init_command_line.h"
#include "common/util/status_macros.h"
#include "common/util/subcommand.h"
#include "verilog/analysis/flow_tree.h"
#include "verilog/analysis/verilog_filelist.h"
#include "verilog/analysis/verilog_project.h"
#include "verilog/parser/verilog_lexer.h"
#include "verilog/preprocessor/verilog_preprocess.h"
#include "verilog/transform/strip_comments.h"
using verible::SubcommandArgsRange;
using verible::SubcommandEntry;
using FileOpener = verilog::VerilogPreprocess::FileOpener;
// TODO(karimtera): Add a boolean flag to configure the macro expansion.
ABSL_FLAG(int, limit_variants, 20, "Maximum number of variants printed");
static absl::Status StripComments(const SubcommandArgsRange& args,
std::istream&, std::ostream& outs,
std::ostream&) {
// Parse the arguments into a FileList.
std::vector<absl::string_view> cmdline_args(args.begin(), args.end());
verilog::FileList file_list;
RETURN_IF_ERROR(
verilog::AppendFileListFromCommandline(cmdline_args, &file_list));
const auto& files = file_list.file_paths;
if (files.empty()) {
return absl::InvalidArgumentError(
"Missing file argument. Use '-' for stdin.");
}
const absl::string_view source_file = files[0];
absl::StatusOr<std::string> source_contents_or =
verible::file::GetContentAsString(source_file);
if (!source_contents_or.ok()) {
return source_contents_or.status();
}
char replace_char;
if (args.size() == 1) {
replace_char = ' ';
} else if (args.size() == 2) {
absl::string_view replace_str(args[1]);
if (replace_str.empty()) {
replace_char = '\0';
} else if (replace_str.length() == 1) {
replace_char = replace_str[0];
} else {
return absl::InvalidArgumentError(
"Replacement must be a single character.");
}
} else {
return absl::InvalidArgumentError("Too many arguments.");
}
verilog::StripVerilogComments(*source_contents_or, &outs, replace_char);
return absl::OkStatus();
}
static absl::Status PreprocessSingleFile(
absl::string_view source_file,
const verilog::FileList::PreprocessingInfo& preprocessing_info,
std::ostream& outs, std::ostream& message_stream) {
absl::StatusOr<std::string> source_contents_or =
verible::file::GetContentAsString(source_file);
if (!source_contents_or.ok()) {
message_stream << source_file << source_contents_or.status();
return source_contents_or.status();
}
verilog::VerilogPreprocess::Config config;
config.filter_branches = true;
config.include_files = true;
config.expand_macros = true;
verilog::VerilogProject project(".", preprocessing_info.include_dirs);
FileOpener file_opener =
[&project](
absl::string_view filename) -> absl::StatusOr<absl::string_view> {
auto result = project.OpenIncludedFile(filename);
if (!result.status().ok()) return result.status();
return (*result)->GetContent();
};
verilog::VerilogPreprocess preprocessor(config, file_opener);
// Setting the preprocessing info (defines, and incdirs) in the preprocessor.
preprocessor.setPreprocessingInfo(preprocessing_info);
verilog::VerilogLexer lexer(*source_contents_or);
verible::TokenSequence lexed_sequence;
for (lexer.DoNextToken(); !lexer.GetLastToken().isEOF();
lexer.DoNextToken()) {
// For now we will store the syntax tree tokens only, ignoring all the
// white-space characters. however that should be stored to output the
// source code just like it was, but with conditionals filtered.
lexed_sequence.push_back(lexer.GetLastToken());
}
verible::TokenStreamView lexed_streamview;
// Initializing the lexed token stream view.
InitTokenStreamView(lexed_sequence, &lexed_streamview);
verilog::VerilogPreprocessData preprocessed_data =
preprocessor.ScanStream(lexed_streamview);
auto& preprocessed_stream = preprocessed_data.preprocessed_token_stream;
for (auto u : preprocessed_stream) outs << u->text();
for (auto& u : preprocessed_data.errors) outs << u.error_message << '\n';
if (!preprocessed_data.errors.empty()) {
return absl::InvalidArgumentError("Error: The preprocessing has failed.");
}
return absl::OkStatus();
}
static absl::Status MultipleCU(const SubcommandArgsRange& args, std::istream&,
std::ostream& outs,
std::ostream& message_stream) {
// Parse the arguments into a FileList.
std::vector<absl::string_view> cmdline_args(args.begin(), args.end());
verilog::FileList file_list;
RETURN_IF_ERROR(
verilog::AppendFileListFromCommandline(cmdline_args, &file_list));
const auto& files = file_list.file_paths;
auto& preprocessing_info = file_list.preprocessing;
// TODO(karimtera): allow including files with absolute paths.
// This is a hacky solution for now.
preprocessing_info.include_dirs.emplace_back("/");
if (files.empty()) {
return absl::InvalidArgumentError("ERROR: Missing file argument.");
}
for (const absl::string_view source_file : files) {
RETURN_IF_ERROR(PreprocessSingleFile(source_file, preprocessing_info, outs,
message_stream));
}
return absl::OkStatus();
}
static absl::Status GenerateVariants(const SubcommandArgsRange& args,
std::istream&, std::ostream& outs,
std::ostream& message_stream) {
// Parse the arguments into a FileList.
std::vector<absl::string_view> cmdline_args(args.begin(), args.end());
verilog::FileList file_list;
RETURN_IF_ERROR(
verilog::AppendFileListFromCommandline(cmdline_args, &file_list));
const auto& files = file_list.file_paths;
// TODO(karimtera): Pass the +define's to the preprocessor, and only
// generate variants with theses defines fixed.
const int limit_variants = absl::GetFlag(FLAGS_limit_variants);
if (files.empty()) {
return absl::InvalidArgumentError("ERROR: Missing file argument.");
}
if (files.size() > 1) {
return absl::InvalidArgumentError(
"ERROR: generate-variants only works on one file.");
}
const auto& source_file = files[0];
absl::StatusOr<std::string> source_contents_or =
verible::file::GetContentAsString(source_file);
if (!source_contents_or.ok()) {
message_stream << source_file << source_contents_or.status();
return source_contents_or.status();
}
// Lexing the input SV source code.
verilog::VerilogLexer lexer(*source_contents_or);
verible::TokenSequence lexed_sequence;
for (lexer.DoNextToken(); !lexer.GetLastToken().isEOF();
lexer.DoNextToken()) {
// For now we will store the syntax tree tokens only, ignoring all the
// white-space characters. however that should be stored to output the
// source code just like it was.
if (verilog::VerilogLexer::KeepSyntaxTreeTokens(lexer.GetLastToken())) {
lexed_sequence.push_back(lexer.GetLastToken());
}
}
// Control flow tree constructing.
verilog::FlowTree control_flow_tree(lexed_sequence);
int counter = 0;
return control_flow_tree.GenerateVariants(
[limit_variants, &outs, &message_stream,
&counter](const verilog::FlowTree::Variant& variant) {
if (counter == limit_variants) return false;
counter++;
message_stream << "Variant number " << counter << ":\n";
for (auto token : variant.sequence) outs << token << '\n';
// TODO(karimtera): Consider creating an output file per vairant,
// Such that the files naming reflects which defines are
// defined/undefined.
return true;
});
}
static const std::pair<absl::string_view, SubcommandEntry> kCommands[] = {
{"preprocess",
{&MultipleCU,
R"(preprocess [define-include-flags] file [file...]
Inputs:
Accepts one or more Verilog or SystemVerilog source files to preprocess.
Each one of them will be prepropcessed independently which means that
declaration scopes will end by the end of each file, and won't be seen from
other files (so multiple files will _not_ be treated as compilation unit).
The +define+ and +include+ directives on the commandline are honored by
the preprocessor.
Output: (stdout)
The preprocessed files content (same contents with directives interpreted)
will be written to stdout, concatenated.
)"}},
{"strip-comments",
{&StripComments,
R"(strip-comments file [replacement-char]
Inputs:
'file' is a Verilog or SystemVerilog source file.
Use '-' to read from stdin.
'replacement-char' is a character to replace comments with.
If not given, or given as a single space character, the comment contents and
delimiters are replaced with spaces.
If an empty string, the comment contents and delimiters are deleted. Newlines
are not deleted.
If a single character, the comment contents are replaced with the character.
Output: (stdout)
Contents of original file with // and /**/ comments removed.
)"}},
{"generate-variants",
{&GenerateVariants,
R"(generate-variants file [-limit_variants number]
Inputs:
'file' is a Verilog or SystemVerilog source file.
'-limit_variants' flag limits variants to 'number' (20 by default).
Output: (stdout)
Generates every possible variant of `ifdef blocks considering the
conditional directives.
)"}},
// TODO(karimtera): We can add another argument to `generate-variants`,
// Which allows us to set some defines, as if we are only interested
// in the variants in which these defines are set.
// TODO(karimtera): Another candidate subcommand is `list-defines`,
// Which would be the output of `GetUsedMacros()`.
};
int main(int argc, char* argv[]) {
// Create a registry of subcommands (locally, rather than as a static global).
verible::SubcommandRegistry commands;
for (const auto& entry : kCommands) {
const auto status = commands.RegisterCommand(entry.first, entry.second);
if (!status.ok()) {
std::cerr << status.message() << std::endl;
return 2; // fatal error
}
}
const std::string usage = absl::StrCat("usage: ", argv[0],
" command args...\n"
"available commands:\n",
commands.ListCommands());
// Process invocation args.
const auto args = verible::InitCommandLine(usage, &argc, &argv);
if (args.size() == 1) {
std::cerr << absl::ProgramUsageMessage() << std::endl;
return 1;
}
// args[0] is the program name
// args[1] is the subcommand
// subcommand args start at [2]
const SubcommandArgsRange command_args(args.cbegin() + 2, args.cend());
const auto& sub = commands.GetSubcommandEntry(args[1]);
// Run the subcommand.
const auto status = sub.main(command_args, std::cin, std::cout, std::cerr);
if (!status.ok()) {
std::cerr << status.message() << std::endl;
return 1;
}
return 0;
}