Thursday 12 September 2013

What is Compiler and Compilation

Compiler and Compilation :
compiler is nothing but a normal program which translates a source program written in some high-level programming language (such as C, C++, Java and etc.,) into executable files which can be understood by some CPU architecture (such as Intel, ARM, AVR, Microchip etc). For this article I have been searching a lot on net, surprisingly there is limited information available with this regard. But Wikipedia and Aho’s famous book I have used as reference.
The process we are doing to get the binary files( executable files) is nothing but “Compilation”.
What’s Cross Compilation?
Before we discuss about Cross Compilation, we should learn about what is compilation and what is compiler. Then we will go for cross compiler and cross compilation.
First we talk about something general in developing field.

Suppose if you are developing a PC application or server application, then development platform and the target platform are the same.
Here first we discuss about two terms in above line.
1. Development Platform
2. Target Platform
Before going to know about these, we need to know a tickle about the word ‘Platform’ what we used here. What I mean to say about ‘Platform’ is just nothing but the combination of CPU architecture and Operating System.
Now, come to Development Platform, it’s a machine which runs your compiler. With this, you will guess Target Platform. It’s a machine that runs your application. An embedded developer understands this very well.
Native Compiler:
As we said at first, desktop developers used to build the binaries for windows or Linux and to run those applications they will use the same machine. The compiler which is used in this case is called as “Native Compiler”.
Cross Compilation:
The process of building executable files on one machine and run them on another machine when the CPU architecture or the Operating system are different is called “Cross Compilation”.
A special program which is used to do the cross compilation process is nothing but “Cross Compiler”.  Most of the people defined it as Tool Chain.
Just take and example, we are developing the applications for a mobile phone running on some X micro controller. So, we write the program in PC and build that source code (Now a day’s most of the people trying this using Android ADK). After compiling this, we dump the code onto the target machine (our micro controller or mobile) and then we run the application. So, when we are developing the applications on one platform for running it on another platform, with host compiler getting executable files to run on the target is not possible. But with the help of cross compiler the suitable executable files to run on the target is possible.
A compiler consists of three main parts: the frontend, the middle-end, and the backend.
The front end checks whether the program is correctly written in terms of the programming language syntax and semantics. Here legal and illegal programs are recognized. Errors are reported, if any, in a useful way. Type checking is also performed by collecting type information. The frontend then generates an intermediate representation or IR of the source code for processing by the middle-end.
The middle end is where optimization takes place. Typical transformations for optimization are removal of useless or unreachable code, discovery and propagation of constant values, relocation of computation to a less frequently executed place (e.g., out of a loop), or specialization of computation based on the context. The middle-end generates another IR for the following backend. Most optimization efforts are focused on this part.
The back end is responsible for translating the IR from the middle-end into assembly code. The target instruction(s) are chosen for each IR instruction. Register allocation assigns processor registers for the program variables where possible. The backend utilizes the hardware by figuring out how to keep parallel execution units busy, filling delay slots, and so on. Although most algorithms for optimization are in NP, heuristic techniques are well-developed.

1 comment: