0

In my main function, I have allocated an i8 pointer:

 %a = alloca i8*, align 8
 store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @1, i32 0, i32 0), i8** %a, align 8

Is it possible to:

  • assign a specific element (like arry[0] = 4)
  • get a specific element (like arry[0])

How can I do that with the C++ API?

6
  • What you've done is to allocate space for two pointers, not two arrays. The argument to alloca must be the type you want, in your case it should be an array type, not a pointer type. And there's no way to allocate+concatenate, LLVM is low-level, assemblylike, and that would be rather a high-level operation. The kind of thing that's provided by a language runtime or standard library, not by an assembly instruction. Commented Sep 13, 2023 at 8:17
  • If you have a pointer to an array and want a pointer to an element within the array, e.g. arr[4], then you use the getelementptr instruction. For example, to do arr[4] = 0, you'd create a GetElementPTrInst with indices 0,4, then a StoreInst with the gep as pointer operand and a constant int 4 as value operand. I can't reach the llvm.org site now and don't have my old compiler code on this computer, so I can't properly anwer. Commented Sep 14, 2023 at 16:48
  • @arnt does it make a difference that this is a string pointer? Commented Sep 14, 2023 at 17:08
  • No. LLVM IR doesn't even have a string type. Assembly languages generally don't have string types. When you want to operate on a string, you have to define the type yourself (the well-known LLVM users have different types for strings) and write the low-level instructions to operate on it. Commented Sep 15, 2023 at 8:57
  • @arnt What I ment to say is that an i8 ptr (created by stringref), not array, would still work for this. Or are they the same thing? Commented Sep 15, 2023 at 15:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.