<div dir="ltr">Wow: that's a lot of text to have written in a short time. I'll try to have a detailed look later, but one borderline thing is you mention there are 6 different casts. It might be worth pointing out there's a 7th, the addrspacecast which is beyond the scope of the document but is used with address spaces describing devices which (may) have different address spaces which are used for different things (either for performance or security reasons).<br>
<div><br><a href="http://llvm.org/docs/LangRef.html#i-addrspacecast">http://llvm.org/docs/LangRef.html#i-addrspacecast</a><br><br></div><div>Cheers,<br>Dave<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Mon, Nov 25, 2013 at 9:46 AM, Mikael Lyngvig <span dir="ltr"><<a href="mailto:mikael@lyngvig.org" target="_blank">mikael@lyngvig.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Hi David,<div><br></div><div>I'm glad you like the idea :-)</div><div><br></div><div>I've been busy and so far only lack about three or four things in the C++ area of features: Proper exception handling (which I need to understand myself first), closures (which I don't think I've ever used), and generators (which I always wondered how they were implemented).  I think you are absolutely right - some day, this document will be routinely be extended with new and wonderful language constructs and this will help these features to propagate much faster into existing and new languages as people will quickly come to realize how simple it is to do this or that feature.  Besides, I think it is an awesome way of teaching people how to use LLVM IR: They can use their knowledge of familiar structures and constructs to see how it can be done in LLVM IR.</div>

<div><br></div><div>So far, it hasn't been too bad with GEPs and loads.  The most advanced example uses like two or three GEPs/loads in a row and I have already explained at the top of the document that the user should not worry - most commonly two or three LLVM IR instructions will be coalesced into a single instruction after optimization.<br>

</div><div><br></div><div>I'll let your idea simmer a bit and see what comes up.  For now, I have had the joy of coding up quite a few snippets in LLVM IR.  I am learning to swim as I am sinking into all the intricacies of LLVM IR.  So far is has only been pleasant working with LLVM IR, albeit I am a tad tired of typing types because LLVM IR demands explicit types on most expressions and arguments.  But I do understand that LLVM IR is meant to be automatically generated by a compiler, not hand-crafted by a tech writer.</div>

<div><br></div><div>I've attached my current draft.  If you are busy, don't care, or prefer to wait for the final result, please don't waste time on looking at it.  There's still a quite a bit to do.  Comments are more than welcome, though.  And feel free to suggest new language features and so on: Even if I cannot document them, I can always ask the list for help and together we can make up a great document, which I personally suspect will one day be almost as popular as the Language Reference.</div>
<span class="HOEnZb"><font color="#888888">
<div><br></div><div><br></div><div>-- Mikael</div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">2013/11/25 David Tweed <span dir="ltr"><<a href="mailto:david.tweed@gmail.com" target="_blank">david.tweed@gmail.com</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi, documentation is always good and this is a great idea. It'll be particularly useful as a place where additional examples of constructs from non-C-family languages could be added (since most compiler tutorials inevitably focus on languages that are a lot like C/C++/Obj-C).<br>


</div><div><br>I'd imagine you've already thought of this, but it might be something where using pseudo-LLVM-IR is of the most pedagogical use. (For example, writing code that accesses complex structured memory using multiple levels of GEP's and then loads is quite tricky, but for a lot of the constructs it's only a detail so you could probably express those bits using some pseudo instruction (going another step in to fully explicit LLVM-IR if necessary.)<br>


<br></div><div>Cheers,<br></div><div>Dave<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Nov 23, 2013 at 6:18 AM, Mikael Lyngvig <span dir="ltr"><<a href="mailto:mikael@lyngvig.org" target="_blank">mikael@lyngvig.org</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks, you have a lot of valid points there.  I have myself long ago abandoned the path of using C as a backend language due to the very factors you mention.  <div>


<br></div><div>However, as I said, the document was put together in 30 minutes.  Not exactly ready for prime time :-)</div>
<div><br></div><div>I do agree that all of the things you mention should be described, including Lambdas, closures, and generators, but I must admit up front that I don't know how to implement half of them.  But I suppose I could do a lot of research and perhaps occasionally ask you guys for specifics.</div>



<div><br></div><div>We are not going to find much common ground on the issue of "calling propagated return values for exception handling", I think :-)  See <a href="https://www.lyngvig.org/Teknik/A-Proposal-for-Exception-Handling-in-C" target="_blank">https://www.lyngvig.org/Teknik/A-Proposal-for-Exception-Handling-in-C</a> for the details.</div>



