1
1
//
2
- // pl0.cc - PL/0 interpreter (https://en.wikipedia.org/wiki/PL/0)
2
+ // pl0.cc - PL/0 language (https://en.wikipedia.org/wiki/PL/0)
3
3
//
4
4
// Copyright (c) 2015 Yuji Hirose. All rights reserved.
5
5
// MIT License
@@ -79,7 +79,6 @@ struct SymbolScope;
79
79
80
80
struct Annotation {
81
81
shared_ptr<SymbolScope> scope;
82
- shared_ptr<vector<string>> freeVariables;
83
82
};
84
83
85
84
typedef AstBase<Annotation> AstPL0;
@@ -538,20 +537,10 @@ struct LLVM {
538
537
void exec () {
539
538
unique_ptr<ExecutionEngine> ee (EngineBuilder (std::move (module_)).create ());
540
539
std::vector<GenericValue> noargs;
541
- auto fn = ee->FindFunctionNamed (" __main__ " );
540
+ auto fn = ee->FindFunctionNamed (" main " );
542
541
auto ret = ee->runFunction (fn, noargs);
543
542
}
544
543
545
- static void dump (const shared_ptr<AstPL0> ast) {
546
- LLVM compiler (ast);
547
- compiler.dump ();
548
- }
549
-
550
- static void exec (const shared_ptr<AstPL0> ast) {
551
- LLVM compiler (ast);
552
- compiler.exec ();
553
- }
554
-
555
544
private:
556
545
LLVMContext context_;
557
546
IRBuilder<> builder_;
@@ -631,7 +620,7 @@ struct LLVM {
631
620
632
621
void compile_program (const shared_ptr<AstPL0> ast) {
633
622
auto fn = cast<Function>(module_->getOrInsertFunction (
634
- " __main__ " , builder_.getVoidTy (), nullptr ));
623
+ " main " , builder_.getVoidTy (), nullptr ));
635
624
{
636
625
auto BB = BasicBlock::Create (context_, " entry" , fn);
637
626
builder_.SetInsertPoint (BB);
@@ -901,14 +890,31 @@ struct LLVM {
901
890
*/
902
891
int main (int argc, const char ** argv) {
903
892
if (argc < 2 ) {
904
- cout << " usage: pl0 PATH [--ast]" << endl;
893
+ cout << " usage: pl0 PATH [--ast] [--llvm] [--jit] " << endl;
905
894
return 1 ;
906
895
}
907
896
908
- // Read a source file into memory
897
+ // Parser commandline parameters
909
898
auto path = argv[1 ];
910
- vector<char > source;
899
+ bool opt_jit = false ;
900
+ bool opt_ast = false ;
901
+ bool opt_llvm = false ;
902
+ {
903
+ auto argi = 2 ;
904
+ while (argi < argc) {
905
+ if (string (" --ast" ) == argv[argi]) {
906
+ opt_ast = true ;
907
+ } else if (string (" --jit" ) == argv[argi]) {
908
+ opt_jit = true ;
909
+ } else if (string (" --llvm" ) == argv[argi]) {
910
+ opt_llvm = true ;
911
+ }
912
+ argi++;
913
+ }
914
+ }
911
915
916
+ // Read a source file into memory
917
+ vector<char > source;
912
918
ifstream ifs (path, ios::in | ios::binary);
913
919
if (ifs.fail ()) {
914
920
cerr << " can't open the source file." << endl;
@@ -930,23 +936,6 @@ int main(int argc, const char** argv) {
930
936
// Parse the source and make an AST
931
937
shared_ptr<AstPL0> ast;
932
938
if (parser.parse_n (source.data (), source.size (), ast, path)) {
933
- bool opt_jit = false ;
934
- bool opt_ast = false ;
935
- bool opt_llvm = false ;
936
- {
937
- auto argi = 2 ;
938
- while (argi < argc) {
939
- if (string (" --ast" ) == argv[argi]) {
940
- opt_ast = true ;
941
- } else if (string (" --jit" ) == argv[argi]) {
942
- opt_jit = true ;
943
- } else if (string (" --llvm" ) == argv[argi]) {
944
- opt_llvm = true ;
945
- }
946
- argi++;
947
- }
948
- }
949
-
950
939
try {
951
940
SymbolTable::build_on_ast (ast);
952
941
@@ -955,11 +944,13 @@ int main(int argc, const char** argv) {
955
944
}
956
945
957
946
if (opt_llvm || opt_jit) {
947
+ LLVM compiler (ast);
948
+
958
949
if (opt_llvm) {
959
- LLVM:: dump (ast );
950
+ compiler. dump ();
960
951
}
961
952
if (opt_jit) {
962
- LLVM:: exec (ast );
953
+ compiler. exec ();
963
954
}
964
955
} else {
965
956
Interpreter::exec (ast);
0 commit comments