@@ -495,37 +495,42 @@ struct Interpreter {
495
495
* LLVM
496
496
*/
497
497
struct LLVM {
498
- LLVM () : builder_(context_) {
498
+ LLVM (const shared_ptr<AstPL0> ast ) : builder_(context_) {
499
499
module_ = make_unique<Module>(" pl0" , context_);
500
- }
501
-
502
- void compile (const shared_ptr<AstPL0> ast) {
503
- InitializeNativeTarget ();
504
- InitializeNativeTargetAsmPrinter ();
505
- compile_libs ();
506
- compile_program (ast);
500
+ compile (ast);
507
501
}
508
502
509
503
void dump () { module_->dump (); }
510
504
511
- static void exec (const shared_ptr<AstPL0> ast) {
512
- LLVM compiler;
513
- compiler.compile (ast);
514
- // compiler.dump();
505
+ void exec () {
506
+ unique_ptr<ExecutionEngine> ee (EngineBuilder (std::move (module_)).create ());
507
+ std::vector<GenericValue> noargs;
508
+ auto fn = ee->FindFunctionNamed (" __main__" );
509
+ auto ret = ee->runFunction (fn, noargs);
510
+ }
515
511
516
- auto EE = EngineBuilder (std::move (compiler.module_ )).create ();
517
- std::unique_ptr<ExecutionEngine> ExecutionEngineOwner (EE);
512
+ static void dump (const shared_ptr<AstPL0> ast) {
513
+ LLVM compiler (ast);
514
+ compiler.dump ();
515
+ }
518
516
519
- std::vector<GenericValue> noargs;
520
- auto fn = EE-> FindFunctionNamed ( " __main__ " );
521
- auto ret = EE-> runFunction (fn, noargs );
517
+ static void exec ( const shared_ptr<AstPL0> ast) {
518
+ LLVM compiler (ast );
519
+ compiler. exec ( );
522
520
}
523
521
524
522
private:
525
523
LLVMContext context_;
526
524
IRBuilder<> builder_;
527
525
unique_ptr<Module> module_;
528
526
527
+ void compile (const shared_ptr<AstPL0> ast) {
528
+ InitializeNativeTarget ();
529
+ InitializeNativeTargetAsmPrinter ();
530
+ compile_libs ();
531
+ compile_program (ast);
532
+ }
533
+
529
534
void compile_switch (const shared_ptr<AstPL0> ast) {
530
535
switch (ast->tag ) {
531
536
case " assignment" _:
@@ -878,13 +883,16 @@ int main(int argc, const char** argv) {
878
883
// Parse the source and make an AST
879
884
shared_ptr<AstPL0> ast;
880
885
if (parser.parse_n (source.data (), source.size (), ast, path)) {
881
- bool opt_llvm = false ;
886
+ bool opt_jit = false ;
882
887
bool opt_ast = false ;
888
+ bool opt_llvm = false ;
883
889
{
884
890
auto argi = 2 ;
885
891
while (argi < argc) {
886
892
if (string (" --ast" ) == argv[argi]) {
887
893
opt_ast = true ;
894
+ } else if (string (" --jit" ) == argv[argi]) {
895
+ opt_jit = true ;
888
896
} else if (string (" --llvm" ) == argv[argi]) {
889
897
opt_llvm = true ;
890
898
}
@@ -898,11 +906,18 @@ int main(int argc, const char** argv) {
898
906
899
907
try {
900
908
SymbolTable::build_on_ast (ast);
901
- if (opt_llvm) {
902
- LLVM::exec (ast);
909
+
910
+ if (opt_llvm || opt_jit) {
911
+ if (opt_llvm) {
912
+ LLVM::dump (ast);
913
+ }
914
+ if (opt_jit) {
915
+ LLVM::exec (ast);
916
+ }
903
917
} else {
904
918
Interpreter::exec (ast);
905
919
}
920
+
906
921
} catch (const runtime_error& e) {
907
922
cerr << e.what () << endl;
908
923
}
0 commit comments