LLVM Value Sign

I'm having trouble loading values. Here is a simple IR example that my compiler generates to show the problem:

define i32 @main() { entry: %a = alloca i32 ; <i32*> [#uses=2] store i32 1, i32* %a %r = load i32* %a ; <i32> [#uses=1] br label %Return Return: ; preds = %entry ret i32 %r } 

but instead of return 1 it returns 16777216. I tested the program with different values ​​and it seems to be a matter of internal binary representation of integer values.

Edited: I show you the trace of llvm calls that my compiler makes to generate the code above. bloques.back (). last is of type Value * and bloques.back (). bl - BasicBlock * (current block)

 bloques.back().last = dyn_cast<Value>(ConstantInt::get( Type::getInt32Ty(getGlobalContext()), $1, true)); // $1 is an int AllocaInst *alloc = new AllocaInst( Type::getInt32Ty(getGlobalContext()), izq.c_str(), bloques.back().bl); Value *derecha = bloques.back().last; StoreInst *s = new StoreInst(derecha, alloc, false, bloques.back().bl); bloques.back().last = alloc; LoadInst* v1 = new LoadInst(bloques.back().last, "r", false, bloques.back().bl); bloques.back().last = v1; BasicBlock* blockReturn = BasicBlock::Create(getGlobalContext(), "Return", Main); Value* last = bloques.back().last; BranchInst::Create(blockReturn, bloques.back().bl); ReturnInst::Create(getGlobalContext(), last, blockReturn); 

The source code corresponding to the IR representation will be:

 a = 1 return a 

Edit2: In my main function, after generating the code and before running the execution through JIT, I do this:

 ExecutionEngine *EE = EngineBuilder(M).create(); string str = EE->getTargetData()->getStringRepresentation(); str[0] = 'e'; M->setDataLayout(str); if (verifyModule(*M)) { errs() << argv[0] << ": Error building the function!\n"; return 1; } vector<GenericValue> noargs; GenericValue GV = EE->runFunction(Main, noargs); outs() << "Result: " << GV.IntVal << "\n"; return 0; 

Solved: finally, I found a solution. To change the statement, I do the following (a little spaguetti, but it works):

 Module *M = new Module("pythoncode", getGlobalContext()); ExecutionEngine *EE2 = EngineBuilder(M).create(); string str = EE2->getTargetData()->getStringRepresentation(); str[0] = 'e'; cout << str << endl; M->setDataLayout(str); ExecutionEngine *EE = EngineBuilder(M).create(); 

Thanks in advance.

Santos Merinon.

+4
source share

Source: https://habr.com/ru/post/1414585/


All Articles