18

What are the differences between an LLVM and a regular compiler?
Is it more dynamic and thus can be used to compile normally very dynamic languages (i.e. Javascript) into static binary code? What are the principles behind creating one?
I know the Dragon Book for compilers, but is there such a thing for a LLVM?

EDIT: I have found this interesting project.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
the_drow
  • 18,571
  • 25
  • 126
  • 193

3 Answers3

26

There are a couple of difference between LLVM and "a regular compiler", which I'll assume to mean "gcc":

  • LLVM is designed for whole-program analysis (aka link-time analysis), so it can optionally compile code to "bitcode", a format that it can re-analyse later.
  • LLVM provides a just-in-time compiler (JIT) so that it can re-analyse programs while they are running, just like the JVM does.
  • LLVM is very well designed:
    • its components are modular and well separated,
    • it has 3 formats for its intermediate representation (textual, binary, and an in-memory representation), which are equivalent,
    • its intermediate representation uses SSA form,
    • its intermediate represenation has a type system.

As for Javascript and other dynamic languages, we're seeing a lot of interesting in LLVM from the dynamic language community, with Python and Ruby implementations trying it out. However, these are not attempting to be static compilers. They are focussed on using the JIT. In particular, the are optimizing long running executables using a "mixed mode interpreter", where they initially interpret the programs, and then compile them using LLVM at run-time. I haven't seen a javascript engine using LLVM, but there probably is one. It just won't create static executables, except in unusual circumstances, or for cut down versions of Javascript.

As for the reason for the creation of LLVM, it started as part of Vikram Adve's research group's work on life long compilation (which means JITs and link-time optimization). After his PhD, Chris Lattner moved to Apple, which is moving the project forward greatly (probably because it is BSD licenced, which has caused them problems in the past with gcc, which is GPL).

reinierpost
  • 8,425
  • 1
  • 38
  • 70
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
  • Curious, what sort of problems did gcc cause Apple? – mt3 Dec 18 '10 at 16:08
  • 4
    @mt3: My understanding is Apple wanted to create extensions to gcc which it did not want to open source, but they were forced to under the GPL. I can't think where I learned this (I wasn't around when it happened, certainly), however [this article](http://www.groklaw.net/article.php?story=20100806143457345) desctibes a similar problem (search the transcript for 'LLVM'). – Paul Biggar Dec 19 '10 at 03:49
  • 1
    The third format of intermediate representation is an in-memory representation. All the three are isomorphic and LLVM provides tools (llvm-as and llsvm-dis) to convert the textual representation to the on-file binary file and vice-versa – Gangadhar Jun 04 '11 at 07:31
5

Nothing it is a regular compiler. Its primary reason for creation was to create a platform for compiler research. Therefore it is designed to be very modular so that you can work on that part of the compiler that deals with your research and not have to worry about other parts of the compiler. There is no dragon compiler just as there is no LLVM book(any theory you read in the dragon book or any other compiler book should be directly applicable). In fact while I haven't looked in on LLVM in a while their documentation was pretty poor.

stonemetal
  • 6,111
  • 23
  • 25
2

I am not developper at all, but : even if gpl forced NeXt to publish its ObjC runtime in the 80's, the main reason for apple to fund another compiler (GPL or not) is its will to integrate with the IDE. GCC proved to have too much inertia in this area, and GCC is not modular enough to enable reuse of code parts (xcode "fix it" feature). That's what I understood from my readings

tetatoto
  • 21
  • 1
  • @toolbear: I don't remember at which location, but I've additionally read an article expressing similar Apple concerns. For what it's worth, this is not brand new research. – J. M. Becker Jul 15 '12 at 18:34