A Compiler is a computer-based program that translates coding statements or code written in one programming language to another programming language that the computer processor can understand. It is a computer software that compiles a source code written in a higher-level language like C, C++, Java, etc. into a set of programming instructions or lower-level language that can be understood by the computer’s processor and based on which then various functions are carried out by the digital machine. Compilers are very large programs with the ability of error-checking and various other functions. Some compilers compile high- level language into low level language directly but then there are some compilers that translate higher-level language into an intermediate assembly language and then this intermediate language using some set of assembly programs or assembler is compiled into lower-level language or machine code.
How does Compiler work?
A Compiler is a program that takes up a higher-level source code and transforms it into a lower-level executable machine code file. When a compiler is run on a code, the pre-processor first reads the source code. It searches for any pre-processor directives (lines starting with #). This is because the pre-processor directives cause the processor to change your code in some specific way.
After this, the compiler works through the pre-processed code lines thus translating each line of code into an appropriate machine language instruction. Here all the parts of compiler take up their action step by step. They check for any errors in the source code and if found will throw an error to the command line. Then if no errors are found then the optimization and translation of the intermediate program code into assembly code takes place. Here, the compiler creates an object file with the machine language binary language necessary to run on the digital machine. The compiler builds all the blocks of the source code but a linker is required that sticks all of them together to finally combine all the object files with the necessary library functions necessary to run the code.
Parts of Compiler
A Compiler comprises of three main parts:
-
Front-End
The function of the frontend part is to check whether the program is written correctly in terms of the syntax of the programming language and the semantics. In short, the frontend has to recognise whether the program is legal or illegal as per the programming language syntax and various semantics. Here errors are recognized and reported, if any present in the program. Also, at this level type-checking is performed by collecting type information. Once everything is recognised by the frontend part as legal, then the frontend generates an intermediate representation of the source code or the program for processing by the middle-end. -
Middle-End
Once the source code representation is processed by the frontend, then optimization of the code has to done. This is what happens in the middle-end. At this stage, transformations for optimizations take place. Typical transformations for optimization are relocation of computation to a less frequently executed place, removal of useless or unreachable code, specialization of computation based on the context, or discovery and propagation of constant values. After all the transformations are done at the middle-end stage, then another set of intermediate representation of the source code is generated for processing by the backend. Almost all the optimization processes are carried out at the middle-end part. -
Back-End
At this stage, the main function is to translate the intermediate representation of the source code received from the middle-end part into an assembly code. The target instructions are chosen for each IR instruction. Also, variables are selected for the registers. At the backend stage, hardware is utilized by figuring out how to keep parallel FUs busy, by filling delay slots and so on. Most algorithms for optimization are in NP, yet heuristic techniques are well-developed.
Advantages of Compiler
- A Compiler creates an executable code program. Since, an executable program file cannot be easily modified, most programmers do prefer supplying executables rather than the source code itself.
- The compiled program can be optimized. Optimized program codes do run quickly and consumes less memory space.
- Also, in the compiler, the programs after the execution are completed works faster than as compared to when done through the interpretation.
- The execution time is less as the program is already compiled into machine code.
- Compilers are capable of producing much more efficient object code than interpreters thus making the program translated through compiler to run faster in real time.
- They are self-contained units that are ready to be executed.
Disadvantages of Compiler
- It does not execute any instructions of the program source.
- In the compiler, each instruction is compiled only once.
- A compiler translates a source code into a specific machine language due to which programs have to be specifically compiled for different operating systems.
- Small programs may take less time to get copied but when larger application programs are taken into consideration, the time taken for compiling could be much longer than expected.
- Each and every time the programmer changes the program, the source code can be re-compiled.
- The machine code is very specific to the processor’s architecture due to which the source code compiled on one platform will not run on any other platform.