Skip to content

Commit d4fabb0

Browse files
committed
Adjusted options of PL/0
1 parent 0388e3e commit d4fabb0

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

pl0/pl0.cc

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -495,37 +495,42 @@ struct Interpreter {
495495
* LLVM
496496
*/
497497
struct LLVM {
498-
LLVM() : builder_(context_) {
498+
LLVM(const shared_ptr<AstPL0> ast) : builder_(context_) {
499499
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);
507501
}
508502

509503
void dump() { module_->dump(); }
510504

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+
}
515511

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+
}
518516

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();
522520
}
523521

524522
private:
525523
LLVMContext context_;
526524
IRBuilder<> builder_;
527525
unique_ptr<Module> module_;
528526

527+
void compile(const shared_ptr<AstPL0> ast) {
528+
InitializeNativeTarget();
529+
InitializeNativeTargetAsmPrinter();
530+
compile_libs();
531+
compile_program(ast);
532+
}
533+
529534
void compile_switch(const shared_ptr<AstPL0> ast) {
530535
switch (ast->tag) {
531536
case "assignment"_:
@@ -878,13 +883,16 @@ int main(int argc, const char** argv) {
878883
// Parse the source and make an AST
879884
shared_ptr<AstPL0> ast;
880885
if (parser.parse_n(source.data(), source.size(), ast, path)) {
881-
bool opt_llvm = false;
886+
bool opt_jit = false;
882887
bool opt_ast = false;
888+
bool opt_llvm = false;
883889
{
884890
auto argi = 2;
885891
while (argi < argc) {
886892
if (string("--ast") == argv[argi]) {
887893
opt_ast = true;
894+
} else if (string("--jit") == argv[argi]) {
895+
opt_jit = true;
888896
} else if (string("--llvm") == argv[argi]) {
889897
opt_llvm = true;
890898
}
@@ -898,11 +906,18 @@ int main(int argc, const char** argv) {
898906

899907
try {
900908
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+
}
903917
} else {
904918
Interpreter::exec(ast);
905919
}
920+
906921
} catch (const runtime_error& e) {
907922
cerr << e.what() << endl;
908923
}

0 commit comments

Comments
 (0)