News for hackers. Stuff that matters.
Re:Clang/LLVM using the built in assembler builds all of Fre... by StavrosK on Saturday October 30, 2010 @08:04AM (#1850703)
Can someone explain why LLVM is better than native code (if it is)? If it's not, what's the use for this?
Re:Clang/LLVM using the built in assembler builds all of Fre... by yan on Saturday October 30, 2010 @08:41AM (#1850754)
That's really comparing apples to oranges.

LLVM can compile to intermediate code in order to make code optimizing passes more generic and better-defined, to cleaner separate compiler components and phases, to better abstract multiple architectures and to impose restrictions that make some analysis and optimization easier (like enforcing Static Single Assignment for forms), you can create a VM that runs IR directly and distribute that, among many other reasons. So IR does eventually make it to machine code, otherwise it won't be able to do anything.

Why LLVM is better than GCC is a different question. It's better, in my opinion, for being a much cleaner, more well-architected, modern, and much much better modularized code base. Also, I am a happy person when I read Clang docs on parsing vs when I read GCC docs on how to manipulate GIMPLE trees. There are also more objective metrics, like the front end being a very nice, portable library for parsing C-derived languages and exposing more functionality via libclang. (This makes very code-aware IDEs more possible and much easier to write, the use of C syntax for custom work, making custom code scanners and analyzers far easier, etc, etc)

Find more out at http://www.llvm.org

Re:Clang/LLVM using the built in assembler builds all of Fre... by StavrosK on Saturday October 30, 2010 @09:19AM (#1850796)
Thank you, I thought it was just the vm, but apparently it's a whole suite of compiler tools.
Re:Clang/LLVM using the built in assembler builds all of Fre... by Tuna-Fish on Saturday October 30, 2010 @10:35AM (#1850925)
Just to extend a little: Nearly every widely used (non-lisp) compiler that exists first compiles the program into some sort of intermediate representation that is easier to manipulate and optimize, works on it a while in this form, and then finally compiles that into machine code.

The level of this intermediate representation varies between compilers -- GCC uses a very simplified and strict form of c, while many others use an IR that is closer to assembly. LLVM is not really a competitor to JVM or .NET, but an attempt to define an unified IR that many languages can compile into, be optimized in, and then be compiled into various different machine codes. So that when you write a compiler for a language you only have to write the top layers until your code turns to IR, or if you make a compiler for new platform you only have to write the bottom layers to turn IR into machine code, and in both cases you get free use of all the various IR-level compiler optimizations that have been already built into LLVM.

Re:Clang/LLVM using the built in assembler builds all of Fre... by illumen on Saturday October 30, 2010 @10:58AM (#1850970)
Indeed.

tinycc is one of the only compilers that directly translates to machine code. This makes it compile very quickly, makes the code base smaller, and as a bonus allows it to be a pretty decent c script 'interpreter'. But it also means that it does not optimise as well.

Tinycc was originally submitted as the winning entry in an obfuscated C code contest - but is surprisingly readable now. Worth a read.

Re:Clang/LLVM using the built in assembler builds all of Fre... by sedachv on Saturday October 30, 2010 @02:26PM (#1851465)
Most Lisp compilers have intermediate representations too, although these IRs are still technically Lisp code (although they may not be executable). There's a really good series of blog posts about the IR of the SBCL compiler by Roman Marynchak:

http://insidelisp.blogspot.com/search/label/python-book

Re:Clang/LLVM using the built in assembler builds all of Fre... by pjscott on Saturday October 30, 2010 @03:38PM (#1851613)
It sometimes fun to take some random Lisp code and fully expand the macros:

    (macroexpand '(loop for x in list minimize (sin x)))
The output depends on your Lisp compiler, but it can get pretty wild. This is why we have the macroexpand-1 function, to just expand the first macro it finds and stop before all collapses into madness.

A compiler writer could get a lot of mileage out of this sort of thing. For example, a LET special form could be implemented as a macro that expands to a call to an anonymous function. All the looping forms could be turned into lambdas and recursion. As long as the compiler can handle that sort of thing nicely, this could really simplify the compiler code.

Re:Clang/LLVM using the built in assembler builds all of Fre... by StavrosK on Saturday October 30, 2010 @07:58PM (#1852098)
That clarifies it very much, thank you. From what other commenters said, I gather that you can also use it as an interpreter for the IR, which is, again, very interesting.
Re:Clang/LLVM using the built in assembler builds all of Fre... by eliben on Saturday October 30, 2010 @10:35AM (#1850926)
You can't compare LLVM to native code, since these are different things. LLVM IR can be compiled to efficient native code if that's what you want. In fact, this is what happens when you use clang+llvm to compile code into native executables.

LLVM is much more than that, however. You might want to use it as a JITting interpreter of the IR instead of as a native compiler with all the benefits that may result from this. You may also use it as a sophisticated optimization framework (including cross-object, link-time optimization) taking IR and giving (optimized) IR back.