Compilers

Akuluru Nikhila
3 min readAug 21, 2022

--

In this article I have written the overview of the compilers and stages involved in conversion of high level code to machine code.

okay, let us start it! Compilers are one of the very few types of software that become as complex as operating systems. Compilers are daunting because they require a deep understanding. I believe this basic understanding is important to all developers. Developers make more efficient software when they know how their tools work .

A compiler, the software that is usually related to a programming language, translates high-level, human-readable programming code to ones and zeros, known as machine code. A transpiler follows a similar process, but instead of producing an executable software, it chooses to produce code for another human-readable language.

Interpreters are like compilers, but require the program source code to execute the program as it compiles it. These typically produce better error messages due to the available high-level program code. Though, compilers commonly make faster programs than interpreters.

Okay! Coming back to the Compilers.Translation of compilers is divided into three major stages Frontend, optimization and backend.

Frontend :

  1. Lexical Analyser:

Lexical Analyser also known as Scanners reads the source program one character at a time, carving the source program into a sequence of automatic units called tokens.

examples : variable, operator, keyword, constant

2. Syntax Analyser:

The second stage of translation is called Syntax analysis or parsing. In this phase expressions, statements, declarations etc… are identified by using the results of lexical analysis. Syntax analysis is aided by using techniques based on formal grammar of the programming language.

3. Semantic Analyser:

Semantic analysis is the task of ensuring that the declarations and statements of a program are semantically correct, i.e, that their meaning is clear and consistent with the way in which control structures and data types are supposed to be used.

Optimization

The optimization phase of a compiler tries to improve the generated code in terms of execution time and/or memory usage.

Optimization is a complex task and is often done using heuristics rather than an exhaustive search. The optimization phase can make the generated code worse rather than better.

Optimization can be done at local, regional and global level.

Back end

Instruction Selection:

Selection of proper instructions from given instruction set Architecture is known as instruction selection. This can be done via Tree pattern Matching and Peephole optimization.

Instruction Scheduling :

Scheduling the selected instructions for proper utilisation of functional units and this is used to improve instruction level parallelism.This can achieved via local list scheduling at local level or Regional scheduling.

Register Allocation:

Maps an unlimited namespace onto that register set to the target machine.

  • Register to Register Model: Maps virtual registers to physical registers but spills excess amount to memory.
  • Memory to Memory Model: Maps some subset of the memory location to a set of names that models the physical register set.

Allocation ensures that code will fit the target machine’s register set at each instruction.

--

--