On Wed, Jul 16, 2008 at 8:19 PM, Bill Wendling <isanbard at gmail.com>
wrote:> On Wed, Jul 16, 2008 at 11:07 AM, HyperQuantum <hyperquantum at
gmail.com> wrote:
>> How can I allocate an array with a size that is not known at compile
time?
> Do you mean a variable length array (as in C99) or something else?
I guess I should have explained what I'm trying to do. I'm working on
a front end for my programming language and using the LLVM libraries
as back end. The arrays in my language are currently represented by an
LLVM structure type that contains integer fields to hold the size for
each array dimension, and then an array type with the actual data for
the array.
A simple example, an array with 3 integers:
var x : [3] int
For this example the following code is generated:
%x = alloca { i32, [3 x i32] } ; <{ i32, [3 x i32] }*> [#uses=4]
getelementptr { i32, [3 x i32] }* %x, i32 0, i32 0 ; <i32*>:1
[#uses=1]
store i32 3, i32* %1
(and followed by a loop that initializes the three elements)
But I only tested this with arrays that have an integer constant as
size. Now if I try it with a size that comes from an expression like
this:
var size : nat = 9
var x : [size] int
Then my compiler does not know the size of the array and uses a zero
for the type:
%size = alloca i32 ; <i32*> [#uses=2]
store i32 9, i32* %size
%x = alloca { i32, [0 x i32] } ; <{ i32, [0 x i32] }*> [#uses=4]
load i32* %size ; <i32>:1 [#uses=1]
getelementptr { i32, [0 x i32] }* %x, i32 0, i32 0 ;
<i32*>:2 [#uses=1]
store i32 %1, i32* %2
Obviously this is wrong, but I don't see how I can use the value from
'size' to allocate the necessary space for this array with alloca. I
could use a pointer to an array in the struct type, instead of a
direct array, and use two allocas (one for the struct and one for the
array data). But then there is no real reason to use the array type
anymore, a pointer to the first element (like llvm-gcc generates for
C/C++ arrays) would be much simpler. Or am I missing something?
Regards,
Kevin André