user1423561
user1423561

Reputation: 329

LLVM StoreInst and AllocaInst

I am trying to write a simple interpreter.

I am trying to generate LLVM IR for assignment operation. The code for the generation part looks like this

llvm::Value* codeGenSymTab(llvm::LLVMContext& context) {
    printf("\n CodeGen SymTab \n");
    Value *num = ConstantInt::get(Type::getInt64Ty(context), aTable.value, true);
    Value *alloc = new AllocaInst(IntegerType::get(context, 32), aTable.variableName,entry);
    StoreInst *ptr = new StoreInst(num,alloc,false,entry);
}

Here goes the SymTab definition:

struct SymTab {
     char* variableName;
     int value; 
     llvm::Value* (*codeGen)(llvm::LLVMContext& context);   
}; 

When I try to execute the output file,I get the following error:

Assertion failed: (getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"), function AssertOK, file Instructions.cpp, line 1084.
Abort trap: 6

Can you help me resolve it ?

Thanks

Upvotes: 2

Views: 4116

Answers (1)

Oak
Oak

Reputation: 26868

You try to store a value of type i64 into an address of type i32*, and these don't match.

You can fix this by using the same type - or preferably, the actual same object:

IntegerType *int_type = Type::getInt64Ty(context);
Value *num = ConstantInt::get(int_type, aTable.value, true);
Value *alloc = new AllocaInst(int_type, aTable.variableName, entry);
StoreInst *ptr = new StoreInst(num,alloc,false,entry);

Upvotes: 5

Related Questions