<div dir="ltr">There are actually two separate things you might want to use the native C++ API for. The first, as you noted, is generating LLVM IR via IRBuilder.  You might also want to have a custom opt/llc-like tool which will make it somewhat easier to integrate LLVM plugins, such as for garbage collection or language-specific passes implementing sanity checks and/or optimizations. Shared library plugins are supported on *nix but not Windows AFAIK.<div>

<br></div><div>One downside of doing your own abstract syntax, beyond the up-front investment of effort, is that as you use more of LLVM's capabilities (vectors, debug metadata, custom calling conventions, etc), you'll have to do more work to represent and unparse each new feature correctly.<br>

</div><div style><br></div><div style>On the other hand, having an explicit representation independent of IRBuilder state is useful when debugging any invalid IR your front-end generates. The problem is that invalid IR will trigger asserts in LLVM, and printing out bits and pieces of your partially-built module  from gdb isn't much fun.</div>

<div><div><br></div><div style>GHC's LLVM backend [1] takes the unparse-abstract-syntax approach, but they also use a relatively small subset of LLVM. For example, they don't make any use of LLVM's GC functionality. The Disciple language [2] has a fork of GHC's LLVM code, but I don't know offhand what the differences are. I don't know of any such projects for OCaml.</div>

<div><br></div><div style>FWIW my approach has been to generate a representation (from Haskell) which is fairly close to LLVM IR but abstracts some things, like GC metadata. This representation is then parsed (via serialized Protocol Buffers) from C++, which performs the appropriate IRBuilder actions, and can integrate statically-linked plugins.</div>

<div><br></div></div><div>[1] <a href="https://github.com/ghc/ghc/tree/master/compiler/llvmGen">https://github.com/ghc/ghc/tree/master/compiler/llvmGen</a></div><div>[2] <a href="http://hackage.haskell.org/package/ddc-core-llvm">http://hackage.haskell.org/package/ddc-core-llvm</a></div>

<div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 15, 2013 at 6:32 PM, Hongbo Zhang <span dir="ltr"><<a href="mailto:bobzhang1988@gmail.com" target="_blank">bobzhang1988@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
   I am trying to compile my toy language to LLVM back end. (I am new to LLVM, so my questions may sound naive)<br>
   I am looking at some tutorials about LLVM, most are about how to use LLVM IRBuilder, however, I find the API provided by IRBuilder is quite imperative and verbose, and the API changes so fast that most of the tutorials are out of dated.<br>


   So I am wondering what's the benefit of emitting LLVM IR using IRBuilder compared with designing my own abstract syntax in a high-level programming language(e.g. Haskell or OCaml) and unparsing it to LLVM IR. Is there some benefit of using IRBuilder I ignored here? And I have some follow-up questions: 1. How stable is the IR format? 2. Is the binary representation of IR format (*.bc) stable and the same across different platforms? 3. Is there any previous work of building a declarative`interface instead of using IRBuilder?<br>


   Thank you in advance!<br>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvmdev</a><br>
</blockquote></div><br></div>