Ram Bhamidipaty
2006-Nov-12 19:13 UTC
[LLVMdev] need help understanding getelementptr assembler instruction
I am trying to understand the hello word assember example. This is my version: %str1 = internal constant [13 x sbyte] c"Hello World\0a\00" declare int %printf(sbyte*, ...) implementation ; Functions: int %main() { %str2 = getelementptr [13 x sbyte]* %str1, long 0, long 0 call int(sbyte*, ...) *%printf(sbyte* %str2) ret int 0 } Why is getelementptr being given two "long 0" indices? Thanks for any help. -Ram
Reid Spencer
2006-Nov-12 19:47 UTC
[LLVMdev] need help understanding getelementptr assembler instruction
Ram, Please read and understand the GetElementPtr FAQ available here: http://llvm.org/docs/GetElementPtr.html That will help you understand how it works. We wrote that document specifically because this question comes up all the time. Reid. On Sun, 2006-11-12 at 11:13 -0800, Ram Bhamidipaty wrote:> I am trying to understand the hello word assember example. This is > my version: > > %str1 = internal constant [13 x sbyte] c"Hello World\0a\00" > > declare int %printf(sbyte*, ...) > > implementation ; Functions: > > int %main() { > %str2 = getelementptr [13 x sbyte]* %str1, long 0, long 0 > call int(sbyte*, ...) *%printf(sbyte* %str2) > ret int 0 > } > > Why is getelementptr being given two "long 0" indices? > > Thanks for any help. > -Ram > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Eric van Riet Paap
2006-Nov-13 07:35 UTC
[LLVMdev] need help understanding getelementptr assembler instruction
Hi, I must say I also was in need of this document but never knew it existed. It seems to be linked from the faq page only. I only read that the first day I came to LLVM. Maybe linking it from the GetElementPtr instruction in LangRef.html would make sense. cheers Eric On Nov 12, 2006, at 8:47 PM, Reid Spencer wrote:> Ram, > > Please read and understand the GetElementPtr FAQ available here: > > http://llvm.org/docs/GetElementPtr.html > > That will help you understand how it works. We wrote that document > specifically because this question comes up all the time. > > Reid. > > On Sun, 2006-11-12 at 11:13 -0800, Ram Bhamidipaty wrote: >> I am trying to understand the hello word assember example. This is >> my version: >> >> %str1 = internal constant [13 x sbyte] c"Hello World\0a\00" >> >> declare int %printf(sbyte*, ...) >> >> implementation ; Functions: >> >> int %main() { >> %str2 = getelementptr [13 x sbyte]* %str1, long 0, long 0 >> call int(sbyte*, ...) *%printf(sbyte* %str2) >> ret int 0 >> } >> >> Why is getelementptr being given two "long 0" indices? >> >> Thanks for any help. >> -Ram >> _______________________________________________ >> 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
Robert Mykland
2006-Nov-13 19:23 UTC
[LLVMdev] need help understanding getelementptr assembler instruction
Ram: Let me explain and hopefully Reid or Chris will correct if I have it wrong. The first 0 is the index into a possible array of sbyte[13] arrays that this pointer points to. I expect GetElementPtr works this way is to keep down the proliferation of arrays of arrays in the type table. So you are pointing at the zeroth, and in this case only, array of sbyte[13]. The next 0 actually indexes into the array of sbyte, so you are pointing at index 0, or pointing at the 'H' in "Hello World". Hope this helps, -- Robert. Ram Bhamidipaty wrote:> I am trying to understand the hello word assember example. This is > my version: > > %str1 = internal constant [13 x sbyte] c"Hello World\0a\00" > > declare int %printf(sbyte*, ...) > > implementation ; Functions: > > int %main() { > %str2 = getelementptr [13 x sbyte]* %str1, long 0, long 0 > call int(sbyte*, ...) *%printf(sbyte* %str2) > ret int 0 > } > > Why is getelementptr being given two "long 0" indices? > > Thanks for any help. > -Ram > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > >
Reid Spencer
2006-Nov-13 19:59 UTC
[LLVMdev] need help understanding getelementptr assembler instruction
Robert, I'm going to comment because it is important that people get this straight. If you're learning LLVM, please read the GEP FAQ at http://llvm.org/GetElementPtr.html. If there is a deficiency there (some question not answered) please let me know and I'll add it. On Mon, 2006-11-13 at 11:23 -0800, Robert Mykland wrote:> Ram: > > Let me explain and hopefully Reid or Chris will correct if I have it > wrong. The first 0 is the index into a possible array of sbyte[13] > arrays that this pointer points to.Yes. The confusion arises with GVs because: %str1 = internal constant [13 x sbyte] c"Hello World\0a\00" is of type [13 x sbyte]* not [13 x sbyte] That isn't plainly clear from the assembly but all GlobalValues are constants of pointer type. Think of %str1 as some magical register floating around that points to some place in memory. The fact that it has the same value as the address of the first element in the array is just a mathematical coincidence. You still have to index through the pointer and then into the array.> I expect GetElementPtr works this > way is to keep down the proliferation of arrays of arrays in the type > table.I don't think so. It works this way because of the LLVM type system.> So you are pointing at the zeroth, and in this case only, array > of sbyte[13].Yes. Although, I prefer to think of it as indexing through the GVar pointer.> The next 0 actually indexes into the array of sbyte, so > you are pointing at index 0, or pointing at the 'H' in "Hello World".Right.> > Hope this helps, > > -- Robert. > > Ram Bhamidipaty wrote: > > I am trying to understand the hello word assember example. This is > > my version: > > > > %str1 = internal constant [13 x sbyte] c"Hello World\0a\00" > > > > declare int %printf(sbyte*, ...) > > > > implementation ; Functions: > > > > int %main() { > > %str2 = getelementptr [13 x sbyte]* %str1, long 0, long 0 > > call int(sbyte*, ...) *%printf(sbyte* %str2) > > ret int 0 > > } > > > > Why is getelementptr being given two "long 0" indices? > > > > Thanks for any help. > > -Ram > > _______________________________________________ > > 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
Reasonably Related Threads
- [LLVMdev] need help understanding getelementptr assembler instruction
- [LLVMdev] need help understanding getelementptr assembler instruction
- [LLVMdev] need help understanding getelementptr assembler instruction
- [LLVMdev] need help understanding getelementptr assembler instruction
- [LLVMdev] need help understanding getelementptr assembler instruction