summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/llvmtranslate/tasks.cc
blob: 09f865c8860d6abb03c4ec8b0c1f235bf8fc3217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 ** \file llvmtranslate/tasks.cc
 ** \brief LLVM Translate tasks.
 */

#include <memory>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"

#include <llvm/Config/llvm-config.h> // LLVM_VERSION_*
#include <llvm/IR/Module.h>
#include <llvm/IR/PassManager.h>
#include <llvm/IRPrinter/IRPrintingPasses.h>
#include <llvm/Linker/Linker.h>
#include <llvm/Support/raw_ostream.h> // llvm::outs()

#pragma GCC diagnostic pop

#include <ast/tasks.hh>
#include <llvmtranslate/fwd.hh>
#include <llvmtranslate/libllvmtranslate.hh>
#define DEFINE_TASKS 1
#include <llvmtranslate/tasks.hh>
#undef DEFINE_TASKS

namespace llvmtranslate::tasks
{
  std::pair<std::unique_ptr<llvm::LLVMContext>, std::unique_ptr<llvm::Module>>
    module = {nullptr, nullptr};

  /// Translate the AST to LLVM IR.
  void llvm_compute() { module = translate(*ast::tasks::the_program); }

  /// Display the LLVM IR.
  void llvm_display()
  {
    // If the runtime has to be displayed, get the runtime module,
    // link it with the program module and print it.
    if (llvm_runtime_display_p)
      {
        auto runtime = runtime_get(*module.first);
#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 7
        auto link =
          llvm::Linker::LinkModules(module.second.get(), runtime.get());
#else
        auto link =
          llvm::Linker::linkModules(*module.second, std::move(runtime));
#endif
        (void)link;
        postcondition(!link); // Returns true on error
      }

    auto& out = llvm::outs();
    llvm::PrintModulePass printer{out};
#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 8
    printer.run(*module.second);
#else
    llvm::ModuleAnalysisManager dummy_mam;
    printer.run(*module.second, dummy_mam);
#endif
  }

} // namespace llvmtranslate::tasks