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 Il 11/01/2013 12:56, Justin Holewinski ha scritto:> > 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. > > On Jan 11, 2013 3:28 AM, "Manuele Conti" <manuele.conti at sirius-es.it > <mailto:manuele.conti at sirius-es.it>> wrote: > > 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 > > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> > http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130111/143d1513/attachment.html>
On Fri, Jan 11, 2013 at 8:20 AM, Manuele Conti <manuele.conti at sirius-es.it>wrote:> 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*.> Cheers, > Manuele > > Il 11/01/2013 12:56, Justin Holewinski ha scritto: > > 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. > On Jan 11, 2013 3:28 AM, "Manuele Conti" <manuele.conti at sirius-es.it> > wrote: > >> 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 >> >> >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >-- Thanks, Justin Holewinski -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130111/ce4e16fd/attachment.html>
Hi Justin, today I followed your suggestion and it work!! Many Thanks. Manuele Il 11/01/2013 14:30, Justin Holewinski ha scritto:> On Fri, Jan 11, 2013 at 8:20 AM, Manuele Conti > <manuele.conti at sirius-es.it <mailto:manuele.conti at sirius-es.it>> wrote: > > 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*. > > Cheers, > Manuele > > Il 11/01/2013 12:56, Justin Holewinski ha scritto: >> >> 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. >> >> On Jan 11, 2013 3:28 AM, "Manuele Conti" >> <manuele.conti at sirius-es.it <mailto:manuele.conti at sirius-es.it>> >> wrote: >> >> 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 >> >> >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> >> http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> > http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > > > > -- > > Thanks, > > Justin Holewinski-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130112/8f03925b/attachment.html>