Example #1
0
int main()
{
    InitializeNativeTarget();
    llvm_start_multithreaded();
    LLVMContext context;
    string error;
    OwningPtr<llvm::MemoryBuffer> fileBuf;
    MemoryBuffer::getFile("hw.bc", fileBuf);
    ErrorOr<Module*> m = parseBitcodeFile(fileBuf.get(), context);
    ExecutionEngine *ee = ExecutionEngine::create(m.get());

    Function* func = ee->FindFunctionNamed("main");
    std::cout << "hop " << m.get()  << " ee " << ee << " f " << func << std::endl;

    typedef void (*PFN)();
    PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
    pfn();

    Function* f = ee->FindFunctionNamed("fib");
    std::cout << "big " << f << std::endl;

    //    typedef std::function<int(int)> fibType;
    typedef int (*fibType)(int);
    fibType ffib = reinterpret_cast<fibType>(ee->getPointerToFunction(f));
    std::cout << "fib " << ffib(7) << std::endl;



    delete ee;
}
int main()
{
   TestClass tc;
   tc.setHealth(100);
   tc.printHealth();
   
   InitializeNativeTarget();
   LLVMContext context;
   string error;

   auto mb = MemoryBuffer::getFile("bitcode/damage.bc");
   if (!mb) {
      cout << "ERROR: Failed to getFile" << endl;
      return 0;
   }

   auto m = parseBitcodeFile(mb->get(), context);
   if(!m) {
      cout << "ERROR: Failed to load script file." << endl;
      return 0;
   }
   
   ExecutionEngine *ee = ExecutionEngine::create(m.get());

   // NOTE: Function names are mangled by the compiler.
   Function* init_func = ee->FindFunctionNamed("_Z9initilizev");
   if(!init_func) {
      cout << "ERROR: Failed to find 'initilize' function." << endl;
      return 0;
   }

   Function* attack_func = ee->FindFunctionNamed("_Z6attackP9TestClass");
   if(!attack_func) {
      cout << "ERROR: Failed to find 'attack' function." << endl;
      return 0;
   }

   typedef void (*init_pfn)();
   init_pfn initilize = reinterpret_cast<init_pfn>(ee->getPointerToFunction(init_func));
   if(!initilize) {
      cout << "ERROR: Failed to cast 'initilize' function." << endl;
      return 0;
   }

   initilize();
   cout << "Running attacking script..." << endl;

   typedef void (*attack_pfn)(TestClass*);
   attack_pfn attack = reinterpret_cast<attack_pfn>(ee->getPointerToFunction(attack_func));
   if(!attack) {
      cout << "ERROR: Failed to cast 'attack' function." << endl;
      return 0;
   }

   attack(&tc);
   
   tc.printHealth();
   delete ee;
}
int main(int argc, char **argv)
{
  using namespace llvm;

  std::cout << "hello_world_ir: " << std::endl;
  std::cout << std::endl;
  std::cout << hello_world_ir << std::endl;
  std::cout << std::endl;

  InitializeNativeTarget();

  LLVMContext context;
  SMDiagnostic error;

  Module *m = ParseIR(MemoryBuffer::getMemBuffer(StringRef(hello_world_ir)), error, context);
  if(!m)
  {
    error.print(argv[0], errs());
  }

  ExecutionEngine *ee = ExecutionEngine::create(m);

  Function *func = ee->FindFunctionNamed("hello_world");

  typedef void (*fcn_ptr)();
  fcn_ptr hello_world = reinterpret_cast<fcn_ptr>(ee->getPointerToFunction(func));
  hello_world();
  delete ee;

  return 0;
}
Example #4
0
extern "C" void*
LLVMRustExecuteJIT(void* mem,
                   LLVMPassManagerRef PMR,
                   LLVMModuleRef M,
                   CodeGenOpt::Level OptLevel,
                   bool EnableSegmentedStacks) {

  InitializeNativeTarget();
  InitializeNativeTargetAsmPrinter();
  InitializeNativeTargetAsmParser();

  std::string Err;
  TargetOptions Options;
  Options.JITExceptionHandling = true;
  Options.JITEmitDebugInfo = true;
  Options.NoFramePointerElim = true;
  Options.EnableSegmentedStacks = EnableSegmentedStacks;
  PassManager *PM = unwrap<PassManager>(PMR);
  RustMCJITMemoryManager* MM = (RustMCJITMemoryManager*) mem;

  assert(MM);

  PM->add(createBasicAliasAnalysisPass());
  PM->add(createInstructionCombiningPass());
  PM->add(createReassociatePass());
  PM->add(createGVNPass());
  PM->add(createCFGSimplificationPass());
  PM->add(createFunctionInliningPass());
  PM->add(createPromoteMemoryToRegisterPass());
  PM->run(*unwrap(M));

  ExecutionEngine* EE = EngineBuilder(unwrap(M))
    .setErrorStr(&Err)
    .setTargetOptions(Options)
    .setJITMemoryManager(MM)
    .setOptLevel(OptLevel)
    .setUseMCJIT(true)
    .setAllocateGVsWithCode(false)
    .create();

  if(!EE || Err != "") {
    LLVMRustError = Err.c_str();
    return 0;
  }

  MM->invalidateInstructionCache();
  Function* func = EE->FindFunctionNamed("_rust_main");

  if(!func || Err != "") {
    LLVMRustError = Err.c_str();
    return 0;
  }

  void* entry = EE->getPointerToFunction(func);
  assert(entry);

  return entry;
}
Example #5
0
	void LoadPlugin(const std::string& path, const std::string& name, MetaScriptRunner* msr) {
		
		SMDiagnostic error;
		std::unique_ptr<Module> Owner = getLazyIRFileModule(path, error, context);
		if(Owner == nullptr) {
			cout << "Load Error: " << path << endl;
			Owner->dump();
			return;
		}

		initEE(std::move(Owner));

		string func_name = name + "_elite_plugin_init";
		Function* func = EE->FindFunctionNamed(func_name.c_str());

		std::vector<GenericValue> args;
		args.push_back(GenericValue(msr->getCodeGenContext()));
		GenericValue gv = EE->runFunction(func, args);
	}
