How to implement list in llvm

Hi!
I want to implement a list (which operates like ArrayList in Java).

I can use ArrayType from llvm, but at creation time I have to specify
size so there is a problem when I want to extend it (I can reallocate
it ofc). Moreover I need to implement all the operations (inserting,
removing etc.) manually.

I though that maybe I can use std::vector, but there is another
problem - std::vector is a template class, so I have to specify type
before compiling it to llvm bytecode. Is there some solution to
specify that type dynamically?

What is the best method to implement such things in llvm?

I would be grateful for any solutions or tips.

Hi,

I guess you could use a template with a std::vector: you don’t need to define right away the type, if you create a template of it.

It should look something like that:

template class List {
std::vector myList;
void insert (int pos, B elem);
}

template
void List::add (int pos, B elem) {
myList.insert(myList.begin()+pos, elem);
}

Then, wherever you’ll use this class, you should define for which class B your class List should match (such as “List intList;” or “List< std::vector > longVectors;”).

Thank you for your answer!

You are right, but I still don’t know how to build such a list dynamically using LLVM API with user type inside (I don’t know this type yet ofc).

Hi,

Sorry, but I’m not sure I understand what you’re trying to do. I guess you want some kind of superclass of some set of classes that are defined in the llvm api(?). In that case, which are specifically the types you could have in this list you’re trying to define?

Hi!

In my compiler I want to give user a list of user defined classes. I tried to use generated bitcode of my own wrapper of std::vector, but there is no possibility to generate bitcode of template, so I finally build it with std::vector<void*>. Do you know about any better solutions how to build such a type with llvm? Maybe there is a llvm built in type that is better choice than external c++ wrapper?

In my compiler I want to give user a list of user defined classes. I tried
to use generated bitcode of my own wrapper of std::vector, but there is no
possibility to generate bitcode of template, so I finally build it with
std::vector<void*>.

Does this mean you wrote a wrapper around std::vector, generated LLVM
IR for it, and then inserted this IR into your compiled code in some
manner?

Do you know about any better solutions how to build such
a type with llvm? Maybe there is a llvm built in type that is better choice
than external c++ wrapper?

If I understand what you're doing, there's no generic LLVM
implementation of any complex datatypes for front-ends to use. Each
one is expected to implement its own requirements.

In principle, some kind of library could be developed, of course.

Tim

Oh, ok, sorry but only now I understood your list will be used by the programmers functions on his code, and not on your pass code for llvm while your pass is being compiled XD

Ok, once I had a similar problem, and I resolved it by creating a mini API in C++ with some classes that encapsulated lists of integer, floating point or pointer types (I actually needed three different classes for those three types). Those are not that hard to write, and I guess mine would not be very welcome to share because I had a very specific implementation (I wanted something with a queue behavior, and I was working with “many producers → one consumer” strategy).

So all I had to do was insert the right calls on the functions being transformed by my pass, and that was it.

Cheers,

Thank you very much for your answers. As you said, we decided too to use std::vector<void*>. Maybe in the future we’ll try to make our own structure from scratch but for now this one is OK.

Best,
blackbox dev team

2013/4/19 Cristianno Martins <cristiannomartins@gmail.com>