-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseCommand.java
More file actions
483 lines (415 loc) · 15 KB
/
BaseCommand.java
File metadata and controls
483 lines (415 loc) · 15 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// SPDX-FileCopyrightText : © 2025-2026 TU Wien <vadl@tuwien.ac.at>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package vadl.cli;
import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;
import static picocli.CommandLine.ScopeType.INHERIT;
import com.google.errorprone.annotations.concurrent.LazyInit;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import picocli.CommandLine;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import vadl.ast.Ast;
import vadl.ast.AstDumper;
import vadl.ast.ModelRemover;
import vadl.ast.TypeChecker;
import vadl.ast.Ungrouper;
import vadl.ast.VadlParser;
import vadl.ast.ViamLowering;
import vadl.configuration.DecoderOptions;
import vadl.configuration.DumpMode;
import vadl.configuration.GeneralConfiguration;
import vadl.dump.ArtifactTracker;
import vadl.error.DeferredDiagnosticStore;
import vadl.error.Diagnostic;
import vadl.error.DiagnosticList;
import vadl.error.DiagnosticPrinter;
import vadl.pass.PassManager;
import vadl.pass.PassOrder;
import vadl.pass.exception.DuplicatedPassKeyException;
import vadl.utils.EditorUtils;
import vadl.utils.SourceLocation;
import vadl.viam.Specification;
/**
* A base command from which the actual commands can inherit from.
*/
public abstract class BaseCommand implements Callable<Integer> {
@Nullable
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;
@Parameters(description = "Path to the input VADL specification")
@LazyInit
Path input;
@Option(names = {"-o",
"--output"}, scope = INHERIT, description = "The output directory (default: \"output\")")
Path output = Paths.get("output");
@Option(names = {"-m", "--model"}, scope = INHERIT,
description = "Override the value of an Id macro model.")
@Nullable
Map<String, String> modelOverrides;
@Option(names = {"--dump"},
scope = INHERIT,
arity = "0..1",
fallbackValue = "always",
paramLabel = "<mode>",
description = "Generate all dumps of intermediate representations. "
+ "Valid values: ${COMPLETION-CANDIDATES}",
parameterConsumer = DumpModeConverter.class,
converter = DumpModeConverter.class,
completionCandidates = DumpModeConverter.class)
DumpMode dump = DumpMode.NONE;
@Option(names = "--timings", scope = INHERIT,
description = "Print timings of the phases of the compiler")
boolean showTimings;
@Option(names = "--expand-macros",
scope = INHERIT,
description = "Expand all macros and write them to disk.")
boolean expandMacros;
@Option(names = "--with-stacktrace",
scope = INHERIT,
description = "Debug option to show the OpenVADL stacktrace of an emitted error."
)
boolean showStacktrace;
@Option(names = "--decoder",
split = ",",
scope = INHERIT,
description = "Options for the decoder generation. Valid options are: "
+ "${COMPLETION-CANDIDATES}",
completionCandidates = DecoderOptsConverter.class,
converter = DecoderOptsConverter.class
)
@Nullable
Set<DecoderOpt> decoderOptions = new LinkedHashSet<>();
/**
* A list of timings. Will only be filled when the timings should be recorded.
*/
private final List<Timing> timings = new ArrayList<>();
private record Timing(String name, long durationMs) {
}
/**
* Dumps should contain their date this method returns the date in a uniform string.
* Format is YYYY-MM-DD hh:mm:ss
*
* @return the current time as a string
*/
private String getTimeString() {
var now = LocalDateTime.now(ZoneId.systemDefault());
return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* Dump a file.
*
* @param fileName of the dump.
* @param content of the dump.
*/
private void dumpFile(String fileName, CharSequence content) {
var folderPath = Paths.get(output.toString(), "dump");
if (!folderPath.toFile().exists()) {
folderPath.toFile().mkdirs();
}
var filePath = Paths.get(folderPath.toString(), fileName);
try (var writer = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8)) {
writer.append(content);
} catch (IOException e) {
e.printStackTrace();
throw Diagnostic.error("Unable to write file %s".formatted(filePath.toString()),
SourceLocation.INVALID_SOURCE_LOCATION)
.build();
}
ArtifactTracker.addDump(filePath);
}
/**
* Parses the input to an AST.
*
* @return the AST.
*/
private Ast parseToAst() {
try {
Ast ast = VadlParser.parse(input, Objects.requireNonNullElseGet(modelOverrides, Map::of));
new Ungrouper().ungroup(ast);
new ModelRemover().removeModels(ast);
return ast;
} catch (IOException e) {
throw Diagnostic.error("Cannot open file", SourceLocation.INVALID_SOURCE_LOCATION)
.description("%s", Objects.requireNonNullElse(e.getMessage(), ""))
.build();
}
}
/**
* Dump the source code with all macros expaned.
*
* @param ast to be expanded.
*/
private void dumpExpaned(Ast ast) {
if (!expandMacros) {
return;
}
var startTime = System.currentTimeMillis();
var content =
new StringBuilder(
"// Sourcecode with expanded macros on %s\n\n".formatted(getTimeString()));
content.append(ast.prettyPrint());
dumpFile("expanded-macros.vadl", content);
timings.add(new Timing("Expanded Macros Dump", System.currentTimeMillis() - startTime));
}
/**
* Dump the AST before it gets enriched with types.
*
* @param ast to be dumped.
*/
private void dumpUntyped(Ast ast) {
if (dump != DumpMode.ALWAYS) {
return;
}
final var startTime = System.currentTimeMillis();
var content =
new StringBuilder(
"// AST Dump without types generated on %s\n".formatted(getTimeString()));
content.append("// The file contains a dump of the AST with all macros expanded but, before "
+ "the type-checker has run.\n\n");
content.append(new AstDumper().dump(ast));
dumpFile("ast-dump-untyped.txt", content);
timings.add(new Timing("Untyped AST Dump", System.currentTimeMillis() - startTime));
}
/**
* Dump the AST after it was enriched with types.
*
* @param ast to be dumped.
*/
private void dumpTyped(Ast ast) {
if (dump != DumpMode.ALWAYS) {
return;
}
final var startTime = System.currentTimeMillis();
var content =
new StringBuilder(
"// AST Dump with types generated on %s\n".formatted(getTimeString()));
content.append("// The file contains a dump of the AST with all macros expanded and "
+ "validated by the typechecker.\n\n");
content.append(new AstDumper().dump(ast));
dumpFile("ast-dump-typed.txt", content);
timings.add(new Timing("Typed AST Dump", System.currentTimeMillis() - startTime));
}
/**
* Parses, typechecks and lowers the input according to the arguments and
* returns a parsed VIAM specification.
*
* <p>If an error occurs, a diagnostic will be thrown.
*
* @return the viam specification
*/
private Specification parseToVIAM() {
var ast = parseToAst();
ast.passTimings.forEach(t -> timings.add(new Timing(t.description(), t.durationMS())));
ast.passTimings.clear();
dumpExpaned(ast);
dumpUntyped(ast);
var typeChecker = new TypeChecker();
typeChecker.verify(ast);
ast.passTimings.forEach(t -> timings.add(new Timing(t.description(), t.durationMS())));
ast.passTimings.clear();
dumpTyped(ast);
var viamGenerator = new ViamLowering();
var spec = viamGenerator.generate(ast);
ast.passTimings.forEach(t -> timings.add(new Timing(t.description(), t.durationMS())));
return spec;
}
protected void printPaths(String message, List<Path> pathList) {
if (pathList.isEmpty()) {
return;
}
System.out.println(message);
for (var path : pathList) {
if (EditorUtils.isIntelliJIDE()) {
var uri = path.toUri();
System.out.printf("\t- %s\n", uri);
} else {
System.out.printf("\t- %s\n", path);
}
}
}
protected void printTimings() {
if (!showTimings) {
return;
}
System.out.println("\nTimings:");
timings.forEach(t -> {
System.out.printf("\t- %-40s %5dms\n", t.name + ":", t.durationMs);
});
}
// lazy evaluated config, do NOT use this directly.
// use getConfig() instead.
@Nullable
private GeneralConfiguration config;
/**
* Generate a general configuration from the arguments.
*
* @return the configuration.
*/
protected GeneralConfiguration getConfig() {
if (config != null) {
return config;
}
config = new GeneralConfiguration(output, dump);
config.setDecoderOptions(getDecoderOptions());
return config;
}
abstract PassOrder passOrder(GeneralConfiguration configuration) throws IOException;
@SuppressWarnings("EmptyCatch")
@Override
public Integer call() {
if (!input.toFile().exists()) {
System.out.printf("\033[01m\033[31merror:\033[0m\033[01m Cannot find file: %s\033[0m\n",
input);
return 1;
}
int returnVal = 0;
try {
final var totalStartTime = System.nanoTime();
var viam = parseToVIAM();
var passOrder = passOrder(getConfig());
var passManager = new PassManager();
passManager.add(passOrder);
passManager.run(viam);
var result = passManager.getPassResults();
result.executedPasses()
.forEach(p -> timings.add(new Timing(p.pass().getName().value(), p.durationMs())));
timings.add(new Timing("Total", (System.nanoTime() - totalStartTime) / 1_000_000));
} catch (CommandLine.TypeConversionException | CommandLine.MaxValuesExceededException e) {
// Re-throw to let Picoli handle it
throw e;
} catch (Diagnostic d) {
System.out.println(new DiagnosticPrinter().toString(d));
if (showStacktrace) {
System.out.println(getStackTrace(d));
}
returnVal = 1;
} catch (DiagnosticList d) {
System.out.println(new DiagnosticPrinter().toString(d));
if (showStacktrace) {
System.out.println(getStackTrace(d));
}
returnVal = 1;
} catch (RuntimeException | IOException | DuplicatedPassKeyException e) {
System.out.println("""
___ ___ _ ___ _ _
/ __| _ \\ /_\\ / __| || |
| (__| / / _ \\\\__ \\ __ |
\\___|_|_\\/_/ \\_\\___/_||_|
🔥 The OpenVADL compiler crashed 🔥
This shouldn't have happened, please open an issue with the stacktrace below at:
https://github.com/OpenVADL/open-vadl/issues/new
""");
printPaths("\nBefore the crash, the following dumps were generated:",
ArtifactTracker.getDumpPaths());
System.out.println();
// Dirty hack to avoid stdout and stderr getting mixed in IntelliJ (flushing wasn't enough).
try {
System.out.flush();
Thread.sleep(10);
} catch (InterruptedException ignored) {
// ignored
}
e.printStackTrace();
return 1;
}
if (!DeferredDiagnosticStore.isEmpty()) {
System.out.println(new DiagnosticPrinter().toString(DeferredDiagnosticStore.getAll()));
// Only exit abnormally if any diagnostic message is an error.
if (DeferredDiagnosticStore.getAll().stream()
.anyMatch(diagnostic -> diagnostic.level == Diagnostic.Level.ERROR)) {
returnVal = 1;
}
}
printPaths(returnVal == 0
? "\nThe following artifacts were generated:"
: "\nEven though some errors occurred, the following artifacts were generated:",
ArtifactTracker.getArtifactPathsPaths());
printPaths(returnVal == 0
? "\nThe following dumps were generated:"
: "\nEven though some errors occurred, the following dumps were generated:",
ArtifactTracker.getDumpPaths()
);
printTimings();
return returnVal;
}
private DecoderOptions getDecoderOptions() {
if (decoderOptions == null) {
return new DecoderOptions();
}
final DecoderOptions result = new DecoderOptions();
var strategies = decoderOptions.stream()
.filter(DecoderStrategy.class::isInstance)
.map(DecoderStrategy.class::cast)
.toList();
if (strategies.size() > 1) {
if (spec == null) {
// Should not happen, but will satisfy Nullaway
throw new IllegalArgumentException("Multiple decoder strategies are not allowed.");
}
throw new CommandLine.MaxValuesExceededException(spec.commandLine(),
"Multiple decoder strategies are not allowed.");
}
if (strategies.size() == 1) {
result.setGenerator(strategies.getFirst().generator());
}
var skipOpts = decoderOptions.stream()
.filter(DecoderSkipOption.class::isInstance)
.map(DecoderSkipOption.class::cast)
.map(DecoderSkipOption::option)
.toList();
if (!skipOpts.isEmpty()) {
result.setOptsToSkip(skipOpts.toArray(new DecoderOptions.OptionToSkip[0]));
}
var statisticOpts = decoderOptions.stream()
.filter(DecoderStatistics.class::isInstance)
.map(DecoderStatistics.class::cast)
.map(DecoderStatistics::stats)
.toList();
if (statisticOpts.size() > 1) {
throw new IllegalArgumentException("Multiple statistics configuration are not allowed.");
}
if (statisticOpts.size() == 1) {
result.setStatistics(statisticOpts.getFirst().getAbsolutePath());
}
var penaltyOpts = decoderOptions.stream()
.filter(DecoderPenaltyFactor.class::isInstance)
.map(DecoderPenaltyFactor.class::cast)
.map(DecoderPenaltyFactor::penalty)
.toList();
if (penaltyOpts.size() > 1) {
throw new IllegalArgumentException("Multiple penalty configuration are not allowed.");
}
if (penaltyOpts.size() == 1) {
result.setMemoryPenalty(penaltyOpts.getFirst());
}
return result;
}
}