Example #6
0
int main(int argc, char **argv)
{
  using namespace llvm;

  InitializeNativeTarget();

  LLVMContext context;
  SMDiagnostic error;

  Module *generic_module = ParseIR(MemoryBuffer::getMemBuffer(StringRef(mul_add_ir)), error, context);
  if(!generic_module)
  {
    std::cerr << "Error after first ParseIR" << std::endl;
    error.print(argv[0], errs());
    std::exit(-1);
  }

  // create a new module to hold the i32 specialization of generic_module
  Module module_i32(StringRef("module_i32"), context);

  // maps generic forms to their specializations
  ValueToValueMapTy vmap;

  specialize_declarations(context, generic_module, vmap, &module_i32);

  specialize_definitions(context, generic_module, vmap, &module_i32);

  Module *user_module = ParseIR(MemoryBuffer::getMemBuffer(StringRef(int_math_ir)), error, context);
  if(!user_module)
  {
    std::cerr << "Error after second ParseIR" << std::endl;
    error.print(argv[0], errs());
    std::exit(-1);
  }

  Linker ld(StringRef(argv[0]), StringRef("mul_add_prog"), context);

  std::string error_msg;
  if(ld.LinkInModule(&module_i32, &error_msg))
  {
    std::cerr << "Error after linkInModule(m1)" << std::endl;
    std::cerr << error_msg << std::endl;
    std::exit(-1);
  }

  if(ld.LinkInModule(user_module, &error_msg))
  {
    std::cerr << "Error after linkInModule(user_module)" << std::endl;
    std::cerr << error_msg << std::endl;
    std::exit(-1);
  }

  Module *composite = ld.releaseModule();

  composite->dump();

  ExecutionEngine *ee = ExecutionEngine::create(composite);

  Function *func = ee->FindFunctionNamed("mul_add");
  if(!func)
  {
    std::cerr << "Couldn't find mul_add" << std::endl;
    std::exit(-1);
  }

  std::cout << std::endl;

  typedef int (*fcn_ptr)(int,int,int);
  fcn_ptr mul_add = reinterpret_cast<fcn_ptr>(ee->getPointerToFunction(func));
  std::cout << "mul_add(1,2,3): " << mul_add(1,2,3) << std::endl;
  delete ee;

  return 0;
}