20

I am looking into the LLVM system and I have read through the Getting Started documentation. However, some of the nomenclature (and the wording in the clang example) is still a little confusing. The following terms and commands are all part of the compilation process, and I was wondering if someone might be able to explain them a little better for me:

  • clang -S vs. clang -c (I know what -c does, but how do the results differ?) * (Edit)
  • LLVM Bitcode vs. LLVM IR (what is the difference?)
  • .ll files vs. .bc files (what are they, how do they differ?)
  • LLVM assembly code vs. native assembly code (is there a difference?)

At a higher level, I understand the overall compilation process, and can track my way through fairly well, I just get stuck at some points where, for example, I am expecting to see "IR", but instead see "bitcode" or "LLVM assembly" which leads me to think I don't understand them nearly as well as I should!

Ephemera
  • 8,672
  • 8
  • 44
  • 84

1 Answers1

32

Clang usage

In general, Clang accepts the same command-line options as GCC. The -c option (only compile and assemble, do not link) and -S option (only compile, do not assemble or link) mean the same thing in both.

LLVM terms regarding the Intermediate Representation

To quote from another answer of mine on this site:

LLVM IR is typically stored on disk in either text files with .ll extension or in binary files with .bc extension. Conversion between the two is trivial, and you can just use llvm-dis for bc -> ll and llvm-as for ll -> bc. The binary format is more memory-efficient, while the textual format is human-readable.

In additional, there are some commonly-used aliases:

  • The binary format, stored in .bc files, is also called bitcode (though I've occasionally heard the term "bitcode" applied to the general IR as well)
  • The IR also called LLVM assembly or the LLVM assembly language

In any case, it all means the same thing, under potentially different representations.

Native Assembly

Native assembly is what many typically think about when hearing the term "assembly" - the low-level language with almost 1:1 mapping to your native machine binary, and unlike LLVM assembly, native assembly is very target-dependent (examples are x86 assembly, ARM assembly, etc.). Native assembly is assembled into native binary via an assembler - LLVM does include one, though you can also use other assemblers as well (e.g. gas).

Native binary - the result of the assembling process - is of course the (only) language the computer really speaks, and after linking it can be loaded into memory and be ran directly on your hardware.

Community
  • 1
  • 1
Oak
  • 26,231
  • 8
  • 93
  • 152
  • +1 Though you may want to differentiate LLVM assembly from "native assembly" too. –  Jan 01 '13 at 07:31
  • @delnan right, missed that in the question. Edited to also include something about it, thanks! – Oak Jan 01 '13 at 07:59
  • `The binary format is more memory-efficient`, is it because that the `bc` file is always smaller than the `ll` ? BTW, I'm wondering whether there exists a pass that serialize `ll` into `bc` format or directly into a representation that resides only in memory when loaded(such as using `opt` tool). – Hongxu Chen Jan 01 '13 at 16:30
  • 9
    @HongxuChen write down "three" and "3" on paper. Now read what you wrote aloud. It's being read the same and it means the same thing, but there are two different representations on paper for it, one taking more paper area than the other. It's the same with LLVM IR. Anyway there's no pass to convert one to the other because when loaded into memory from LLVM you get the exact same thing. – Oak Jan 01 '13 at 16:41