1

Based on llvmlite docs I can create static array like in a code snippet bellow.

ir.Constant(ir.ArrayType(ir.ArrayType(i8, STR_SIZE), ARR_SIZE), elements)

But can I create dynamic array in llvmlite or convert variable above to i8.as_pointer().as_pointer() type?

  • `char**` is a pointer type, not an array type, and yes, you can create pointer types in LLVM. – sepp2k Jul 26 '23 at 21:56
  • @sepp2k, is it possible to convert ArrayType variable to Pointer Type variable? – Eugenos_Programos Jul 27 '23 at 08:43
  • You can turn a pointer to a T array into a pointer to T using `getelementptr`. You can't get a pointer into an array that is stored directly in a register because registers don't have addresses. – sepp2k Jul 27 '23 at 11:14

1 Answers1

1

char** isn't an array, it's a pointer to pointer to char. C programs often use such a pointer and pretend that the type pointed to is something else.

LLVM lets you do the same, and in a fairly similar way. If you look closely at the getelementptr instruction, you'll notice that it takes both a pointer argument and a type argument. The type is the type pointed to. If you pass gep an array type, a pointer and an index, it pretends that the pointer points to that type and returns a pointer to an array entry with the specified index, which you can then load and treat as a Value.

arnt
  • 8,949
  • 5
  • 24
  • 32