GenericValue OrcMCJITReplacement::runFunction(Function *F, const std::vector<GenericValue> &ArgValues) { assert(F && "Function *F was null at entry to run()"); void *FPtr = getPointerToFunction(F); assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); FunctionType *FTy = F->getFunctionType(); Type *RetTy = FTy->getReturnType(); assert((FTy->getNumParams() == ArgValues.size() || (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && "Wrong number of arguments passed into function!"); assert(FTy->getNumParams() == ArgValues.size() && "This doesn't support passing arguments through varargs (yet)!"); // Handle some common cases first. These cases correspond to common `main' // prototypes. if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { switch (ArgValues.size()) { case 3: if (FTy->getParamType(0)->isIntegerTy(32) && FTy->getParamType(1)->isPointerTy() && FTy->getParamType(2)->isPointerTy()) { int (*PF)(int, char **, const char **) = (int (*)(int, char **, const char **))(intptr_t)FPtr; // Call the function. GenericValue rv; rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), (char **)GVTOP(ArgValues[1]), (const char **)GVTOP(ArgValues[2]))); return rv; } break; case 2: if (FTy->getParamType(0)->isIntegerTy(32) && FTy->getParamType(1)->isPointerTy()) { int (*PF)(int, char **) = (int (*)(int, char **))(intptr_t)FPtr; // Call the function. GenericValue rv; rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), (char **)GVTOP(ArgValues[1]))); return rv; } break; case 1: if (FTy->getNumParams() == 1 && FTy->getParamType(0)->isIntegerTy(32)) { GenericValue rv; int (*PF)(int) = (int (*)(int))(intptr_t)FPtr; rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); return rv; } break; } } // Handle cases where no arguments are passed first. if (ArgValues.empty()) { GenericValue rv; switch (RetTy->getTypeID()) { default: llvm_unreachable("Unknown return type for function call!"); case Type::IntegerTyID: { unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); if (BitWidth == 1) rv.IntVal = APInt(BitWidth, ((bool (*)())(intptr_t)FPtr)()); else if (BitWidth <= 8) rv.IntVal = APInt(BitWidth, ((char (*)())(intptr_t)FPtr)()); else if (BitWidth <= 16) rv.IntVal = APInt(BitWidth, ((short (*)())(intptr_t)FPtr)()); else if (BitWidth <= 32) rv.IntVal = APInt(BitWidth, ((int (*)())(intptr_t)FPtr)()); else if (BitWidth <= 64) rv.IntVal = APInt(BitWidth, ((int64_t (*)())(intptr_t)FPtr)()); else llvm_unreachable("Integer types > 64 bits not supported"); return rv; } case Type::VoidTyID: rv.IntVal = APInt(32, ((int (*)())(intptr_t)FPtr)()); return rv; case Type::FloatTyID: rv.FloatVal = ((float (*)())(intptr_t)FPtr)(); return rv; case Type::DoubleTyID: rv.DoubleVal = ((double (*)())(intptr_t)FPtr)(); return rv; case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: llvm_unreachable("long double not supported yet"); case Type::PointerTyID: return PTOGV(((void *(*)())(intptr_t)FPtr)()); } } llvm_unreachable("Full-featured argument passing not supported yet!"); }
bool JITRotation::initializeWithFile(const char* filename) { // note: string literals in rotation scripts are actually compiled as their FNV1A hash instead const char header[] = "class Actor;\n" "uint64 AuraCount(const Actor* actor, uint64 identifier, const Actor* source);\n" "double GlobalCooldownRemaining(const Actor* actor);\n" "double CooldownRemaining(const Actor* actor, uint64 identifier);\n" "double AuraTimeRemaining(const Actor* actor, uint64 identifier, const Actor* source);\n" "Actor* Pet(Actor* owner);\n" "uint64 TP(const Actor* actor);\n" "uint64 MP(const Actor* actor);\n" "uint64 MaximumMP(const Actor* actor);\n" "double GlobalCooldown(const Actor* actor);\n" "double Time(const Actor* actor);\n" "void RemoveAura(Actor* actor, uint64 identifier, const Actor* source);\n" "uint8 IsReady(const Actor* actor, uint64 identifier);\n" "void Command(Actor* actor, uint64 identifier);\n" "void StopAutoAttack(Actor* actor);\n" "__end __hidden const uint64 NextAction(Actor* self, const Actor* target) {\n" ; const char footer[] = "\n}"; FILE* f = fopen(filename, "r"); if (!f) { printf("Unable to open file %s\n", filename); return false; } fseek(f, 0, SEEK_END); auto fsize = ftell(f); rewind(f); auto sourceLength = sizeof(header) + fsize + sizeof(footer); auto source = (char*)malloc(sourceLength); if (!source) { printf("Unable to allocate memory for rotation source\n"); fclose(f); return false; } memcpy(source, header, sizeof(header)); if (fread(source + sizeof(header), fsize, 1, f) != 1) { printf("Unable to read file %s\n", filename); fclose(f); return false; } memcpy(source + sizeof(header) + fsize, footer, sizeof(footer)); fclose(f); // PREPROCESS Preprocessor pp; if (!pp.process_file(filename, source, sourceLength)) { printf("Couldn't preprocess file.\n"); free(source); return false; } free(source); // PARSE Parser p; ASTSequence* ast = p.generate_ast(pp.tokens()); if (p.errors().size() > 0) { delete ast; for (const ParseError& e : p.errors()) { printf("Error: %s\n", e.message.c_str()); e.token->print_pointer(); } return false; } if (!ast) { printf("Couldn't generate AST.\n"); return false; } // GENERATE CODE LLVMCodeGenerator cg; if (!cg.build_ir(ast)) { printf("Couldn't build IR.\n"); delete ast; return false; } delete ast; // COMPILE LLVMInitializeNativeTarget(); auto module = cg.release_module(); std::string error; auto engine = llvm::EngineBuilder(module).setErrorStr(&error).setEngineKind(llvm::EngineKind::JIT).create(); if (!engine) { printf("Couldn't build execution engine: %s\n", error.c_str()); return false; } engine->addGlobalMapping(module->getFunction("^AuraCount"), (void*)&JITRotation::ActorAuraCount); engine->addGlobalMapping(module->getFunction("^GlobalCooldownRemaining"), (void*)&JITRotation::ActorGlobalCooldownRemaining); engine->addGlobalMapping(module->getFunction("^CooldownRemaining"), (void*)&JITRotation::ActorCooldownRemaining); engine->addGlobalMapping(module->getFunction("^AuraTimeRemaining"), (void*)&JITRotation::ActorAuraTimeRemaining); engine->addGlobalMapping(module->getFunction("^Pet"), (void*)&JITRotation::ActorPet); engine->addGlobalMapping(module->getFunction("^TP"), (void*)&JITRotation::ActorTP); engine->addGlobalMapping(module->getFunction("^MP"), (void*)&JITRotation::ActorMP); engine->addGlobalMapping(module->getFunction("^MaximumMP"), (void*)&JITRotation::ActorMaximumMP); engine->addGlobalMapping(module->getFunction("^GlobalCooldown"), (void*)&JITRotation::ActorGlobalCooldown); engine->addGlobalMapping(module->getFunction("^Time"), (void*)&JITRotation::ActorTime); engine->addGlobalMapping(module->getFunction("^RemoveAura"), (void*)&JITRotation::ActorRemoveAura); engine->addGlobalMapping(module->getFunction("^IsReady"), (void*)&JITRotation::ActionIsReady); engine->addGlobalMapping(module->getFunction("^Command"), (void*)&JITRotation::ActorCommand); engine->addGlobalMapping(module->getFunction("^StopAutoAttack"), (void*)&JITRotation::ActorStopAutoAttack); _jitNextAction = decltype(_jitNextAction)((std::intptr_t)engine->getPointerToFunction(module->getFunction("^NextAction"))); return _jitNextAction; }