Execute module and get result of value

48 Views Asked by At

I try make tool for creating bitcode, execute and check result so i currently have that code but i get Access violation reading location 0x0000000000000000. in the runFunction what i need fix there for normal executing code?

void Execute()
{
    LLVMInitializeNativeTarget();
    LLVMInitializeNativeAsmPrinter();

    LLVMContext context;
    std::unique_ptr<Module> mods = std::make_unique<Module>("fnc", context);
    Module* mod = mods.get();

    FunctionType* add_func_type = FunctionType::get(Type::getInt32Ty(context), { Type::getInt32Ty(context), Type::getInt32Ty(context) }, false);
    Function* func = Function::Create(add_func_type, Function::ExternalLinkage, "add", mod);

    BasicBlock* entry = BasicBlock::Create(context, "entry", func);
    IRBuilder<> builder(entry);
    Value* x = func->getArg(0);
    Value* y = func->getArg(1);
    Value* sum = builder.CreateAdd(x, y, "sum");
    builder.CreateRet(sum);

    if (verifyModule(*mod)) {
        return;
    }

    std::unique_ptr<ExecutionEngine> engine(EngineBuilder(std::move(mods)).create());
    std::vector<GenericValue> args(2);
    args[0].IntVal = APInt(32, 2);
    args[1].IntVal = APInt(32, 3);
    GenericValue result = engine->runFunction(func, args);
}

I need executable bitcode but it crash. When i debug i see mods its empty and i don't know why.

0

There are 0 best solutions below