Make a comparation with IR builder

Hi All,
I'm writing a code generation with my compiler. I read sever example and documentation but I did understand what I make wrong.
What I try to do is a compare a local variable with a constant.
But when I create a ICMP instruction I get that instruction are not of same type.
I'm using llvm by svn repository updated at two week ago.

The code that I try to generation is something like:

if varInt = 1 then
    varInt := 10;
end_if;

This is my code:

void *visit(integer_c *symbol) {
  int64_t value = GET_CVALUE(int64, symbol);
  if (typeid(*currentType) == typeid(get_datatype_info_c::bool_type_name)) {
    std::cout << "Creating integer: " << value << std::endl;
    return (void *)ConstantInt::get(Type::getInt1Ty(getGlobalContext()), value, false);
  }
  return (void *)ConstantInt::get(Type::getInt16Ty(getGlobalContext()), value, false);
}

void *visit(symbolic_variable_c *symbol) {
   std::string varName;
   AllocaInst *alloc;

   varName = get_var_name_c::get_name(symbol->var_name)->value;
   alloc = locals[varName];

   return (void *)alloc;
}

void *visit(equ_expression_c *symbol) {
  Value *lValue;
  Value *rValue;

  lValue = (Value *)symbol->l_exp->accept(*this);
  rValue = (Value *)symbol->r_exp->accept(*this);

  return Builder.CreateICmpEQ (lValue, rValue, "cond");

}

  Cheers,
Manuele

you’re not showing enough code. What does accept() do?

Based on your description, I strongly suspect that your alloca and constant are not the same type. Remember that alloca returns a pointer type that you must load to get at the actual variable.

Hi Justin,
my class is a visitor pattern and I use accept method to go recursive in suboject in my AST.
In locals I store AllocaInst pointer.

void *visit(var1_init_decl_c *symbol) {
   llvm::Type *lType;

   varNames.clear();
   varType = "";
   symbol->var1_list->accept(*this); /* get a vector contains variable names */
   symbol->spec_init->accept(*this); /* Store in varType variable list */
   lType = typeOf(varType);
   for (unsigned int i = 0; i < varNames.size(); i++) {
    AllocaInst *alloc = new AllocaInst(lType, varNames[i].c_str(), currentBBlock);
    locals[varNames[i]] = alloc;
   }

   return NULL;
}

Can I load a AllocaInst using CreateLoad instruction?

Cheers,
Manuele

Hi Justin,
my class is a visitor pattern and I use accept method to go recursive in
suboject in my AST.
In locals I store AllocaInst pointer.

void *visit(var1_init_decl_c *symbol) {
  llvm::Type *lType;

  varNames.clear();
  varType = "";
  symbol->var1_list->accept(*this); /* get a vector contains variable names */
  symbol->spec_init->accept(*this); /* Store in varType variable list */
  lType = typeOf(varType);
  for (unsigned int i = 0; i < varNames.size(); i++) {
    AllocaInst *alloc = new AllocaInst(lType, varNames[i].c_str(), currentBBlock);
    locals[varNames[i]] = alloc;
  }

  return NULL;
}

Can I load a AllocaInst using CreateLoad instruction?

Yes, your generated alloca is actually returning a "pointer of lType" type.
You need to load the value to an "lType" type. If lType is i32, then
alloca returns an i32*.

Hi Justin,
today I followed your suggestion and it work!!
Many Thanks.
Manuele