Serializing LLVM IR in custom fashion

Hello.

I need to write a pass that converts a whole LLVM module into a bytecode for some other VM. VM’s instructions resemble LLVM ones pretty much, so it is almost 1-to-1 translation. Basically, I need to do what BitWriterPass does, but using different format for the resulting bytecode.

The obvious solution is just to iterate over all Instructions and serialize them using giant switch(i->getOpcode()), however this involves writing much boilerplate code. And I would eventually need to write a deserializer too, so that doubles the boilerplate work.

Any ideas how to do that easier way? How much of existing LLVM code can I reuse?

Thanks in advance.

Hi,

InstVisitor will give you that switch. Derive from that and you get to handle the different instruction types appropriately.

Cheers,
Philip

To really reduce boilerplate you might be able to define the right macros and then #include include/llvm/IR/Instruction.def.