Hi, 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: 6Can you help me resolve it ? Thanks Prakash -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140804/a7b13602/attachment.html>
>> 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);OK, try writing out the LLVM IR you expect to get for that final store in full (assume num is named "%num" and alloc is named "%alloc"). Check each detail of it against the lines above; do you see any discrepancies? Cheers. Tim.
As you may have noticed, although the assertions from the debug+assert LLVM build are factually correct, for the purposes of debugging code linked against it, it's not all that useful. I have found it useful to build a wrapper layer around LLVM in my project. The primary purpose of the wrapper was to isolate my application from changes in the LLVM API, as it seems to be a moving target. However, for places where I trigger LLVM's assertions frequently, I have "copied" those assertions into my wrapper to do the same checks prior to calling into LLVM, and to produce useful diagnostics in the case of failure. For example, I often get argument mismatches between a function call and its definition. So, in my callFunction wrapper, I do the type checks, and in the event of failure, I print out: - the name of the function being called - the position of the argument whose type mismatches - the formal and actual types in question. You can consider doing the same - create a wrapper around your calls into LLVM, and add better diagnostics where required. If there is sufficient interest, I might document better what I did. On Mon, Aug 4, 2014 at 8:18 AM, Prakash Premkumar <prakash.prax at gmail.com> wrote:> Hi, > > 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 > Prakash > > _______________________________________________ > LLVM Developers mailing list > 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/20140804/aa55da4c/attachment.html>
Thanks a lot Tim and David. I really found it useful to compare the instructions in the IR with the C++ Classes. The error in the above code is> Value *num = ConstantInt:: get(Type::getInt64Ty(context), aTable.value, > true);I should use a 32 bit int as my AllocaInst instrcution uses 32 bit int Thanks Prakash On Mon, Aug 4, 2014 at 6:45 PM, David Jones <djones at xtreme-eda.com> wrote:> As you may have noticed, although the assertions from the debug+assert > LLVM build are factually correct, for the purposes of debugging code linked > against it, it's not all that useful. > > I have found it useful to build a wrapper layer around LLVM in my project. > The primary purpose of the wrapper was to isolate my application from > changes in the LLVM API, as it seems to be a moving target. However, for > places where I trigger LLVM's assertions frequently, I have "copied" those > assertions into my wrapper to do the same checks prior to calling into > LLVM, and to produce useful diagnostics in the case of failure. > > For example, I often get argument mismatches between a function call and > its definition. So, in my callFunction wrapper, I do the type checks, and > in the event of failure, I print out: > - the name of the function being called > - the position of the argument whose type mismatches > - the formal and actual types in question. > > You can consider doing the same - create a wrapper around your calls into > LLVM, and add better diagnostics where required. > > If there is sufficient interest, I might document better what I did. > > > > On Mon, Aug 4, 2014 at 8:18 AM, Prakash Premkumar <prakash.prax at gmail.com> > wrote: > >> Hi, >> >> 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 >> Prakash >> >> _______________________________________________ >> LLVM Developers mailing list >> 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/20140805/334e6ac7/attachment.html>
Apparently Analagous Threads
- [LLVMdev] about creating the first value of the storeinst
- [LLVMdev] Retrieving Underlying Type from AllocaInst
- [LLVMdev] Retrieving Underlying Type from AllocaInst
- [LLVMdev] Retrieving Underlying Type from AllocaInst
- [LLVMdev] need to store the address of a variable