What’s a Compiler and Compilation?
A 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.
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.
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”.
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.
The compiler writer, like any software developer, would probably like to use modern software development environments containing latest version of tools such as language editors, debuggers, version managers, profilers, test harnesses, and so on. In addition to these general software-development tools, other more specialized tools have been created to help implement various phases of a compiler.
These tools use specialized languages for specifying and implementing specific components, and many use quite sophisticated algorithms. The most successful tools are those that hide the details of the generation algorithm and produce components that can be easily integrated into the remainder of the compiler. Some commonly used compiler-construction tools include
- Parser generators that automatically produce syntax analyzers from agrammatical description of a programming language.
- Scanner generators that produce lexical analyzers from a regular-expression description of the tokens of a language.
- Syntax-directed translation engines that produce collections of routines for walking a parse tree and generating intermediate code.
- Code-generator generators that produce a code generator from a collection of rules for translating each operation of the intermediate language into the machine language for a target machine.
- Data-flow analysis engines that facilitate the gathering of information about how values are transmitted from one part of a program to each other part. Data-flow analysis is a key part of code optimization.
- Compiler-construction toolk2ts that provide an integrated set of routines for constructing various phases of a compiler.
There is lot to be read and understand which we will discuss, as it require. Today I think we have covered at least basics, for in depth knowledge you need to do little more search on internet. Meet you in next write-up, don’t forget to like our facebook page and please subscribe to this blog to receive latest article information directly in your inbox.