> This is almost exactly what you want to do. Please make the array be [0 x > int] though, as it is undefined in llvm to access past the end of an array > with non-zero length. I added a note about zero-length arrays here: > > http://llvm.cs.uiuc.edu/docs/LangRef.html#i_getelementptr > http://llvm.cs.uiuc.edu/docs/LangRef.html#t_array > > As you mention above, you should use malloc and cast the return value. If > you want to get the desired size in a target-independent way (always > good), you should do something like this: > > %array = type { int, int, [0 x int] } > implementation > %array *%NewArray(int %Length) { > ;; Get the offset of the 'Length' element of the array from the null > ;; pointer. > %Size = getelementptr %array* null, int 0, uint 2, int %Length > %SizeU = cast int* %Size to uint > %Ptr = malloc sbyte, uint %SizeU > %Result = cast sbyte* %Ptr to %array* > ret %array* %Result > }Chris you should maybe add this example to the getelementptr documentation. Aaron
On Fri, 24 Jun 2005, Aaron Gray wrote:>> As you mention above, you should use malloc and cast the return value. If >> you want to get the desired size in a target-independent way (always good), >> you should do something like this:...> Chris you should maybe add this example to the getelementptr documentation.This isn't really appropriate for the reference manual, but I added it to the "Source Language Implementation Notes" of my web page here: http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt Hope this helps! -Chris -- http://nondot.org/sabre/ http://llvm.cs.uiuc.edu/
Chris Lattner wrote:> On Fri, 24 Jun 2005, Aaron Gray wrote: >>> As you mention above, you should use malloc and cast the return value. >>> If you want to get the desired size in a target-independent way (always >>> good), you should do something like this: > > ... > >> Chris you should maybe add this example to the getelementptr >> documentation. > > This isn't really appropriate for the reference manual, but I added it to > the "Source Language Implementation Notes" of my web page here: > > http://nondot.org/sabre/LLVMNotes/SizeOf-OffsetOf-VariableSizedStructs.txt > > Hope this helps!Bingo, it does help! Thanks Chris! However, I'm with Aaron on where this belongs, your link above really should be integrated into or at least an addendum to the IR Language Reference, http://llvm.cs.uiuc.edu/docs/LangRef.html unless you're keeping it out of the official IR docs for now because you will/might handle this more directly or differently in a future revision of the IR. If that's the case then the IR Lang Ref should at least for now provide a link to the above from within itself, IMHO. In any case, thanks! :) Ed