summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/llvmtranslate/libllvmtranslate.cc
blob: b86b03add1eea1afc550a8a0f569aa325f5ecf38 (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
/**
 ** \file llvmtranslate/libllvmtranslate.hh
 ** \brief Public llvmtranslate module interface implementation.
 **/

#include <ast/default-visitor.hh>
#include <ast/non-object-visitor.hh>
#include <common.hh> // program_name

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

#include <llvm/AsmParser/Parser.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/SourceMgr.h> // llvm::SMDiagnostic
#include <llvmtranslate/escapes-collector.hh>
#include <llvmtranslate/fwd.hh>
#include <llvmtranslate/libllvmtranslate.hh>
#include <llvmtranslate/translator.hh>

#pragma GCC diagnostic pop

namespace llvmtranslate
{
  std::pair<std::unique_ptr<llvm::LLVMContext>, std::unique_ptr<llvm::Module>>
  translate(const ast::Ast& the_program)
  {
    auto ctx = std::make_unique<llvm::LLVMContext>();
    auto module = std::make_unique<llvm::Module>(program_name, *ctx);

    Translator translate{*module, collect_escapes(the_program)};
    translate(the_program);

    llvm::verifyModule(*module);

    return {std::move(ctx), std::move(module)};
  }

  std::unique_ptr<llvm::Module> runtime_get(llvm::LLVMContext& ctx)
  {
    llvm::SMDiagnostic diag;
    return llvm::parseAssemblyString(runtime_string(), diag, ctx);
  }

} // namespace llvmtranslate