<div><br></div><div>I started out with C++ as the example language because a lot of people know that language - and most certainly the majority of the LLVM user base.  Obviously, you'd have to add source code from other languages than C++ when C++ does not provide features to illustrate the process.</div>



<div><br></div><div>I now agree that the lowering into C is not such a good idea after all.  So I'll go straight from source language to LLVM IR, which is not that difficult after all, and won't be very different for the reader.  In fact, I think it will be much better than my original approach.</div>



<div><br></div><div>Thanks again for your valid objections.</div><span><font color="#888888"><div><br></div><div><br></div><div>-- Mikael</div><div><br></div><div><br></div></font></span></div><div>
<div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/11/23 Joshua Cranmer 🐧 <span dir="ltr"><<a href="mailto:Pidgeot18@gmail.com" target="_blank">Pidgeot18@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 11/22/2013 9:25 PM, Mikael Lyngvig wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi guys,<br>
<br>
I have begun writing on a new document, named "Mapping High-Level Constructs to LLVM IR", in which I hope to eventually explain how to map pretty much every contemporary high-level imperative and/or OOP language construct to LLVM IR.<br>




<br>
I write it for two reasons:<br>
<br>
1. I need to know this stuff myself to be able to continue on my own language project.<br>
2. I feel that this needs to be documented once and for all, to save tons of time for everybody out there, especially for the language inventors who just want to use LLVM as a backend.<br>
<br>
So my plan is to write this document and continue to revise and enhance it as I understand more and helpful people on the list and elsewhere explain to me how these things are done.<br>
<br>
Basically, I just want to know if there is any interest in such a document or if I should put it on my own website.  If you know of any books or articles that already do this, then please let me know about them.<br>
<br>
I've attached the result of 30 minutes work, just so that you can see what I mean.  Please don't review the document as it is still in its very early infancy.<br>
</blockquote>
<br>
There is a strong bias towards C++ in the document, which isn't a particularly strong slice of higher-level constructs. For example, C++'s RTTI constructs serve three distinct purposes: exception handling, dynamic casts, and reflection (although C++'s reflection capabilities are extremely weak). You'll need to talk about inheritance in the three cases: single, multiple, and virtual (to use C++'s terminology) (note that Java's interfaces can be implemented as virtual inheritance). Boxing is another important topic. Lambdas, closures, and generators (yield keyword) are becoming increasingly common in modern programming languages, and should not be ignored.<br>




<br>
Finally, calling propagated return values "exception handling" does an extreme disservice to your readers. LLVM IR explicitly models exception handling, and attempting to describe it lowered as return values is not how anyone should implement it. If you badly want to describe it in C terms, you could at least use C's setjmp/longjmp to describe it; the truth is, this is a feature which doesn't exist cleanly in C.<br>




<br>
Trying to describe mapping higher-level languages to C and then C to IR is a poor idea. C is in some ways an extremely limited language (no native exception handling constructs, e.g.). If you want to be a guide to how to lower languages to LLVM IR, you need to also explain how to take advantage of features in the IR to optimize code better (e.g., TBAA). Cfront-like C++ compilers are extremely rare-to-nonexistent (in part because it is difficult to map some features, most notably exception handling, cleanly and efficiently into C); if your guide is describing such an approach, it reads like an implicit endorsement. It is possible to describe some aspects of the IR in C, but if the goal is to lower to IR, then the description should be lowering to IR, not lowering to C.<span><font color="#888888"><br>




<br>
-- <br>
Joshua Cranmer<br>
Thunderbird and DXR developer<br>
Source code archæologist<br>
<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>
</font></span></blockquote></div><br></div>
</div></div><br>_______________________________________________<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/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><span><font color="#888888"><br><br clear="all"><br>-- <br><div>cheers, dave tweed__________________________</div><div>high-performance computing and machine vision expert: <a href="mailto:david.tweed@gmail.com" target="_blank">david.tweed@gmail.com</a></div>


<div>"while having code so boring anyone can maintain it, use Python." -- attempted insult seen on slashdot</div><div> </div>
</font></span></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div>cheers, dave tweed__________________________</div><div>high-performance computing and machine vision expert: <a href="mailto:david.tweed@gmail.com" target="_blank">david.tweed@gmail.com</a></div>
<div>"while having code so boring anyone can maintain it, use Python." -- attempted insult seen on slashdot</div><div> </div>
</div>