Niddodi, Chaitra via llvm-dev
2020-Sep-22 22:52 UTC
[llvm-dev] Creating a global variable for a struct array
Hello, I would like to create a global variable for the following struct array, h1 dhash* h1 = new dhash[10]; typedef struct dhash{ char* filenm; dlist* llist; }dhash; typedef struct dlist{ int soffst; int eoffst; uint8_t* dptr; }dlist; I also need to allocate space for: 1) the field llist in struct dhash which is a pointer to another struct dlist and 2) the field dptr in struct dlist Is there an example that I can refer to for doing this ? I tried to create a GlobalVariable using ConstantStruct::get(StructType*, ArrayRef<Constant *>). I'm not sure how to get the second argument of type ArrayRef<Constant *> from the above variable h1. Thanks, Chaitra -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200922/ff31f363/attachment-0001.html>
Tim Northover via llvm-dev
2020-Sep-23 08:04 UTC
[llvm-dev] Creating a global variable for a struct array
On Tue, 22 Sep 2020 at 23:52, Niddodi, Chaitra via llvm-dev <llvm-dev at lists.llvm.org> wrote:> dhash* h1 = new dhash[10];Note that this "new" expression needs a runtime call to actually allocate the memory on the heap. In LLVM terms this is A GlobalVariable with type PointerType::get(StructTy, ...), initialized to null, together with a global init function (runs when the program starts) that makes the call to new and stores it to that global. My guess is that's not what you intended> I also need to allocate space for: > 1) the field llist in struct dhash which is a pointer to another struct dlist and > 2) the field dptr in struct dlist > > Is there an example that I can refer to for doing this ?I think you should start by writing exactly what you want in C++, and then (when you're happy with the assembly output) using Clang's "-S -emit-llvm" options to see what LLVM IR that produces. That will clear up the question of how things should be allocated, which isn't quite obvious from the questions you're asking, and make translating the IR into LLVM API calls more straightforward.> I tried to create a GlobalVariable using ConstantStruct::get(StructType*, ArrayRef<Constant *>). I'm not sure how to get the second argument of type ArrayRef<Constant *> from the above variable h1.Hopefully it'll be clearer when you have the real IR you want (in textual form) in front of you. Cheers. Tim.
Niddodi, Chaitra via llvm-dev
2020-Sep-30 00:47 UTC
[llvm-dev] Creating a global variable for a struct array
Let me clarify my question. I have a struct array h1 as follows: dhash h1[10]; I want to get a Constant* to variable h1. It looks like I can use ConstantStruct::get(StructType*, ArrayRef<Constant *>) to do this. My question is how to get the second argument of type ArrayRef<Constant *> from the above variable h1. Thanks, Chaitra ________________________________ From: Tim Northover <t.p.northover at gmail.com> Sent: Wednesday, September 23, 2020 03:04 To: Niddodi, Chaitra Cc: llvm-dev at lists.llvm.org Subject: Re: [llvm-dev] Creating a global variable for a struct array On Tue, 22 Sep 2020 at 23:52, Niddodi, Chaitra via llvm-dev <llvm-dev at lists.llvm.org> wrote:> dhash* h1 = new dhash[10];Note that this "new" expression needs a runtime call to actually allocate the memory on the heap. In LLVM terms this is A GlobalVariable with type PointerType::get(StructTy, ...), initialized to null, together with a global init function (runs when the program starts) that makes the call to new and stores it to that global. My guess is that's not what you intended> I also need to allocate space for: > 1) the field llist in struct dhash which is a pointer to another struct dlist and > 2) the field dptr in struct dlist > > Is there an example that I can refer to for doing this ?I think you should start by writing exactly what you want in C++, and then (when you're happy with the assembly output) using Clang's "-S -emit-llvm" options to see what LLVM IR that produces. That will clear up the question of how things should be allocated, which isn't quite obvious from the questions you're asking, and make translating the IR into LLVM API calls more straightforward.> I tried to create a GlobalVariable using ConstantStruct::get(StructType*, ArrayRef<Constant *>). I'm not sure how to get the second argument of type ArrayRef<Constant *> from the above variable h1.Hopefully it'll be clearer when you have the real IR you want (in textual form) in front of you. Cheers. Tim. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200930/bb9a09b1/attachment.html>