LLVM API Documentation

TargetJITInfo.h

Go to the documentation of this file.
00001 //===- Target/TargetJITInfo.h - Target Information for JIT ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file exposes an abstract interface used by the Just-In-Time code
00011 // generator to perform target-specific activities, such as emitting stubs.  If
00012 // a TargetMachine supports JIT code generation, it should provide one of these
00013 // objects through the getJITInfo() method.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_TARGET_TARGETJITINFO_H
00018 #define LLVM_TARGET_TARGETJITINFO_H
00019 
00020 #include <cassert>
00021 #include <vector>
00022 
00023 namespace llvm {
00024   class Function;
00025   class FunctionPassManager;
00026   class MachineBasicBlock;
00027   class MachineCodeEmitter;
00028   class MachineRelocation;
00029 
00030   /// TargetJITInfo - Target specific information required by the Just-In-Time
00031   /// code generator.
00032   class TargetJITInfo {
00033   public:
00034     virtual ~TargetJITInfo() {}
00035 
00036     /// addPassesToJITCompile - Add passes to the specified pass manager to
00037     /// implement a fast code generator for this target.
00038     ///
00039     virtual void addPassesToJITCompile(FunctionPassManager &PM) = 0;
00040 
00041     /// replaceMachineCodeForFunction - Make it so that calling the function
00042     /// whose machine code is at OLD turns into a call to NEW, perhaps by
00043     /// overwriting OLD with a branch to NEW.  This is used for self-modifying
00044     /// code.
00045     ///
00046     virtual void replaceMachineCodeForFunction(void *Old, void *New) = 0;
00047 
00048     /// emitFunctionStub - Use the specified MachineCodeEmitter object to emit a
00049     /// small native function that simply calls the function at the specified
00050     /// address.  Return the address of the resultant function.
00051     virtual void *emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
00052       assert(0 && "This target doesn't implement emitFunctionStub!");
00053       return 0;
00054     }
00055 
00056     /// LazyResolverFn - This typedef is used to represent the function that
00057     /// unresolved call points should invoke.  This is a target specific
00058     /// function that knows how to walk the stack and find out which stub the
00059     /// call is coming from.
00060     typedef void (*LazyResolverFn)();
00061 
00062     /// JITCompilerFn - This typedef is used to represent the JIT function that
00063     /// lazily compiles the function corresponding to a stub.  The JIT keeps
00064     /// track of the mapping between stubs and LLVM Functions, the target
00065     /// provides the ability to figure out the address of a stub that is called
00066     /// by the LazyResolverFn.
00067     typedef void* (*JITCompilerFn)(void *);
00068 
00069     /// getLazyResolverFunction - This method is used to initialize the JIT,
00070     /// giving the target the function that should be used to compile a
00071     /// function, and giving the JIT the target function used to do the lazy
00072     /// resolving.
00073     virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn) {
00074       assert(0 && "Not implemented for this target!");
00075       return 0;
00076     }
00077 
00078     /// relocate - Before the JIT can run a block of code that has been emitted,
00079     /// it must rewrite the code to contain the actual addresses of any
00080     /// referenced global symbols.
00081     virtual void relocate(void *Function, MachineRelocation *MR,
00082                           unsigned NumRelocs, unsigned char* GOTBase) {
00083       assert(NumRelocs == 0 && "This target does not have relocations!");
00084     }
00085 
00086     /// resolveBBRefs - Resolve branches to BasicBlocks for the JIT emitted
00087     /// function.
00088     virtual void resolveBBRefs(MachineCodeEmitter &MCE) {}
00089 
00090     /// synchronizeICache - On some targets, the JIT emitted code must be
00091     /// explicitly refetched to ensure correct execution.
00092     virtual void synchronizeICache(const void *Addr, size_t len) {}
00093 
00094     /// addBBRef - Add a BasicBlock reference to be resolved after the function
00095     /// is emitted.
00096     void addBBRef(MachineBasicBlock *BB, intptr_t PC) {
00097       BBRefs.push_back(std::make_pair(BB, PC));
00098     }
00099 
00100     /// needsGOT - Allows a target to specify that it would like the
00101     // JIT to manage a GOT for it.
00102     bool needsGOT() const { return useGOT; }
00103 
00104   protected:
00105     bool useGOT;
00106 
00107     // Tracks which instruction references which BasicBlock
00108     std::vector<std::pair<MachineBasicBlock*, intptr_t> > BBRefs;
00109     
00110   };
00111 } // End llvm namespace
00112 
00113 #endif