Ultimately a compiler takes in source code which is written in a high-level language and converts it to machine code. However this is a non-trivial exercise as unlike with assembly code there is no easy one to one conversion between high-level languages and machine code. Therefore many stages have to be individually performed by the compiler before outputting anything useful. Notably due to the nature of most coding languages allowing referencing to various sections of code throughout a program, often a compiler will have to pass over the code multiple times to accurately understand what is happening.
It is also important to understand that compilers do not exist in a vacuum. They are actually just one part of the overall language processing system. In general after a compiler has actually been through the code and produced the assembly language version of the original program, there are still a few stages that remain before the code itself is actually usable:
- An assembler is required to convert the assembly code into machine code which will actually be readable by the computer. This process is minimal in effort but still notable as most compilers will output in assembly rather than machine code.
- A linker is a very important part of this process as it collates together the various files associated and referenced within the program and makes sure they are all present and accessible. It will generally attempt to collect this data into a single executable file where possible. It also deals with memory allocation and ensuring that memory is available.
- The loader loads the program into the machine's available memory and calculates the size of the program itself. It is generally a part of the operating system, which means the surrounding parts of the system need to be able to communicate with it and account for the specific demands of the loader.
This shows that the actual process of getting any code to run on a machine extends beyond the compiler itself. This is a relevant consideration when thinking about the optimisation of running code through any machine. It is even more interesting when you consider that each of these additional processes, the linker, the loader and the assembler each have to at some point have been compiled in some form or another. This means that somewhere in the past they were all written in machine code of some form, likely built up over time as I described in my last blog post. My next blog post will go into greater detail about compilers themselves, which are the most complex part of this language processing system. I will look into breaking down the various phases the compiler goes through and why each of them is important.
References
1. Bolton D. What is a Compiler? [Internet]. About.com Tech. [cited 20 October 2016]. Available from: http://cplus.about.com/od/introductiontoprogramming/p/compiler.htm
2. Compiler Design Tutorial [Internet]. www.tutorialspoint.com. [cited 20 October 2016]. Available from: https://www.tutorialspoint.com/compiler_design/index.htm
No comments:
Post a Comment