Browse Source

fixes and added self-contained tutorial build system

Andrea Gussoni 8 years ago
parent
commit
fa0db362a9
4 changed files with 30 additions and 6 deletions
  1. 3 5
      source/Ast.cpp
  2. 1 1
      source/Ast.h
  3. 23 0
      source/Chapters/CMakeLists.txt
  4. 3 0
      source/Chapters/Chapter3.cpp

+ 3 - 5
source/Ast.cpp

@@ -89,8 +89,6 @@ Function *PrototypeAST::codegen() {
   return F;
 }
 
-const std::string &PrototypeAST::getName() const { return Name; }
-
 Function *FunctionAST::codegen() {
 
   // First, check for an existing function from a previous 'extern' declaration.
@@ -98,13 +96,13 @@ Function *FunctionAST::codegen() {
       AstObjects::TheModule->getFunction(Proto->getName());
 
   if (!TheFunction)
-  TheFunction = Proto->codegen();
+    TheFunction = Proto->codegen();
 
   if (!TheFunction)
-  return nullptr;
+    return nullptr;
 
   if (!TheFunction->empty())
-  return (Function*)ErrorV("Function cannot be redefined.");
+    return (Function*)ErrorV("Function cannot be redefined.");
 
   // Create a new basic block to start insertion into.
   BasicBlock *BB = BasicBlock::Create(getGlobalContext(),

+ 1 - 1
source/Ast.h

@@ -81,7 +81,7 @@ public:
   PrototypeAST(const std::string &Name,
       std::vector<std::string> Args) : Name(Name), Args(std::move(Args)) {}
   Function *codegen();
-  const std::string &getName() const;
+  const std::string &getName() const { return Name; }
 };
 
 /// FunctionAST - This class represents a function definition itself.

+ 23 - 0
source/Chapters/CMakeLists.txt

@@ -0,0 +1,23 @@
+cmake_minimum_required(VERSION 3.5)
+project(llvm_tutorial CXX)
+
+find_package(LLVM REQUIRED CONFIG)
+
+
+message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
+message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
+
+# Options to compile correctly the llvm tutorial
+add_compile_options(-std=c++14 -g -O0 -fno-rtti)
+
+include_directories(${LLVM_INCLUDE_DIRS})
+add_definitions(${LLVM_DEFINITIONS})
+
+add_executable(kaleidoscope Chapter3.cpp)
+
+# Find the libraries that correspond to the LLVM components
+# that we wish to use
+llvm_map_components_to_libnames(llvm_libs core support native mcjit)
+
+# Link against LLVM libraries
+target_link_libraries(kaleidoscope ${llvm_libs})

+ 3 - 0
source/Chapters/Chapter3.cpp

@@ -472,6 +472,9 @@ Function *FunctionAST::codegen() {
   if (!TheFunction)
     return nullptr;
 
+  if (!TheFunction->empty())
+    return (Function*)ErrorV("Function cannot be redefined.");  
+
   // Create a new basic block to start insertion into.
   BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
   Builder.SetInsertPoint(BB);