[llvm-dev] What was the IR made for precisely?

mats petersson via llvm-dev llvm-dev at lists.llvm.org
Fri Oct 28 06:33:37 PDT 2016


On 28 October 2016 at 02:43, ジョウェットジェームス <b3i4zz1gu1 at docomo.ne.jp> wrote:

> Thank you for your answers.
>
> In fact I'm still hesitating between C and C++, sorry for my incomplete
> message. What I want to write is some sort of DSL; so I can afford both I
> think.
> I'm thinking about generating standard-compliant code using numeric limits
> and stuff; given that I don't care about compilation time or debugging, I
> think I can still use C++ in case I decide to include exceptions or other
> things hard to implement in C.
>
So, you basically don't care if you can debug your DSL at all? It's
actually quite nice to NOT have to add code to print value of X during
debugging, or type "bt" to see how you got to a crash-point - but I got a
long way in my compiler project before I started adding debug symbol
support. However, if you EVER want to add that, it's MUCH easier to achieve
that in LLVM-IR than in C - and if you machine generate C, it can be very
hard to follow what of the C code is what of the original language
(depending on how different they are, and what happens during the
translation - I have seen 50k lines in one function being the result of a
"generate C code" project, and none of the generated variables had sensible
names, they were just called a1, a2, a3, ... a9345 ... a12345 or some such
- there were some struct fields with names from the original input, but
everything else was just meaningless - it was also completely unformatted -
do try to format the output code for human readability, if you can).


> I understood from your replies that I still have a small part of target
> specific code to write, but honestly I don't know about all the main
> targets enough to do this correctly and efficiently I think. I would need
> to sum up all the rules and ABIs and sizes for all the targets I need and
> generate different IR for each, am I correct? I don't even know if there
> are many off such rules, or even where they are all defined precisely. I
> have used assembly and read processor manuals etc before but that was
> obviously never for writing cross platform code.
>

LLVM-IR is less target-specific than assembler, and more so than C or C++ -
the biggest difference is that uses specific types of 1, 8, 16, 17, 32, 36
or 64 bit integer types, so you need to KNOW what size integer you want for
some function call. If you are interfacing your DSL to some existing
libraries or OS's (and other such things), then perhaps a solution would be
to write an interface library in C or C++, that uses a fixed ABI, and let
the C compiler deal with the conversions there. This is how I've solved
such problems in my Pascal compiler for things like text and binary
file-I/O functions, some math functions, and so on, that are hard to
implement in Pascal, and also hard to write as LLVM-IR (not necessarily for
size and ABI functionality, but simply that "it's a fair bit of code to
write").

--
Mats
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161028/4691e14a/attachment.html>


More information about the llvm-dev mailing list