<div dir="ltr"><div><div><div><div>Calling C portable assembler is, in my view, doing both assembler and C a disservice. Yes, you can, with a lot of knowledge of how a particular compiler works, and what processor you are on, know what will acutally happen on a particular machine. But C is not very clearly defined in a lot of cases. Sizes of types require at least a special header file to be included to ensure you KNOW if you get a 16, 32, 36, 48 or 64 bit sizes integer as "int".<br><br></div>LLVM-IR is designed to overcome the trouble of describing some of these things internally in a compiler. It allows you to write a compiler that has a well known behaviour, with a little more effort than producing strings in C (actually, it may be LESS effort, if you get it right and you struggle with string handling). Also, producing C code for some things will be quite hard - especially if the C representation isn't straight forward. It's then just as easy to write LLVM-IR representation (and you can always "cheat" and write the corresponding code in C, compile it with `clang -emit-llvm -S myfile.c`, and look at `myfile.ll` to see what was produced, then reproduce it in your own compiler). <br><br></div>I have written a Pascal compiler using LLVM, and it's really not hard (I'd say, easier than a Pascal to C compiler, although I have never written one of those). <br><br></div>One thing to watch out for is that LLVM doesn't "like" big structure/array arguments being passed from one function to another. You will need to check the size, and use some trick to copy to temporary, pass a pointer, and use "byval" to tell the LLVM layer that it's a copy.<br><br>--<br></div><div>Mats<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 27 October 2016 at 16:30, Renato Golin via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 27 October 2016 at 16:18, David Chisnall via llvm-dev<br>
<span class=""><<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
> LLVM IR is intended as a compiler intermediate representation.  It aims to be able to express anything that arbitrary source languages might want and anything that target architectures may implement, in a way that is not closely tied to either specific source languages or target architectures.  It doesn’t entirely succeed in this, but it comes a lot closer than C to this goal (not surprising, as this was never a goal of C).<br>
<br>
</span>Yeah, "LLVM IR is a compiler IR, not a target-independent language".<br>
<br>
Lowering your language to LLVM IR is a great way to reduce the cost of<br>
target-specific knowledge in your front-end 10-fold. But there's still<br>
the remaining 1/10th that you need to do.<br>
<br>
Exception handling is one obviously complicated one, but there's also:<br>
<br>
* Procedure-call standard differences: most arguments will marshal<br>
correctly in LLVM IR, but complex structures sometimes need to be<br>
lowered to arrays, or moved around.<br>
* Type sizes: The LLVM lowering is smart enough (in most cases) to<br>
split too-large values (64-bit int on 32-bit machine), but that's<br>
error prone and inefficient. You should emit the best types per<br>
architecture.<br>
* Your language's ABI: C++ has an ABI which defines the layout of<br>
classes and objects that can be different between targets. You may<br>
want to make special concessions due to speed and code-size concerns.<br>
<br>
Pretty much everything else should be dealt with by the IR passes and lowering.<br>
<br>
But the biggest benefit of lowering to LLVM IR is safety.<br>
<br>
Lowering to C will actually be lowering to "strings", not proper C.<br>
Unless you write your own C validation engine (ie. a compiler), you<br>
can't know if you have lowered to valid C or not.<br>
<br>
The IR libraries (IRBuilder et al) give you that for free. This<br>
immensely simplifies the task of lowering your language, and you can<br>
focus on what really matters: the semantics of your language.<br>
<br>
Hope that helps.<br>
<br>
--renato<br>
<div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br></div>