Main.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Standard includes
  2. #include <cstdio>
  3. //LLVM includes
  4. #include "llvm/Support/TargetSelect.h"
  5. #include "KaleidoscopeJIT.h"
  6. #include "llvm/IR/DIBuilder.h"
  7. // Local includes
  8. #include "JIT.h"
  9. #include "Ast.h"
  10. #include "Lexer.h"
  11. #include "Parser.h"
  12. #include "Debug.h"
  13. namespace helper {
  14. // Cloning make_unique here until it's standard in C++14.
  15. // Using a namespace to avoid conflicting with MSVC's std::make_unique (which
  16. // ADL can sometimes find in unqualified calls).
  17. template <class T, class... Args>
  18. static
  19. typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
  20. make_unique(Args &&... args) {
  21. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  22. }
  23. }
  24. //===----------------------------------------------------------------------===//
  25. // "Library" functions that can be "extern'd" from user code.
  26. //===----------------------------------------------------------------------===//
  27. //===----------------------------------------------------------------------===//
  28. // Main driver code.
  29. //===----------------------------------------------------------------------===//
  30. int main() {
  31. InitializeNativeTarget();
  32. InitializeNativeTargetAsmPrinter();
  33. InitializeNativeTargetAsmParser();
  34. jit::JITObjects::TheJIT = llvm::make_unique<llvm::orc::KaleidoscopeJIT>();
  35. jit::InitializeModule();
  36. // Prime the first token.
  37. parser::getNextToken();
  38. // Add the current debug info version into the module.
  39. AstObjects::TheModule->addModuleFlag(Module::Warning, "Debug Info Version",
  40. DEBUG_METADATA_VERSION);
  41. // Darwin only supports dwarf2.
  42. if (Triple(sys::getProcessTriple()).isOSDarwin()) {
  43. AstObjects::TheModule->addModuleFlag(llvm::Module::Warning, "Dwarf Version",
  44. 2);
  45. }
  46. // Construct the DIBuilder, we do this here because we need the module.
  47. debugger::DebugObjects::DBuilder =
  48. llvm::make_unique<DIBuilder>(*AstObjects::TheModule);
  49. // Create the compile unit for the module.
  50. // Currently down as "fib.ks" as a filename since we're redirecting stdin
  51. // but we'd like actual source locations.
  52. debugger::KSDbgInfo.TheCU = debugger::DebugObjects::DBuilder->createCompileUnit(
  53. dwarf::DW_LANG_C, "fib.ks", ".", "Kaleidoscope Compiler", 0, "", 0);
  54. // Run the main "interpreter loop" now.
  55. parser::MainLoop();
  56. // Finalize the debug info.
  57. debugger::DebugObjects::DBuilder->finalize();
  58. AstObjects::TheModule->dump();
  59. return 0;
  60. }
  61. }