@@ -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
0 commit comments