LLVM  3.7.0
CompileUtils.h
Go to the documentation of this file.
1 //===-- CompileUtils.h - Utilities for compiling IR in the JIT --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Contains utilities for compiling IR to object files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
16 
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/Object/ObjectFile.h"
22 
23 namespace llvm {
24 namespace orc {
25 
26 /// @brief Simple compile functor: Takes a single IR module and returns an
27 /// ObjectFile.
29 public:
30  /// @brief Construct a simple compile functor with the given target.
31  SimpleCompiler(TargetMachine &TM) : TM(TM) {}
32 
33  /// @brief Compile a Module to an ObjectFile.
35  SmallVector<char, 0> ObjBufferSV;
36  raw_svector_ostream ObjStream(ObjBufferSV);
37 
39  MCContext *Ctx;
40  if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
41  llvm_unreachable("Target does not support MC emission.");
42  PM.run(M);
43  ObjStream.flush();
44  std::unique_ptr<MemoryBuffer> ObjBuffer(
45  new ObjectMemoryBuffer(std::move(ObjBufferSV)));
47  object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
48  // TODO: Actually report errors helpfully.
50  if (Obj)
51  return OwningObj(std::move(*Obj), std::move(ObjBuffer));
52  return OwningObj(nullptr, nullptr);
53  }
54 
55 private:
56  TargetMachine &TM;
57 };
58 
59 } // End namespace orc.
60 } // End namespace llvm.
61 
62 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
Represents either an error or a value T.
Definition: ErrorOr.h:82
SimpleCompiler(TargetMachine &TM)
Construct a simple compile functor with the given target.
Definition: CompileUtils.h:31
virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&, raw_pwrite_stream &, bool=true)
Add passes to the specified pass manager to get machine code emitted with the MCJIT.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:28
Context object for machine code objects.
Definition: MCContext.h:48
PassManager manages ModulePassManagers.
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
SmallVector-backed MemoryBuffer instance.
static ErrorOr< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:102
Primary interface to the complete machine description for the target machine.
object::OwningBinary< object::ObjectFile > operator()(Module &M) const
Compile a Module to an ObjectFile.
Definition: CompileUtils.h:34