Yafei Liu via llvm-dev
2019-Oct-16 08:38 UTC
[llvm-dev] Does llvm has IR level implementation of vector and map?
Hi all, I want to add the concept of vector and map (just like vector and map in C++) in language level, for example: var shoppingList = ["catfish", "water", "tulips"] 1. var occupations = [ 2. "Malcolm": "Captain", 3. "Kaylee": "Mechanic", 4. ] (this is how Swift language declarate a vector and a map indeed) My question is I don't want to implement things like vector or map in my compiler code, so this any implemented classes in llvm API which can help me to do this? Or, I know how to change the code above to C++, is there any way I can turn the paring into calling C++ libraries? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20191016/3f24e908/attachment.html>
Tim Northover via llvm-dev
2019-Oct-16 17:12 UTC
[llvm-dev] Does llvm has IR level implementation of vector and map?
Hi, On Wed, 16 Oct 2019 at 01:38, Yafei Liu via llvm-dev <llvm-dev at lists.llvm.org> wrote:> My question is I don't want to implement things like vector or map in my compiler code, so this any implemented classes in llvm API which can help me to do this?LLVM doesn't have those kinds of types natively. The easiest way to get them is to write C wrapper functions that do the operations you want and insert calls to those functions with your compiler. The C wrapper functions could even be C++ declared with 'extern "C"' and directly use std::vector as the underlying implementation. For example one function might be: extern "C" vectorPushBack(void *Vec, void *Elt) { std::vector<void *> *RealVec = static_cast<std::vector<void *>>(Vec); RealVec.push_back(Elt); } Then you'd insert a call to @vectorPushBack in the IR you generate and link against this library of helper functions.> Or, I know how to change the code above to C++, is there any way I can turn the paring into calling C++ libraries?Calling C++ functions directly is quite hard because there are so many different ABIs out in the world. But in principle it can be done. I believe Swift does it, and uses Clang itself to get the correct ABI. Using 'extern "C"' above sidesteps quite a few of those issues. Cheers. Tim.