llvm.org GIT mirror llvm / 2dce4f2
Update code listings in LLVM tutorial. Several LLVM headers are moved. The code listings in LLVM tutorial are not updated yet. This CL removes the code replica in the .rst, and replace them with a literalinclude directive, so that sphinx can include the latest code automatically. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183607 91177308-0d34-0410-b5e6-96231b3b80d8 Logan Chien 6 years ago
6 changed file(s) with 12 addition(s) and 4569 deletion(s). Raw diff Collapse all Expand all
690690
691691 Here is the code:
692692
693 .. code-block:: c++
694
695 #include
696 #include
697 #include
698 #include
699 #include
700
701 //===----------------------------------------------------------------------===//
702 // Lexer
703 //===----------------------------------------------------------------------===//
704
705 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
706 // of these for known things.
707 enum Token {
708 tok_eof = -1,
709
710 // commands
711 tok_def = -2, tok_extern = -3,
712
713 // primary
714 tok_identifier = -4, tok_number = -5
715 };
716
717 static std::string IdentifierStr; // Filled in if tok_identifier
718 static double NumVal; // Filled in if tok_number
719
720 /// gettok - Return the next token from standard input.
721 static int gettok() {
722 static int LastChar = ' ';
723
724 // Skip any whitespace.
725 while (isspace(LastChar))
726 LastChar = getchar();
727
728 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
729 IdentifierStr = LastChar;
730 while (isalnum((LastChar = getchar())))
731 IdentifierStr += LastChar;
732
733 if (IdentifierStr == "def") return tok_def;
734 if (IdentifierStr == "extern") return tok_extern;
735 return tok_identifier;
736 }
737
738 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
739 std::string NumStr;
740 do {
741 NumStr += LastChar;
742 LastChar = getchar();
743 } while (isdigit(LastChar) || LastChar == '.');
744
745 NumVal = strtod(NumStr.c_str(), 0);
746 return tok_number;
747 }
748
749 if (LastChar == '#') {
750 // Comment until end of line.
751 do LastChar = getchar();
752 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
753
754 if (LastChar != EOF)
755 return gettok();
756 }
757
758 // Check for end of file. Don't eat the EOF.
759 if (LastChar == EOF)
760 return tok_eof;
761
762 // Otherwise, just return the character as its ascii value.
763 int ThisChar = LastChar;
764 LastChar = getchar();
765 return ThisChar;
766 }
767
768 //===----------------------------------------------------------------------===//
769 // Abstract Syntax Tree (aka Parse Tree)
770 //===----------------------------------------------------------------------===//
771
772 /// ExprAST - Base class for all expression nodes.
773 class ExprAST {
774 public:
775 virtual ~ExprAST() {}
776 };
777
778 /// NumberExprAST - Expression class for numeric literals like "1.0".
779 class NumberExprAST : public ExprAST {
780 double Val;
781 public:
782 NumberExprAST(double val) : Val(val) {}
783 };
784
785 /// VariableExprAST - Expression class for referencing a variable, like "a".
786 class VariableExprAST : public ExprAST {
787 std::string Name;
788 public:
789 VariableExprAST(const std::string &name) : Name(name) {}
790 };
791
792 /// BinaryExprAST - Expression class for a binary operator.
793 class BinaryExprAST : public ExprAST {
794 char Op;
795 ExprAST *LHS, *RHS;
796 public:
797 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
798 : Op(op), LHS(lhs), RHS(rhs) {}
799 };
800
801 /// CallExprAST - Expression class for function calls.
802 class CallExprAST : public ExprAST {
803 std::string Callee;
804 std::vector Args;
805 public:
806 CallExprAST(const std::string &callee, std::vector &args)
807 : Callee(callee), Args(args) {}
808 };
809
810 /// PrototypeAST - This class represents the "prototype" for a function,
811 /// which captures its name, and its argument names (thus implicitly the number
812 /// of arguments the function takes).
813 class PrototypeAST {
814 std::string Name;
815 std::vector Args;
816 public:
817 PrototypeAST(const std::string &name, const std::vector &args)
818 : Name(name), Args(args) {}
819
820 };
821
822 /// FunctionAST - This class represents a function definition itself.
823 class FunctionAST {
824 PrototypeAST *Proto;
825 ExprAST *Body;
826 public:
827 FunctionAST(PrototypeAST *proto, ExprAST *body)
828 : Proto(proto), Body(body) {}
829
830 };
831
832 //===----------------------------------------------------------------------===//
833 // Parser
834 //===----------------------------------------------------------------------===//
835
836 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
837 /// token the parser is looking at. getNextToken reads another token from the
838 /// lexer and updates CurTok with its results.
839 static int CurTok;
840 static int getNextToken() {
841 return CurTok = gettok();
842 }
843
844 /// BinopPrecedence - This holds the precedence for each binary operator that is
845 /// defined.
846 static std::map BinopPrecedence;
847
848 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
849 static int GetTokPrecedence() {
850 if (!isascii(CurTok))
851 return -1;
852
853 // Make sure it's a declared binop.
854 int TokPrec = BinopPrecedence[CurTok];
855 if (TokPrec <= 0) return -1;
856 return TokPrec;
857 }
858
859 /// Error* - These are little helper functions for error handling.
860 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
861 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
862 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
863
864 static ExprAST *ParseExpression();
865
866 /// identifierexpr
867 /// ::= identifier
868 /// ::= identifier '(' expression* ')'
869 static ExprAST *ParseIdentifierExpr() {
870 std::string IdName = IdentifierStr;
871
872 getNextToken(); // eat identifier.
873
874 if (CurTok != '(') // Simple variable ref.
875 return new VariableExprAST(IdName);
876
877 // Call.
878 getNextToken(); // eat (
879 std::vector Args;
880 if (CurTok != ')') {
881 while (1) {
882 ExprAST *Arg = ParseExpression();
883 if (!Arg) return 0;
884 Args.push_back(Arg);
885
886 if (CurTok == ')') break;
887
888 if (CurTok != ',')
889 return Error("Expected ')' or ',' in argument list");
890 getNextToken();
891 }
892 }
893
894 // Eat the ')'.
895 getNextToken();
896
897 return new CallExprAST(IdName, Args);
898 }
899
900 /// numberexpr ::= number
901 static ExprAST *ParseNumberExpr() {
902 ExprAST *Result = new NumberExprAST(NumVal);
903 getNextToken(); // consume the number
904 return Result;
905 }
906
907 /// parenexpr ::= '(' expression ')'
908 static ExprAST *ParseParenExpr() {
909 getNextToken(); // eat (.
910 ExprAST *V = ParseExpression();
911 if (!V) return 0;
912
913 if (CurTok != ')')
914 return Error("expected ')'");
915 getNextToken(); // eat ).
916 return V;
917 }
918
919 /// primary
920 /// ::= identifierexpr
921 /// ::= numberexpr
922 /// ::= parenexpr
923 static ExprAST *ParsePrimary() {
924 switch (CurTok) {
925 default: return Error("unknown token when expecting an expression");
926 case tok_identifier: return ParseIdentifierExpr();
927 case tok_number: return ParseNumberExpr();
928 case '(': return ParseParenExpr();
929 }
930 }
931
932 /// binoprhs
933 /// ::= ('+' primary)*
934 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
935 // If this is a binop, find its precedence.
936 while (1) {
937 int TokPrec = GetTokPrecedence();
938
939 // If this is a binop that binds at least as tightly as the current binop,
940 // consume it, otherwise we are done.
941 if (TokPrec < ExprPrec)
942 return LHS;
943
944 // Okay, we know this is a binop.
945 int BinOp = CurTok;
946 getNextToken(); // eat binop
947
948 // Parse the primary expression after the binary operator.
949 ExprAST *RHS = ParsePrimary();
950 if (!RHS) return 0;
951
952 // If BinOp binds less tightly with RHS than the operator after RHS, let
953 // the pending operator take RHS as its LHS.
954 int NextPrec = GetTokPrecedence();
955 if (TokPrec < NextPrec) {
956 RHS = ParseBinOpRHS(TokPrec+1, RHS);
957 if (RHS == 0) return 0;
958 }
959
960 // Merge LHS/RHS.
961 LHS = new BinaryExprAST(BinOp, LHS, RHS);
962 }
963 }
964
965 /// expression
966 /// ::= primary binoprhs
967 ///
968 static ExprAST *ParseExpression() {
969 ExprAST *LHS = ParsePrimary();
970 if (!LHS) return 0;
971
972 return ParseBinOpRHS(0, LHS);
973 }
974
975 /// prototype
976 /// ::= id '(' id* ')'
977 static PrototypeAST *ParsePrototype() {
978 if (CurTok != tok_identifier)
979 return ErrorP("Expected function name in prototype");
980
981 std::string FnName = IdentifierStr;
982 getNextToken();
983
984 if (CurTok != '(')
985 return ErrorP("Expected '(' in prototype");
986
987 std::vector ArgNames;
988 while (getNextToken() == tok_identifier)
989 ArgNames.push_back(IdentifierStr);
990 if (CurTok != ')')
991 return ErrorP("Expected ')' in prototype");
992
993 // success.
994 getNextToken(); // eat ')'.
995
996 return new PrototypeAST(FnName, ArgNames);
997 }
998
999 /// definition ::= 'def' prototype expression
1000 static FunctionAST *ParseDefinition() {
1001 getNextToken(); // eat def.
1002 PrototypeAST *Proto = ParsePrototype();
1003 if (Proto == 0) return 0;
1004
1005 if (ExprAST *E = ParseExpression())
1006 return new FunctionAST(Proto, E);
1007 return 0;
1008 }
1009
1010 /// toplevelexpr ::= expression
1011 static FunctionAST *ParseTopLevelExpr() {
1012 if (ExprAST *E = ParseExpression()) {
1013 // Make an anonymous proto.
1014 PrototypeAST *Proto = new PrototypeAST("", std::vector());
1015 return new FunctionAST(Proto, E);
1016 }
1017 return 0;
1018 }
1019
1020 /// external ::= 'extern' prototype
1021 static PrototypeAST *ParseExtern() {
1022 getNextToken(); // eat extern.
1023 return ParsePrototype();
1024 }
1025
1026 //===----------------------------------------------------------------------===//
1027 // Top-Level parsing
1028 //===----------------------------------------------------------------------===//
1029
1030 static void HandleDefinition() {
1031 if (ParseDefinition()) {
1032 fprintf(stderr, "Parsed a function definition.\n");
1033 } else {
1034 // Skip token for error recovery.
1035 getNextToken();
1036 }
1037 }
1038
1039 static void HandleExtern() {
1040 if (ParseExtern()) {
1041 fprintf(stderr, "Parsed an extern\n");
1042 } else {
1043 // Skip token for error recovery.
1044 getNextToken();
1045 }
1046 }
1047
1048 static void HandleTopLevelExpression() {
1049 // Evaluate a top-level expression into an anonymous function.
1050 if (ParseTopLevelExpr()) {
1051 fprintf(stderr, "Parsed a top-level expr\n");
1052 } else {
1053 // Skip token for error recovery.
1054 getNextToken();
1055 }
1056 }
1057
1058 /// top ::= definition | external | expression | ';'
1059 static void MainLoop() {
1060 while (1) {
1061 fprintf(stderr, "ready> ");
1062 switch (CurTok) {
1063 case tok_eof: return;
1064 case ';': getNextToken(); break; // ignore top-level semicolons.
1065 case tok_def: HandleDefinition(); break;
1066 case tok_extern: HandleExtern(); break;
1067 default: HandleTopLevelExpression(); break;
1068 }
1069 }
1070 }
1071
1072 //===----------------------------------------------------------------------===//
1073 // Main driver code.
1074 //===----------------------------------------------------------------------===//
1075
1076 int main() {
1077 // Install standard binary operators.
1078 // 1 is lowest precedence.
1079 BinopPrecedence['<'] = 10;
1080 BinopPrecedence['+'] = 20;
1081 BinopPrecedence['-'] = 20;
1082 BinopPrecedence['*'] = 40; // highest.
1083
1084 // Prime the first token.
1085 fprintf(stderr, "ready> ");
1086 getNextToken();
1087
1088 // Run the main "interpreter loop" now.
1089 MainLoop();
1090
1091 return 0;
1092 }
693 .. literalinclude:: ../../examples/Kaleidoscope/Chapter2/toy.cpp
694 :language: c++
1093695
1094696 `Next: Implementing Code Generation to LLVM IR `_
1095697
586586
587587 Here is the code:
588588
589 .. code-block:: c++
590
591 // To build this:
592 // See example below.
593
594 #include "llvm/DerivedTypes.h"
595 #include "llvm/IRBuilder.h"
596 #include "llvm/LLVMContext.h"
597 #include "llvm/Module.h"
598 #include "llvm/Analysis/Verifier.h"
599 #include
600 #include
601 #include
602 #include
603 using namespace llvm;
604
605 //===----------------------------------------------------------------------===//
606 // Lexer
607 //===----------------------------------------------------------------------===//
608
609 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
610 // of these for known things.
611 enum Token {
612 tok_eof = -1,
613
614 // commands
615 tok_def = -2, tok_extern = -3,
616
617 // primary
618 tok_identifier = -4, tok_number = -5
619 };
620
621 static std::string IdentifierStr; // Filled in if tok_identifier
622 static double NumVal; // Filled in if tok_number
623
624 /// gettok - Return the next token from standard input.
625 static int gettok() {
626 static int LastChar = ' ';
627
628 // Skip any whitespace.
629 while (isspace(LastChar))
630 LastChar = getchar();
631
632 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
633 IdentifierStr = LastChar;
634 while (isalnum((LastChar = getchar())))
635 IdentifierStr += LastChar;
636
637 if (IdentifierStr == "def") return tok_def;
638 if (IdentifierStr == "extern") return tok_extern;
639 return tok_identifier;
640 }
641
642 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
643 std::string NumStr;
644 do {
645 NumStr += LastChar;
646 LastChar = getchar();
647 } while (isdigit(LastChar) || LastChar == '.');
648
649 NumVal = strtod(NumStr.c_str(), 0);
650 return tok_number;
651 }
652
653 if (LastChar == '#') {
654 // Comment until end of line.
655 do LastChar = getchar();
656 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
657
658 if (LastChar != EOF)
659 return gettok();
660 }
661
662 // Check for end of file. Don't eat the EOF.
663 if (LastChar == EOF)
664 return tok_eof;
665
666 // Otherwise, just return the character as its ascii value.
667 int ThisChar = LastChar;
668 LastChar = getchar();
669 return ThisChar;
670 }
671
672 //===----------------------------------------------------------------------===//
673 // Abstract Syntax Tree (aka Parse Tree)
674 //===----------------------------------------------------------------------===//
675
676 /// ExprAST - Base class for all expression nodes.
677 class ExprAST {
678 public:
679 virtual ~ExprAST() {}
680 virtual Value *Codegen() = 0;
681 };
682
683 /// NumberExprAST - Expression class for numeric literals like "1.0".
684 class NumberExprAST : public ExprAST {
685 double Val;
686 public:
687 NumberExprAST(double val) : Val(val) {}
688 virtual Value *Codegen();
689 };
690
691 /// VariableExprAST - Expression class for referencing a variable, like "a".
692 class VariableExprAST : public ExprAST {
693 std::string Name;
694 public:
695 VariableExprAST(const std::string &name) : Name(name) {}
696 virtual Value *Codegen();
697 };
698
699 /// BinaryExprAST - Expression class for a binary operator.
700 class BinaryExprAST : public ExprAST {
701 char Op;
702 ExprAST *LHS, *RHS;
703 public:
704 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
705 : Op(op), LHS(lhs), RHS(rhs) {}
706 virtual Value *Codegen();
707 };
708
709 /// CallExprAST - Expression class for function calls.
710 class CallExprAST : public ExprAST {
711 std::string Callee;
712 std::vector Args;
713 public:
714 CallExprAST(const std::string &callee, std::vector &args)
715 : Callee(callee), Args(args) {}
716 virtual Value *Codegen();
717 };
718
719 /// PrototypeAST - This class represents the "prototype" for a function,
720 /// which captures its name, and its argument names (thus implicitly the number
721 /// of arguments the function takes).
722 class PrototypeAST {
723 std::string Name;
724 std::vector Args;
725 public:
726 PrototypeAST(const std::string &name, const std::vector &args)
727 : Name(name), Args(args) {}
728
729 Function *Codegen();
730 };
731
732 /// FunctionAST - This class represents a function definition itself.
733 class FunctionAST {
734 PrototypeAST *Proto;
735 ExprAST *Body;
736 public:
737 FunctionAST(PrototypeAST *proto, ExprAST *body)
738 : Proto(proto), Body(body) {}
739
740 Function *Codegen();
741 };
742
743 //===----------------------------------------------------------------------===//
744 // Parser
745 //===----------------------------------------------------------------------===//
746
747 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
748 /// token the parser is looking at. getNextToken reads another token from the
749 /// lexer and updates CurTok with its results.
750 static int CurTok;
751 static int getNextToken() {
752 return CurTok = gettok();
753 }
754
755 /// BinopPrecedence - This holds the precedence for each binary operator that is
756 /// defined.
757 static std::map BinopPrecedence;
758
759 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
760 static int GetTokPrecedence() {
761 if (!isascii(CurTok))
762 return -1;
763
764 // Make sure it's a declared binop.
765 int TokPrec = BinopPrecedence[CurTok];
766 if (TokPrec <= 0) return -1;
767 return TokPrec;
768 }
769
770 /// Error* - These are little helper functions for error handling.
771 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
772 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
773 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
774
775 static ExprAST *ParseExpression();
776
777 /// identifierexpr
778 /// ::= identifier
779 /// ::= identifier '(' expression* ')'
780 static ExprAST *ParseIdentifierExpr() {
781 std::string IdName = IdentifierStr;
782
783 getNextToken(); // eat identifier.
784
785 if (CurTok != '(') // Simple variable ref.
786 return new VariableExprAST(IdName);
787
788 // Call.
789 getNextToken(); // eat (
790 std::vector Args;
791 if (CurTok != ')') {
792 while (1) {
793 ExprAST *Arg = ParseExpression();
794 if (!Arg) return 0;
795 Args.push_back(Arg);
796
797 if (CurTok == ')') break;
798
799 if (CurTok != ',')
800 return Error("Expected ')' or ',' in argument list");
801 getNextToken();
802 }
803 }
804
805 // Eat the ')'.
806 getNextToken();
807
808 return new CallExprAST(IdName, Args);
809 }
810
811 /// numberexpr ::= number
812 static ExprAST *ParseNumberExpr() {
813 ExprAST *Result = new NumberExprAST(NumVal);
814 getNextToken(); // consume the number
815 return Result;
816 }
817
818 /// parenexpr ::= '(' expression ')'
819 static ExprAST *ParseParenExpr() {
820 getNextToken(); // eat (.
821 ExprAST *V = ParseExpression();
822 if (!V) return 0;
823
824 if (CurTok != ')')
825 return Error("expected ')'");
826 getNextToken(); // eat ).
827 return V;
828 }
829
830 /// primary
831 /// ::= identifierexpr
832 /// ::= numberexpr
833 /// ::= parenexpr
834 static ExprAST *ParsePrimary() {
835 switch (CurTok) {
836 default: return Error("unknown token when expecting an expression");
837 case tok_identifier: return ParseIdentifierExpr();
838 case tok_number: return ParseNumberExpr();
839 case '(': return ParseParenExpr();
840 }
841 }
842
843 /// binoprhs
844 /// ::= ('+' primary)*
845 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
846 // If this is a binop, find its precedence.
847 while (1) {
848 int TokPrec = GetTokPrecedence();
849
850 // If this is a binop that binds at least as tightly as the current binop,
851 // consume it, otherwise we are done.
852 if (TokPrec < ExprPrec)
853 return LHS;
854
855 // Okay, we know this is a binop.
856 int BinOp = CurTok;
857 getNextToken(); // eat binop
858
859 // Parse the primary expression after the binary operator.
860 ExprAST *RHS = ParsePrimary();
861 if (!RHS) return 0;
862
863 // If BinOp binds less tightly with RHS than the operator after RHS, let
864 // the pending operator take RHS as its LHS.
865 int NextPrec = GetTokPrecedence();
866 if (TokPrec < NextPrec) {
867 RHS = ParseBinOpRHS(TokPrec+1, RHS);
868 if (RHS == 0) return 0;
869 }
870
871 // Merge LHS/RHS.
872 LHS = new BinaryExprAST(BinOp, LHS, RHS);
873 }
874 }
875
876 /// expression
877 /// ::= primary binoprhs
878 ///
879 static ExprAST *ParseExpression() {
880 ExprAST *LHS = ParsePrimary();
881 if (!LHS) return 0;
882
883 return ParseBinOpRHS(0, LHS);
884 }
885
886 /// prototype
887 /// ::= id '(' id* ')'
888 static PrototypeAST *ParsePrototype() {
889 if (CurTok != tok_identifier)
890 return ErrorP("Expected function name in prototype");
891
892 std::string FnName = IdentifierStr;
893 getNextToken();
894
895 if (CurTok != '(')
896 return ErrorP("Expected '(' in prototype");
897
898 std::vector ArgNames;
899 while (getNextToken() == tok_identifier)
900 ArgNames.push_back(IdentifierStr);
901 if (CurTok != ')')
902 return ErrorP("Expected ')' in prototype");
903
904 // success.
905 getNextToken(); // eat ')'.
906
907 return new PrototypeAST(FnName, ArgNames);
908 }
909
910 /// definition ::= 'def' prototype expression
911 static FunctionAST *ParseDefinition() {
912 getNextToken(); // eat def.
913 PrototypeAST *Proto = ParsePrototype();
914 if (Proto == 0) return 0;
915
916 if (ExprAST *E = ParseExpression())
917 return new FunctionAST(Proto, E);
918 return 0;
919 }
920
921 /// toplevelexpr ::= expression
922 static FunctionAST *ParseTopLevelExpr() {
923 if (ExprAST *E = ParseExpression()) {
924 // Make an anonymous proto.
925 PrototypeAST *Proto = new PrototypeAST("", std::vector());
926 return new FunctionAST(Proto, E);
927 }
928 return 0;
929 }
930
931 /// external ::= 'extern' prototype
932 static PrototypeAST *ParseExtern() {
933 getNextToken(); // eat extern.
934 return ParsePrototype();
935 }
936
937 //===----------------------------------------------------------------------===//
938 // Code Generation
939 //===----------------------------------------------------------------------===//
940
941 static Module *TheModule;
942 static IRBuilder<> Builder(getGlobalContext());
943 static std::map NamedValues;
944
945 Value *ErrorV(const char *Str) { Error(Str); return 0; }
946
947 Value *NumberExprAST::Codegen() {
948 return ConstantFP::get(getGlobalContext(), APFloat(Val));
949 }
950
951 Value *VariableExprAST::Codegen() {
952 // Look this variable up in the function.
953 Value *V = NamedValues[Name];
954 return V ? V : ErrorV("Unknown variable name");
955 }
956
957 Value *BinaryExprAST::Codegen() {
958 Value *L = LHS->Codegen();
959 Value *R = RHS->Codegen();
960 if (L == 0 || R == 0) return 0;
961
962 switch (Op) {
963 case '+': return Builder.CreateFAdd(L, R, "addtmp");
964 case '-': return Builder.CreateFSub(L, R, "subtmp");
965 case '*': return Builder.CreateFMul(L, R, "multmp");
966 case '<':
967 L = Builder.CreateFCmpULT(L, R, "cmptmp");
968 // Convert bool 0/1 to double 0.0 or 1.0
969 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
970 "booltmp");
971 default: return ErrorV("invalid binary operator");
972 }
973 }
974
975 Value *CallExprAST::Codegen() {
976 // Look up the name in the global module table.
977 Function *CalleeF = TheModule->getFunction(Callee);
978 if (CalleeF == 0)
979 return ErrorV("Unknown function referenced");
980
981 // If argument mismatch error.
982 if (CalleeF->arg_size() != Args.size())
983 return ErrorV("Incorrect # arguments passed");
984
985 std::vector ArgsV;
986 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
987 ArgsV.push_back(Args[i]->Codegen());
988 if (ArgsV.back() == 0) return 0;
989 }
990
991 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
992 }
993
994 Function *PrototypeAST::Codegen() {
995 // Make the function type: double(double,double) etc.
996 std::vector Doubles(Args.size(),
997 Type::getDoubleTy(getGlobalContext()));
998 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
999 Doubles, false);
1000
1001 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
1002
1003 // If F conflicted, there was already something named 'Name'. If it has a
1004 // body, don't allow redefinition or reextern.
1005 if (F->getName() != Name) {
1006 // Delete the one we just made and get the existing one.
1007 F->eraseFromParent();
1008 F = TheModule->getFunction(Name);
1009
1010 // If F already has a body, reject this.
1011 if (!F->empty()) {
1012 ErrorF("redefinition of function");
1013 return 0;
1014 }
1015
1016 // If F took a different number of args, reject.
1017 if (F->arg_size() != Args.size()) {
1018 ErrorF("redefinition of function with different # args");
1019 return 0;
1020 }
1021 }
1022
1023 // Set names for all arguments.
1024 unsigned Idx = 0;
1025 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1026 ++AI, ++Idx) {
1027 AI->setName(Args[Idx]);
1028
1029 // Add arguments to variable symbol table.
1030 NamedValues[Args[Idx]] = AI;
1031 }
1032
1033 return F;
1034 }
1035
1036 Function *FunctionAST::Codegen() {
1037 NamedValues.clear();
1038
1039 Function *TheFunction = Proto->Codegen();
1040 if (TheFunction == 0)
1041 return 0;
1042
1043 // Create a new basic block to start insertion into.
1044 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1045 Builder.SetInsertPoint(BB);
1046
1047 if (Value *RetVal = Body->Codegen()) {
1048 // Finish off the function.
1049 Builder.CreateRet(RetVal);
1050
1051 // Validate the generated code, checking for consistency.
1052 verifyFunction(*TheFunction);
1053
1054 return TheFunction;
1055 }
1056
1057 // Error reading body, remove function.
1058 TheFunction->eraseFromParent();
1059 return 0;
1060 }
1061
1062 //===----------------------------------------------------------------------===//
1063 // Top-Level parsing and JIT Driver
1064 //===----------------------------------------------------------------------===//
1065
1066 static void HandleDefinition() {
1067 if (FunctionAST *F = ParseDefinition()) {
1068 if (Function *LF = F->Codegen()) {
1069 fprintf(stderr, "Read function definition:");
1070 LF->dump();
1071 }
1072 } else {
1073 // Skip token for error recovery.
1074 getNextToken();
1075 }
1076 }
1077
1078 static void HandleExtern() {
1079 if (PrototypeAST *P = ParseExtern()) {
1080 if (Function *F = P->Codegen()) {
1081 fprintf(stderr, "Read extern: ");
1082 F->dump();
1083 }
1084 } else {
1085 // Skip token for error recovery.
1086 getNextToken();
1087 }
1088 }
1089
1090 static void HandleTopLevelExpression() {
1091 // Evaluate a top-level expression into an anonymous function.
1092 if (FunctionAST *F = ParseTopLevelExpr()) {
1093 if (Function *LF = F->Codegen()) {
1094 fprintf(stderr, "Read top-level expression:");
1095 LF->dump();
1096 }
1097 } else {
1098 // Skip token for error recovery.
1099 getNextToken();
1100 }
1101 }
1102
1103 /// top ::= definition | external | expression | ';'
1104 static void MainLoop() {
1105 while (1) {
1106 fprintf(stderr, "ready> ");
1107 switch (CurTok) {
1108 case tok_eof: return;
1109 case ';': getNextToken(); break; // ignore top-level semicolons.
1110 case tok_def: HandleDefinition(); break;
1111 case tok_extern: HandleExtern(); break;
1112 default: HandleTopLevelExpression(); break;
1113 }
1114 }
1115 }
1116
1117 //===----------------------------------------------------------------------===//
1118 // "Library" functions that can be "extern'd" from user code.
1119 //===----------------------------------------------------------------------===//
1120
1121 /// putchard - putchar that takes a double and returns 0.
1122 extern "C"
1123 double putchard(double X) {
1124 putchar((char)X);
1125 return 0;
1126 }
1127
1128 //===----------------------------------------------------------------------===//
1129 // Main driver code.
1130 //===----------------------------------------------------------------------===//
1131
1132 int main() {
1133 LLVMContext &Context = getGlobalContext();
1134
1135 // Install standard binary operators.
1136 // 1 is lowest precedence.
1137 BinopPrecedence['<'] = 10;
1138 BinopPrecedence['+'] = 20;
1139 BinopPrecedence['-'] = 20;
1140 BinopPrecedence['*'] = 40; // highest.
1141
1142 // Prime the first token.
1143 fprintf(stderr, "ready> ");
1144 getNextToken();
1145
1146 // Make the module, which holds all the code.
1147 TheModule = new Module("my cool jit", Context);
1148
1149 // Run the main "interpreter loop" now.
1150 MainLoop();
1151
1152 // Print out all of the generated code.
1153 TheModule->dump();
1154
1155 return 0;
1156 }
589 .. literalinclude:: ../../examples/Kaleidoscope/Chapter3/toy.cpp
590 :language: c++
1157591
1158592 `Next: Adding JIT and Optimizer Support `_
1159593
437437
438438 Here is the code:
439439
440 .. code-block:: c++
441
442 #include "llvm/DerivedTypes.h"
443 #include "llvm/ExecutionEngine/ExecutionEngine.h"
444 #include "llvm/ExecutionEngine/JIT.h"
445 #include "llvm/IRBuilder.h"
446 #include "llvm/LLVMContext.h"
447 #include "llvm/Module.h"
448 #include "llvm/PassManager.h"
449 #include "llvm/Analysis/Verifier.h"
450 #include "llvm/Analysis/Passes.h"
451 #include "llvm/DataLayout.h"
452 #include "llvm/Transforms/Scalar.h"
453 #include "llvm/Support/TargetSelect.h"
454 #include
455 #include
456 #include
457 #include
458 using namespace llvm;
459
460 //===----------------------------------------------------------------------===//
461 // Lexer
462 //===----------------------------------------------------------------------===//
463
464 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
465 // of these for known things.
466 enum Token {
467 tok_eof = -1,
468
469 // commands
470 tok_def = -2, tok_extern = -3,
471
472 // primary
473 tok_identifier = -4, tok_number = -5
474 };
475
476 static std::string IdentifierStr; // Filled in if tok_identifier
477 static double NumVal; // Filled in if tok_number
478
479 /// gettok - Return the next token from standard input.
480 static int gettok() {
481 static int LastChar = ' ';
482
483 // Skip any whitespace.
484 while (isspace(LastChar))
485 LastChar = getchar();
486
487 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
488 IdentifierStr = LastChar;
489 while (isalnum((LastChar = getchar())))
490 IdentifierStr += LastChar;
491
492 if (IdentifierStr == "def") return tok_def;
493 if (IdentifierStr == "extern") return tok_extern;
494 return tok_identifier;
495 }
496
497 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
498 std::string NumStr;
499 do {
500 NumStr += LastChar;
501 LastChar = getchar();
502 } while (isdigit(LastChar) || LastChar == '.');
503
504 NumVal = strtod(NumStr.c_str(), 0);
505 return tok_number;
506 }
507
508 if (LastChar == '#') {
509 // Comment until end of line.
510 do LastChar = getchar();
511 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
512
513 if (LastChar != EOF)
514 return gettok();
515 }
516
517 // Check for end of file. Don't eat the EOF.
518 if (LastChar == EOF)
519 return tok_eof;
520
521 // Otherwise, just return the character as its ascii value.
522 int ThisChar = LastChar;
523 LastChar = getchar();
524 return ThisChar;
525 }
526
527 //===----------------------------------------------------------------------===//
528 // Abstract Syntax Tree (aka Parse Tree)
529 //===----------------------------------------------------------------------===//
530
531 /// ExprAST - Base class for all expression nodes.
532 class ExprAST {
533 public:
534 virtual ~ExprAST() {}
535 virtual Value *Codegen() = 0;
536 };
537
538 /// NumberExprAST - Expression class for numeric literals like "1.0".
539 class NumberExprAST : public ExprAST {
540 double Val;
541 public:
542 NumberExprAST(double val) : Val(val) {}
543 virtual Value *Codegen();
544 };
545
546 /// VariableExprAST - Expression class for referencing a variable, like "a".
547 class VariableExprAST : public ExprAST {
548 std::string Name;
549 public:
550 VariableExprAST(const std::string &name) : Name(name) {}
551 virtual Value *Codegen();
552 };
553
554 /// BinaryExprAST - Expression class for a binary operator.
555 class BinaryExprAST : public ExprAST {
556 char Op;
557 ExprAST *LHS, *RHS;
558 public:
559 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
560 : Op(op), LHS(lhs), RHS(rhs) {}
561 virtual Value *Codegen();
562 };
563
564 /// CallExprAST - Expression class for function calls.
565 class CallExprAST : public ExprAST {
566 std::string Callee;
567 std::vector Args;
568 public:
569 CallExprAST(const std::string &callee, std::vector &args)
570 : Callee(callee), Args(args) {}
571 virtual Value *Codegen();
572 };
573
574 /// PrototypeAST - This class represents the "prototype" for a function,
575 /// which captures its name, and its argument names (thus implicitly the number
576 /// of arguments the function takes).
577 class PrototypeAST {
578 std::string Name;
579 std::vector Args;
580 public:
581 PrototypeAST(const std::string &name, const std::vector &args)
582 : Name(name), Args(args) {}
583
584 Function *Codegen();
585 };
586
587 /// FunctionAST - This class represents a function definition itself.
588 class FunctionAST {
589 PrototypeAST *Proto;
590 ExprAST *Body;
591 public:
592 FunctionAST(PrototypeAST *proto, ExprAST *body)
593 : Proto(proto), Body(body) {}
594
595 Function *Codegen();
596 };
597
598 //===----------------------------------------------------------------------===//
599 // Parser
600 //===----------------------------------------------------------------------===//
601
602 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
603 /// token the parser is looking at. getNextToken reads another token from the
604 /// lexer and updates CurTok with its results.
605 static int CurTok;
606 static int getNextToken() {
607 return CurTok = gettok();
608 }
609
610 /// BinopPrecedence - This holds the precedence for each binary operator that is
611 /// defined.
612 static std::map BinopPrecedence;
613
614 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
615 static int GetTokPrecedence() {
616 if (!isascii(CurTok))
617 return -1;
618
619 // Make sure it's a declared binop.
620 int TokPrec = BinopPrecedence[CurTok];
621 if (TokPrec <= 0) return -1;
622 return TokPrec;
623 }
624
625 /// Error* - These are little helper functions for error handling.
626 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
627 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
628 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
629
630 static ExprAST *ParseExpression();
631
632 /// identifierexpr
633 /// ::= identifier
634 /// ::= identifier '(' expression* ')'
635 static ExprAST *ParseIdentifierExpr() {
636 std::string IdName = IdentifierStr;
637
638 getNextToken(); // eat identifier.
639
640 if (CurTok != '(') // Simple variable ref.
641 return new VariableExprAST(IdName);
642
643 // Call.
644 getNextToken(); // eat (
645 std::vector Args;
646 if (CurTok != ')') {
647 while (1) {
648 ExprAST *Arg = ParseExpression();
649 if (!Arg) return 0;
650 Args.push_back(Arg);
651
652 if (CurTok == ')') break;
653
654 if (CurTok != ',')
655 return Error("Expected ')' or ',' in argument list");
656 getNextToken();
657 }
658 }
659
660 // Eat the ')'.
661 getNextToken();
662
663 return new CallExprAST(IdName, Args);
664 }
665
666 /// numberexpr ::= number
667 static ExprAST *ParseNumberExpr() {
668 ExprAST *Result = new NumberExprAST(NumVal);
669 getNextToken(); // consume the number
670 return Result;
671 }
672
673 /// parenexpr ::= '(' expression ')'
674 static ExprAST *ParseParenExpr() {
675 getNextToken(); // eat (.
676 ExprAST *V = ParseExpression();
677 if (!V) return 0;
678
679 if (CurTok != ')')
680 return Error("expected ')'");
681 getNextToken(); // eat ).
682 return V;
683 }
684
685 /// primary
686 /// ::= identifierexpr
687 /// ::= numberexpr
688 /// ::= parenexpr
689 static ExprAST *ParsePrimary() {
690 switch (CurTok) {
691 default: return Error("unknown token when expecting an expression");
692 case tok_identifier: return ParseIdentifierExpr();
693 case tok_number: return ParseNumberExpr();
694 case '(': return ParseParenExpr();
695 }
696 }
697
698 /// binoprhs
699 /// ::= ('+' primary)*
700 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
701 // If this is a binop, find its precedence.
702 while (1) {
703 int TokPrec = GetTokPrecedence();
704
705 // If this is a binop that binds at least as tightly as the current binop,
706 // consume it, otherwise we are done.
707 if (TokPrec < ExprPrec)
708 return LHS;
709
710 // Okay, we know this is a binop.
711 int BinOp = CurTok;
712 getNextToken(); // eat binop
713
714 // Parse the primary expression after the binary operator.
715 ExprAST *RHS = ParsePrimary();
716 if (!RHS) return 0;
717
718 // If BinOp binds less tightly with RHS than the operator after RHS, let
719 // the pending operator take RHS as its LHS.
720 int NextPrec = GetTokPrecedence();
721 if (TokPrec < NextPrec) {
722 RHS = ParseBinOpRHS(TokPrec+1, RHS);
723 if (RHS == 0) return 0;
724 }
725
726 // Merge LHS/RHS.
727 LHS = new BinaryExprAST(BinOp, LHS, RHS);
728 }
729 }
730
731 /// expression
732 /// ::= primary binoprhs
733 ///
734 static ExprAST *ParseExpression() {
735 ExprAST *LHS = ParsePrimary();
736 if (!LHS) return 0;
737
738 return ParseBinOpRHS(0, LHS);
739 }
740
741 /// prototype
742 /// ::= id '(' id* ')'
743 static PrototypeAST *ParsePrototype() {
744 if (CurTok != tok_identifier)
745 return ErrorP("Expected function name in prototype");
746
747 std::string FnName = IdentifierStr;
748 getNextToken();
749
750 if (CurTok != '(')
751 return ErrorP("Expected '(' in prototype");
752
753 std::vector ArgNames;
754 while (getNextToken() == tok_identifier)
755 ArgNames.push_back(IdentifierStr);
756 if (CurTok != ')')
757 return ErrorP("Expected ')' in prototype");
758
759 // success.
760 getNextToken(); // eat ')'.
761
762 return new PrototypeAST(FnName, ArgNames);
763 }
764
765 /// definition ::= 'def' prototype expression
766 static FunctionAST *ParseDefinition() {
767 getNextToken(); // eat def.
768 PrototypeAST *Proto = ParsePrototype();
769 if (Proto == 0) return 0;
770
771 if (ExprAST *E = ParseExpression())
772 return new FunctionAST(Proto, E);
773 return 0;
774 }
775
776 /// toplevelexpr ::= expression
777 static FunctionAST *ParseTopLevelExpr() {
778 if (ExprAST *E = ParseExpression()) {
779 // Make an anonymous proto.
780 PrototypeAST *Proto = new PrototypeAST("", std::vector());
781 return new FunctionAST(Proto, E);
782 }
783 return 0;
784 }
785
786 /// external ::= 'extern' prototype
787 static PrototypeAST *ParseExtern() {
788 getNextToken(); // eat extern.
789 return ParsePrototype();
790 }
791
792 //===----------------------------------------------------------------------===//
793 // Code Generation
794 //===----------------------------------------------------------------------===//
795
796 static Module *TheModule;
797 static IRBuilder<> Builder(getGlobalContext());
798 static std::map NamedValues;
799 static FunctionPassManager *TheFPM;
800
801 Value *ErrorV(const char *Str) { Error(Str); return 0; }
802
803 Value *NumberExprAST::Codegen() {
804 return ConstantFP::get(getGlobalContext(), APFloat(Val));
805 }
806
807 Value *VariableExprAST::Codegen() {
808 // Look this variable up in the function.
809 Value *V = NamedValues[Name];
810 return V ? V : ErrorV("Unknown variable name");
811 }
812
813 Value *BinaryExprAST::Codegen() {
814 Value *L = LHS->Codegen();
815 Value *R = RHS->Codegen();
816 if (L == 0 || R == 0) return 0;
817
818 switch (Op) {
819 case '+': return Builder.CreateFAdd(L, R, "addtmp");
820 case '-': return Builder.CreateFSub(L, R, "subtmp");
821 case '*': return Builder.CreateFMul(L, R, "multmp");
822 case '<':
823 L = Builder.CreateFCmpULT(L, R, "cmptmp");
824 // Convert bool 0/1 to double 0.0 or 1.0
825 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
826 "booltmp");
827 default: return ErrorV("invalid binary operator");
828 }
829 }
830
831 Value *CallExprAST::Codegen() {
832 // Look up the name in the global module table.
833 Function *CalleeF = TheModule->getFunction(Callee);
834 if (CalleeF == 0)
835 return ErrorV("Unknown function referenced");
836
837 // If argument mismatch error.
838 if (CalleeF->arg_size() != Args.size())
839 return ErrorV("Incorrect # arguments passed");
840
841 std::vector ArgsV;
842 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
843 ArgsV.push_back(Args[i]->Codegen());
844 if (ArgsV.back() == 0) return 0;
845 }
846
847 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
848 }
849
850 Function *PrototypeAST::Codegen() {
851 // Make the function type: double(double,double) etc.
852 std::vector Doubles(Args.size(),
853 Type::getDoubleTy(getGlobalContext()));
854 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
855 Doubles, false);
856
857 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
858
859 // If F conflicted, there was already something named 'Name'. If it has a
860 // body, don't allow redefinition or reextern.
861 if (F->getName() != Name) {
862 // Delete the one we just made and get the existing one.
863 F->eraseFromParent();
864 F = TheModule->getFunction(Name);
865
866 // If F already has a body, reject this.
867 if (!F->empty()) {
868 ErrorF("redefinition of function");
869 return 0;
870 }
871
872 // If F took a different number of args, reject.
873 if (F->arg_size() != Args.size()) {
874 ErrorF("redefinition of function with different # args");
875 return 0;
876 }
877 }
878
879 // Set names for all arguments.
880 unsigned Idx = 0;
881 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
882 ++AI, ++Idx) {
883 AI->setName(Args[Idx]);
884
885 // Add arguments to variable symbol table.
886 NamedValues[Args[Idx]] = AI;
887 }
888
889 return F;
890 }
891
892 Function *FunctionAST::Codegen() {
893 NamedValues.clear();
894
895 Function *TheFunction = Proto->Codegen();
896 if (TheFunction == 0)
897 return 0;
898
899 // Create a new basic block to start insertion into.
900 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
901 Builder.SetInsertPoint(BB);
902
903 if (Value *RetVal = Body->Codegen()) {
904 // Finish off the function.
905 Builder.CreateRet(RetVal);
906
907 // Validate the generated code, checking for consistency.
908 verifyFunction(*TheFunction);
909
910 // Optimize the function.
911 TheFPM->run(*TheFunction);
912
913 return TheFunction;
914 }
915
916 // Error reading body, remove function.
917 TheFunction->eraseFromParent();
918 return 0;
919 }
920
921 //===----------------------------------------------------------------------===//
922 // Top-Level parsing and JIT Driver
923 //===----------------------------------------------------------------------===//
924
925 static ExecutionEngine *TheExecutionEngine;
926
927 static void HandleDefinition() {
928 if (FunctionAST *F = ParseDefinition()) {
929 if (Function *LF = F->Codegen()) {
930 fprintf(stderr, "Read function definition:");
931 LF->dump();
932 }
933 } else {
934 // Skip token for error recovery.
935 getNextToken();
936 }
937 }
938
939 static void HandleExtern() {
940 if (PrototypeAST *P = ParseExtern()) {
941 if (Function *F = P->Codegen()) {
942 fprintf(stderr, "Read extern: ");
943 F->dump();
944 }
945 } else {
946 // Skip token for error recovery.
947 getNextToken();
948 }
949 }
950
951 static void HandleTopLevelExpression() {
952 // Evaluate a top-level expression into an anonymous function.
953 if (FunctionAST *F = ParseTopLevelExpr()) {
954 if (Function *LF = F->Codegen()) {
955 fprintf(stderr, "Read top-level expression:");
956 LF->dump();
957
958 // JIT the function, returning a function pointer.
959 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
960
961 // Cast it to the right type (takes no arguments, returns a double) so we
962 // can call it as a native function.
963 double (*FP)() = (double (*)())(intptr_t)FPtr;
964 fprintf(stderr, "Evaluated to %f\n", FP());
965 }
966 } else {
967 // Skip token for error recovery.
968 getNextToken();
969 }
970 }
971
972 /// top ::= definition | external | expression | ';'
973 static void MainLoop() {
974 while (1) {
975 fprintf(stderr, "ready> ");
976 switch (CurTok) {
977 case tok_eof: return;
978 case ';': getNextToken(); break; // ignore top-level semicolons.
979 case tok_def: HandleDefinition(); break;
980 case tok_extern: HandleExtern(); break;
981 default: HandleTopLevelExpression(); break;
982 }
983 }
984 }
985
986 //===----------------------------------------------------------------------===//
987 // "Library" functions that can be "extern'd" from user code.
988 //===----------------------------------------------------------------------===//
989
990 /// putchard - putchar that takes a double and returns 0.
991 extern "C"
992 double putchard(double X) {
993 putchar((char)X);
994 return 0;
995 }
996
997 //===----------------------------------------------------------------------===//
998 // Main driver code.
999 //===----------------------------------------------------------------------===//
1000
1001 int main() {
1002 InitializeNativeTarget();
1003 LLVMContext &Context = getGlobalContext();
1004
1005 // Install standard binary operators.
1006 // 1 is lowest precedence.
1007 BinopPrecedence['<'] = 10;
1008 BinopPrecedence['+'] = 20;
1009 BinopPrecedence['-'] = 20;
1010 BinopPrecedence['*'] = 40; // highest.
1011
1012 // Prime the first token.
1013 fprintf(stderr, "ready> ");
1014 getNextToken();
1015
1016 // Make the module, which holds all the code.
1017 TheModule = new Module("my cool jit", Context);
1018
1019 // Create the JIT. This takes ownership of the module.
1020 std::string ErrStr;
1021 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1022 if (!TheExecutionEngine) {
1023 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
1024 exit(1);
1025 }
1026
1027 FunctionPassManager OurFPM(TheModule);
1028
1029 // Set up the optimizer pipeline. Start with registering info about how the
1030 // target lays out data structures.
1031 OurFPM.add(new DataLayout(*TheExecutionEngine->getDataLayout()));
1032 // Provide basic AliasAnalysis support for GVN.
1033 OurFPM.add(createBasicAliasAnalysisPass());
1034 // Do simple "peephole" optimizations and bit-twiddling optzns.
1035 OurFPM.add(createInstructionCombiningPass());
1036 // Reassociate expressions.
1037 OurFPM.add(createReassociatePass());
1038 // Eliminate Common SubExpressions.
1039 OurFPM.add(createGVNPass());
1040 // Simplify the control flow graph (deleting unreachable blocks, etc).
1041 OurFPM.add(createCFGSimplificationPass());
1042
1043 OurFPM.doInitialization();
1044
1045 // Set the global so the code gen can use this.
1046 TheFPM = &OurFPM;
1047
1048 // Run the main "interpreter loop" now.
1049 MainLoop();
1050
1051 TheFPM = 0;
1052
1053 // Print out all of the generated code.
1054 TheModule->dump();
1055
1056 return 0;
1057 }
440 .. literalinclude:: ../../examples/Kaleidoscope/Chapter4/toy.cpp
441 :language: c++
1058442
1059443 `Next: Extending the language: control flow `_
1060444
741741
742742 Here is the code:
743743
744 .. code-block:: c++
745
746 #include "llvm/DerivedTypes.h"
747 #include "llvm/ExecutionEngine/ExecutionEngine.h"
748 #include "llvm/ExecutionEngine/JIT.h"
749 #include "llvm/IRBuilder.h"
750 #include "llvm/LLVMContext.h"
751 #include "llvm/Module.h"
752 #include "llvm/PassManager.h"
753 #include "llvm/Analysis/Verifier.h"
754 #include "llvm/Analysis/Passes.h"
755 #include "llvm/DataLayout.h"
756 #include "llvm/Transforms/Scalar.h"
757 #include "llvm/Support/TargetSelect.h"
758 #include
759 #include
760 #include
761 #include
762 using namespace llvm;
763
764 //===----------------------------------------------------------------------===//
765 // Lexer
766 //===----------------------------------------------------------------------===//
767
768 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
769 // of these for known things.
770 enum Token {
771 tok_eof = -1,
772
773 // commands
774 tok_def = -2, tok_extern = -3,
775
776 // primary
777 tok_identifier = -4, tok_number = -5,
778
779 // control
780 tok_if = -6, tok_then = -7, tok_else = -8,
781 tok_for = -9, tok_in = -10
782 };
783
784 static std::string IdentifierStr; // Filled in if tok_identifier
785 static double NumVal; // Filled in if tok_number
786
787 /// gettok - Return the next token from standard input.
788 static int gettok() {
789 static int LastChar = ' ';
790
791 // Skip any whitespace.
792 while (isspace(LastChar))
793 LastChar = getchar();
794
795 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
796 IdentifierStr = LastChar;
797 while (isalnum((LastChar = getchar())))
798 IdentifierStr += LastChar;
799
800 if (IdentifierStr == "def") return tok_def;
801 if (IdentifierStr == "extern") return tok_extern;
802 if (IdentifierStr == "if") return tok_if;
803 if (IdentifierStr == "then") return tok_then;
804 if (IdentifierStr == "else") return tok_else;
805 if (IdentifierStr == "for") return tok_for;
806 if (IdentifierStr == "in") return tok_in;
807 return tok_identifier;
808 }
809
810 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
811 std::string NumStr;
812 do {
813 NumStr += LastChar;
814 LastChar = getchar();
815 } while (isdigit(LastChar) || LastChar == '.');
816
817 NumVal = strtod(NumStr.c_str(), 0);
818 return tok_number;
819 }
820
821 if (LastChar == '#') {
822 // Comment until end of line.
823 do LastChar = getchar();
824 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
825
826 if (LastChar != EOF)
827 return gettok();
828 }
829
830 // Check for end of file. Don't eat the EOF.
831 if (LastChar == EOF)
832 return tok_eof;
833
834 // Otherwise, just return the character as its ascii value.
835 int ThisChar = LastChar;
836 LastChar = getchar();
837 return ThisChar;
838 }
839
840 //===----------------------------------------------------------------------===//
841 // Abstract Syntax Tree (aka Parse Tree)
842 //===----------------------------------------------------------------------===//
843
844 /// ExprAST - Base class for all expression nodes.
845 class ExprAST {
846 public:
847 virtual ~ExprAST() {}
848 virtual Value *Codegen() = 0;
849 };
850
851 /// NumberExprAST - Expression class for numeric literals like "1.0".
852 class NumberExprAST : public ExprAST {
853 double Val;
854 public:
855 NumberExprAST(double val) : Val(val) {}
856 virtual Value *Codegen();
857 };
858
859 /// VariableExprAST - Expression class for referencing a variable, like "a".
860 class VariableExprAST : public ExprAST {
861 std::string Name;
862 public:
863 VariableExprAST(const std::string &name) : Name(name) {}
864 virtual Value *Codegen();
865 };
866
867 /// BinaryExprAST - Expression class for a binary operator.
868 class BinaryExprAST : public ExprAST {
869 char Op;
870 ExprAST *LHS, *RHS;
871 public:
872 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
873 : Op(op), LHS(lhs), RHS(rhs) {}
874 virtual Value *Codegen();
875 };
876
877 /// CallExprAST - Expression class for function calls.
878 class CallExprAST : public ExprAST {
879 std::string Callee;
880 std::vector Args;
881 public:
882 CallExprAST(const std::string &callee, std::vector &args)
883 : Callee(callee), Args(args) {}
884 virtual Value *Codegen();
885 };
886
887 /// IfExprAST - Expression class for if/then/else.
888 class IfExprAST : public ExprAST {
889 ExprAST *Cond, *Then, *Else;
890 public:
891 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
892 : Cond(cond), Then(then), Else(_else) {}
893 virtual Value *Codegen();
894 };
895
896 /// ForExprAST - Expression class for for/in.
897 class ForExprAST : public ExprAST {
898 std::string VarName;
899 ExprAST *Start, *End, *Step, *Body;
900 public:
901 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
902 ExprAST *step, ExprAST *body)
903 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
904 virtual Value *Codegen();
905 };
906
907 /// PrototypeAST - This class represents the "prototype" for a function,
908 /// which captures its name, and its argument names (thus implicitly the number
909 /// of arguments the function takes).
910 class PrototypeAST {
911 std::string Name;
912 std::vector Args;
913 public:
914 PrototypeAST(const std::string &name, const std::vector &args)
915 : Name(name), Args(args) {}
916
917 Function *Codegen();
918 };
919
920 /// FunctionAST - This class represents a function definition itself.
921 class FunctionAST {
922 PrototypeAST *Proto;
923 ExprAST *Body;
924 public:
925 FunctionAST(PrototypeAST *proto, ExprAST *body)
926 : Proto(proto), Body(body) {}
927
928 Function *Codegen();
929 };
930
931 //===----------------------------------------------------------------------===//
932 // Parser
933 //===----------------------------------------------------------------------===//
934
935 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
936 /// token the parser is looking at. getNextToken reads another token from the
937 /// lexer and updates CurTok with its results.
938 static int CurTok;
939 static int getNextToken() {
940 return CurTok = gettok();
941 }
942
943 /// BinopPrecedence - This holds the precedence for each binary operator that is
944 /// defined.
945 static std::map BinopPrecedence;
946
947 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
948 static int GetTokPrecedence() {
949 if (!isascii(CurTok))
950 return -1;
951
952 // Make sure it's a declared binop.
953 int TokPrec = BinopPrecedence[CurTok];
954 if (TokPrec <= 0) return -1;
955 return TokPrec;
956 }
957
958 /// Error* - These are little helper functions for error handling.
959 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
960 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
961 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
962
963 static ExprAST *ParseExpression();
964
965 /// identifierexpr
966 /// ::= identifier
967 /// ::= identifier '(' expression* ')'
968 static ExprAST *ParseIdentifierExpr() {
969 std::string IdName = IdentifierStr;
970
971 getNextToken(); // eat identifier.
972
973 if (CurTok != '(') // Simple variable ref.
974 return new VariableExprAST(IdName);
975
976 // Call.
977 getNextToken(); // eat (
978 std::vector Args;
979 if (CurTok != ')') {
980 while (1) {
981 ExprAST *Arg = ParseExpression();
982 if (!Arg) return 0;
983 Args.push_back(Arg);
984
985 if (CurTok == ')') break;
986
987 if (CurTok != ',')
988 return Error("Expected ')' or ',' in argument list");
989 getNextToken();
990 }
991 }
992
993 // Eat the ')'.
994 getNextToken();
995
996 return new CallExprAST(IdName, Args);
997 }
998
999 /// numberexpr ::= number
1000 static ExprAST *ParseNumberExpr() {
1001 ExprAST *Result = new NumberExprAST(NumVal);
1002 getNextToken(); // consume the number
1003 return Result;
1004 }
1005
1006 /// parenexpr ::= '(' expression ')'
1007 static ExprAST *ParseParenExpr() {
1008 getNextToken(); // eat (.
1009 ExprAST *V = ParseExpression();
1010 if (!V) return 0;
1011
1012 if (CurTok != ')')
1013 return Error("expected ')'");
1014 getNextToken(); // eat ).
1015 return V;
1016 }
1017
1018 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
1019 static ExprAST *ParseIfExpr() {
1020 getNextToken(); // eat the if.
1021
1022 // condition.
1023 ExprAST *Cond = ParseExpression();
1024 if (!Cond) return 0;
1025
1026 if (CurTok != tok_then)
1027 return Error("expected then");
1028 getNextToken(); // eat the then
1029
1030 ExprAST *Then = ParseExpression();
1031 if (Then == 0) return 0;
1032
1033 if (CurTok != tok_else)
1034 return Error("expected else");
1035
1036 getNextToken();
1037
1038 ExprAST *Else = ParseExpression();
1039 if (!Else) return 0;
1040
1041 return new IfExprAST(Cond, Then, Else);
1042 }
1043
1044 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
1045 static ExprAST *ParseForExpr() {
1046 getNextToken(); // eat the for.
1047
1048 if (CurTok != tok_identifier)
1049 return Error("expected identifier after for");
1050
1051 std::string IdName = IdentifierStr;
1052 getNextToken(); // eat identifier.
1053
1054 if (CurTok != '=')
1055 return Error("expected '=' after for");
1056 getNextToken(); // eat '='.
1057
1058
1059 ExprAST *Start = ParseExpression();
1060 if (Start == 0) return 0;
1061 if (CurTok != ',')
1062 return Error("expected ',' after for start value");
1063 getNextToken();
1064
1065 ExprAST *End = ParseExpression();
1066 if (End == 0) return 0;
1067
1068 // The step value is optional.
1069 ExprAST *Step = 0;
1070 if (CurTok == ',') {
1071 getNextToken();
1072 Step = ParseExpression();
1073 if (Step == 0) return 0;
1074 }
1075
1076 if (CurTok != tok_in)
1077 return Error("expected 'in' after for");
1078 getNextToken(); // eat 'in'.
1079
1080 ExprAST *Body = ParseExpression();
1081 if (Body == 0) return 0;
1082
1083 return new ForExprAST(IdName, Start, End, Step, Body);
1084 }
1085
1086 /// primary
1087 /// ::= identifierexpr
1088 /// ::= numberexpr
1089 /// ::= parenexpr
1090 /// ::= ifexpr
1091 /// ::= forexpr
1092 static ExprAST *ParsePrimary() {
1093 switch (CurTok) {
1094 default: return Error("unknown token when expecting an expression");
1095 case tok_identifier: return ParseIdentifierExpr();
1096 case tok_number: return ParseNumberExpr();
1097 case '(': return ParseParenExpr();
1098 case tok_if: return ParseIfExpr();
1099 case tok_for: return ParseForExpr();
1100 }
1101 }
1102
1103 /// binoprhs
1104 /// ::= ('+' primary)*
1105 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
1106 // If this is a binop, find its precedence.
1107 while (1) {
1108 int TokPrec = GetTokPrecedence();
1109
1110 // If this is a binop that binds at least as tightly as the current binop,
1111 // consume it, otherwise we are done.
1112 if (TokPrec < ExprPrec)
1113 return LHS;
1114
1115 // Okay, we know this is a binop.
1116 int BinOp = CurTok;
1117 getNextToken(); // eat binop
1118
1119 // Parse the primary expression after the binary operator.
1120 ExprAST *RHS = ParsePrimary();
1121 if (!RHS) return 0;
1122
1123 // If BinOp binds less tightly with RHS than the operator after RHS, let
1124 // the pending operator take RHS as its LHS.
1125 int NextPrec = GetTokPrecedence();
1126 if (TokPrec < NextPrec) {
1127 RHS = ParseBinOpRHS(TokPrec+1, RHS);
1128 if (RHS == 0) return 0;
1129 }
1130
1131 // Merge LHS/RHS.
1132 LHS = new BinaryExprAST(BinOp, LHS, RHS);
1133 }
1134 }
1135
1136 /// expression
1137 /// ::= primary binoprhs
1138 ///
1139 static ExprAST *ParseExpression() {
1140 ExprAST *LHS = ParsePrimary();
1141 if (!LHS) return 0;
1142
1143 return ParseBinOpRHS(0, LHS);
1144 }
1145
1146 /// prototype
1147 /// ::= id '(' id* ')'
1148 static PrototypeAST *ParsePrototype() {
1149 if (CurTok != tok_identifier)
1150 return ErrorP("Expected function name in prototype");
1151
1152 std::string FnName = IdentifierStr;
1153 getNextToken();
1154
1155 if (CurTok != '(')
1156 return ErrorP("Expected '(' in prototype");
1157
1158 std::vector ArgNames;
1159 while (getNextToken() == tok_identifier)
1160 ArgNames.push_back(IdentifierStr);
1161 if (CurTok != ')')
1162 return ErrorP("Expected ')' in prototype");
1163
1164 // success.
1165 getNextToken(); // eat ')'.
1166
1167 return new PrototypeAST(FnName, ArgNames);
1168 }
1169
1170 /// definition ::= 'def' prototype expression
1171 static FunctionAST *ParseDefinition() {
1172 getNextToken(); // eat def.
1173 PrototypeAST *Proto = ParsePrototype();
1174 if (Proto == 0) return 0;
1175
1176 if (ExprAST *E = ParseExpression())
1177 return new FunctionAST(Proto, E);
1178 return 0;
1179 }
1180
1181 /// toplevelexpr ::= expression
1182 static FunctionAST *ParseTopLevelExpr() {
1183 if (ExprAST *E = ParseExpression()) {
1184 // Make an anonymous proto.
1185 PrototypeAST *Proto = new PrototypeAST("", std::vector());
1186 return new FunctionAST(Proto, E);
1187 }
1188 return 0;
1189 }
1190
1191 /// external ::= 'extern' prototype
1192 static PrototypeAST *ParseExtern() {
1193 getNextToken(); // eat extern.
1194 return ParsePrototype();
1195 }
1196
1197 //===----------------------------------------------------------------------===//
1198 // Code Generation
1199 //===----------------------------------------------------------------------===//
1200
1201 static Module *TheModule;
1202 static IRBuilder<> Builder(getGlobalContext());
1203 static std::map NamedValues;
1204 static FunctionPassManager *TheFPM;
1205
1206 Value *ErrorV(const char *Str) { Error(Str); return 0; }
1207
1208 Value *NumberExprAST::Codegen() {
1209 return ConstantFP::get(getGlobalContext(), APFloat(Val));
1210 }
1211
1212 Value *VariableExprAST::Codegen() {
1213 // Look this variable up in the function.
1214 Value *V = NamedValues[Name];
1215 return V ? V : ErrorV("Unknown variable name");
1216 }
1217
1218 Value *BinaryExprAST::Codegen() {
1219 Value *L = LHS->Codegen();
1220 Value *R = RHS->Codegen();
1221 if (L == 0 || R == 0) return 0;
1222
1223 switch (Op) {
1224 case '+': return Builder.CreateFAdd(L, R, "addtmp");
1225 case '-': return Builder.CreateFSub(L, R, "subtmp");
1226 case '*': return Builder.CreateFMul(L, R, "multmp");
1227 case '<':
1228 L = Builder.CreateFCmpULT(L, R, "cmptmp");
1229 // Convert bool 0/1 to double 0.0 or 1.0
1230 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
1231 "booltmp");
1232 default: return ErrorV("invalid binary operator");
1233 }
1234 }
1235
1236 Value *CallExprAST::Codegen() {
1237 // Look up the name in the global module table.
1238 Function *CalleeF = TheModule->getFunction(Callee);
1239 if (CalleeF == 0)
1240 return ErrorV("Unknown function referenced");
1241
1242 // If argument mismatch error.
1243 if (CalleeF->arg_size() != Args.size())
1244 return ErrorV("Incorrect # arguments passed");
1245
1246 std::vector ArgsV;
1247 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1248 ArgsV.push_back(Args[i]->Codegen());
1249 if (ArgsV.back() == 0) return 0;
1250 }
1251
1252 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
1253 }
1254
1255 Value *IfExprAST::Codegen() {
1256 Value *CondV = Cond->Codegen();
1257 if (CondV == 0) return 0;
1258
1259 // Convert condition to a bool by comparing equal to 0.0.
1260 CondV = Builder.CreateFCmpONE(CondV,
1261 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1262 "ifcond");
1263
1264 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1265
1266 // Create blocks for the then and else cases. Insert the 'then' block at the
1267 // end of the function.
1268 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1269 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1270 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1271
1272 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1273
1274 // Emit then value.
1275 Builder.SetInsertPoint(ThenBB);
1276
1277 Value *ThenV = Then->Codegen();
1278 if (ThenV == 0) return 0;
1279
1280 Builder.CreateBr(MergeBB);
1281 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1282 ThenBB = Builder.GetInsertBlock();
1283
1284 // Emit else block.
1285 TheFunction->getBasicBlockList().push_back(ElseBB);
1286 Builder.SetInsertPoint(ElseBB);
1287
1288 Value *ElseV = Else->Codegen();
1289 if (ElseV == 0) return 0;
1290
1291 Builder.CreateBr(MergeBB);
1292 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1293 ElseBB = Builder.GetInsertBlock();
1294
1295 // Emit merge block.
1296 TheFunction->getBasicBlockList().push_back(MergeBB);
1297 Builder.SetInsertPoint(MergeBB);
1298 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1299 "iftmp");
1300
1301 PN->addIncoming(ThenV, ThenBB);
1302 PN->addIncoming(ElseV, ElseBB);
1303 return PN;
1304 }
1305
1306 Value *ForExprAST::Codegen() {
1307 // Output this as:
1308 // ...
1309 // start = startexpr
1310 // goto loop
1311 // loop:
1312 // variable = phi [start, loopheader], [nextvariable, loopend]
1313 // ...
1314 // bodyexpr
1315 // ...
1316 // loopend:
1317 // step = stepexpr
1318 // nextvariable = variable + step
1319 // endcond = endexpr
1320 // br endcond, loop, endloop
1321 // outloop:
1322
1323 // Emit the start code first, without 'variable' in scope.
1324 Value *StartVal = Start->Codegen();
1325 if (StartVal == 0) return 0;
1326
1327 // Make the new basic block for the loop header, inserting after current
1328 // block.
1329 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1330 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
1331 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1332
1333 // Insert an explicit fall through from the current block to the LoopBB.
1334 Builder.CreateBr(LoopBB);
1335
1336 // Start insertion in LoopBB.
1337 Builder.SetInsertPoint(LoopBB);
1338
1339 // Start the PHI node with an entry for Start.
1340 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, VarName.c_str());
1341 Variable->addIncoming(StartVal, PreheaderBB);
1342
1343 // Within the loop, the variable is defined equal to the PHI node. If it
1344 // shadows an existing variable, we have to restore it, so save it now.
1345 Value *OldVal = NamedValues[VarName];
1346 NamedValues[VarName] = Variable;
1347
1348 // Emit the body of the loop. This, like any other expr, can change the
1349 // current BB. Note that we ignore the value computed by the body, but don't
1350 // allow an error.
1351 if (Body->Codegen() == 0)
1352 return 0;
1353
1354 // Emit the step value.
1355 Value *StepVal;
1356 if (Step) {
1357 StepVal = Step->Codegen();
1358 if (StepVal == 0) return 0;
1359 } else {
1360 // If not specified, use 1.0.
1361 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1362 }
1363
1364 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
1365
1366 // Compute the end condition.
1367 Value *EndCond = End->Codegen();
1368 if (EndCond == 0) return EndCond;
1369
1370 // Convert condition to a bool by comparing equal to 0.0.
1371 EndCond = Builder.CreateFCmpONE(EndCond,
1372 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1373 "loopcond");
1374
1375 // Create the "after loop" block and insert it.
1376 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
1377 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1378
1379 // Insert the conditional branch into the end of LoopEndBB.
1380 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1381
1382 // Any new code will be inserted in AfterBB.
1383 Builder.SetInsertPoint(AfterBB);
1384
1385 // Add a new entry to the PHI node for the backedge.
1386 Variable->addIncoming(NextVar, LoopEndBB);
1387
1388 // Restore the unshadowed variable.
1389 if (OldVal)
1390 NamedValues[VarName] = OldVal;
1391 else
1392 NamedValues.erase(VarName);
1393
1394
1395 // for expr always returns 0.0.
1396 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1397 }
1398
1399 Function *PrototypeAST::Codegen() {
1400 // Make the function type: double(double,double) etc.
1401 std::vector Doubles(Args.size(),
1402 Type::getDoubleTy(getGlobalContext()));
1403 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1404 Doubles, false);
1405
1406 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
1407
1408 // If F conflicted, there was already something named 'Name'. If it has a
1409 // body, don't allow redefinition or reextern.
1410 if (F->getName() != Name) {
1411 // Delete the one we just made and get the existing one.
1412 F->eraseFromParent();
1413 F = TheModule->getFunction(Name);
1414
1415 // If F already has a body, reject this.
1416 if (!F->empty()) {
1417 ErrorF("redefinition of function");
1418 return 0;
1419 }
1420
1421 // If F took a different number of args, reject.
1422 if (F->arg_size() != Args.size()) {
1423 ErrorF("redefinition of function with different # args");
1424 return 0;
1425 }
1426 }
1427
1428 // Set names for all arguments.
1429 unsigned Idx = 0;
1430 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1431 ++AI, ++Idx) {
1432 AI->setName(Args[Idx]);
1433
1434 // Add arguments to variable symbol table.
1435 NamedValues[Args[Idx]] = AI;
1436 }
1437
1438 return F;
1439 }
1440
1441 Function *FunctionAST::Codegen() {
1442 NamedValues.clear();
1443
1444 Function *TheFunction = Proto->Codegen();
1445 if (TheFunction == 0)
1446 return 0;
1447
1448 // Create a new basic block to start insertion into.
1449 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1450 Builder.SetInsertPoint(BB);
1451
1452 if (Value *RetVal = Body->Codegen()) {
1453 // Finish off the function.
1454 Builder.CreateRet(RetVal);
1455
1456 // Validate the generated code, checking for consistency.
1457 verifyFunction(*TheFunction);
1458
1459 // Optimize the function.
1460 TheFPM->run(*TheFunction);
1461
1462 return TheFunction;
1463 }
1464
1465 // Error reading body, remove function.
1466 TheFunction->eraseFromParent();
1467 return 0;
1468 }
1469
1470 //===----------------------------------------------------------------------===//
1471 // Top-Level parsing and JIT Driver
1472 //===----------------------------------------------------------------------===//
1473
1474 static ExecutionEngine *TheExecutionEngine;
1475
1476 static void HandleDefinition() {
1477 if (FunctionAST *F = ParseDefinition()) {
1478 if (Function *LF = F->Codegen()) {
1479 fprintf(stderr, "Read function definition:");
1480 LF->dump();
1481 }
1482 } else {
1483 // Skip token for error recovery.
1484 getNextToken();
1485 }
1486 }
1487
1488 static void HandleExtern() {
1489 if (PrototypeAST *P = ParseExtern()) {
1490 if (Function *F = P->Codegen()) {
1491 fprintf(stderr, "Read extern: ");
1492 F->dump();
1493 }
1494 } else {
1495 // Skip token for error recovery.
1496 getNextToken();
1497 }
1498 }
1499
1500 static void HandleTopLevelExpression() {
1501 // Evaluate a top-level expression into an anonymous function.
1502 if (FunctionAST *F = ParseTopLevelExpr()) {
1503 if (Function *LF = F->Codegen()) {
1504 // JIT the function, returning a function pointer.
1505 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
1506
1507 // Cast it to the right type (takes no arguments, returns a double) so we
1508 // can call it as a native function.
1509 double (*FP)() = (double (*)())(intptr_t)FPtr;
1510 fprintf(stderr, "Evaluated to %f\n", FP());
1511 }
1512 } else {
1513 // Skip token for error recovery.
1514 getNextToken();
1515 }
1516 }
1517
1518 /// top ::= definition | external | expression | ';'
1519 static void MainLoop() {
1520 while (1) {
1521 fprintf(stderr, "ready> ");
1522 switch (CurTok) {
1523 case tok_eof: return;
1524 case ';': getNextToken(); break; // ignore top-level semicolons.
1525 case tok_def: HandleDefinition(); break;
1526 case tok_extern: HandleExtern(); break;
1527 default: HandleTopLevelExpression(); break;
1528 }
1529 }
1530 }
1531
1532 //===----------------------------------------------------------------------===//
1533 // "Library" functions that can be "extern'd" from user code.
1534 //===----------------------------------------------------------------------===//
1535
1536 /// putchard - putchar that takes a double and returns 0.
1537 extern "C"
1538 double putchard(double X) {
1539 putchar((char)X);
1540 return 0;
1541 }
1542
1543 //===----------------------------------------------------------------------===//
1544 // Main driver code.
1545 //===----------------------------------------------------------------------===//
1546
1547 int main() {
1548 InitializeNativeTarget();
1549 LLVMContext &Context = getGlobalContext();
1550
1551 // Install standard binary operators.
1552 // 1 is lowest precedence.
1553 BinopPrecedence['<'] = 10;
1554 BinopPrecedence['+'] = 20;
1555 BinopPrecedence['-'] = 20;
1556 BinopPrecedence['*'] = 40; // highest.
1557
1558 // Prime the first token.
1559 fprintf(stderr, "ready> ");
1560 getNextToken();
1561
1562 // Make the module, which holds all the code.
1563 TheModule = new Module("my cool jit", Context);
1564
1565 // Create the JIT. This takes ownership of the module.
1566 std::string ErrStr;
1567 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1568 if (!TheExecutionEngine) {
1569 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
1570 exit(1);
1571 }
1572
1573 FunctionPassManager OurFPM(TheModule);
1574
1575 // Set up the optimizer pipeline. Start with registering info about how the
1576 // target lays out data structures.
1577 OurFPM.add(new DataLayout(*TheExecutionEngine->getDataLayout()));
1578 // Provide basic AliasAnalysis support for GVN.
1579 OurFPM.add(createBasicAliasAnalysisPass());
1580 // Do simple "peephole" optimizations and bit-twiddling optzns.
1581 OurFPM.add(createInstructionCombiningPass());
1582 // Reassociate expressions.
1583 OurFPM.add(createReassociatePass());
1584 // Eliminate Common SubExpressions.
1585 OurFPM.add(createGVNPass());
1586 // Simplify the control flow graph (deleting unreachable blocks, etc).
1587 OurFPM.add(createCFGSimplificationPass());
1588
1589 OurFPM.doInitialization();
1590
1591 // Set the global so the code gen can use this.
1592 TheFPM = &OurFPM;
1593
1594 // Run the main "interpreter loop" now.
1595 MainLoop();
1596
1597 TheFPM = 0;
1598
1599 // Print out all of the generated code.
1600 TheModule->dump();
1601
1602 return 0;
1603 }
744 .. literalinclude:: ../../examples/Kaleidoscope/Chapter5/toy.cpp
745 :language: c++
1604746
1605747 `Next: Extending the language: user-defined operators `_
1606748
741741
742742 Here is the code:
743743
744 .. code-block:: c++
745
746 #include "llvm/DerivedTypes.h"
747 #include "llvm/ExecutionEngine/ExecutionEngine.h"
748 #include "llvm/ExecutionEngine/JIT.h"
749 #include "llvm/IRBuilder.h"
750 #include "llvm/LLVMContext.h"
751 #include "llvm/Module.h"
752 #include "llvm/PassManager.h"
753 #include "llvm/Analysis/Verifier.h"
754 #include "llvm/Analysis/Passes.h"
755 #include "llvm/DataLayout.h"
756 #include "llvm/Transforms/Scalar.h"
757 #include "llvm/Support/TargetSelect.h"
758 #include
759 #include
760 #include
761 #include
762 using namespace llvm;
763
764 //===----------------------------------------------------------------------===//
765 // Lexer
766 //===----------------------------------------------------------------------===//
767
768 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
769 // of these for known things.
770 enum Token {
771 tok_eof = -1,
772
773 // commands
774 tok_def = -2, tok_extern = -3,
775
776 // primary
777 tok_identifier = -4, tok_number = -5,
778
779 // control
780 tok_if = -6, tok_then = -7, tok_else = -8,
781 tok_for = -9, tok_in = -10,
782
783 // operators
784 tok_binary = -11, tok_unary = -12
785 };
786
787 static std::string IdentifierStr; // Filled in if tok_identifier
788 static double NumVal; // Filled in if tok_number
789
790 /// gettok - Return the next token from standard input.
791 static int gettok() {
792 static int LastChar = ' ';
793
794 // Skip any whitespace.
795 while (isspace(LastChar))
796 LastChar = getchar();
797
798 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
799 IdentifierStr = LastChar;
800 while (isalnum((LastChar = getchar())))
801 IdentifierStr += LastChar;
802
803 if (IdentifierStr == "def") return tok_def;
804 if (IdentifierStr == "extern") return tok_extern;
805 if (IdentifierStr == "if") return tok_if;
806 if (IdentifierStr == "then") return tok_then;
807 if (IdentifierStr == "else") return tok_else;
808 if (IdentifierStr == "for") return tok_for;
809 if (IdentifierStr == "in") return tok_in;
810 if (IdentifierStr == "binary") return tok_binary;
811 if (IdentifierStr == "unary") return tok_unary;
812 return tok_identifier;
813 }
814
815 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
816 std::string NumStr;
817 do {
818 NumStr += LastChar;
819 LastChar = getchar();
820 } while (isdigit(LastChar) || LastChar == '.');
821
822 NumVal = strtod(NumStr.c_str(), 0);
823 return tok_number;
824 }
825
826 if (LastChar == '#') {
827 // Comment until end of line.
828 do LastChar = getchar();
829 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
830
831 if (LastChar != EOF)
832 return gettok();
833 }
834
835 // Check for end of file. Don't eat the EOF.
836 if (LastChar == EOF)
837 return tok_eof;
838
839 // Otherwise, just return the character as its ascii value.
840 int ThisChar = LastChar;
841 LastChar = getchar();
842 return ThisChar;
843 }
844
845 //===----------------------------------------------------------------------===//
846 // Abstract Syntax Tree (aka Parse Tree)
847 //===----------------------------------------------------------------------===//
848
849 /// ExprAST - Base class for all expression nodes.
850 class ExprAST {
851 public:
852 virtual ~ExprAST() {}
853 virtual Value *Codegen() = 0;
854 };
855
856 /// NumberExprAST - Expression class for numeric literals like "1.0".
857 class NumberExprAST : public ExprAST {
858 double Val;
859 public:
860 NumberExprAST(double val) : Val(val) {}
861 virtual Value *Codegen();
862 };
863
864 /// VariableExprAST - Expression class for referencing a variable, like "a".
865 class VariableExprAST : public ExprAST {
866 std::string Name;
867 public:
868 VariableExprAST(const std::string &name) : Name(name) {}
869 virtual Value *Codegen();
870 };
871
872 /// UnaryExprAST - Expression class for a unary operator.
873 class UnaryExprAST : public ExprAST {
874 char Opcode;
875 ExprAST *Operand;
876 public:
877 UnaryExprAST(char opcode, ExprAST *operand)
878 : Opcode(opcode), Operand(operand) {}
879 virtual Value *Codegen();
880 };
881
882 /// BinaryExprAST - Expression class for a binary operator.
883 class BinaryExprAST : public ExprAST {
884 char Op;
885 ExprAST *LHS, *RHS;
886 public:
887 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
888 : Op(op), LHS(lhs), RHS(rhs) {}
889 virtual Value *Codegen();
890 };
891
892 /// CallExprAST - Expression class for function calls.
893 class CallExprAST : public ExprAST {
894 std::string Callee;
895 std::vector Args;
896 public:
897 CallExprAST(const std::string &callee, std::vector &args)
898 : Callee(callee), Args(args) {}
899 virtual Value *Codegen();
900 };
901
902 /// IfExprAST - Expression class for if/then/else.
903 class IfExprAST : public ExprAST {
904 ExprAST *Cond, *Then, *Else;
905 public:
906 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
907 : Cond(cond), Then(then), Else(_else) {}
908 virtual Value *Codegen();
909 };
910
911 /// ForExprAST - Expression class for for/in.
912 class ForExprAST : public ExprAST {
913 std::string VarName;
914 ExprAST *Start, *End, *Step, *Body;
915 public:
916 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
917 ExprAST *step, ExprAST *body)
918 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
919 virtual Value *Codegen();
920 };
921
922 /// PrototypeAST - This class represents the "prototype" for a function,
923 /// which captures its name, and its argument names (thus implicitly the number
924 /// of arguments the function takes), as well as if it is an operator.
925 class PrototypeAST {
926 std::string Name;
927 std::vector Args;
928 bool isOperator;
929 unsigned Precedence; // Precedence if a binary op.
930 public:
931 PrototypeAST(const std::string &name, const std::vector &args,
932 bool isoperator = false, unsigned prec = 0)
933 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
934
935 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
936 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
937
938 char getOperatorName() const {
939 assert(isUnaryOp() || isBinaryOp());
940 return Name[Name.size()-1];
941 }
942
943 unsigned getBinaryPrecedence() const { return Precedence; }
944
945 Function *Codegen();
946 };
947
948 /// FunctionAST - This class represents a function definition itself.
949 class FunctionAST {
950 PrototypeAST *Proto;
951 ExprAST *Body;
952 public:
953 FunctionAST(PrototypeAST *proto, ExprAST *body)
954 : Proto(proto), Body(body) {}
955
956 Function *Codegen();
957 };
958
959 //===----------------------------------------------------------------------===//
960 // Parser
961 //===----------------------------------------------------------------------===//
962
963 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
964 /// token the parser is looking at. getNextToken reads another token from the
965 /// lexer and updates CurTok with its results.
966 static int CurTok;
967 static int getNextToken() {
968 return CurTok = gettok();
969 }
970
971 /// BinopPrecedence - This holds the precedence for each binary operator that is
972 /// defined.
973 static std::map BinopPrecedence;
974
975 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
976 static int GetTokPrecedence() {
977 if (!isascii(CurTok))
978 return -1;
979
980 // Make sure it's a declared binop.
981 int TokPrec = BinopPrecedence[CurTok];
982 if (TokPrec <= 0) return -1;
983 return TokPrec;
984 }
985
986 /// Error* - These are little helper functions for error handling.
987 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
988 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
989 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
990
991 static ExprAST *ParseExpression();
992
993 /// identifierexpr
994 /// ::= identifier
995 /// ::= identifier '(' expression* ')'
996 static ExprAST *ParseIdentifierExpr() {
997 std::string IdName = IdentifierStr;
998
999 getNextToken(); // eat identifier.
1000
1001 if (CurTok != '(') // Simple variable ref.
1002 return new VariableExprAST(IdName);
1003
1004 // Call.
1005 getNextToken(); // eat (
1006 std::vector Args;
1007 if (CurTok != ')') {
1008 while (1) {
1009 ExprAST *Arg = ParseExpression();
1010 if (!Arg) return 0;
1011 Args.push_back(Arg);
1012
1013 if (CurTok == ')') break;
1014
1015 if (CurTok != ',')
1016 return Error("Expected ')' or ',' in argument list");
1017 getNextToken();
1018 }
1019 }
1020
1021 // Eat the ')'.
1022 getNextToken();
1023
1024 return new CallExprAST(IdName, Args);
1025 }
1026
1027 /// numberexpr ::= number
1028 static ExprAST *ParseNumberExpr() {
1029 ExprAST *Result = new NumberExprAST(NumVal);
1030 getNextToken(); // consume the number
1031 return Result;
1032 }
1033
1034 /// parenexpr ::= '(' expression ')'
1035 static ExprAST *ParseParenExpr() {
1036 getNextToken(); // eat (.
1037 ExprAST *V = ParseExpression();
1038 if (!V) return 0;
1039
1040 if (CurTok != ')')
1041 return Error("expected ')'");
1042 getNextToken(); // eat ).
1043 return V;
1044 }
1045
1046 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
1047 static ExprAST *ParseIfExpr() {
1048 getNextToken(); // eat the if.
1049
1050 // condition.
1051 ExprAST *Cond = ParseExpression();
1052 if (!Cond) return 0;
1053
1054 if (CurTok != tok_then)
1055 return Error("expected then");
1056 getNextToken(); // eat the then
1057
1058 ExprAST *Then = ParseExpression();
1059 if (Then == 0) return 0;
1060
1061 if (CurTok != tok_else)
1062 return Error("expected else");
1063
1064 getNextToken();
1065
1066 ExprAST *Else = ParseExpression();
1067 if (!Else) return 0;
1068
1069 return new IfExprAST(Cond, Then, Else);
1070 }
1071
1072 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
1073 static ExprAST *ParseForExpr() {
1074 getNextToken(); // eat the for.
1075
1076 if (CurTok != tok_identifier)
1077 return Error("expected identifier after for");
1078
1079 std::string IdName = IdentifierStr;
1080 getNextToken(); // eat identifier.
1081
1082 if (CurTok != '=')
1083 return Error("expected '=' after for");
1084 getNextToken(); // eat '='.
1085
1086
1087 ExprAST *Start = ParseExpression();
1088 if (Start == 0) return 0;
1089 if (CurTok != ',')
1090 return Error("expected ',' after for start value");
1091 getNextToken();
1092
1093 ExprAST *End = ParseExpression();
1094 if (End == 0) return 0;
1095
1096 // The step value is optional.
1097 ExprAST *Step = 0;
1098 if (CurTok == ',') {
1099 getNextToken();
1100 Step = ParseExpression();
1101 if (Step == 0) return 0;
1102 }
1103
1104 if (CurTok != tok_in)
1105 return Error("expected 'in' after for");
1106 getNextToken(); // eat 'in'.
1107
1108 ExprAST *Body = ParseExpression();
1109 if (Body == 0) return 0;
1110
1111 return new ForExprAST(IdName, Start, End, Step, Body);
1112 }
1113
1114 /// primary
1115 /// ::= identifierexpr
1116 /// ::= numberexpr
1117 /// ::= parenexpr
1118 /// ::= ifexpr
1119 /// ::= forexpr
1120 static ExprAST *ParsePrimary() {
1121 switch (CurTok) {
1122 default: return Error("unknown token when expecting an expression");
1123 case tok_identifier: return ParseIdentifierExpr();
1124 case tok_number: return ParseNumberExpr();
1125 case '(': return ParseParenExpr();
1126 case tok_if: return ParseIfExpr();
1127 case tok_for: return ParseForExpr();
1128 }
1129 }
1130
1131 /// unary
1132 /// ::= primary
1133 /// ::= '!' unary
1134 static ExprAST *ParseUnary() {
1135 // If the current token is not an operator, it must be a primary expr.
1136 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
1137 return ParsePrimary();
1138
1139 // If this is a unary operator, read it.
1140 int Opc = CurTok;
1141 getNextToken();
1142 if (ExprAST *Operand = ParseUnary())
1143 return new UnaryExprAST(Opc, Operand);
1144 return 0;
1145 }
1146
1147 /// binoprhs
1148 /// ::= ('+' unary)*
1149 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
1150 // If this is a binop, find its precedence.
1151 while (1) {
1152 int TokPrec = GetTokPrecedence();
1153
1154 // If this is a binop that binds at least as tightly as the current binop,
1155 // consume it, otherwise we are done.
1156 if (TokPrec < ExprPrec)
1157 return LHS;
1158
1159 // Okay, we know this is a binop.
1160 int BinOp = CurTok;
1161 getNextToken(); // eat binop
1162
1163 // Parse the unary expression after the binary operator.
1164 ExprAST *RHS = ParseUnary();
1165 if (!RHS) return 0;
1166
1167 // If BinOp binds less tightly with RHS than the operator after RHS, let
1168 // the pending operator take RHS as its LHS.
1169 int NextPrec = GetTokPrecedence();
1170 if (TokPrec < NextPrec) {
1171 RHS = ParseBinOpRHS(TokPrec+1, RHS);
1172 if (RHS == 0) return 0;
1173 }
1174
1175 // Merge LHS/RHS.
1176 LHS = new BinaryExprAST(BinOp, LHS, RHS);
1177 }
1178 }
1179
1180 /// expression
1181 /// ::= unary binoprhs
1182 ///
1183 static ExprAST *ParseExpression() {
1184 ExprAST *LHS = ParseUnary();
1185 if (!LHS) return 0;
1186
1187 return ParseBinOpRHS(0, LHS);
1188 }
1189
1190 /// prototype
1191 /// ::= id '(' id* ')'
1192 /// ::= binary LETTER number? (id, id)
1193 /// ::= unary LETTER (id)
1194 static PrototypeAST *ParsePrototype() {
1195 std::string FnName;
1196
1197 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
1198 unsigned BinaryPrecedence = 30;
1199
1200 switch (CurTok) {
1201 default:
1202 return ErrorP("Expected function name in prototype");
1203 case tok_identifier:
1204 FnName = IdentifierStr;
1205 Kind = 0;
1206 getNextToken();
1207 break;
1208 case tok_unary:
1209 getNextToken();
1210 if (!isascii(CurTok))
1211 return ErrorP("Expected unary operator");
1212 FnName = "unary";
1213 FnName += (char)CurTok;
1214 Kind = 1;
1215 getNextToken();
1216 break;
1217 case tok_binary:
1218 getNextToken();
1219 if (!isascii(CurTok))
1220 return ErrorP("Expected binary operator");
1221 FnName = "binary";
1222 FnName += (char)CurTok;
1223 Kind = 2;
1224 getNextToken();
1225
1226 // Read the precedence if present.
1227 if (CurTok == tok_number) {
1228 if (NumVal < 1 || NumVal > 100)
1229 return ErrorP("Invalid precedecnce: must be 1..100");
1230 BinaryPrecedence = (unsigned)NumVal;
1231 getNextToken();
1232 }
1233 break;
1234 }
1235
1236 if (CurTok != '(')
1237 return ErrorP("Expected '(' in prototype");
1238
1239 std::vector ArgNames;
1240 while (getNextToken() == tok_identifier)
1241 ArgNames.push_back(IdentifierStr);
1242 if (CurTok != ')')
1243 return ErrorP("Expected ')' in prototype");
1244
1245 // success.
1246 getNextToken(); // eat ')'.
1247
1248 // Verify right number of names for operator.
1249 if (Kind && ArgNames.size() != Kind)
1250 return ErrorP("Invalid number of operands for operator");
1251
1252 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
1253 }
1254
1255 /// definition ::= 'def' prototype expression
1256 static FunctionAST *ParseDefinition() {
1257 getNextToken(); // eat def.
1258 PrototypeAST *Proto = ParsePrototype();
1259 if (Proto == 0) return 0;
1260
1261 if (ExprAST *E = ParseExpression())
1262 return new FunctionAST(Proto, E);
1263 return 0;
1264 }
1265
1266 /// toplevelexpr ::= expression
1267 static FunctionAST *ParseTopLevelExpr() {
1268 if (ExprAST *E = ParseExpression()) {
1269 // Make an anonymous proto.
1270 PrototypeAST *Proto = new PrototypeAST("", std::vector());
1271 return new FunctionAST(Proto, E);
1272 }
1273 return 0;
1274 }
1275
1276 /// external ::= 'extern' prototype
1277 static PrototypeAST *ParseExtern() {
1278 getNextToken(); // eat extern.
1279 return ParsePrototype();
1280 }
1281
1282 //===----------------------------------------------------------------------===//
1283 // Code Generation
1284 //===----------------------------------------------------------------------===//
1285
1286 static Module *TheModule;
1287 static IRBuilder<> Builder(getGlobalContext());
1288 static std::map NamedValues;
1289 static FunctionPassManager *TheFPM;
1290
1291 Value *ErrorV(const char *Str) { Error(Str); return 0; }
1292
1293 Value *NumberExprAST::Codegen() {
1294 return ConstantFP::get(getGlobalContext(), APFloat(Val));
1295 }
1296
1297 Value *VariableExprAST::Codegen() {
1298 // Look this variable up in the function.
1299 Value *V = NamedValues[Name];
1300 return V ? V : ErrorV("Unknown variable name");
1301 }
1302
1303 Value *UnaryExprAST::Codegen() {
1304 Value *OperandV = Operand->Codegen();
1305 if (OperandV == 0) return 0;
1306
1307 Function *F = TheModule->getFunction(std::string("unary")+Opcode);
1308 if (F == 0)
1309 return ErrorV("Unknown unary operator");
1310
1311 return Builder.CreateCall(F, OperandV, "unop");
1312 }
1313
1314 Value *BinaryExprAST::Codegen() {
1315 Value *L = LHS->Codegen();
1316 Value *R = RHS->Codegen();
1317 if (L == 0 || R == 0) return 0;
1318
1319 switch (Op) {
1320 case '+': return Builder.CreateFAdd(L, R, "addtmp");
1321 case '-': return Builder.CreateFSub(L, R, "subtmp");
1322 case '*': return Builder.CreateFMul(L, R, "multmp");
1323 case '<':
1324 L = Builder.CreateFCmpULT(L, R, "cmptmp");
1325 // Convert bool 0/1 to double 0.0 or 1.0
1326 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
1327 "booltmp");
1328 default: break;
1329 }
1330
1331 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
1332 // a call to it.
1333 Function *F = TheModule->getFunction(std::string("binary")+Op);
1334 assert(F && "binary operator not found!");
1335
1336 Value *Ops[2] = { L, R };
1337 return Builder.CreateCall(F, Ops, "binop");
1338 }
1339
1340 Value *CallExprAST::Codegen() {
1341 // Look up the name in the global module table.
1342 Function *CalleeF = TheModule->getFunction(Callee);
1343 if (CalleeF == 0)
1344 return ErrorV("Unknown function referenced");
1345
1346 // If argument mismatch error.
1347 if (CalleeF->arg_size() != Args.size())
1348 return ErrorV("Incorrect # arguments passed");
1349
1350 std::vector ArgsV;
1351 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1352 ArgsV.push_back(Args[i]->Codegen());
1353 if (ArgsV.back() == 0) return 0;
1354 }
1355
1356 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
1357 }
1358
1359 Value *IfExprAST::Codegen() {
1360 Value *CondV = Cond->Codegen();
1361 if (CondV == 0) return 0;
1362
1363 // Convert condition to a bool by comparing equal to 0.0.
1364 CondV = Builder.CreateFCmpONE(CondV,
1365 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1366 "ifcond");
1367
1368 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1369
1370 // Create blocks for the then and else cases. Insert the 'then' block at the
1371 // end of the function.
1372 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1373 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1374 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1375
1376 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1377
1378 // Emit then value.
1379 Builder.SetInsertPoint(ThenBB);
1380
1381 Value *ThenV = Then->Codegen();
1382 if (ThenV == 0) return 0;
1383
1384 Builder.CreateBr(MergeBB);
1385 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1386 ThenBB = Builder.GetInsertBlock();
1387
1388 // Emit else block.
1389 TheFunction->getBasicBlockList().push_back(ElseBB);
1390 Builder.SetInsertPoint(ElseBB);
1391
1392 Value *ElseV = Else->Codegen();
1393 if (ElseV == 0) return 0;
1394
1395 Builder.CreateBr(MergeBB);
1396 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1397 ElseBB = Builder.GetInsertBlock();
1398
1399 // Emit merge block.
1400 TheFunction->getBasicBlockList().push_back(MergeBB);
1401 Builder.SetInsertPoint(MergeBB);
1402 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1403 "iftmp");
1404
1405 PN->addIncoming(ThenV, ThenBB);
1406 PN->addIncoming(ElseV, ElseBB);
1407 return PN;
1408 }
1409
1410 Value *ForExprAST::Codegen() {
1411 // Output this as:
1412 // ...
1413 // start = startexpr
1414 // goto loop
1415 // loop:
1416 // variable = phi [start, loopheader], [nextvariable, loopend]
1417 // ...
1418 // bodyexpr
1419 // ...
1420 // loopend:
1421 // step = stepexpr
1422 // nextvariable = variable + step
1423 // endcond = endexpr
1424 // br endcond, loop, endloop
1425 // outloop:
1426
1427 // Emit the start code first, without 'variable' in scope.
1428 Value *StartVal = Start->Codegen();
1429 if (StartVal == 0) return 0;
1430
1431 // Make the new basic block for the loop header, inserting after current
1432 // block.
1433 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1434 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
1435 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1436
1437 // Insert an explicit fall through from the current block to the LoopBB.
1438 Builder.CreateBr(LoopBB);
1439
1440 // Start insertion in LoopBB.
1441 Builder.SetInsertPoint(LoopBB);
1442
1443 // Start the PHI node with an entry for Start.
1444 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, VarName.c_str());
1445 Variable->addIncoming(StartVal, PreheaderBB);
1446
1447 // Within the loop, the variable is defined equal to the PHI node. If it
1448 // shadows an existing variable, we have to restore it, so save it now.
1449 Value *OldVal = NamedValues[VarName];
1450 NamedValues[VarName] = Variable;
1451
1452 // Emit the body of the loop. This, like any other expr, can change the
1453 // current BB. Note that we ignore the value computed by the body, but don't
1454 // allow an error.
1455 if (Body->Codegen() == 0)
1456 return 0;
1457
1458 // Emit the step value.
1459 Value *StepVal;
1460 if (Step) {
1461 StepVal = Step->Codegen();
1462 if (StepVal == 0) return 0;
1463 } else {
1464 // If not specified, use 1.0.
1465 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1466 }
1467
1468 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
1469
1470 // Compute the end condition.
1471 Value *EndCond = End->Codegen();
1472 if (EndCond == 0) return EndCond;
1473
1474 // Convert condition to a bool by comparing equal to 0.0.
1475 EndCond = Builder.CreateFCmpONE(EndCond,
1476 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1477 "loopcond");
1478
1479 // Create the "after loop" block and insert it.
1480 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
1481 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1482
1483 // Insert the conditional branch into the end of LoopEndBB.
1484 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1485
1486 // Any new code will be inserted in AfterBB.
1487 Builder.SetInsertPoint(AfterBB);
1488
1489 // Add a new entry to the PHI node for the backedge.
1490 Variable->addIncoming(NextVar, LoopEndBB);
1491
1492 // Restore the unshadowed variable.
1493 if (OldVal)
1494 NamedValues[VarName] = OldVal;
1495 else
1496 NamedValues.erase(VarName);
1497
1498
1499 // for expr always returns 0.0.
1500 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1501 }
1502
1503 Function *PrototypeAST::Codegen() {
1504 // Make the function type: double(double,double) etc.
1505 std::vector Doubles(Args.size(),
1506 Type::getDoubleTy(getGlobalContext()));
1507 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1508 Doubles, false);
1509
1510 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
1511
1512 // If F conflicted, there was already something named 'Name'. If it has a
1513 // body, don't allow redefinition or reextern.
1514 if (F->getName() != Name) {
1515 // Delete the one we just made and get the existing one.
1516 F->eraseFromParent();
1517 F = TheModule->getFunction(Name);
1518
1519 // If F already has a body, reject this.
1520 if (!F->empty()) {
1521 ErrorF("redefinition of function");
1522 return 0;
1523 }
1524
1525 // If F took a different number of args, reject.
1526 if (F->arg_size() != Args.size()) {
1527 ErrorF("redefinition of function with different # args");
1528 return 0;
1529 }
1530 }
1531
1532 // Set names for all arguments.
1533 unsigned Idx = 0;
1534 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1535 ++AI, ++Idx) {
1536 AI->setName(Args[Idx]);
1537
1538 // Add arguments to variable symbol table.
1539 NamedValues[Args[Idx]] = AI;
1540 }
1541
1542 return F;
1543 }
1544
1545 Function *FunctionAST::Codegen() {
1546 NamedValues.clear();
1547
1548 Function *TheFunction = Proto->Codegen();
1549 if (TheFunction == 0)
1550 return 0;
1551
1552 // If this is an operator, install it.
1553 if (Proto->isBinaryOp())
1554 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
1555
1556 // Create a new basic block to start insertion into.
1557 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1558 Builder.SetInsertPoint(BB);
1559
1560 if (Value *RetVal = Body->Codegen()) {
1561 // Finish off the function.
1562 Builder.CreateRet(RetVal);
1563
1564 // Validate the generated code, checking for consistency.
1565 verifyFunction(*TheFunction);
1566
1567 // Optimize the function.
1568 TheFPM->run(*TheFunction);
1569
1570 return TheFunction;
1571 }
1572
1573 // Error reading body, remove function.
1574 TheFunction->eraseFromParent();
1575
1576 if (Proto->isBinaryOp())
1577 BinopPrecedence.erase(Proto->getOperatorName());
1578 return 0;
1579 }
1580
1581 //===----------------------------------------------------------------------===//
1582 // Top-Level parsing and JIT Driver
1583 //===----------------------------------------------------------------------===//
1584
1585 static ExecutionEngine *TheExecutionEngine;
1586
1587 static void HandleDefinition() {
1588 if (FunctionAST *F = ParseDefinition()) {
1589 if (Function *LF = F->Codegen()) {
1590 fprintf(stderr, "Read function definition:");
1591 LF->dump();
1592 }
1593 } else {
1594 // Skip token for error recovery.
1595 getNextToken();
1596 }
1597 }
1598
1599 static void HandleExtern() {
1600 if (PrototypeAST *P = ParseExtern()) {
1601 if (Function *F = P->Codegen()) {
1602 fprintf(stderr, "Read extern: ");
1603 F->dump();
1604 }
1605 } else {
1606 // Skip token for error recovery.
1607 getNextToken();
1608 }
1609 }
1610
1611 static void HandleTopLevelExpression() {
1612 // Evaluate a top-level expression into an anonymous function.
1613 if (FunctionAST *F = ParseTopLevelExpr()) {
1614 if (Function *LF = F->Codegen()) {
1615 // JIT the function, returning a function pointer.
1616 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
1617
1618 // Cast it to the right type (takes no arguments, returns a double) so we
1619 // can call it as a native function.
1620 double (*FP)() = (double (*)())(intptr_t)FPtr;
1621 fprintf(stderr, "Evaluated to %f\n", FP());
1622 }
1623 } else {
1624 // Skip token for error recovery.
1625 getNextToken();
1626 }
1627 }
1628
1629 /// top ::= definition | external | expression | ';'
1630 static void MainLoop() {
1631 while (1) {
1632 fprintf(stderr, "ready> ");
1633 switch (CurTok) {
1634 case tok_eof: return;
1635 case ';': getNextToken(); break; // ignore top-level semicolons.
1636 case tok_def: HandleDefinition(); break;
1637 case tok_extern: HandleExtern(); break;
1638 default: HandleTopLevelExpression(); break;
1639 }
1640 }
1641 }
1642
1643 //===----------------------------------------------------------------------===//
1644 // "Library" functions that can be "extern'd" from user code.
1645 //===----------------------------------------------------------------------===//
1646
1647 /// putchard - putchar that takes a double and returns 0.
1648 extern "C"
1649 double putchard(double X) {
1650 putchar((char)X);
1651 return 0;
1652 }
1653
1654 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1655 extern "C"
1656 double printd(double X) {
1657 printf("%f\n", X);
1658 return 0;
1659 }
1660
1661 //===----------------------------------------------------------------------===//
1662 // Main driver code.
1663 //===----------------------------------------------------------------------===//
1664
1665 int main() {
1666 InitializeNativeTarget();
1667 LLVMContext &Context = getGlobalContext();
1668
1669 // Install standard binary operators.
1670 // 1 is lowest precedence.
1671 BinopPrecedence['<'] = 10;
1672 BinopPrecedence['+'] = 20;
1673 BinopPrecedence['-'] = 20;
1674 BinopPrecedence['*'] = 40; // highest.
1675
1676 // Prime the first token.
1677 fprintf(stderr, "ready> ");
1678 getNextToken();
1679
1680 // Make the module, which holds all the code.
1681 TheModule = new Module("my cool jit", Context);
1682
1683 // Create the JIT. This takes ownership of the module.
1684 std::string ErrStr;
1685 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1686 if (!TheExecutionEngine) {
1687 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
1688 exit(1);
1689 }
1690
1691 FunctionPassManager OurFPM(TheModule);
1692
1693 // Set up the optimizer pipeline. Start with registering info about how the
1694 // target lays out data structures.
1695 OurFPM.add(new DataLayout(*TheExecutionEngine->getDataLayout()));
1696 // Provide basic AliasAnalysis support for GVN.
1697 OurFPM.add(createBasicAliasAnalysisPass());
1698 // Do simple "peephole" optimizations and bit-twiddling optzns.
1699 OurFPM.add(createInstructionCombiningPass());
1700 // Reassociate expressions.
1701 OurFPM.add(createReassociatePass());
1702 // Eliminate Common SubExpressions.
1703 OurFPM.add(createGVNPass());
1704 // Simplify the control flow graph (deleting unreachable blocks, etc).
1705 OurFPM.add(createCFGSimplificationPass());
1706
1707 OurFPM.doInitialization();
1708
1709 // Set the global so the code gen can use this.
1710 TheFPM = &OurFPM;
1711
1712 // Run the main "interpreter loop" now.
1713 MainLoop();
1714
1715 TheFPM = 0;
1716
1717 // Print out all of the generated code.
1718 TheModule->dump();
1719
1720 return 0;
1721 }
744 .. literalinclude:: ../../examples/Kaleidoscope/Chapter6/toy.cpp
745 :language: c++
1722746
1723747 `Next: Extending the language: mutable variables / SSA
1724748 construction `_
852852
853853 Here is the code:
854854
855 .. code-block:: c++
856
857 #include "llvm/DerivedTypes.h"
858 #include "llvm/ExecutionEngine/ExecutionEngine.h"
859 #include "llvm/ExecutionEngine/JIT.h"
860 #include "llvm/IRBuilder.h"
861 #include "llvm/LLVMContext.h"
862 #include "llvm/Module.h"
863 #include "llvm/PassManager.h"
864 #include "llvm/Analysis/Verifier.h"
865 #include "llvm/Analysis/Passes.h"
866 #include "llvm/DataLayout.h"
867 #include "llvm/Transforms/Scalar.h"
868 #include "llvm/Support/TargetSelect.h"
869 #include
870 #include
871 #include
872 #include
873 using namespace llvm;
874
875 //===----------------------------------------------------------------------===//
876 // Lexer
877 //===----------------------------------------------------------------------===//
878
879 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
880 // of these for known things.
881 enum Token {
882 tok_eof = -1,
883
884 // commands
885 tok_def = -2, tok_extern = -3,
886
887 // primary
888 tok_identifier = -4, tok_number = -5,
889
890 // control
891 tok_if = -6, tok_then = -7, tok_else = -8,
892 tok_for = -9, tok_in = -10,
893
894 // operators
895 tok_binary = -11, tok_unary = -12,
896
897 // var definition
898 tok_var = -13
899 };
900
901 static std::string IdentifierStr; // Filled in if tok_identifier
902 static double NumVal; // Filled in if tok_number
903
904 /// gettok - Return the next token from standard input.
905 static int gettok() {
906 static int LastChar = ' ';
907
908 // Skip any whitespace.
909 while (isspace(LastChar))
910 LastChar = getchar();
911
912 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
913 IdentifierStr = LastChar;
914 while (isalnum((LastChar = getchar())))
915 IdentifierStr += LastChar;
916
917 if (IdentifierStr == "def") return tok_def;
918 if (IdentifierStr == "extern") return tok_extern;
919 if (IdentifierStr == "if") return tok_if;
920 if (IdentifierStr == "then") return tok_then;
921 if (IdentifierStr == "else") return tok_else;
922 if (IdentifierStr == "for") return tok_for;
923 if (IdentifierStr == "in") return tok_in;
924 if (IdentifierStr == "binary") return tok_binary;
925 if (IdentifierStr == "unary") return tok_unary;
926 if (IdentifierStr == "var") return tok_var;
927 return tok_identifier;
928 }
929
930 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
931 std::string NumStr;
932 do {
933 NumStr += LastChar;
934 LastChar = getchar();
935 } while (isdigit(LastChar) || LastChar == '.');
936
937 NumVal = strtod(NumStr.c_str(), 0);
938 return tok_number;
939 }
940
941 if (LastChar == '#') {
942 // Comment until end of line.
943 do LastChar = getchar();
944 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
945
946 if (LastChar != EOF)
947 return gettok();
948 }
949
950 // Check for end of file. Don't eat the EOF.
951 if (LastChar == EOF)
952 return tok_eof;
953
954 // Otherwise, just return the character as its ascii value.
955 int ThisChar = LastChar;
956 LastChar = getchar();
957 return ThisChar;
958 }
959
960 //===----------------------------------------------------------------------===//
961 // Abstract Syntax Tree (aka Parse Tree)
962 //===----------------------------------------------------------------------===//
963
964 /// ExprAST - Base class for all expression nodes.
965 class ExprAST {
966 public:
967 virtual ~ExprAST() {}
968 virtual Value *Codegen() = 0;
969 };
970
971 /// NumberExprAST - Expression class for numeric literals like "1.0".
972 class NumberExprAST : public ExprAST {
973 double Val;
974 public:
975 NumberExprAST(double val) : Val(val) {}
976 virtual Value *Codegen();
977 };
978
979 /// VariableExprAST - Expression class for referencing a variable, like "a".
980 class VariableExprAST : public ExprAST {
981 std::string Name;
982 public:
983 VariableExprAST(const std::string &name) : Name(name) {}
984 const std::string &getName() const { return Name; }
985 virtual Value *Codegen();
986 };
987
988 /// UnaryExprAST - Expression class for a unary operator.
989 class UnaryExprAST : public ExprAST {
990 char Opcode;
991 ExprAST *Operand;
992 public:
993 UnaryExprAST(char opcode, ExprAST *operand)
994 : Opcode(opcode), Operand(operand) {}
995 virtual Value *Codegen();
996 };
997
998 /// BinaryExprAST - Expression class for a binary operator.
999 class BinaryExprAST : public ExprAST {
1000 char Op;
1001 ExprAST *LHS, *RHS;
1002 public:
1003 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
1004 : Op(op), LHS(lhs), RHS(rhs) {}
1005 virtual Value *Codegen();
1006 };
1007
1008 /// CallExprAST - Expression class for function calls.
1009 class CallExprAST : public ExprAST {
1010 std::string Callee;
1011 std::vector Args;
1012 public:
1013 CallExprAST(const std::string &callee, std::vector &args)
1014 : Callee(callee), Args(args) {}
1015 virtual Value *Codegen();
1016 };
1017
1018 /// IfExprAST - Expression class for if/then/else.
1019 class IfExprAST : public ExprAST {
1020 ExprAST *Cond, *Then, *Else;
1021 public:
1022 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
1023 : Cond(cond), Then(then), Else(_else) {}
1024 virtual Value *Codegen();
1025 };
1026
1027 /// ForExprAST - Expression class for for/in.
1028 class ForExprAST : public ExprAST {
1029 std::string VarName;
1030 ExprAST *Start, *End, *Step, *Body;
1031 public:
1032 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
1033 ExprAST *step, ExprAST *body)
1034 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
1035 virtual Value *Codegen();
1036 };
1037
1038 /// VarExprAST - Expression class for var/in
1039 class VarExprAST : public ExprAST {
1040 std::vector > VarNames;
1041 ExprAST *Body;
1042 public:
1043 VarExprAST(const std::vector > &varnames,
1044 ExprAST *body)
1045 : VarNames(varnames), Body(body) {}
1046
1047 virtual Value *Codegen();
1048 };
1049
1050 /// PrototypeAST - This class represents the "prototype" for a function,
1051 /// which captures its name, and its argument names (thus implicitly the number
1052 /// of arguments the function takes), as well as if it is an operator.
1053 class PrototypeAST {
1054 std::string Name;
1055 std::vector Args;
1056 bool isOperator;
1057 unsigned Precedence; // Precedence if a binary op.
1058 public:
1059 PrototypeAST(const std::string &name, const std::vector &args,
1060 bool isoperator = false, unsigned prec = 0)
1061 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
1062
1063 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
1064 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
1065
1066 char getOperatorName() const {
1067 assert(isUnaryOp() || isBinaryOp());
1068 return Name[Name.size()-1];
1069 }
1070
1071 unsigned getBinaryPrecedence() const { return Precedence; }
1072
1073 Function *Codegen();
1074
1075 void CreateArgumentAllocas(Function *F);
1076 };
1077
1078 /// FunctionAST - This class represents a function definition itself.
1079 class FunctionAST {
1080 PrototypeAST *Proto;
1081 ExprAST *Body;
1082 public:
1083 FunctionAST(PrototypeAST *proto, ExprAST *body)
1084 : Proto(proto), Body(body) {}
1085
1086 Function *Codegen();
1087 };
1088
1089 //===----------------------------------------------------------------------===//
1090 // Parser
1091 //===----------------------------------------------------------------------===//
1092
1093 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
1094 /// token the parser is looking at. getNextToken reads another token from the
1095 /// lexer and updates CurTok with its results.
1096 static int CurTok;
1097 static int getNextToken() {
1098 return CurTok = gettok();
1099 }
1100
1101 /// BinopPrecedence - This holds the precedence for each binary operator that is
1102 /// defined.
1103 static std::map BinopPrecedence;
1104
1105 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
1106 static int GetTokPrecedence() {
1107 if (!isascii(CurTok))
1108 return -1;
1109
1110 // Make sure it's a declared binop.
1111 int TokPrec = BinopPrecedence[CurTok];
1112 if (TokPrec <= 0) return -1;
1113 return TokPrec;
1114 }
1115
1116 /// Error* - These are little helper functions for error handling.
1117 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
1118 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
1119 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
1120
1121 static ExprAST *ParseExpression();
1122
1123 /// identifierexpr
1124 /// ::= identifier
1125 /// ::= identifier '(' expression* ')'