Skip to content

Commit f899634

Browse files
committed
refactor(driver): split processPreCompilationOptions into separate steps
1 parent b5edc57 commit f899634

2 files changed

Lines changed: 85 additions & 26 deletions

File tree

tools/gluc/sources/CompilerDriver.cpp

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void CompilerDriver::printTokens()
245245
}
246246
}
247247

248-
bool CompilerDriver::configureParser()
248+
bool CompilerDriver::configureScanner()
249249
{
250250
_fileID.emplace(_sourceManager.loadFile(_config.inputFile));
251251

@@ -256,24 +256,31 @@ bool CompilerDriver::configureParser()
256256
}
257257

258258
_scanner.emplace(_sourceManager.getBuffer(**_fileID));
259-
260-
_parser.emplace(
261-
_scanner.value(), _context.value(), _sourceManager, _diagManager.value()
262-
);
263259
return true;
264260
}
265261

266-
int CompilerDriver::processPreCompilationOptions()
262+
int CompilerDriver::runParser()
267263
{
268-
_ast = llvm::cast<glu::ast::ModuleDecl>(_parser->getAST());
264+
glu::Parser parser(
265+
_scanner.value(), _context.value(), _sourceManager, _diagManager.value()
266+
);
267+
268+
if (!parser.parse() || _diagManager->hasErrors()) {
269+
return 1;
270+
}
269271

270-
assert(_ast && "AST should always exist if parse is successful");
272+
_ast = llvm::cast<glu::ast::ModuleDecl>(parser.getAST());
271273

272274
if (_config.stage == PrintASTGen) {
273275
_ast->print(*_outputStream);
274276
return 0;
275277
}
276278

279+
return 0;
280+
}
281+
282+
int CompilerDriver::runSema()
283+
{
277284
sema::constrainAST(
278285
_ast, *_diagManager, &(*_importManager),
279286
_config.stage == PrintConstraints
@@ -298,19 +305,27 @@ int CompilerDriver::processPreCompilationOptions()
298305
return 1;
299306
}
300307

308+
return 0;
309+
}
310+
311+
int CompilerDriver::runGILGen()
312+
{
301313
glu::gilgen::GILGen gilgen;
302314

303-
_gilModule.emplace(gilgen.generateModule(_ast, _GILFuncArena));
315+
_gilModule.emplace(gilgen.generateModule(_ast, _gilArena));
304316

305317
if (_config.stage == PrintGILGen) {
306318
// Print all functions in the generated function list
307319
_gilPrinter->visit(*_gilModule);
308-
return 0;
309320
}
310321

322+
return 0;
323+
}
324+
325+
int CompilerDriver::runOptimizer()
326+
{
311327
glu::optimizer::PassManager passManager(
312-
*_diagManager, _sourceManager, *_outputStream, *_gilModule,
313-
_GILFuncArena
328+
*_diagManager, _sourceManager, *_outputStream, *_gilModule, _gilArena
314329
);
315330
passManager.runPasses();
316331

@@ -323,7 +338,11 @@ int CompilerDriver::processPreCompilationOptions()
323338
if (_diagManager->hasErrors()) {
324339
return 1;
325340
}
341+
return 0;
342+
}
326343

344+
int CompilerDriver::runIRGen()
345+
{
327346
glu::irgen::IRGen irgen;
328347
_llvmModule.emplace(
329348
_sourceManager.getBufferName(
@@ -502,7 +521,7 @@ int CompilerDriver::executeCompilation(char const *argv0)
502521
_importManager.emplace(*_context, *_diagManager, _config.importDirs);
503522

504523
// Configure parser
505-
if (!configureParser()) {
524+
if (!configureScanner()) {
506525
return 1;
507526
}
508527

@@ -513,20 +532,45 @@ int CompilerDriver::executeCompilation(char const *argv0)
513532
}
514533

515534
// Parse the source code
516-
if (!_parser->parse() || _diagManager->hasErrors()) {
535+
if (runParser()) {
517536
return 1;
518537
}
519538

539+
if (_config.stage <= PrintASTGen) {
540+
return 0;
541+
}
542+
520543
// Process pre-compilation options
521-
auto compileResult = processPreCompilationOptions();
544+
if (runSema()) {
545+
return 1;
546+
}
522547

523-
// Handle early exit cases for print options and bitcode emission
524-
if (_config.stage <= EmitBitcode) {
525-
return compileResult;
548+
if (_config.stage <= PrintAST) {
549+
return 0;
550+
}
551+
552+
if (runGILGen()) {
553+
return 1;
554+
}
555+
556+
if (_config.stage <= PrintGILGen) {
557+
return 0;
558+
}
559+
560+
if (runOptimizer()) {
561+
return 1;
562+
}
563+
564+
if (_config.stage <= PrintGIL) {
565+
return 0;
566+
}
567+
568+
if (runIRGen()) {
569+
return 1;
526570
}
527571

528-
if (compileResult != 0) {
529-
return compileResult;
572+
if (_config.stage <= EmitBitcode) {
573+
return 0;
530574
}
531575

532576
// Verify generated IR

tools/gluc/sources/CompilerDriver.hpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,12 @@ class CompilerDriver {
7474
_diagManager; ///< Handles error/warning reporting
7575
std::optional<glu::ast::ASTContext>
7676
_context; ///< AST memory management and context
77-
std::optional<glu::Parser> _parser; ///< Parses tokens into AST
7877
std::optional<glu::Scanner> _scanner; ///< Tokenizes source code
7978
std::optional<glu::sema::ImportManager>
8079
_importManager; ///< Handles module imports
8180

8281
// Code generation components
83-
llvm::BumpPtrAllocator _GILFuncArena; ///< Memory arena for GIL functions
82+
llvm::BumpPtrAllocator _gilArena; ///< Memory arena for GIL functions
8483
std::optional<glu::gil::GILPrinter>
8584
_gilPrinter; ///< Prints GIL representation
8685
llvm::LLVMContext _llvmContext; ///< LLVM context for IR generation
@@ -130,16 +129,32 @@ class CompilerDriver {
130129

131130
/// @brief Configure the parser with loaded source file and create scanner
132131
/// @return True if successful, false otherwise
133-
bool configureParser();
132+
bool configureScanner();
134133

135134
/// @brief Print tokens for debugging (when --print-tokens is specified)
136135
void printTokens();
137136

138-
/// @brief Process pre-compilation options like AST/GIL/IR printing
137+
/// @brief Run the parser to generate the AST
139138
/// @return Exit code (0 for success, non-zero for error)
140-
int processPreCompilationOptions();
139+
int runParser();
141140

142-
/// @brief Perform the main compilation steps (parsing, sema, codegen)
141+
/// @brief Run semantic analysis on the AST
142+
/// @return Exit code (0 for success, non-zero for error)
143+
int runSema();
144+
145+
/// @brief Run GIL generation from the AST
146+
/// @return Exit code (0 for success, non-zero for error)
147+
int runGILGen();
148+
149+
/// @brief Run optimization passes on the GIL module
150+
/// @return Exit code (0 for success, non-zero for error)
151+
int runOptimizer();
152+
153+
/// @brief Run LLVM IR generation from the GIL module
154+
/// @return Exit code (0 for success, non-zero for error)
155+
int runIRGen();
156+
157+
/// @brief Compile the generated LLVM IR to object code or assembly
143158
/// @return Exit code (0 for success, non-zero for error)
144159
int compile();
145160

0 commit comments

Comments
 (0)