4

A source code can be directly converted to machine code (100000). Then why do we need to convert it to IR (Intermediate Representation) assembly language?

What are the Pros and Cons if I cannot directly convert into machine language?

r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
  • 1
    There is no need to use an intermediate representation, but it certainly makes writing and porting compilers much easier. – fuz Oct 30 '18 at 16:38
  • 4
    The really hard part of a compiler is the code generator and optimizer. Parts that do not depend on the programming language implemented by the compiler. LLVM is the thousand pound gorilla, you don't want to have to re-invent it and do so for every processor architecture in common use. Also the approach taken by VM languages that run on the JVM or the CLR. – Hans Passant Oct 30 '18 at 16:59

1 Answers1

5

Then why do we need to convert it to IR (Intermediate Representation) assembly language?

You don't.

Text representations of IR exist for the purposes of debugging compiler internals. (Compilers normally transform through some internal representations, usually some form of SSA, while optimizing the logic of your program into efficient asm and then machine code.) See Disable all optimization options in GCC.


And for LLVM especially, IR is useful as a platform-neutral asm-like language that's useful for JIT compiling. Your portable application can generate LLVM-IR, and feed that to LLVM to get optimize machine code for whatever platform it's running on.

Or for an ahead-of-time compiler for a new language, it means you don't have to write an optimizing compiler that goes straight from source to machine code for every possible platform. So you get portability to multiple targets, and most importantly you get an optimizing back-end while only having to write a front-end that emits simple un-optimized LLVM-IR.

Writing an optimizing compiler is hard, but at least somewhat optimized machine code is basically necessary if you want anyone to use it for real work. See Why are there so few C compilers?


TL:DR; an IR makes it possible for one optimizing back-end to be used for many front-ends for different languages.

Johan
  • 74,508
  • 24
  • 191
  • 319
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847