19
19
#include < llvm/IR/LegacyPassManager.h>
20
20
#include < llvm/Transforms/Scalar.h>
21
21
22
+ #include " header.h"
23
+
22
24
namespace Singleton {
23
25
llvm::LLVMContext& context () {
24
26
static llvm::LLVMContext context_;
@@ -47,6 +49,17 @@ namespace Singleton {
47
49
std::make_unique<llvm::legacy::FunctionPassManager>(module_ptr.get ());
48
50
return fpm_;
49
51
}
52
+
53
+ std::unique_ptr<llvm::orc::KaleidoscopeJIT>& jit_ptr () {
54
+ static auto jit_ =
55
+ std::make_unique<llvm::orc::KaleidoscopeJIT>();
56
+ return jit_;
57
+ }
58
+ }
59
+
60
+ void init_module () {
61
+ Singleton::module_ptr ()->setDataLayout (
62
+ Singleton::jit_ptr ()->getTargetMachine ().createDataLayout ());
50
63
}
51
64
52
65
void init_function_pass_manager () {
@@ -722,7 +735,7 @@ std::unique_ptr<PrototypeAST> Parser::parse_extern () {
722
735
std::unique_ptr<FunctionAST> Parser::parse_top_level_expression () {
723
736
auto expr = parse_expression ();
724
737
if (expr) {
725
- auto proto = std::make_unique<PrototypeAST>(" anonymous " , std::vector<std::string>());
738
+ auto proto = std::make_unique<PrototypeAST>(" __anonymous " , std::vector<std::string>());
726
739
return std::make_unique<FunctionAST>(std::move (proto), std::move (expr));
727
740
} else {
728
741
return nullptr ;
@@ -739,15 +752,15 @@ std::unique_ptr<ExprAST> Parser::parse () {
739
752
case Token::Extern:
740
753
return parse_extern ();
741
754
default :
742
- return parse_expression ();
755
+ return parse_top_level_expression ();
743
756
}
744
757
}
745
758
746
759
void parse (std::string& code) {
747
760
char * ptr = const_cast <char *>(code.data ());
748
761
uint32_t len = code.size ();
749
762
750
- printf (" Code: %s\n " , code.c_str ());
763
+ printf (" Code:\n %s\n " , code.c_str ());
751
764
752
765
// lexer
753
766
#if 0
@@ -767,23 +780,29 @@ void parse (std::string& code) {
767
780
#endif
768
781
769
782
printf (" Parse:\n " );
783
+ printf (" ========================\n " );
770
784
// parse
771
- {
772
- Lexer lexer (ptr, len );
773
- Parser parser (lexer);
774
- auto res = parser.parse ();
785
+ Lexer lexer (ptr, len);
786
+ Parser parser (lexer );
787
+ while ( auto res = parser. parse ()) {
788
+ // auto res = parser.parse();
775
789
res->print ();
776
790
printf (" \n " );
777
791
printf (" IR:\n " );
778
792
auto * ir = res->codegen ();
779
793
ir->print (llvm::errs ());
780
794
printf (" \n " );
795
+ printf (" ========================\n " );
781
796
}
782
- printf (" ========================\n " );
783
797
}
784
798
785
799
int main () {
786
800
801
+ LLVMInitializeNativeTarget ();
802
+ LLVMInitializeNativeAsmPrinter ();
803
+ LLVMInitializeNativeAsmParser ();
804
+
805
+ init_module ();
787
806
init_function_pass_manager ();
788
807
789
808
#if 0
@@ -797,20 +816,31 @@ int main () {
797
816
std::string("# This expression will compute the 40th number. \n") +
798
817
std::string(" fib(40)\n");
799
818
#else
800
- std::vector<std::string> codes = {
801
- std::string (" 4+5" ),
802
- std::string (" def foo(a b) a*a + 2*a*b + b*b" ),
803
- std::string (" def bar(a) foo(a, 4.0) + bar(31337)" ),
804
- std::string (" extern cos(x)" ),
805
- std::string (" cos(1.234)" ),
806
- std::string (" def test(x) 1+2+x" ),
807
- std::string (" def testToOpt(x) (1+2+x)*(x+(1+2))" )
808
- };
819
+ // std::vector<std::string> codes = {
820
+ // std::string("4+5"),
821
+ // std::string("def foo(a b) a*a + 2*a*b + b*b"),
822
+ // std::string("def bar(a) foo(a, 4.0) + bar(31337)"),
823
+ // std::string("extern cos(x)"),
824
+ // std::string("cos(1.234)"),
825
+ // std::string("def test(x) 1+2+x"),
826
+ // std::string("def testToOpt(x) (1+2+x)*(x+(1+2))")
827
+ // };
828
+ std::string codes =
829
+ // std::string("4+5\n") +
830
+ // std::string("def foo(a b) a*a + 2*a*b + b*b\n") +
831
+ // std::string("def bar(a) foo(a, 4.0) + bar(31337)\n") +
832
+ // std::string("extern cos(x)\n") +
833
+ // std::string("cos(1.234)\n") +
834
+ // std::string("def test(x) 1+2+x\n") +
835
+ std::string (" def testToOpt(x) (1+2+x)*(x+(1+2))\n " ) +
836
+ std::string (" testToOpt(1)\n " );
809
837
#endif
810
838
811
- for (auto & str : codes) {
812
- parse (str);
813
- }
839
+ // for (auto& str : codes) {
840
+ // parse(str);
841
+ // }
842
+
843
+ parse (codes);
814
844
815
845
816
846
return 0 ;
0 commit comments