I want to model dynamic arrays. This is the plan i've come up with: there will be a base struct for all my arrays, including a vtable-pointer, and also the runtime-size of the array:
%anyarray_base = type {
%other_stuff,
i64, ; runtime size
}
%bytearray = type { %anyarray_base, [0 x i8] }
This works for arrays created fully at runtime. I malloc memory for the %anyarray_base plus the size of the "payload". I can access the data in that [0 x i8] using getelementptr just fine.
The problem i have is constants. Very concrete case: my program has a constant string "Hello World", and i want to create a constant in my LLVM module to hold that string. So, i'd write
@myConstantString = global %bytearray {
%anyarray_base {
@other_stuff, ; constant misc data about the array
i64 12 ; array size, 12 bytes
},
[12 x i8] c"Hello, World!" ; the actual literal from the source code
}
llvm-as doesn't accept this:
error: element 1 of struct initializer doesn't match struct element type
(points at the global %bytearray constant)
I'm clearly missing some understanding on how LLVM works. Please help me build Hello World in my toy language :)