1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the abstract interface that implements execution support
13 //===----------------------------------------------------------------------===//
15 #ifndef EXECUTION_ENGINE_H
16 #define EXECUTION_ENGINE_H
33 class IntrinsicLowering;
35 class ExecutionEngine {
39 // GlobalAddress - A mapping between LLVM global values and their actualized
41 std::map<const GlobalValue*, void *> GlobalAddress;
45 void setTargetData(const TargetData &td) {
50 ExecutionEngine(ModuleProvider *P);
51 ExecutionEngine(Module *M);
52 virtual ~ExecutionEngine();
54 Module &getModule() const { return CurMod; }
55 const TargetData &getTargetData() const { return *TD; }
57 /// create - This is the factory method for creating an execution engine which
58 /// is appropriate for the current machine. If specified, the
59 /// IntrinsicLowering implementation should be allocated on the heap.
60 static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter,
61 IntrinsicLowering *IL = 0);
63 /// runFunction - Execute the specified function with the specified arguments,
64 /// and return the result.
66 virtual GenericValue runFunction(Function *F,
67 const std::vector<GenericValue> &ArgValues) = 0;
69 /// runFunctionAsMain - This is a helper function which wraps runFunction to
70 /// handle the common task of starting up main with the specified argc, argv,
71 /// and envp parameters.
72 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
73 const char * const * envp);
76 void addGlobalMapping(const GlobalValue *GV, void *Addr) {
77 void *&CurVal = GlobalAddress[GV];
78 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
82 /// getPointerToGlobalIfAvailable - This returns the address of the specified
83 /// global value if it is available, otherwise it returns null.
85 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) {
86 std::map<const GlobalValue*, void*>::iterator I = GlobalAddress.find(GV);
87 return I != GlobalAddress.end() ? I->second : 0;
90 /// getPointerToGlobal - This returns the address of the specified global
91 /// value. This may involve code generation if it's a function.
93 void *getPointerToGlobal(const GlobalValue *GV);
95 /// getPointerToFunction - The different EE's represent function bodies in
96 /// different ways. They should each implement this to say what a function
97 /// pointer should look like.
99 virtual void *getPointerToFunction(Function *F) = 0;
101 /// getPointerToFunctionOrStub - If the specified function has been
102 /// code-gen'd, return a pointer to the function. If not, compile it, or use
103 /// a stub to implement lazy compilation if available.
105 virtual void *getPointerToFunctionOrStub(Function *F) {
106 // Default implementation, just codegen the function.
107 return getPointerToFunction(F);
110 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty);
111 void InitializeMemory(const Constant *Init, void *Addr);
113 /// recompileAndRelinkFunction - This method is used to force a function
114 /// which has already been compiled to be compiled again, possibly
115 /// after it has been modified. Then the entry to the old copy is overwritten
116 /// with a branch to the new copy. If there was no old copy, this acts
117 /// just like VM::getPointerToFunction().
119 virtual void *recompileAndRelinkFunction(Function *F) = 0;
121 /// getOrEmitGlobalVariable - Return the address of the specified global
122 /// variable, possibly emitting it to memory if needed. This is used by the
124 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
125 return getPointerToGlobal((GlobalValue*)GV);
131 // EmitGlobalVariable - This method emits the specified global variable to the
132 // address specified in GlobalAddresses, or allocates new memory if it's not
133 // already in the map.
134 void EmitGlobalVariable(const GlobalVariable *GV);
136 GenericValue getConstantValue(const Constant *C);
137 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty);
140 } // End llvm namespace