Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "SourceFile.h" | ||
4 | |||
5 | #include "visualizer/DependencyGraphVisualizer.h" | ||
6 | |||
7 | #include <llvm/IR/Module.h> | ||
8 | #include <llvm/MC/TargetRegistry.h> | ||
9 | |||
10 | #include <ast/ASTBuilder.h> | ||
11 | #include <exception/AntlrThrowingErrorListener.h> | ||
12 | #include <exception/CompilerError.h> | ||
13 | #include <global/GlobalResourceManager.h> | ||
14 | #include <global/TypeRegistry.h> | ||
15 | #include <importcollector/ImportCollector.h> | ||
16 | #include <irgenerator/IRGenerator.h> | ||
17 | #include <iroptimizer/IROptimizer.h> | ||
18 | #include <linker/BitcodeLinker.h> | ||
19 | #include <objectemitter/ObjectEmitter.h> | ||
20 | #include <symboltablebuilder/SymbolTable.h> | ||
21 | #include <symboltablebuilder/SymbolTableBuilder.h> | ||
22 | #include <typechecker/MacroDefs.h> | ||
23 | #include <typechecker/TypeChecker.h> | ||
24 | #include <util/CompilerWarning.h> | ||
25 | #include <util/FileUtil.h> | ||
26 | #include <util/Timer.h> | ||
27 | #include <visualizer/ASTVisualizer.h> | ||
28 | #include <visualizer/CSTVisualizer.h> | ||
29 | |||
30 | namespace spice::compiler { | ||
31 | |||
32 | 1131 | SourceFile::SourceFile(GlobalResourceManager &resourceManager, SourceFile *parent, std::string name, | |
33 | 1131 | const std::filesystem::path &filePath, bool stdFile) | |
34 |
1/2✓ Branch 5 → 6 taken 1131 times.
✗ Branch 5 → 119 not taken.
|
2262 | : name(std::move(name)), filePath(filePath), isStdFile(stdFile), parent(parent), |
35 |
3/4✓ Branch 13 → 14 taken 2 times.
✓ Branch 13 → 15 taken 1129 times.
✓ Branch 16 → 17 taken 1131 times.
✗ Branch 16 → 58 not taken.
|
1131 | builder(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : context), resourceManager(resourceManager), |
36 |
1/2✓ Branch 11 → 12 taken 1131 times.
✗ Branch 11 → 107 not taken.
|
3393 | cliOptions(resourceManager.cliOptions) { |
37 | // Deduce fileName and fileDir | ||
38 |
3/6✓ Branch 24 → 25 taken 1131 times.
✗ Branch 24 → 63 not taken.
✓ Branch 25 → 26 taken 1131 times.
✗ Branch 25 → 61 not taken.
✓ Branch 26 → 27 taken 1131 times.
✗ Branch 26 → 59 not taken.
|
1131 | fileName = std::filesystem::path(filePath).filename().string(); |
39 |
3/6✓ Branch 31 → 32 taken 1131 times.
✗ Branch 31 → 70 not taken.
✓ Branch 32 → 33 taken 1131 times.
✗ Branch 32 → 68 not taken.
✓ Branch 33 → 34 taken 1131 times.
✗ Branch 33 → 66 not taken.
|
1131 | fileDir = std::filesystem::path(filePath).parent_path().string(); |
40 | |||
41 | // Search after the selected target | ||
42 | 1131 | std::string error; | |
43 |
1/2✓ Branch 39 → 40 taken 1131 times.
✗ Branch 39 → 86 not taken.
|
1131 | const llvm::Target *target = llvm::TargetRegistry::lookupTarget(cliOptions.targetTriple, error); |
44 |
1/2✗ Branch 40 → 41 not taken.
✓ Branch 40 → 46 taken 1131 times.
|
1131 | if (!target) |
45 | − | throw CompilerError(TARGET_NOT_AVAILABLE, "Selected target was not found: " + error); // GCOV_EXCL_LINE | |
46 | |||
47 | // Create the target machine | ||
48 |
1/2✓ Branch 46 → 47 taken 1131 times.
✗ Branch 46 → 86 not taken.
|
1131 | llvm::TargetOptions opt; |
49 | 1131 | opt.MCOptions.AsmVerbose = true; | |
50 | 1131 | opt.MCOptions.PreserveAsmComments = true; | |
51 | 1131 | const std::string &cpuName = resourceManager.cpuName; | |
52 | 1131 | const std::string &features = resourceManager.cpuFeatures; | |
53 | 1131 | const llvm::Triple &targetTriple = cliOptions.targetTriple; | |
54 |
1/2✓ Branch 51 → 52 taken 1131 times.
✗ Branch 51 → 79 not taken.
|
1131 | llvm::TargetMachine *targetMachineRaw = target->createTargetMachine(targetTriple, cpuName, features, opt, llvm::Reloc::PIC_); |
55 | 1131 | targetMachine = std::unique_ptr<llvm::TargetMachine>(targetMachineRaw); | |
56 | 1131 | } | |
57 | |||
58 | 1326 | void SourceFile::runLexer() { | |
59 |
2/2✓ Branch 2 → 3 taken 416 times.
✓ Branch 2 → 4 taken 910 times.
|
1326 | if (isMainFile) |
60 |
1/2✓ Branch 3 → 4 taken 416 times.
✗ Branch 3 → 89 not taken.
|
416 | resourceManager.totalTimer.start(); |
61 | |||
62 | // Check if this stage has already been done | ||
63 |
2/2✓ Branch 4 → 5 taken 197 times.
✓ Branch 4 → 6 taken 1129 times.
|
1326 | if (previousStage >= LEXER) |
64 | 197 | return; | |
65 | |||
66 |
1/2✓ Branch 6 → 7 taken 1129 times.
✗ Branch 6 → 89 not taken.
|
1129 | Timer timer(&compilerOutput.times.lexer); |
67 |
1/2✓ Branch 7 → 8 taken 1129 times.
✗ Branch 7 → 89 not taken.
|
1129 | timer.start(); |
68 | |||
69 | // Read from the input source file | ||
70 |
1/2✓ Branch 8 → 9 taken 1129 times.
✗ Branch 8 → 89 not taken.
|
1129 | std::ifstream fileInputStream(filePath); |
71 |
3/4✓ Branch 9 → 10 taken 1129 times.
✗ Branch 9 → 87 not taken.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 20 taken 1128 times.
|
1129 | if (!fileInputStream) |
72 |
4/8✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 65 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 63 not taken.
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 61 not taken.
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 58 not taken.
|
1 | throw CompilerError(SOURCE_FILE_NOT_FOUND, "Source file at path '" + filePath.string() + "' does not exist."); |
73 | |||
74 | // Tokenize input | ||
75 |
1/2✓ Branch 20 → 21 taken 1128 times.
✗ Branch 20 → 70 not taken.
|
1128 | antlrCtx.inputStream = std::make_unique<antlr4::ANTLRInputStream>(fileInputStream); |
76 |
1/2✓ Branch 24 → 25 taken 1128 times.
✗ Branch 24 → 71 not taken.
|
1128 | antlrCtx.lexer = std::make_unique<SpiceLexer>(antlrCtx.inputStream.get()); |
77 |
1/2✓ Branch 28 → 29 taken 1128 times.
✗ Branch 28 → 87 not taken.
|
1128 | antlrCtx.lexer->removeErrorListeners(); |
78 |
1/2✓ Branch 29 → 30 taken 1128 times.
✗ Branch 29 → 73 not taken.
|
1128 | antlrCtx.lexerErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::LEXER, this); |
79 |
1/2✓ Branch 34 → 35 taken 1128 times.
✗ Branch 34 → 87 not taken.
|
1128 | antlrCtx.lexer->addErrorListener(antlrCtx.lexerErrorHandler.get()); |
80 |
1/2✓ Branch 36 → 37 taken 1128 times.
✗ Branch 36 → 76 not taken.
|
1128 | antlrCtx.tokenStream = std::make_unique<antlr4::CommonTokenStream>(antlrCtx.lexer.get()); |
81 | |||
82 | // Calculate cache key | ||
83 |
1/2✓ Branch 39 → 40 taken 1128 times.
✗ Branch 39 → 87 not taken.
|
1128 | std::stringstream cacheKeyString; |
84 |
4/6✓ Branch 40 → 41 taken 1128 times.
✗ Branch 40 → 85 not taken.
✓ Branch 42 → 43 taken 1127 times.
✓ Branch 42 → 80 taken 1 time.
✓ Branch 44 → 45 taken 1127 times.
✗ Branch 44 → 78 not taken.
|
1128 | cacheKeyString << std::hex << std::hash<std::string>{}(antlrCtx.tokenStream->getText()); |
85 |
1/2✓ Branch 46 → 47 taken 1127 times.
✗ Branch 46 → 82 not taken.
|
1127 | cacheKey = cacheKeyString.str(); |
86 | |||
87 | // Try to load from cache | ||
88 |
1/2✗ Branch 49 → 50 not taken.
✓ Branch 49 → 52 taken 1127 times.
|
1127 | if (!cliOptions.ignoreCache) |
89 | ✗ | restoredFromCache = resourceManager.cacheManager.lookupSourceFile(this); | |
90 | |||
91 | 1127 | previousStage = LEXER; | |
92 |
1/2✓ Branch 52 → 53 taken 1127 times.
✗ Branch 52 → 85 not taken.
|
1127 | timer.stop(); |
93 |
1/2✓ Branch 53 → 54 taken 1127 times.
✗ Branch 53 → 83 not taken.
|
1127 | printStatusMessage("Lexer", IO_CODE, IO_TOKENS, compilerOutput.times.lexer); |
94 | 1130 | } | |
95 | |||
96 | 1324 | void SourceFile::runParser() { | |
97 | // Skip if restored from the cache or this stage has already been done | ||
98 |
3/4✓ Branch 2 → 3 taken 1324 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 197 times.
✓ Branch 3 → 5 taken 1127 times.
|
1324 | if (restoredFromCache || previousStage >= PARSER) |
99 | 197 | return; | |
100 | |||
101 |
1/2✓ Branch 5 → 6 taken 1127 times.
✗ Branch 5 → 32 not taken.
|
1127 | Timer timer(&compilerOutput.times.parser); |
102 |
1/2✓ Branch 6 → 7 taken 1127 times.
✗ Branch 6 → 32 not taken.
|
1127 | timer.start(); |
103 | |||
104 | // Parse input | ||
105 |
1/2✓ Branch 8 → 9 taken 1127 times.
✗ Branch 8 → 25 not taken.
|
1127 | antlrCtx.parser = std::make_unique<SpiceParser>(antlrCtx.tokenStream.get()); // Check for syntax errors |
106 |
1/2✓ Branch 12 → 13 taken 1127 times.
✗ Branch 12 → 32 not taken.
|
1127 | antlrCtx.parser->removeErrorListeners(); |
107 |
1/2✓ Branch 13 → 14 taken 1127 times.
✗ Branch 13 → 27 not taken.
|
1127 | antlrCtx.parserErrorHandler = std::make_unique<AntlrThrowingErrorListener>(ThrowingErrorListenerMode::PARSER, this); |
108 |
1/2✓ Branch 18 → 19 taken 1127 times.
✗ Branch 18 → 32 not taken.
|
1127 | antlrCtx.parser->addErrorListener(antlrCtx.parserErrorHandler.get()); |
109 |
1/2✓ Branch 20 → 21 taken 1127 times.
✗ Branch 20 → 32 not taken.
|
1127 | antlrCtx.parser->removeParseListeners(); |
110 | |||
111 | 1127 | previousStage = PARSER; | |
112 |
1/2✓ Branch 21 → 22 taken 1127 times.
✗ Branch 21 → 32 not taken.
|
1127 | timer.stop(); |
113 |
1/2✓ Branch 22 → 23 taken 1127 times.
✗ Branch 22 → 30 not taken.
|
1127 | printStatusMessage("Parser", IO_TOKENS, IO_CST, compilerOutput.times.parser); |
114 | } | ||
115 | |||
116 | 918 | void SourceFile::runCSTVisualizer() { | |
117 | // Only execute if enabled | ||
118 |
3/6✓ Branch 2 → 3 taken 918 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 918 times.
✗ Branch 3 → 6 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 918 times.
|
918 | if (restoredFromCache || (!cliOptions.dumpSettings.dumpCST && !cliOptions.testMode)) |
119 | 197 | return; | |
120 | // Check if this stage has already been done | ||
121 |
2/2✓ Branch 6 → 7 taken 197 times.
✓ Branch 6 → 8 taken 721 times.
|
918 | if (previousStage >= CST_VISUALIZER) |
122 | 197 | return; | |
123 | |||
124 |
1/2✓ Branch 8 → 9 taken 721 times.
✗ Branch 8 → 66 not taken.
|
721 | Timer timer(&compilerOutput.times.cstVisualizer); |
125 |
1/2✓ Branch 9 → 10 taken 721 times.
✗ Branch 9 → 66 not taken.
|
721 | timer.start(); |
126 | |||
127 | // Generate dot code for this source file | ||
128 |
1/2✓ Branch 10 → 11 taken 721 times.
✗ Branch 10 → 66 not taken.
|
721 | std::stringstream dotCode; |
129 |
1/2✓ Branch 11 → 12 taken 721 times.
✗ Branch 11 → 64 not taken.
|
721 | visualizerPreamble(dotCode); |
130 |
1/2✓ Branch 14 → 15 taken 721 times.
✗ Branch 14 → 64 not taken.
|
721 | CSTVisualizer cstVisualizer(resourceManager, this, antlrCtx.lexer.get(), antlrCtx.parser.get()); |
131 |
6/12✓ Branch 15 → 16 taken 721 times.
✗ Branch 15 → 62 not taken.
✓ Branch 17 → 18 taken 721 times.
✗ Branch 17 → 51 not taken.
✓ Branch 18 → 19 taken 721 times.
✗ Branch 18 → 51 not taken.
✓ Branch 19 → 20 taken 721 times.
✗ Branch 19 → 49 not taken.
✓ Branch 20 → 21 taken 721 times.
✗ Branch 20 → 47 not taken.
✓ Branch 21 → 22 taken 721 times.
✗ Branch 21 → 47 not taken.
|
721 | dotCode << " " << std::any_cast<std::string>(cstVisualizer.visit(antlrCtx.parser->entry())) << "}"; |
132 |
1/2✓ Branch 25 → 26 taken 721 times.
✗ Branch 25 → 62 not taken.
|
721 | antlrCtx.parser->reset(); |
133 | |||
134 | // Dump the serialized CST string and the SVG file | ||
135 |
2/4✓ Branch 26 → 27 taken 721 times.
✗ Branch 26 → 28 not taken.
✓ Branch 27 → 28 taken 721 times.
✗ Branch 27 → 32 not taken.
|
721 | if (cliOptions.dumpSettings.dumpCST || cliOptions.testMode) |
136 |
1/2✓ Branch 28 → 29 taken 721 times.
✗ Branch 28 → 53 not taken.
|
721 | compilerOutput.cstString = dotCode.str(); |
137 | |||
138 |
1/2✗ Branch 32 → 33 not taken.
✓ Branch 32 → 40 taken 721 times.
|
721 | if (cliOptions.dumpSettings.dumpCST) |
139 | ✗ | visualizerOutput("CST", compilerOutput.cstString); | |
140 | |||
141 | 721 | previousStage = CST_VISUALIZER; | |
142 |
1/2✓ Branch 40 → 41 taken 721 times.
✗ Branch 40 → 62 not taken.
|
721 | timer.stop(); |
143 |
1/2✓ Branch 41 → 42 taken 721 times.
✗ Branch 41 → 60 not taken.
|
721 | printStatusMessage("CST Visualizer", IO_CST, IO_CST, compilerOutput.times.cstVisualizer); |
144 | 721 | } | |
145 | |||
146 | 1324 | void SourceFile::runASTBuilder() { | |
147 | // Skip if restored from the cache or this stage has already been done | ||
148 |
3/4✓ Branch 2 → 3 taken 1324 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 197 times.
✓ Branch 3 → 5 taken 1127 times.
|
1324 | if (restoredFromCache || previousStage >= AST_BUILDER) |
149 | 197 | return; | |
150 | |||
151 |
1/2✓ Branch 5 → 6 taken 1127 times.
✗ Branch 5 → 36 not taken.
|
1127 | Timer timer(&compilerOutput.times.astBuilder); |
152 |
1/2✓ Branch 6 → 7 taken 1127 times.
✗ Branch 6 → 36 not taken.
|
1127 | timer.start(); |
153 | |||
154 | // Build AST for this source file | ||
155 |
1/2✓ Branch 8 → 9 taken 1127 times.
✗ Branch 8 → 36 not taken.
|
1127 | ASTBuilder astBuilder(resourceManager, this, antlrCtx.inputStream.get()); |
156 |
5/6✓ Branch 10 → 11 taken 1125 times.
✓ Branch 10 → 26 taken 2 times.
✓ Branch 11 → 12 taken 1119 times.
✓ Branch 11 → 26 taken 6 times.
✓ Branch 12 → 13 taken 1119 times.
✗ Branch 12 → 24 not taken.
|
1127 | ast = std::any_cast<EntryNode *>(astBuilder.visit(antlrCtx.parser->entry())); |
157 |
1/2✓ Branch 15 → 16 taken 1119 times.
✗ Branch 15 → 34 not taken.
|
1119 | antlrCtx.parser->reset(); |
158 | |||
159 | // Create global scope | ||
160 |
1/2✓ Branch 16 → 17 taken 1119 times.
✗ Branch 16 → 27 not taken.
|
1119 | globalScope = std::make_unique<Scope>(nullptr, this, ScopeType::GLOBAL, &ast->codeLoc); |
161 | |||
162 | 1119 | previousStage = AST_BUILDER; | |
163 |
1/2✓ Branch 19 → 20 taken 1119 times.
✗ Branch 19 → 34 not taken.
|
1119 | timer.stop(); |
164 |
1/2✓ Branch 20 → 21 taken 1119 times.
✗ Branch 20 → 32 not taken.
|
1119 | printStatusMessage("AST Builder", IO_CST, IO_AST, compilerOutput.times.astBuilder); |
165 | 1127 | } | |
166 | |||
167 | 918 | void SourceFile::runASTVisualizer() { | |
168 | // Only execute if enabled | ||
169 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 918 times.
|
918 | if (restoredFromCache) |
170 | 197 | return; | |
171 |
2/4✓ Branch 4 → 5 taken 918 times.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 918 times.
|
918 | if (!cliOptions.dumpSettings.dumpAST && !cliOptions.testMode) |
172 | ✗ | return; | |
173 | // Check if this stage has already been done | ||
174 |
2/2✓ Branch 7 → 8 taken 197 times.
✓ Branch 7 → 9 taken 721 times.
|
918 | if (previousStage >= AST_VISUALIZER) |
175 | 197 | return; | |
176 | |||
177 |
1/2✓ Branch 9 → 10 taken 721 times.
✗ Branch 9 → 58 not taken.
|
721 | Timer timer(&compilerOutput.times.astVisualizer); |
178 |
1/2✓ Branch 10 → 11 taken 721 times.
✗ Branch 10 → 58 not taken.
|
721 | timer.start(); |
179 | |||
180 | // Generate dot code for this source file | ||
181 |
1/2✓ Branch 11 → 12 taken 721 times.
✗ Branch 11 → 58 not taken.
|
721 | std::stringstream dotCode; |
182 |
1/2✓ Branch 12 → 13 taken 721 times.
✗ Branch 12 → 56 not taken.
|
721 | visualizerPreamble(dotCode); |
183 |
1/2✓ Branch 13 → 14 taken 721 times.
✗ Branch 13 → 56 not taken.
|
721 | ASTVisualizer astVisualizer(resourceManager, this); |
184 |
5/10✓ Branch 14 → 15 taken 721 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 721 times.
✗ Branch 15 → 43 not taken.
✓ Branch 16 → 17 taken 721 times.
✗ Branch 16 → 41 not taken.
✓ Branch 17 → 18 taken 721 times.
✗ Branch 17 → 39 not taken.
✓ Branch 18 → 19 taken 721 times.
✗ Branch 18 → 39 not taken.
|
721 | dotCode << " " << std::any_cast<std::string>(astVisualizer.visit(ast)) << "}"; |
185 | |||
186 | // Dump the serialized AST string and the SVG file | ||
187 |
1/2✓ Branch 21 → 22 taken 721 times.
✗ Branch 21 → 45 not taken.
|
721 | compilerOutput.astString = dotCode.str(); |
188 | |||
189 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 32 taken 721 times.
|
721 | if (cliOptions.dumpSettings.dumpAST) |
190 | ✗ | visualizerOutput("AST", compilerOutput.astString); | |
191 | |||
192 | 721 | previousStage = AST_VISUALIZER; | |
193 |
1/2✓ Branch 32 → 33 taken 721 times.
✗ Branch 32 → 54 not taken.
|
721 | timer.stop(); |
194 |
1/2✓ Branch 33 → 34 taken 721 times.
✗ Branch 33 → 52 not taken.
|
721 | printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.astVisualizer); |
195 | 721 | } | |
196 | |||
197 | 1316 | void SourceFile::runImportCollector() { // NOLINT(misc-no-recursion) | |
198 | // Skip if restored from the cache or this stage has already been done | ||
199 |
3/4✓ Branch 2 → 3 taken 1316 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 197 times.
✓ Branch 3 → 5 taken 1119 times.
|
1316 | if (restoredFromCache || previousStage >= IMPORT_COLLECTOR) |
200 | 197 | return; | |
201 | |||
202 |
1/2✓ Branch 5 → 6 taken 1119 times.
✗ Branch 5 → 30 not taken.
|
1119 | Timer timer(&compilerOutput.times.importCollector); |
203 |
1/2✓ Branch 6 → 7 taken 1119 times.
✗ Branch 6 → 30 not taken.
|
1119 | timer.start(); |
204 | |||
205 | // Collect the imports for this source file | ||
206 |
1/2✓ Branch 7 → 8 taken 1119 times.
✗ Branch 7 → 30 not taken.
|
1119 | ImportCollector importCollector(resourceManager, this); |
207 |
2/2✓ Branch 8 → 9 taken 1114 times.
✓ Branch 8 → 24 taken 5 times.
|
1119 | importCollector.visit(ast); |
208 | |||
209 | 1114 | previousStage = IMPORT_COLLECTOR; | |
210 |
1/2✓ Branch 10 → 11 taken 1114 times.
✗ Branch 10 → 28 not taken.
|
1114 | timer.stop(); |
211 | |||
212 | // Run first part of pipeline for the imported source file | ||
213 |
5/8✓ Branch 11 → 12 taken 1114 times.
✗ Branch 11 → 25 not taken.
✓ Branch 12 → 13 taken 1114 times.
✗ Branch 12 → 25 not taken.
✓ Branch 13 → 14 taken 1114 times.
✗ Branch 13 → 25 not taken.
✓ Branch 19 → 15 taken 591 times.
✓ Branch 19 → 20 taken 1112 times.
|
1703 | for (SourceFile *sourceFile : dependencies | std::views::values) |
214 |
2/2✓ Branch 16 → 17 taken 589 times.
✓ Branch 16 → 25 taken 2 times.
|
591 | sourceFile->runFrontEnd(); |
215 | |||
216 |
1/2✓ Branch 20 → 21 taken 1112 times.
✗ Branch 20 → 26 not taken.
|
1112 | printStatusMessage("Import Collector", IO_AST, IO_AST, compilerOutput.times.importCollector); |
217 | 1119 | } | |
218 | |||
219 | 1309 | void SourceFile::runSymbolTableBuilder() { | |
220 | // Skip if restored from the cache or this stage has already been done | ||
221 |
3/4✓ Branch 2 → 3 taken 1309 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 197 times.
✓ Branch 3 → 5 taken 1112 times.
|
1309 | if (restoredFromCache || previousStage >= SYMBOL_TABLE_BUILDER) |
222 | 197 | return; | |
223 | |||
224 |
1/2✓ Branch 5 → 6 taken 1112 times.
✗ Branch 5 → 30 not taken.
|
1112 | Timer timer(&compilerOutput.times.symbolTableBuilder); |
225 |
1/2✓ Branch 6 → 7 taken 1112 times.
✗ Branch 6 → 30 not taken.
|
1112 | timer.start(); |
226 | |||
227 | // The symbol tables of all dependencies are present at this point, so we can merge the exported name registries in | ||
228 |
2/2✓ Branch 15 → 9 taken 589 times.
✓ Branch 15 → 16 taken 1112 times.
|
1701 | for (const auto &[importName, sourceFile] : dependencies) |
229 |
1/2✓ Branch 12 → 13 taken 589 times.
✗ Branch 12 → 24 not taken.
|
589 | mergeNameRegistries(*sourceFile, importName); |
230 | |||
231 | // Build symbol table of the current file | ||
232 |
1/2✓ Branch 16 → 17 taken 1112 times.
✗ Branch 16 → 30 not taken.
|
1112 | SymbolTableBuilder symbolTableBuilder(resourceManager, this); |
233 |
2/2✓ Branch 17 → 18 taken 1094 times.
✓ Branch 17 → 25 taken 18 times.
|
1112 | symbolTableBuilder.visit(ast); |
234 | |||
235 | 1094 | previousStage = SYMBOL_TABLE_BUILDER; | |
236 |
1/2✓ Branch 19 → 20 taken 1094 times.
✗ Branch 19 → 28 not taken.
|
1094 | timer.stop(); |
237 |
1/2✓ Branch 20 → 21 taken 1094 times.
✗ Branch 20 → 26 not taken.
|
1094 | printStatusMessage("Symbol Table Builder", IO_AST, IO_AST, compilerOutput.times.symbolTableBuilder); |
238 | 1112 | } | |
239 | |||
240 | 1290 | void SourceFile::runTypeCheckerPre() { // NOLINT(misc-no-recursion) | |
241 | // Skip if restored from the cache or this stage has already been done | ||
242 |
3/4✓ Branch 2 → 3 taken 1290 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 197 times.
✓ Branch 3 → 5 taken 1093 times.
|
1290 | if (restoredFromCache || previousStage >= TYPE_CHECKER_PRE) |
243 | 197 | return; | |
244 | |||
245 | // Type-check all dependencies first | ||
246 |
5/8✓ Branch 5 → 6 taken 1093 times.
✗ Branch 5 → 24 not taken.
✓ Branch 6 → 7 taken 1093 times.
✗ Branch 6 → 24 not taken.
✓ Branch 7 → 8 taken 1093 times.
✗ Branch 7 → 24 not taken.
✓ Branch 13 → 9 taken 588 times.
✓ Branch 13 → 14 taken 1093 times.
|
1681 | for (SourceFile *sourceFile : dependencies | std::views::values) |
247 |
1/2✓ Branch 10 → 11 taken 588 times.
✗ Branch 10 → 24 not taken.
|
588 | sourceFile->runTypeCheckerPre(); |
248 | |||
249 |
1/2✓ Branch 14 → 15 taken 1093 times.
✗ Branch 14 → 30 not taken.
|
1093 | Timer timer(&compilerOutput.times.typeCheckerPre); |
250 |
1/2✓ Branch 15 → 16 taken 1093 times.
✗ Branch 15 → 30 not taken.
|
1093 | timer.start(); |
251 | |||
252 | // Then type-check the current file | ||
253 |
1/2✓ Branch 16 → 17 taken 1093 times.
✗ Branch 16 → 30 not taken.
|
1093 | TypeChecker typeChecker(resourceManager, this, TC_MODE_PRE); |
254 |
2/2✓ Branch 17 → 18 taken 1079 times.
✓ Branch 17 → 25 taken 14 times.
|
1093 | typeChecker.visit(ast); |
255 | |||
256 | 1079 | previousStage = TYPE_CHECKER_PRE; | |
257 |
1/2✓ Branch 19 → 20 taken 1079 times.
✗ Branch 19 → 28 not taken.
|
1079 | timer.stop(); |
258 |
1/2✓ Branch 20 → 21 taken 1079 times.
✗ Branch 20 → 26 not taken.
|
1079 | printStatusMessage("Type Checker Pre", IO_AST, IO_AST, compilerOutput.times.typeCheckerPre); |
259 | 1093 | } | |
260 | |||
261 | 2708 | void SourceFile::runTypeCheckerPost() { // NOLINT(misc-no-recursion) | |
262 | // Skip if restored from cache, this stage has already been done, or not all dependants finished type checking | ||
263 |
6/8✓ Branch 2 → 3 taken 2708 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 2708 times.
✗ Branch 3 → 80 not taken.
✓ Branch 4 → 5 taken 477 times.
✓ Branch 4 → 6 taken 2231 times.
✓ Branch 7 → 8 taken 477 times.
✓ Branch 7 → 9 taken 2231 times.
|
2708 | if (restoredFromCache || !haveAllDependantsBeenTypeChecked()) |
264 | 477 | return; | |
265 | |||
266 |
1/2✓ Branch 9 → 10 taken 2231 times.
✗ Branch 9 → 80 not taken.
|
2231 | Timer timer(&compilerOutput.times.typeCheckerPost); |
267 |
1/2✓ Branch 10 → 11 taken 2231 times.
✗ Branch 10 → 80 not taken.
|
2231 | timer.start(); |
268 | |||
269 | // Start type-checking loop. The type-checker can request a re-execution. The max number of type-checker runs is limited | ||
270 |
1/2✓ Branch 11 → 12 taken 2231 times.
✗ Branch 11 → 80 not taken.
|
2231 | TypeChecker typeChecker(resourceManager, this, TC_MODE_POST); |
271 | 2231 | unsigned short typeCheckerRuns = 0; | |
272 |
2/2✓ Branch 27 → 13 taken 1505 times.
✓ Branch 27 → 28 taken 2170 times.
|
3675 | while (reVisitRequested) { |
273 | 1505 | typeCheckerRuns++; | |
274 | 1505 | totalTypeCheckerRuns++; | |
275 | 1505 | reVisitRequested = false; | |
276 | |||
277 | // Type-check the current file first. Multiple times, if requested | ||
278 | 1505 | timer.resume(); | |
279 |
2/2✓ Branch 14 → 15 taken 1475 times.
✓ Branch 14 → 58 taken 30 times.
|
1505 | typeChecker.visit(ast); |
280 |
1/2✓ Branch 16 → 17 taken 1475 times.
✗ Branch 16 → 78 not taken.
|
1475 | timer.pause(); |
281 | |||
282 | // Then type-check all dependencies | ||
283 |
5/8✓ Branch 17 → 18 taken 1475 times.
✗ Branch 17 → 59 not taken.
✓ Branch 18 → 19 taken 1475 times.
✗ Branch 18 → 59 not taken.
✓ Branch 19 → 20 taken 1475 times.
✗ Branch 19 → 59 not taken.
✓ Branch 25 → 21 taken 2339 times.
✓ Branch 25 → 26 taken 1444 times.
|
3783 | for (SourceFile *sourceFile : dependencies | std::views::values) |
284 |
2/2✓ Branch 22 → 23 taken 2308 times.
✓ Branch 22 → 59 taken 31 times.
|
2339 | sourceFile->runTypeCheckerPost(); |
285 | } | ||
286 | |||
287 |
2/2✓ Branch 28 → 29 taken 2068 times.
✓ Branch 28 → 78 taken 102 times.
|
2170 | checkForSoftErrors(); |
288 | |||
289 | // Check if all dyn variables were type-inferred successfully | ||
290 |
2/2✓ Branch 30 → 31 taken 2067 times.
✓ Branch 30 → 78 taken 1 time.
|
2068 | globalScope->ensureSuccessfulTypeInference(); |
291 | |||
292 | 2067 | previousStage = TYPE_CHECKER_POST; | |
293 |
1/2✓ Branch 31 → 32 taken 2067 times.
✗ Branch 31 → 78 not taken.
|
2067 | timer.stop(); |
294 |
1/2✓ Branch 32 → 33 taken 2067 times.
✗ Branch 32 → 60 not taken.
|
2067 | printStatusMessage("Type Checker Post", IO_AST, IO_AST, compilerOutput.times.typeCheckerPost, typeCheckerRuns); |
295 | |||
296 | // Save the JSON version in the compiler output | ||
297 |
2/4✓ Branch 33 → 34 taken 2067 times.
✗ Branch 33 → 35 not taken.
✓ Branch 34 → 35 taken 2067 times.
✗ Branch 34 → 42 not taken.
|
2067 | if (cliOptions.dumpSettings.dumpSymbolTable || cliOptions.testMode) |
298 |
2/4✓ Branch 36 → 37 taken 2067 times.
✗ Branch 36 → 64 not taken.
✓ Branch 37 → 38 taken 2067 times.
✗ Branch 37 → 62 not taken.
|
2067 | compilerOutput.symbolTableString = globalScope->getSymbolTableJSON().dump(/*indent=*/2); |
299 | |||
300 | // Dump symbol table | ||
301 |
1/2✗ Branch 42 → 43 not taken.
✓ Branch 42 → 55 taken 2067 times.
|
2067 | if (cliOptions.dumpSettings.dumpSymbolTable) |
302 | ✗ | dumpOutput(compilerOutput.symbolTableString, "Symbol Table", "symbol-table.json"); | |
303 | 2231 | } | |
304 | |||
305 | 238 | void SourceFile::runDependencyGraphVisualizer() { | |
306 | // Only execute if enabled | ||
307 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 238 times.
|
238 | if (restoredFromCache) |
308 | 2 | return; | |
309 |
2/4✓ Branch 4 → 5 taken 238 times.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 238 times.
|
238 | if (!cliOptions.dumpSettings.dumpDependencyGraph && !cliOptions.testMode) |
310 | ✗ | return; | |
311 | // Check if this stage has already been done | ||
312 |
2/2✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 236 times.
|
238 | if (previousStage >= DEP_GRAPH_VISUALIZER) |
313 | 2 | return; | |
314 | |||
315 |
1/2✓ Branch 9 → 10 taken 236 times.
✗ Branch 9 → 47 not taken.
|
236 | Timer timer(&compilerOutput.times.depGraphVisualizer); |
316 |
1/2✓ Branch 10 → 11 taken 236 times.
✗ Branch 10 → 47 not taken.
|
236 | timer.start(); |
317 | |||
318 | // Generate dot code for this source file | ||
319 |
1/2✓ Branch 11 → 12 taken 236 times.
✗ Branch 11 → 47 not taken.
|
236 | std::stringstream dotCode; |
320 |
1/2✓ Branch 12 → 13 taken 236 times.
✗ Branch 12 → 45 not taken.
|
236 | visualizerPreamble(dotCode); |
321 |
1/2✓ Branch 13 → 14 taken 236 times.
✗ Branch 13 → 45 not taken.
|
236 | DependencyGraphVisualizer depGraphVisualizer(resourceManager, this); |
322 |
1/2✓ Branch 14 → 15 taken 236 times.
✗ Branch 14 → 43 not taken.
|
236 | depGraphVisualizer.getDependencyGraph(dotCode); |
323 |
1/2✓ Branch 15 → 16 taken 236 times.
✗ Branch 15 → 43 not taken.
|
236 | dotCode << "}"; |
324 | |||
325 | // Dump the serialized AST string and the SVG file | ||
326 |
1/2✓ Branch 16 → 17 taken 236 times.
✗ Branch 16 → 34 not taken.
|
236 | compilerOutput.depGraphString = dotCode.str(); |
327 | |||
328 |
1/2✗ Branch 19 → 20 not taken.
✓ Branch 19 → 27 taken 236 times.
|
236 | if (cliOptions.dumpSettings.dumpDependencyGraph) |
329 | ✗ | visualizerOutput("Dependency Graph", compilerOutput.depGraphString); | |
330 | |||
331 | 236 | previousStage = DEP_GRAPH_VISUALIZER; | |
332 |
1/2✓ Branch 27 → 28 taken 236 times.
✗ Branch 27 → 43 not taken.
|
236 | timer.stop(); |
333 |
1/2✓ Branch 28 → 29 taken 236 times.
✗ Branch 28 → 41 not taken.
|
236 | printStatusMessage("AST Visualizer", IO_AST, IO_AST, compilerOutput.times.depGraphVisualizer); |
334 | 236 | } | |
335 | |||
336 | 3269 | void SourceFile::runIRGenerator() { | |
337 | // Skip if restored from the cache or this stage has already been done | ||
338 |
3/4✓ Branch 2 → 3 taken 3269 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 2377 times.
✓ Branch 3 → 5 taken 892 times.
|
3269 | if (restoredFromCache || previousStage >= IR_GENERATOR) |
339 | 2377 | return; | |
340 | |||
341 |
1/2✓ Branch 5 → 6 taken 892 times.
✗ Branch 5 → 60 not taken.
|
892 | Timer timer(&compilerOutput.times.irGenerator); |
342 |
1/2✓ Branch 6 → 7 taken 892 times.
✗ Branch 6 → 60 not taken.
|
892 | timer.start(); |
343 | |||
344 | // Create the LLVM module for this source file | ||
345 |
2/2✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 890 times.
|
892 | llvm::LLVMContext &llvmContext = cliOptions.useLTO ? resourceManager.ltoContext : context; |
346 |
1/2✓ Branch 10 → 11 taken 892 times.
✗ Branch 10 → 41 not taken.
|
892 | llvmModule = std::make_unique<llvm::Module>(fileName, llvmContext); |
347 | |||
348 | // Generate this source file | ||
349 |
1/2✓ Branch 13 → 14 taken 892 times.
✗ Branch 13 → 60 not taken.
|
892 | IRGenerator irGenerator(resourceManager, this); |
350 |
1/2✓ Branch 14 → 15 taken 892 times.
✗ Branch 14 → 42 not taken.
|
892 | irGenerator.visit(ast); |
351 | |||
352 | // Save the ir string in the compiler output | ||
353 |
2/4✓ Branch 16 → 17 taken 892 times.
✗ Branch 16 → 18 not taken.
✓ Branch 17 → 18 taken 892 times.
✗ Branch 17 → 23 not taken.
|
892 | if (cliOptions.dumpSettings.dumpIR || cliOptions.testMode) |
354 |
1/2✓ Branch 19 → 20 taken 892 times.
✗ Branch 19 → 43 not taken.
|
892 | compilerOutput.irString = IRGenerator::getIRString(llvmModule.get(), cliOptions.comparableOutput); |
355 | |||
356 | // Dump unoptimized IR code | ||
357 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 36 taken 892 times.
|
892 | if (cliOptions.dumpSettings.dumpIR) |
358 | ✗ | dumpOutput(compilerOutput.irString, "Unoptimized IR Code", "ir-code.ll"); | |
359 | |||
360 | 892 | previousStage = IR_GENERATOR; | |
361 |
1/2✓ Branch 36 → 37 taken 892 times.
✗ Branch 36 → 58 not taken.
|
892 | timer.stop(); |
362 |
1/2✓ Branch 37 → 38 taken 892 times.
✗ Branch 37 → 56 not taken.
|
892 | printStatusMessage("IR Generator", IO_AST, IO_IR, compilerOutput.times.irGenerator); |
363 | 892 | } | |
364 | |||
365 | 3062 | void SourceFile::runDefaultIROptimizer() { | |
366 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3062 times.
|
3062 | assert(!cliOptions.useLTO); |
367 | |||
368 | // Skip if restored from the cache or this stage has already been done | ||
369 |
4/6✓ Branch 4 → 5 taken 3062 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 2381 times.
✓ Branch 5 → 8 taken 681 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2381 times.
|
3062 | if (restoredFromCache || (previousStage >= IR_OPTIMIZER && !cliOptions.testMode)) |
370 | 3032 | return; | |
371 | |||
372 | // Skip this stage if optimization is disabled | ||
373 | 3062 | const OptLevel optLevel = cliOptions.optLevel; | |
374 |
3/4✓ Branch 8 → 9 taken 30 times.
✓ Branch 8 → 10 taken 3032 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 30 times.
|
3062 | if (optLevel < O1 || optLevel > Oz) |
375 | 3032 | return; | |
376 | |||
377 |
1/2✓ Branch 11 → 12 taken 30 times.
✗ Branch 11 → 63 not taken.
|
30 | Timer timer(&compilerOutput.times.irOptimizer); |
378 |
1/2✓ Branch 12 → 13 taken 30 times.
✗ Branch 12 → 63 not taken.
|
30 | timer.start(); |
379 | |||
380 | // Optimize this source file | ||
381 |
1/2✓ Branch 13 → 14 taken 30 times.
✗ Branch 13 → 63 not taken.
|
30 | IROptimizer irOptimizer(resourceManager, this); |
382 |
1/2✓ Branch 14 → 15 taken 30 times.
✗ Branch 14 → 61 not taken.
|
30 | irOptimizer.prepare(); |
383 |
1/2✓ Branch 15 → 16 taken 30 times.
✗ Branch 15 → 61 not taken.
|
30 | irOptimizer.optimizeDefault(); |
384 | |||
385 | // Save the optimized ir string in the compiler output | ||
386 |
2/4✓ Branch 16 → 17 taken 30 times.
✗ Branch 16 → 18 not taken.
✓ Branch 17 → 18 taken 30 times.
✗ Branch 17 → 23 not taken.
|
30 | if (cliOptions.dumpSettings.dumpIR || cliOptions.testMode) |
387 |
1/2✓ Branch 19 → 20 taken 30 times.
✗ Branch 19 → 43 not taken.
|
30 | compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), cliOptions.comparableOutput); |
388 | |||
389 | // Dump optimized IR code | ||
390 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 37 taken 30 times.
|
30 | if (cliOptions.dumpSettings.dumpIR) |
391 | ✗ | dumpOutput(compilerOutput.irOptString, "Optimized IR Code", "ir-code-O" + std::to_string(optLevel) + ".ll"); | |
392 | |||
393 | 30 | previousStage = IR_OPTIMIZER; | |
394 |
1/2✓ Branch 37 → 38 taken 30 times.
✗ Branch 37 → 61 not taken.
|
30 | timer.stop(); |
395 |
1/2✓ Branch 38 → 39 taken 30 times.
✗ Branch 38 → 59 not taken.
|
30 | printStatusMessage("IR Optimizer", IO_IR, IO_IR, compilerOutput.times.irOptimizer); |
396 | 30 | } | |
397 | |||
398 | 2 | void SourceFile::runPreLinkIROptimizer() { | |
399 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
|
2 | assert(cliOptions.useLTO); |
400 | |||
401 | // Skip if restored from the cache or this stage has already been done | ||
402 |
2/4✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
|
2 | if (restoredFromCache || previousStage >= IR_OPTIMIZER) |
403 | 1 | return; | |
404 | |||
405 | // Skip this stage if optimization is disabled | ||
406 |
3/4✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 1 time.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1 time.
|
2 | if (cliOptions.optLevel < O1 || cliOptions.optLevel > Oz) |
407 | 1 | return; | |
408 | |||
409 |
1/2✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 55 not taken.
|
1 | Timer timer(&compilerOutput.times.irOptimizer); |
410 |
1/2✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 55 not taken.
|
1 | timer.start(); |
411 | |||
412 | // Optimize this source file | ||
413 |
1/2✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 55 not taken.
|
1 | IROptimizer irOptimizer(resourceManager, this); |
414 |
1/2✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 53 not taken.
|
1 | irOptimizer.prepare(); |
415 |
1/2✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 53 not taken.
|
1 | irOptimizer.optimizePreLink(); |
416 | |||
417 | // Save the optimized ir string in the compiler output | ||
418 |
2/4✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 17 not taken.
✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 22 not taken.
|
1 | if (cliOptions.dumpSettings.dumpIR || cliOptions.testMode) |
419 |
1/2✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 40 not taken.
|
1 | compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), cliOptions.comparableOutput); |
420 | |||
421 | // Dump optimized IR code | ||
422 |
1/2✗ Branch 22 → 23 not taken.
✓ Branch 22 → 35 taken 1 time.
|
1 | if (cliOptions.dumpSettings.dumpIR) |
423 | ✗ | dumpOutput(compilerOutput.irOptString, "Optimized IR Code (pre-link)", "ir-code-lto-pre-link.ll"); | |
424 | |||
425 |
1/2✓ Branch 35 → 36 taken 1 time.
✗ Branch 35 → 53 not taken.
|
1 | timer.pause(); |
426 | 1 | } | |
427 | |||
428 | 2 | void SourceFile::runBitcodeLinker() { | |
429 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
|
2 | assert(cliOptions.useLTO); |
430 | |||
431 | // Skip if this is not the main source file | ||
432 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
|
2 | if (!isMainFile) |
433 | 1 | return; | |
434 | |||
435 | // Skip if restored from the cache or this stage has already been done | ||
436 |
2/4✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 8 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
|
1 | if (restoredFromCache || previousStage >= IR_OPTIMIZER) |
437 | ✗ | return; | |
438 | |||
439 |
1/2✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 20 not taken.
|
1 | Timer timer(&compilerOutput.times.irOptimizer); |
440 | 1 | timer.resume(); | |
441 | |||
442 | // Link all source files together | ||
443 |
1/2✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 20 not taken.
|
1 | BitcodeLinker linker(resourceManager); |
444 |
1/2✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 18 not taken.
|
1 | linker.link(); |
445 | |||
446 |
1/2✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 18 not taken.
|
1 | timer.pause(); |
447 | 1 | } | |
448 | |||
449 | 2 | void SourceFile::runPostLinkIROptimizer() { | |
450 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
|
2 | assert(cliOptions.useLTO); |
451 | |||
452 | // Skip if this is not the main source file | ||
453 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1 time.
|
2 | if (!isMainFile) |
454 | 1 | return; | |
455 | |||
456 | // Skip if restored from the cache or this stage has already been done | ||
457 |
2/4✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 8 not taken.
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
|
1 | if (restoredFromCache || previousStage >= IR_OPTIMIZER) |
458 | ✗ | return; | |
459 | |||
460 | // Skip this stage if optimization is disabled | ||
461 |
2/4✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 11 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 1 time.
|
1 | if (cliOptions.optLevel < O1 || cliOptions.optLevel > Oz) |
462 | ✗ | return; | |
463 | |||
464 |
1/2✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 60 not taken.
|
1 | Timer timer(&compilerOutput.times.irOptimizer); |
465 | 1 | timer.resume(); | |
466 | |||
467 | // Optimize LTO module | ||
468 |
1/2✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 60 not taken.
|
1 | IROptimizer irOptimizer(resourceManager, this); |
469 |
1/2✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 58 not taken.
|
1 | irOptimizer.prepare(); |
470 |
1/2✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 58 not taken.
|
1 | irOptimizer.optimizePostLink(); |
471 | |||
472 | // Save the optimized ir string in the compiler output | ||
473 |
2/4✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 19 not taken.
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 24 not taken.
|
1 | if (cliOptions.dumpSettings.dumpIR || cliOptions.testMode) { |
474 | 1 | llvm::Module *module = resourceManager.ltoModule.get(); | |
475 |
1/2✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 43 not taken.
|
1 | compilerOutput.irOptString = IRGenerator::getIRString(module, cliOptions.comparableOutput); |
476 | } | ||
477 | |||
478 | // Dump optimized IR code | ||
479 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 37 taken 1 time.
|
1 | if (cliOptions.dumpSettings.dumpIR) |
480 | ✗ | dumpOutput(compilerOutput.irOptString, "Optimized IR Code (post-Link)", "ir-code-lto-post-link.ll"); | |
481 | |||
482 | 1 | previousStage = IR_OPTIMIZER; | |
483 |
1/2✓ Branch 37 → 38 taken 1 time.
✗ Branch 37 → 58 not taken.
|
1 | timer.stop(); |
484 |
1/2✓ Branch 38 → 39 taken 1 time.
✗ Branch 38 → 56 not taken.
|
1 | printStatusMessage("IR Optimizer", IO_IR, IO_IR, compilerOutput.times.irOptimizer); |
485 | 1 | } | |
486 | |||
487 | 3230 | void SourceFile::runObjectEmitter() { | |
488 | // Skip if restored from the cache or this stage has already been done | ||
489 |
3/4✓ Branch 2 → 3 taken 3230 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 2377 times.
✓ Branch 3 → 5 taken 853 times.
|
3230 | if (restoredFromCache || previousStage >= OBJECT_EMITTER) |
490 | 2378 | return; | |
491 | |||
492 | // Skip if LTO is enabled and this is not the main source file | ||
493 |
4/4✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 8 taken 851 times.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 1 time.
|
853 | if (cliOptions.useLTO && !isMainFile) |
494 | 1 | return; | |
495 | |||
496 |
1/2✓ Branch 8 → 9 taken 852 times.
✗ Branch 8 → 72 not taken.
|
852 | Timer timer(&compilerOutput.times.objectEmitter); |
497 |
1/2✓ Branch 9 → 10 taken 852 times.
✗ Branch 9 → 72 not taken.
|
852 | timer.start(); |
498 | |||
499 | // Deduce an object file path | ||
500 |
2/4✓ Branch 10 → 11 taken 852 times.
✗ Branch 10 → 47 not taken.
✓ Branch 11 → 12 taken 852 times.
✗ Branch 11 → 45 not taken.
|
852 | std::filesystem::path objectFilePath = cliOptions.outputDir / filePath.filename(); |
501 |
2/4✓ Branch 13 → 14 taken 852 times.
✗ Branch 13 → 50 not taken.
✓ Branch 14 → 15 taken 852 times.
✗ Branch 14 → 48 not taken.
|
852 | objectFilePath.replace_extension("o"); |
502 | |||
503 | // Emit object for this source file | ||
504 |
1/2✓ Branch 16 → 17 taken 852 times.
✗ Branch 16 → 70 not taken.
|
852 | const ObjectEmitter objectEmitter(resourceManager, this); |
505 |
1/2✓ Branch 17 → 18 taken 852 times.
✗ Branch 17 → 68 not taken.
|
852 | objectEmitter.emit(objectFilePath); |
506 | |||
507 | // Save assembly string in the compiler output | ||
508 |
3/6✓ Branch 18 → 19 taken 852 times.
✗ Branch 18 → 22 not taken.
✓ Branch 19 → 20 taken 852 times.
✗ Branch 19 → 21 not taken.
✓ Branch 20 → 21 taken 852 times.
✗ Branch 20 → 22 not taken.
|
852 | if (cliOptions.isNativeTarget && (cliOptions.dumpSettings.dumpAssembly || cliOptions.testMode)) |
509 |
1/2✓ Branch 21 → 22 taken 852 times.
✗ Branch 21 → 68 not taken.
|
852 | objectEmitter.getASMString(compilerOutput.asmString); |
510 | |||
511 | // Dump assembly code | ||
512 |
1/2✗ Branch 22 → 23 not taken.
✓ Branch 22 → 35 taken 852 times.
|
852 | if (cliOptions.dumpSettings.dumpAssembly) |
513 | ✗ | dumpOutput(compilerOutput.asmString, "Assembly code", "assembly-code.s"); | |
514 | |||
515 | // Add the object file to the linker objects | ||
516 |
2/4✓ Branch 35 → 36 taken 852 times.
✗ Branch 35 → 65 not taken.
✓ Branch 36 → 37 taken 852 times.
✗ Branch 36 → 63 not taken.
|
852 | resourceManager.linker.addObjectFilePath(objectFilePath.string()); |
517 | |||
518 | 852 | previousStage = OBJECT_EMITTER; | |
519 |
1/2✓ Branch 38 → 39 taken 852 times.
✗ Branch 38 → 68 not taken.
|
852 | timer.stop(); |
520 |
1/2✓ Branch 39 → 40 taken 852 times.
✗ Branch 39 → 66 not taken.
|
852 | printStatusMessage("Object Emitter", IO_IR, IO_OBJECT_FILE, compilerOutput.times.objectEmitter); |
521 | 852 | } | |
522 | |||
523 | 3230 | void SourceFile::concludeCompilation() { | |
524 | // Skip if restored from the cache or this stage has already been done | ||
525 |
3/4✓ Branch 2 → 3 taken 3230 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 2377 times.
✓ Branch 3 → 5 taken 853 times.
|
3230 | if (restoredFromCache || previousStage >= FINISHED) |
526 | 2377 | return; | |
527 | |||
528 | // Cache the source file | ||
529 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 853 times.
|
853 | if (!cliOptions.ignoreCache) |
530 | ✗ | resourceManager.cacheManager.cacheSourceFile(this); | |
531 | |||
532 | // Save type registry as string in the compiler output | ||
533 |
4/6✓ Branch 7 → 8 taken 197 times.
✓ Branch 7 → 14 taken 656 times.
✓ Branch 8 → 9 taken 197 times.
✗ Branch 8 → 10 not taken.
✓ Branch 9 → 10 taken 197 times.
✗ Branch 9 → 14 not taken.
|
853 | if (isMainFile && (cliOptions.dumpSettings.dumpTypes || cliOptions.testMode)) |
534 |
1/2✓ Branch 10 → 11 taken 197 times.
✗ Branch 10 → 66 not taken.
|
197 | compilerOutput.typesString = TypeRegistry::dump(); |
535 | |||
536 | // Dump type registry | ||
537 |
3/4✓ Branch 14 → 15 taken 197 times.
✓ Branch 14 → 28 taken 656 times.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 28 taken 197 times.
|
853 | if (isMainFile && cliOptions.dumpSettings.dumpTypes) |
538 | ✗ | dumpOutput(compilerOutput.typesString, "Type Registry", "type-registry.out"); | |
539 | |||
540 | // Save cache statistics as string in the compiler output | ||
541 |
4/6✓ Branch 28 → 29 taken 197 times.
✓ Branch 28 → 32 taken 656 times.
✓ Branch 29 → 30 taken 197 times.
✗ Branch 29 → 31 not taken.
✓ Branch 30 → 31 taken 197 times.
✗ Branch 30 → 32 not taken.
|
853 | if (isMainFile && (cliOptions.dumpSettings.dumpCacheStats || cliOptions.testMode)) |
542 | 197 | dumpCacheStats(); | |
543 | |||
544 | // Dump lookup cache statistics | ||
545 |
3/4✓ Branch 32 → 33 taken 197 times.
✓ Branch 32 → 46 taken 656 times.
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 46 taken 197 times.
|
853 | if (isMainFile && cliOptions.dumpSettings.dumpCacheStats) |
546 | ✗ | dumpOutput(compilerOutput.cacheStats, "Cache Statistics", "cache-stats.out"); | |
547 | |||
548 | // Print warning if the verifier is disabled | ||
549 |
3/4✓ Branch 46 → 47 taken 197 times.
✓ Branch 46 → 60 taken 656 times.
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 60 taken 197 times.
|
853 | if (isMainFile && cliOptions.disableVerifier) { |
550 | const std::string warningMessage = | ||
551 | ✗ | CompilerWarning(VERIFIER_DISABLED, "The LLVM verifier passes are disabled. Please use this cli option carefully.") | |
552 | ✗ | .warningMessage; | |
553 | ✗ | std::cout << "\n" << warningMessage; | |
554 | ✗ | } | |
555 | |||
556 |
1/2✗ Branch 60 → 61 not taken.
✓ Branch 60 → 64 taken 853 times.
|
853 | if (cliOptions.printDebugOutput) |
557 | ✗ | std::cout << "Finished compiling " << fileName << std::endl; | |
558 | |||
559 | 853 | previousStage = FINISHED; | |
560 | } | ||
561 | |||
562 | 910 | void SourceFile::runFrontEnd() { // NOLINT(misc-no-recursion) | |
563 | 910 | runLexer(); | |
564 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 910 times.
|
910 | CHECK_ABORT_FLAG_V() |
565 | 910 | runParser(); | |
566 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 910 times.
|
910 | CHECK_ABORT_FLAG_V() |
567 | 910 | runCSTVisualizer(); | |
568 |
1/2✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 910 times.
|
910 | CHECK_ABORT_FLAG_V() |
569 | 910 | runASTBuilder(); | |
570 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 910 times.
|
910 | CHECK_ABORT_FLAG_V() |
571 | 910 | runASTVisualizer(); | |
572 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 910 times.
|
910 | CHECK_ABORT_FLAG_V() |
573 | 910 | runImportCollector(); | |
574 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 908 times.
|
908 | CHECK_ABORT_FLAG_V() |
575 | 908 | runSymbolTableBuilder(); | |
576 |
1/2✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 908 times.
|
908 | CHECK_ABORT_FLAG_V() |
577 | } | ||
578 | |||
579 | 383 | void SourceFile::runMiddleEnd() { | |
580 | // We need two runs here due to generics. | ||
581 | // The first run to determine all concrete function/struct/interface substantiations | ||
582 | 383 | runTypeCheckerPre(); // Visit the dependency tree from bottom to top | |
583 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 369 times.
|
369 | CHECK_ABORT_FLAG_V() |
584 | // The second run to ensure, also generic scopes are type-checked properly | ||
585 | 369 | runTypeCheckerPost(); // Visit the dependency tree from top to bottom in topological order | |
586 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 236 times.
|
236 | CHECK_ABORT_FLAG_V() |
587 | // Visualize dependency graph | ||
588 | 236 | runDependencyGraphVisualizer(); | |
589 |
1/2✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 236 times.
|
236 | CHECK_ABORT_FLAG_V() |
590 | } | ||
591 | |||
592 | 3033 | void SourceFile::runBackEnd() { // NOLINT(misc-no-recursion) | |
593 | // Run backend for all dependencies first | ||
594 |
5/8✓ Branch 2 → 3 taken 3033 times.
✗ Branch 2 → 36 not taken.
✓ Branch 3 → 4 taken 3033 times.
✗ Branch 3 → 36 not taken.
✓ Branch 4 → 5 taken 3033 times.
✗ Branch 4 → 36 not taken.
✓ Branch 10 → 6 taken 2858 times.
✓ Branch 10 → 11 taken 3033 times.
|
5891 | for (SourceFile *sourceFile : dependencies | std::views::values) |
595 |
1/2✓ Branch 7 → 8 taken 2858 times.
✗ Branch 7 → 36 not taken.
|
2858 | sourceFile->runBackEnd(); |
596 | |||
597 | 3033 | runIRGenerator(); | |
598 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 3033 times.
|
3033 | CHECK_ABORT_FLAG_V() |
599 |
2/2✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 24 taken 3032 times.
|
3033 | if (cliOptions.useLTO) { |
600 | 1 | runPreLinkIROptimizer(); | |
601 |
1/2✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1 time.
|
1 | CHECK_ABORT_FLAG_V() |
602 | 1 | runBitcodeLinker(); | |
603 |
1/2✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 1 time.
|
1 | CHECK_ABORT_FLAG_V() |
604 | 1 | runPostLinkIROptimizer(); | |
605 |
1/2✗ Branch 22 → 23 not taken.
✓ Branch 22 → 27 taken 1 time.
|
1 | CHECK_ABORT_FLAG_V() |
606 | } else { | ||
607 | 3032 | runDefaultIROptimizer(); | |
608 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 3032 times.
|
3032 | CHECK_ABORT_FLAG_V() |
609 | } | ||
610 | 3033 | runObjectEmitter(); | |
611 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 3033 times.
|
3033 | CHECK_ABORT_FLAG_V() |
612 | 3033 | concludeCompilation(); | |
613 | |||
614 |
1/2✗ Branch 31 → 32 not taken.
✓ Branch 31 → 35 taken 3033 times.
|
3033 | if (isMainFile) { |
615 | ✗ | resourceManager.totalTimer.stop(); | |
616 | ✗ | if (cliOptions.printDebugOutput) | |
617 | ✗ | dumpCompilationStats(); | |
618 | } | ||
619 | } | ||
620 | |||
621 | 1314 | void SourceFile::addDependency(SourceFile *sourceFile, const ASTNode *declNode, const std::string &dependencyName, | |
622 | const std::string &path) { | ||
623 | // Check if this would cause a circular dependency | ||
624 |
1/2✓ Branch 2 → 3 taken 1314 times.
✗ Branch 2 → 41 not taken.
|
1314 | std::stack<const SourceFile *> dependencyCircle; |
625 |
3/4✓ Branch 3 → 4 taken 1314 times.
✗ Branch 3 → 39 not taken.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 17 taken 1313 times.
|
1314 | if (isAlreadyImported(path, dependencyCircle)) { |
626 | // Build the error message | ||
627 |
1/2✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 34 not taken.
|
1 | std::stringstream errorMessage; |
628 |
3/6✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 32 not taken.
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 32 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 32 not taken.
|
1 | errorMessage << "Circular import detected while importing '" << sourceFile->fileName << "':\n\n"; |
629 |
2/4✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 25 not taken.
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 23 not taken.
|
1 | errorMessage << CommonUtil::getCircularImportMessage(dependencyCircle); |
630 |
2/4✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 29 not taken.
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 26 not taken.
|
1 | throw SemanticError(declNode, CIRCULAR_DEPENDENCY, errorMessage.str()); |
631 | 1 | } | |
632 | |||
633 | // Add the dependency | ||
634 | 1313 | sourceFile->isMainFile = false; | |
635 |
2/4✓ Branch 17 → 18 taken 1313 times.
✗ Branch 17 → 37 not taken.
✓ Branch 18 → 19 taken 1313 times.
✗ Branch 18 → 35 not taken.
|
1313 | dependencies.insert({dependencyName, sourceFile}); |
636 | |||
637 | // Add the dependant | ||
638 |
1/2✓ Branch 20 → 21 taken 1313 times.
✗ Branch 20 → 38 not taken.
|
1313 | sourceFile->dependants.push_back(this); |
639 | 1314 | } | |
640 | |||
641 | 87847 | bool SourceFile::imports(const SourceFile *sourceFile) const { | |
642 | 277639 | return std::ranges::any_of(dependencies, [=](const auto &dependency) { return dependency.second == sourceFile; }); | |
643 | } | ||
644 | |||
645 | 6198 | bool SourceFile::isAlreadyImported(const std::string &filePathSearch, // NOLINT(misc-no-recursion) | |
646 | std::stack<const SourceFile *> &circle) const { | ||
647 |
1/2✓ Branch 2 → 3 taken 6198 times.
✗ Branch 2 → 20 not taken.
|
6198 | circle.push(this); |
648 | |||
649 | // Check if the current source file corresponds to the path to search | ||
650 |
4/6✓ Branch 3 → 4 taken 6198 times.
✗ Branch 3 → 23 not taken.
✓ Branch 4 → 5 taken 6198 times.
✗ Branch 4 → 21 not taken.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 6197 times.
|
6198 | if (std::filesystem::equivalent(filePath, filePathSearch)) |
651 | 1 | return true; | |
652 | |||
653 | // Check dependants recursively | ||
654 |
2/2✓ Branch 16 → 10 taken 4884 times.
✓ Branch 16 → 17 taken 6195 times.
|
11079 | for (const SourceFile *dependant : dependants) |
655 |
3/4✓ Branch 11 → 12 taken 4884 times.
✗ Branch 11 → 24 not taken.
✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 14 taken 4882 times.
|
4884 | if (dependant->isAlreadyImported(filePathSearch, circle)) |
656 | 2 | return true; | |
657 | |||
658 | // If no dependant was found, remove the current source file from the circle to continue with the next sibling | ||
659 | 6195 | circle.pop(); | |
660 | 6195 | return false; | |
661 | } | ||
662 | |||
663 | 3290 | SourceFile *SourceFile::requestRuntimeModule(RuntimeModule runtimeModule) { | |
664 | // Check if the module was already imported | ||
665 |
2/2✓ Branch 3 → 4 taken 2570 times.
✓ Branch 3 → 6 taken 720 times.
|
3290 | if (isRuntimeModuleAvailable(runtimeModule)) |
666 | 2570 | return resourceManager.runtimeModuleManager.getModule(runtimeModule); | |
667 | 720 | return resourceManager.runtimeModuleManager.requestModule(this, runtimeModule); | |
668 | } | ||
669 | |||
670 | 3602 | bool SourceFile::isRuntimeModuleAvailable(RuntimeModule runtimeModule) const { return importedRuntimeModules & runtimeModule; } | |
671 | |||
672 | 34448 | void SourceFile::addNameRegistryEntry(const std::string &symbolName, uint64_t typeId, SymbolTableEntry *entry, Scope *scope, | |
673 | bool keepNewOnCollision, SymbolTableEntry *importEntry) { | ||
674 |
6/6✓ Branch 2 → 3 taken 8521 times.
✓ Branch 2 → 5 taken 25927 times.
✓ Branch 4 → 5 taken 8454 times.
✓ Branch 4 → 6 taken 67 times.
✓ Branch 7 → 8 taken 34381 times.
✓ Branch 7 → 13 taken 67 times.
|
34448 | if (keepNewOnCollision || !exportedNameRegistry.contains(symbolName)) // Overwrite potential existing entry |
675 | 34381 | exportedNameRegistry[symbolName] = {symbolName, typeId, entry, scope, importEntry}; | |
676 | else // Name collision => we must remove the existing entry | ||
677 | 67 | exportedNameRegistry.erase(symbolName); | |
678 |
2/6✓ Branch 8 → 9 taken 34381 times.
✗ Branch 8 → 20 not taken.
✓ Branch 9 → 10 taken 34381 times.
✗ Branch 9 → 15 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
|
68829 | } |
679 | |||
680 | 191405 | const NameRegistryEntry *SourceFile::getNameRegistryEntry(const std::string &symbolName) const { | |
681 |
2/2✓ Branch 3 → 4 taken 96317 times.
✓ Branch 3 → 5 taken 95088 times.
|
191405 | if (!exportedNameRegistry.contains(symbolName)) |
682 | 96317 | return nullptr; | |
683 | |||
684 | // Resolve registry entry for the given name | ||
685 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 95088 times.
|
95088 | assert(exportedNameRegistry.contains(symbolName)); |
686 | 95088 | const NameRegistryEntry *entry = &exportedNameRegistry.at(symbolName); | |
687 | |||
688 | // Mark the import entry as used | ||
689 |
2/2✓ Branch 9 → 10 taken 5085 times.
✓ Branch 9 → 11 taken 90003 times.
|
95088 | if (entry->importEntry != nullptr) |
690 | 5085 | entry->importEntry->used = true; | |
691 | |||
692 | 95088 | return entry; | |
693 | } | ||
694 | |||
695 | 228133 | llvm::Type *SourceFile::getLLVMType(const Type *type) { | |
696 | // Check if the type is already in the mapping | ||
697 |
1/2✓ Branch 2 → 3 taken 228133 times.
✗ Branch 2 → 13 not taken.
|
228133 | const auto it = typeToLLVMTypeMapping.find(type); |
698 |
2/2✓ Branch 5 → 6 taken 221163 times.
✓ Branch 5 → 8 taken 6970 times.
|
228133 | if (it != typeToLLVMTypeMapping.end()) |
699 | 221163 | return it->second; | |
700 | |||
701 | // If not, generate the LLVM type | ||
702 |
1/2✓ Branch 8 → 9 taken 6970 times.
✗ Branch 8 → 13 not taken.
|
6970 | llvm::Type *llvmType = type->toLLVMType(this); |
703 |
1/2✓ Branch 9 → 10 taken 6970 times.
✗ Branch 9 → 13 not taken.
|
6970 | typeToLLVMTypeMapping[type] = llvmType; |
704 | 6970 | return llvmType; | |
705 | } | ||
706 | |||
707 | 2172 | void SourceFile::checkForSoftErrors() const { | |
708 | // Check if there are any soft errors and if so, print them | ||
709 |
2/2✓ Branch 3 → 4 taken 104 times.
✓ Branch 3 → 19 taken 2068 times.
|
2172 | if (!resourceManager.errorManager.softErrors.empty()) { |
710 |
1/2✓ Branch 4 → 5 taken 104 times.
✗ Branch 4 → 29 not taken.
|
104 | std::stringstream errorStream; |
711 |
1/2✓ Branch 5 → 6 taken 104 times.
✗ Branch 5 → 27 not taken.
|
104 | errorStream << "There are unresolved errors. Please fix them and recompile."; |
712 |
2/2✓ Branch 13 → 8 taken 126 times.
✓ Branch 13 → 14 taken 104 times.
|
230 | for (const auto &[codeLoc, message] : resourceManager.errorManager.softErrors) |
713 |
2/4✓ Branch 9 → 10 taken 126 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 126 times.
✗ Branch 10 → 20 not taken.
|
126 | errorStream << "\n\n" << message; |
714 |
2/4✓ Branch 15 → 16 taken 104 times.
✗ Branch 15 → 24 not taken.
✓ Branch 16 → 17 taken 104 times.
✗ Branch 16 → 21 not taken.
|
104 | throw CompilerError(UNRESOLVED_SOFT_ERRORS, errorStream.str()); |
715 | 104 | } | |
716 | 2068 | } | |
717 | |||
718 | 270 | void SourceFile::collectAndPrintWarnings() { // NOLINT(misc-no-recursion) | |
719 | // Print warnings for all dependencies | ||
720 |
5/8✓ Branch 2 → 3 taken 270 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 270 times.
✗ Branch 3 → 23 not taken.
✓ Branch 4 → 5 taken 270 times.
✗ Branch 4 → 23 not taken.
✓ Branch 11 → 6 taken 236 times.
✓ Branch 11 → 12 taken 270 times.
|
506 | for (SourceFile *sourceFile : dependencies | std::views::values) |
721 |
2/2✓ Branch 7 → 8 taken 34 times.
✓ Branch 7 → 9 taken 202 times.
|
236 | if (!sourceFile->isStdFile) |
722 |
1/2✓ Branch 8 → 9 taken 34 times.
✗ Branch 8 → 23 not taken.
|
34 | sourceFile->collectAndPrintWarnings(); |
723 | // Collect warnings for this file | ||
724 |
2/2✓ Branch 12 → 13 taken 268 times.
✓ Branch 12 → 15 taken 2 times.
|
270 | if (!ignoreWarnings) |
725 | 268 | globalScope->collectWarnings(compilerOutput.warnings); | |
726 | // Print warnings for this file | ||
727 |
2/2✓ Branch 21 → 17 taken 220 times.
✓ Branch 21 → 22 taken 270 times.
|
490 | for (const CompilerWarning &warning : compilerOutput.warnings) |
728 |
1/2✓ Branch 18 → 19 taken 220 times.
✗ Branch 18 → 24 not taken.
|
220 | warning.print(); |
729 | 270 | } | |
730 | |||
731 | 4100 | const SourceFile *SourceFile::getRootSourceFile() const { // NOLINT(misc-no-recursion) | |
732 |
2/2✓ Branch 2 → 3 taken 1487 times.
✓ Branch 2 → 4 taken 2613 times.
|
4100 | return isMainFile ? this : parent->getRootSourceFile(); |
733 | } | ||
734 | |||
735 | 10626 | bool SourceFile::isRT(RuntimeModule runtimeModule) const { | |
736 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 10626 times.
|
10626 | assert(IDENTIFYING_TOP_LEVEL_NAMES.contains(runtimeModule)); |
737 | 10626 | const char *topLevelName = IDENTIFYING_TOP_LEVEL_NAMES.at(runtimeModule); | |
738 |
4/6✓ Branch 8 → 9 taken 10626 times.
✗ Branch 8 → 26 not taken.
✓ Branch 9 → 10 taken 10626 times.
✗ Branch 9 → 24 not taken.
✓ Branch 12 → 13 taken 1612 times.
✓ Branch 12 → 14 taken 9014 times.
|
31878 | if (!exportedNameRegistry.contains(topLevelName)) |
739 | 1612 | return false; | |
740 |
2/4✓ Branch 16 → 17 taken 9014 times.
✗ Branch 16 → 32 not taken.
✓ Branch 17 → 18 taken 9014 times.
✗ Branch 17 → 30 not taken.
|
27042 | return exportedNameRegistry.at(topLevelName).targetEntry->scope == globalScope.get(); |
741 | } | ||
742 | |||
743 | 2708 | bool SourceFile::haveAllDependantsBeenTypeChecked() const { | |
744 | 8197 | return std::ranges::all_of(dependants, [](const SourceFile *dependant) { return dependant->totalTypeCheckerRuns >= 1; }); | |
745 | } | ||
746 | |||
747 | /** | ||
748 | * Acquire all publicly visible symbols from the imported source file and put them in the name registry of the current one. | ||
749 | * But only do that for the symbols that are actually defined in the imported source file. Do not allow transitive dependencies. | ||
750 | * Here, we also register privately visible symbols to know that the symbol exist. The error handling regarding the visibility | ||
751 | * is issued later in the pipeline. | ||
752 | * | ||
753 | * @param importedSourceFile Imported source file | ||
754 | * @param importName First fragment of all fully qualified symbol names from that import | ||
755 | */ | ||
756 | 1309 | void SourceFile::mergeNameRegistries(const SourceFile &importedSourceFile, const std::string &importName) { | |
757 | // Retrieve import entry | ||
758 | 1309 | SymbolTableEntry *importEntry = globalScope->lookupStrict(importName); | |
759 |
3/4✓ Branch 6 → 7 taken 720 times.
✓ Branch 6 → 10 taken 589 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 720 times.
|
1309 | assert(importEntry != nullptr || importName.starts_with("__")); // Runtime imports start with two underscores |
760 | |||
761 |
2/2✓ Branch 31 → 12 taken 37025 times.
✓ Branch 31 → 32 taken 1309 times.
|
38334 | for (const auto &[originalName, entry] : importedSourceFile.exportedNameRegistry) { |
762 | // Skip if we introduce a transitive dependency | ||
763 |
2/2✓ Branch 16 → 17 taken 18305 times.
✓ Branch 16 → 18 taken 18720 times.
|
37025 | if (entry.targetScope->sourceFile->globalScope != importedSourceFile.globalScope) |
764 | 18305 | continue; | |
765 | // Add the fully qualified name | ||
766 |
1/2✓ Branch 18 → 19 taken 18720 times.
✗ Branch 18 → 44 not taken.
|
18720 | std::string newName = importName; |
767 |
1/2✓ Branch 19 → 20 taken 18720 times.
✗ Branch 19 → 42 not taken.
|
18720 | newName += SCOPE_ACCESS_TOKEN; |
768 |
1/2✓ Branch 20 → 21 taken 18720 times.
✗ Branch 20 → 42 not taken.
|
18720 | newName += originalName; |
769 |
1/2✓ Branch 23 → 24 taken 18720 times.
✗ Branch 23 → 33 not taken.
|
18720 | exportedNameRegistry.insert({newName, {newName, entry.typeId, entry.targetEntry, entry.targetScope, importEntry}}); |
770 | // Add the shortened name, considering the name collision | ||
771 | 18720 | const bool keepOnCollision = importedSourceFile.alwaysKeepSymbolsOnNameCollision; | |
772 |
1/2✓ Branch 26 → 27 taken 18720 times.
✗ Branch 26 → 42 not taken.
|
18720 | addNameRegistryEntry(originalName, entry.typeId, entry.targetEntry, entry.targetScope, keepOnCollision, importEntry); |
773 | 18720 | } | |
774 |
2/6✓ Branch 21 → 22 taken 18720 times.
✗ Branch 21 → 40 not taken.
✓ Branch 22 → 23 taken 18720 times.
✗ Branch 22 → 35 not taken.
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 39 not taken.
|
20029 | } |
775 | |||
776 | 197 | void SourceFile::dumpCacheStats() { | |
777 |
1/2✓ Branch 2 → 3 taken 197 times.
✗ Branch 2 → 32 not taken.
|
197 | std::stringstream cacheStats; |
778 |
3/6✓ Branch 3 → 4 taken 197 times.
✗ Branch 3 → 22 not taken.
✓ Branch 4 → 5 taken 197 times.
✗ Branch 4 → 20 not taken.
✓ Branch 5 → 6 taken 197 times.
✗ Branch 5 → 20 not taken.
|
197 | cacheStats << FunctionManager::dumpLookupCacheStatistics() << std::endl; |
779 |
3/6✓ Branch 7 → 8 taken 197 times.
✗ Branch 7 → 25 not taken.
✓ Branch 8 → 9 taken 197 times.
✗ Branch 8 → 23 not taken.
✓ Branch 9 → 10 taken 197 times.
✗ Branch 9 → 23 not taken.
|
197 | cacheStats << StructManager::dumpLookupCacheStatistics() << std::endl; |
780 |
3/6✓ Branch 11 → 12 taken 197 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 197 times.
✗ Branch 12 → 26 not taken.
✓ Branch 13 → 14 taken 197 times.
✗ Branch 13 → 26 not taken.
|
197 | cacheStats << InterfaceManager::dumpLookupCacheStatistics() << std::endl; |
781 |
1/2✓ Branch 15 → 16 taken 197 times.
✗ Branch 15 → 29 not taken.
|
197 | compilerOutput.cacheStats = cacheStats.str(); |
782 | 197 | } | |
783 | |||
784 | ✗ | void SourceFile::dumpCompilationStats() const { | |
785 | ✗ | const size_t sourceFileCount = resourceManager.sourceFiles.size(); | |
786 | ✗ | const size_t totalLineCount = resourceManager.getTotalLineCount(); | |
787 | ✗ | const size_t totalTypeCount = TypeRegistry::getTypeCount(); | |
788 | ✗ | const size_t allocatedBytes = resourceManager.astNodeAlloc.getTotalAllocatedSize(); | |
789 | ✗ | const size_t allocationCount = resourceManager.astNodeAlloc.getAllocationCount(); | |
790 | ✗ | const size_t totalDuration = resourceManager.totalTimer.getDurationMilliseconds(); | |
791 | ✗ | std::cout << "\nSuccessfully compiled " << std::to_string(sourceFileCount) << " source file(s)"; | |
792 | ✗ | std::cout << " or " << std::to_string(totalLineCount) << " lines in total.\n"; | |
793 | ✗ | std::cout << "Total number of blocks allocated via BlockAllocator: " << CommonUtil::formatBytes(allocatedBytes); | |
794 | ✗ | std::cout << " in " << std::to_string(allocationCount) << " allocations.\n"; | |
795 | #ifndef NDEBUG | ||
796 | ✗ | resourceManager.astNodeAlloc.printAllocatedClassStatistic(); | |
797 | #endif | ||
798 | ✗ | std::cout << "Total number of types: " << std::to_string(totalTypeCount) << "\n"; | |
799 | ✗ | std::cout << "Total compile time: " << std::to_string(totalDuration) << " ms\n"; | |
800 | ✗ | } | |
801 | |||
802 | ✗ | void SourceFile::dumpOutput(const std::string &content, const std::string &caption, const std::string &fileSuffix) const { | |
803 | ✗ | if (cliOptions.dumpSettings.dumpToFiles) { | |
804 | // Dump to file | ||
805 | ✗ | const std::string dumpFileName = filePath.stem().string() + "-" + fileSuffix; | |
806 | ✗ | std::filesystem::path dumpFilePath = cliOptions.outputDir / dumpFileName; | |
807 | ✗ | dumpFilePath.make_preferred(); | |
808 | ✗ | FileUtil::writeToFile(dumpFilePath, content); | |
809 | ✗ | } else { | |
810 | // Dump to console | ||
811 | ✗ | std::cout << "\n" << caption << ":\n" << content; | |
812 | } | ||
813 | |||
814 | // If the abort after dump is requested, set the abort compilation flag | ||
815 | ✗ | if (cliOptions.dumpSettings.abortAfterDump) { | |
816 | // If this is an IR dump whilst having optimization enabled, we may not abort when dumping unoptimized IR, | ||
817 | // because we also have to dump the optimized IR | ||
818 | ✗ | if (cliOptions.dumpSettings.dumpIR && fileSuffix == "ir-code.ll") { | |
819 | ✗ | resourceManager.abortCompilation = cliOptions.optLevel == O0; | |
820 | } else { | ||
821 | ✗ | resourceManager.abortCompilation = true; | |
822 | } | ||
823 | } | ||
824 | ✗ | } | |
825 | |||
826 | 1678 | void SourceFile::visualizerPreamble(std::stringstream &output) const { | |
827 |
2/2✓ Branch 2 → 3 taken 252 times.
✓ Branch 2 → 4 taken 1426 times.
|
1678 | if (isMainFile) |
828 | 252 | output << "digraph {\n rankdir=\"TB\";\n"; | |
829 | else | ||
830 | 1426 | output << "subgraph {\n"; | |
831 |
3/6✓ Branch 6 → 7 taken 1678 times.
✗ Branch 6 → 13 not taken.
✓ Branch 7 → 8 taken 1678 times.
✗ Branch 7 → 11 not taken.
✓ Branch 8 → 9 taken 1678 times.
✗ Branch 8 → 11 not taken.
|
1678 | output << " label=\"" << filePath.generic_string() << "\";\n"; |
832 | 1678 | } | |
833 | |||
834 | ✗ | void SourceFile::visualizerOutput(std::string outputName, const std::string &output) const { | |
835 | ✗ | if (cliOptions.dumpSettings.dumpToFiles) { | |
836 | // Check if graphviz is installed | ||
837 | // GCOV_EXCL_START | ||
838 | − | if (!FileUtil::isGraphvizInstalled()) | |
839 | − | throw CompilerError(IO_ERROR, "Please check if you have installed Graphviz and added it to the PATH variable"); | |
840 | // GCOV_EXCL_STOP | ||
841 | |||
842 | // Write to a dot file | ||
843 | ✗ | std::ranges::transform(outputName, outputName.begin(), ::tolower); | |
844 | ✗ | dumpOutput(output, outputName, outputName + ".dot"); | |
845 | |||
846 | // Generate SVG. This only works if the dot code was dumped into a file | ||
847 | ✗ | std::cout << "\nGenerating SVG file ... "; | |
848 | ✗ | const std::string dotFileName = filePath.stem().string() + "-" + outputName + ".dot"; | |
849 | ✗ | std::filesystem::path dotFilePath = cliOptions.outputDir / dotFileName; | |
850 | ✗ | std::filesystem::path svgFilePath = dotFilePath; | |
851 | ✗ | svgFilePath.replace_extension("svg"); | |
852 | ✗ | dotFilePath.make_preferred(); | |
853 | ✗ | svgFilePath.make_preferred(); | |
854 | ✗ | FileUtil::exec("dot -T svg -o" + svgFilePath.string() + " " + dotFilePath.string()); | |
855 | ✗ | std::cout << "done.\nSVG file can be found at: " << svgFilePath << "\n"; | |
856 | ✗ | } else { | |
857 | // Dump to console | ||
858 | ✗ | std::cout << "\nSerialized " << outputName << ":\n\n" << output << "\n"; | |
859 | } | ||
860 | |||
861 | // If the abort after dump is requested, set the abort compilation flag | ||
862 | ✗ | if (cliOptions.dumpSettings.abortAfterDump) | |
863 | ✗ | resourceManager.abortCompilation = true; | |
864 | ✗ | } | |
865 | |||
866 | 12178 | void SourceFile::printStatusMessage(const char *stage, const CompileStageIOType &in, const CompileStageIOType &out, | |
867 | uint64_t stageRuntime, unsigned short stageRuns) const { | ||
868 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 30 taken 12178 times.
|
12178 | if (cliOptions.printDebugOutput) { |
869 | static constexpr const char *const compilerStageIoTypeName[6] = {"Code", "Tokens", "CST", "AST", "IR", "Obj"}; | ||
870 | // Build output string | ||
871 | ✗ | std::stringstream outputStr; | |
872 | ✗ | outputStr << "[" << stage << "] for " << fileName << ": "; | |
873 | ✗ | outputStr << compilerStageIoTypeName[in] << " --> " << compilerStageIoTypeName[out]; | |
874 | ✗ | outputStr << " (" << std::to_string(stageRuntime) << " ms"; | |
875 | ✗ | if (stageRuns > 0) | |
876 | ✗ | outputStr << "; " << std::to_string(stageRuns) << " run(s)"; | |
877 | ✗ | outputStr << ")\n"; | |
878 | |||
879 | ✗ | std::cout << outputStr.str(); | |
880 | ✗ | } | |
881 | 12178 | } | |
882 | |||
883 | } // namespace spice::compiler | ||
884 |