
LLVM Tutorial - thewarrior
http://llvm.org/docs/tutorial/LangImpl1.html
======
c3d
Surprising that this topic has (as of now) 60 point and only two comments.

At Taodyne, we use LLVM as the foundation for our 3D dynamic document
description language. This makes it possible to do interesting things in real-
time, see e.g. [http://www.taodyne.com/shop/en/blog/58-live-coding-
creating-...](http://www.taodyne.com/shop/en/blog/58-live-coding-
creating-a-3d-dna-strand). LLVM is pretty powerful, and now relatively stable
as far as even the C++ API is concerned. In earlier days, having the C++ API
change underneath you in significant way at every release was a real pain.

That being said, I was not a real fan of their documentation in general, and
the Kaleidoscope tutorial was no exception. But it looks like they are putting
a lot of effort into this, and it looks much much better than it used to.

~~~
confluence
HN is one of those great communities where people usually don't comment when
they they aren't qualified to comment. It's either that, or they get down
voted to heck and hell banned if they continuously post low quality material.

It's probably more of the latter :)

The one problem I have with HN is the basic/extreme bike-shedding situation we
have here.

Articles on hiring, firing, culture, etc. (softer - more bs subjects) and the
singularity, complex economics and quantum physics (softer - more bs subjects
for laymen) seem to get a metric ton of crappy comments from people who aren't
qualified to talk about said topics and HNers seem to be much too nice (or
don't care) enough to point out the crap for what it is.

I try to avoid those types of posts nowadays.

------
Rickasaurus
The OCaml version of the same tutorial is so much nicer
<http://llvm.org/docs/tutorial/OCamlLangImpl1.html>

------
super_mario
Are there any books or resources that teach how you would go about using flex,
bison and llvm together to create something more substantial. I would find
this incredibly useful.

~~~
arcatek
You can check my WIP language for an example. The following is the repository
where every component (lexer, parser, compiler, running-time library) is in
its own module.

<https://github.com/castel/libcastel>

According to my experience on this language, I would recommand you ragel and
lemon over flex and bison. They are very, very easy to bundle into a C++
application, without messing too much with the global symbol namespace.

A nice example is the Castel lexer, which is actually a preprocessed C++
source file :
[https://github.com/castel/libcastel/blob/master/parse/source...](https://github.com/castel/libcastel/blob/master/parse/sources/lex/Lexer.rl)

~~~
super_mario
Thanks for pointing this out. I will definitely check them out.

------
program
Making my first toy language is one of the most interesting advertures as a
programmer. LLVM is quite complex and the documentation is far from stellar
but it's usable for teaching how a compiler works.

Also I like the restyling. LLVM landing page is so 90'.

~~~
pjmlp
One thing you might find interesting is to generate some kind of bytecode as
textual representation.

Then write Assembly macros that translate those bytecode into real Assembly
code.

This will give you a nice picture without much effort how code generation can
be done.

Surely the performance won't be stellar, but is a nice experience.

On the other hand you can plug it directly into LLVM, but there you get more
things to learn at the same time, which might slow you down.

------
igravious
I would love a tutorial about LLVM that used a decent proper subset of scheme
as its tutorial language and not a procedural language. Anyone know any?

~~~
alexchamberlain
There's an OCaml version of the same tutorial...

~~~
igravious
That's not what I meant. Maybe I was unclear. I mean that instead of the
makey-uppy Kaleidoscope procedural langauge a makey-uppy mini scheme was used
instead, that is to say KaleidoscopeScheme ...

~~~
mahmud
Of course, pedagogy goes out of the window and we enter the realm of magic
once we try to implement continuations.

Scheme without continuations is no different than any other simple Algol.
(minus GC, numeric tower, macros, etc. of course)

------
tehwalrus
Thank you, I'd not seen this before.

I have always been interested in compiler design having not been put off it by
doing a computer science degree, but, having not done the degree I've also
never learned anything about it. This looks like it should help.

Although, I was very confused by the first couple of sentences - the author
apologises for their global variables, and not knowing which part of the
compiler they're talking about I don't know if they means they're going to
implement kaleidoscope without any notion of _scope_ , and just make
everything global, or if they're just talking about the parser code. It's
quite clear soon after that it's the latter, but it makes it difficult to read
the introduction!

------
nextparadigms
LLVM seems to support 64 bit ARM, now, too:

[http://www.phoronix.com/scan.php?page=news_item&px=MTI4O...](http://www.phoronix.com/scan.php?page=news_item&px=MTI4OTA)

~~~
Symmetry
I do wonder what on earth ARM was doing that was preventing them from
including NEON support, though.

~~~
jevinskie
LLVM now has NEON support. Was this an issue in the past?

~~~
Symmetry
Well, the addition of LLVM support referenced in the Phoronix article had this
bit:

"This patch excludes the NEON support also reviewed due to an outbreak of
batshit insanity in our legal department. That will be committed soon bringing
the changes to precisely what has been approved."

[http://comments.gmane.org/gmane.comp.compilers.llvm.cvs/1349...](http://comments.gmane.org/gmane.comp.compilers.llvm.cvs/134902)

I'm just wondering what that was about and how serious it was.

------
seivan
Amazing, thank you so much. This is so interesting. I've always wanted to look
into building something small.

------
gosukiwi
Uh... I'm not good at C++ :(

~~~
freyrs3
There's a Python version here: [http://www.llvmpy.org/llvmpy-
doc/dev/doc/kaleidoscope/index....](http://www.llvmpy.org/llvmpy-
doc/dev/doc/kaleidoscope/index.html)

~~~
lloeki
Interesting, I'm read through the tutorial, and took the source, while
tweaking it to conform to RPython, then feeding it into PyPy's translator.

What strikes me is that:

\- with PyPy everything is implemented natively, i.e without resorting to
foreign abstractions: instead of _def CodeGen_ , I'd write _def eval_ (or
whatever my native AST evaluation wants), and instead of
_g_llvm_builder.fadd(left, right)_ , I'd just do the native _left+right_. This
makes it conceptually less of a mind split, and writing stuff like the BF
interpreter is just straightforward [0].

\- with LLVM, the concepts are nicely abstracted, and the API reasonably
pythonic. I know I'm using LLVM but I don't feel like I'm hitting some thin C
wrapper. Those concepts are ported and reinvested into other languages with
ease: now that I got them by reading the python tutorial, I'm confident I can
find my way in another project on another, less known to me language. Also,
due to this abstracted common ground, access to functions coming from outside,
(possibly other languages) is made easy. With PyPy I'm stuck to what's made
available to RPython land.

[0]: [http://morepypy.blogspot.fr/2011/04/tutorial-writing-
interpr...](http://morepypy.blogspot.fr/2011/04/tutorial-writing-interpreter-
with-pypy.html)

~~~
freyrs3
Your observations are pretty accurate. I'm fortunate enough to work with the
maintainer of llvmpy and he's done some great work in making llvmpy pretty
Pythonic. Should also check llvm-cbuilder the EDSL for building up higher
level LLVM constructs:

[https://github.com/llvmpy/llvmpy/blob/master/README_LLVM_CBU...](https://github.com/llvmpy/llvmpy/blob/master/README_LLVM_CBUILDER.md)

------
marblar
Is the site down for everybody else?

------
lenkite
llvm.org has been down nearly all day.

------
pretoriusB
OK, here's a question: is this the same version that was around for half a
decade, or is it updated to the latest LLVM API etc?

~~~
sanxiyn
It is constantly updated to the latest LLVM API. Ok, it is not promptly
updated, but it is updated.

------
hackelly
Je voudrai hacker un compte moviestarplanet

