1 //===-------- OrcMCJITReplacement.cpp - Orc-based MCJIT replacement -------===//
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 #include "OrcMCJITReplacement.h"
11 #include "llvm/ExecutionEngine/GenericValue.h"
15 static struct RegisterJIT {
16 RegisterJIT() { llvm::OrcMCJITReplacement::Register(); }
19 extern "C" void LLVMLinkInOrcMCJITReplacement() {}
25 OrcMCJITReplacement::runFunction(Function *F,
26 const std::vector<GenericValue> &ArgValues) {
27 assert(F && "Function *F was null at entry to run()");
29 void *FPtr = getPointerToFunction(F);
30 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
31 FunctionType *FTy = F->getFunctionType();
32 Type *RetTy = FTy->getReturnType();
34 assert((FTy->getNumParams() == ArgValues.size() ||
35 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
36 "Wrong number of arguments passed into function!");
37 assert(FTy->getNumParams() == ArgValues.size() &&
38 "This doesn't support passing arguments through varargs (yet)!");
40 // Handle some common cases first. These cases correspond to common `main'
42 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
43 switch (ArgValues.size()) {
45 if (FTy->getParamType(0)->isIntegerTy(32) &&
46 FTy->getParamType(1)->isPointerTy() &&
47 FTy->getParamType(2)->isPointerTy()) {
48 int (*PF)(int, char **, const char **) =
49 (int (*)(int, char **, const char **))(intptr_t)FPtr;
53 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
54 (char **)GVTOP(ArgValues[1]),
55 (const char **)GVTOP(ArgValues[2])));
60 if (FTy->getParamType(0)->isIntegerTy(32) &&
61 FTy->getParamType(1)->isPointerTy()) {
62 int (*PF)(int, char **) = (int (*)(int, char **))(intptr_t)FPtr;
66 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
67 (char **)GVTOP(ArgValues[1])));
72 if (FTy->getNumParams() == 1 && FTy->getParamType(0)->isIntegerTy(32)) {
74 int (*PF)(int) = (int (*)(int))(intptr_t)FPtr;
75 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
82 // Handle cases where no arguments are passed first.
83 if (ArgValues.empty()) {
85 switch (RetTy->getTypeID()) {
87 llvm_unreachable("Unknown return type for function call!");
88 case Type::IntegerTyID: {
89 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
91 rv.IntVal = APInt(BitWidth, ((bool (*)())(intptr_t)FPtr)());
92 else if (BitWidth <= 8)
93 rv.IntVal = APInt(BitWidth, ((char (*)())(intptr_t)FPtr)());
94 else if (BitWidth <= 16)
95 rv.IntVal = APInt(BitWidth, ((short (*)())(intptr_t)FPtr)());
96 else if (BitWidth <= 32)
97 rv.IntVal = APInt(BitWidth, ((int (*)())(intptr_t)FPtr)());
98 else if (BitWidth <= 64)
99 rv.IntVal = APInt(BitWidth, ((int64_t (*)())(intptr_t)FPtr)());
101 llvm_unreachable("Integer types > 64 bits not supported");
105 rv.IntVal = APInt(32, ((int (*)())(intptr_t)FPtr)());
107 case Type::FloatTyID:
108 rv.FloatVal = ((float (*)())(intptr_t)FPtr)();
110 case Type::DoubleTyID:
111 rv.DoubleVal = ((double (*)())(intptr_t)FPtr)();
113 case Type::X86_FP80TyID:
114 case Type::FP128TyID:
115 case Type::PPC_FP128TyID:
116 llvm_unreachable("long double not supported yet");
117 case Type::PointerTyID:
118 return PTOGV(((void *(*)())(intptr_t)FPtr)());
122 llvm_unreachable("Full-featured argument passing not supported yet!");