3

I have a project written in C which I wish to convert to Perl. A friend of mine suggested to use LLVM. I compiled my C code to LLVM assembly using Clang. Now I'm trying to convert this intermediate representation into Perl but I can't seem to find any backend for it. Would someone point me in the right direction?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Rohan Durve
  • 340
  • 2
  • 14
  • 1
    Given that the resulting code would be utterly unreadable even to someone who actually enjoys reading bad Perl code (we all know that there is an abundance of Perl code out there most people would consider unreadable already), I can't help but ask: Why? – Christopher Creutzig Mar 16 '13 at 11:17
  • @ChristopherCreutzig - Cause MetaSploit OP. – Rohan Durve Mar 16 '13 at 11:22
  • 2
    If you want to marry Perl with C code, I suggest using Inline::C or XS. These technologies allow you to call Perl from C and vice versa, as long as you take some precautions on the C side, and use the Perl data structures. You would then be able to translate some functions to Perl. I don't know of any way to use Perl as a compilation *target*. Even if there were, the resulting code would be an unidiomatic (and thus painfully slow) pile of shit. – amon Mar 16 '13 at 13:48

2 Answers2

7
  1. No, there isn't such a backend. At least not directly. Emscripten converts LLVM IR to Javascript, and maybe you can use something else to convert that to Perl.
  2. It's a pretty bad idea for moving a project from one language to another. The code will be completely unreadable and un-maintainable. Either do a honest rewrite or wrap your C as a library with Perl's foreign-function interface tools and use it from Perl.
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
6

Converting to Perl is a no go. It hasn't been done because emulating C in Perl would be insanely slow. They have completely different type systems, for starters. However, it's wholly unnecessary to convert the code to Perl to call it from Perl. You can load compiled C code from Perl.

What you'll have to do is tell Perl how to call those C functions. Perl deals with scalars and so on, but C deals with ints and so on. This is done using XS.

perlxstut contains the documentation on how to do all this.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    nowdays [`FFI::Platypus`](https://metacpan.org/pod/FFI::Platypus) - `Write Perl bindings to non-Perl libraries with FFI` is avaiable – Eugen Konkov Nov 07 '18 at 15:45