1 //===-- CompileUtils.h - Utilities for compiling IR in the JIT --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains utilities for compiling IR to object files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
17 #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Target/TargetMachine.h"
25 /// @brief Simple compile functor: Takes a single IR module and returns an
27 class SimpleCompiler {
29 /// @brief Construct a simple compile functor with the given target.
30 SimpleCompiler(TargetMachine &TM) : TM(TM) {}
32 /// @brief Compile a Module to an ObjectFile.
33 object::OwningBinary<object::ObjectFile> operator()(Module &M) const {
34 SmallVector<char, 0> ObjBufferSV;
35 raw_svector_ostream ObjStream(ObjBufferSV);
39 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
40 llvm_unreachable("Target does not support MC emission.");
43 std::unique_ptr<MemoryBuffer> ObjBuffer(
44 new ObjectMemoryBuffer(std::move(ObjBufferSV)));
45 ErrorOr<std::unique_ptr<object::ObjectFile>> Obj =
46 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
47 // TODO: Actually report errors helpfully.
48 typedef object::OwningBinary<object::ObjectFile> OwningObj;
50 return OwningObj(std::move(*Obj), std::move(ObjBuffer));
51 return OwningObj(nullptr, nullptr);
59 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H