Code Compiler: What Does It Do in Programming

When we write a program, we usually write it in a programming language. This can be low level (if it is closer to machine language than ours) or high level (when it has a simple syntax). However, no matter how low the level of the programming language we use, machines still do not understand our instructions as such, since they work with binary orders. Therefore, if we want our program to work, it is necessary to use a compiler .

What is a code compiler

A compiler is, roughly speaking, a translator . It is responsible for transforming the source code of the program that we have created (generally using a high-level language), along with all the dependencies and libraries necessary so that the program can run and function without problems, into a binary file. This compiler is in charge of verifying that there are no errors in the code (to avoid critical errors), as well as analyzing and grouping the syntax and structure of the program. If everything is correct, it passes it to a common intermediate language, assembler, to later optimize it and translate it into binary language . In this way, through the operating system, the program can be run on the machine.

Code Compiler: What Does It Do in Programming

Compilers do not normally pass the instructions from our source code directly to binary code, but usually carry out their tasks in 3 phases.

Analysis phase

The first one is the analysis phase . In this phase, the compiler is in charge of analyzing that the structure and semantics of the program are correct to generate an intermediate code (bytecode). The lexicon of the code is analyzed to group all the components that have a meaning for the program. This step, for example, is where all unnecessary information, such as comments or spaces, is removed. The parsing begins next. In it, the lexical components are grouped hierarchically into grammar phrases, which will be used to create the output of the program. And finally, semantic analysis takes place. In it, based on the hierarchical structure above, the compiler looks for possible errors in the code to avoid critical failures in the system. When everything is correct, then the synthesis phase begins.

Synthesis phase

The synthesis phase is responsible for generating the object code from the source code. This phase only begins when the analysis phase has not given any errors, thus avoiding possible problems both in the compilation and in the execution of a corrupt program. The object code is almost always in assembly language, one of the lowest-level languages that we can find. But it is not in binary yet, so a last step is necessary, which is known as optimization.

Optimization phase

Starting from the object code, the compiler begins the optimization phase . What you do in this phase is interpret the code and look for possible optimizations so that the instructions are as short as possible and can be executed more quickly on the computer. Different types of optimization can be executed depending on whether we want a less optimized program, but that compiles faster, or more optimized but that takes much longer to compile.

Esquema compilar programa

Compiler types

Not all code compilers are the same. Initially, in the early decades of the computer age, compilers were the most complex programs we could find. Typically, programmers used assembler, or directly binary, to create these tools. Nowadays things have changed a lot and, although they are still very complex elements, they are not really that complicated to create or update to improve them.

There are several types of compilers. And each compiler can belong to one or more groups:

  • Crossed : are those that are designed to generate a code to run on an operating system other than the one that is running. For example, if we compile an Android program from Windows.
  • In a single pass : they are responsible for generating the machine code (binary) from a single reading of the source code. They do not normally make use of advanced optimizers or other intermediate stages.
  • Multi-pass : they need to give the code several passes to check that everything is correct and optimize it before producing the machine code.
  • JIT (Just In Time) : compile the code in real time as needed.
  • Optimizers : make changes to the code to improve the performance of the program and optimize it, but without spoiling the functionality of the original program.

How to compile a program

Operating systems, and programming IDEs, have their own compilers. For example, in Linux we find a well-known tool called ” Make “. which is used to compile code from the terminal without having to write long and complex commands in the system console. When we have a complex program, it is able to know which parts of the code have changed to collect only what is necessary instead of the whole program.

If we use a programming IDE, such as Visual Studio , it will have its own compilers to shape the programs we write in C, C ++, C # or Visual Basic. Other programming IDEs, such as Android Studio, have their own compiler that allows us to create executables to be able to run apps on Android.

Then we can also find third-party compilers that help us shape programs that do not include their own compilers. This is common, for example, if we use Visual Studio Code, Microsoft‘s OpenSource IDE, to create programs. Unlike its older brother, this one does not come with its own compilers, so we will have to download an alternative, such as MinGW , that allows us to compile code.

The debugging console: the programmer’s best ally

The compilation process is usually automatic (we cannot interfere with it) and, usually, invisible. However, both compilers and IDEs have debugging environments that can be very useful when it comes to detecting and repairing errors that we may have made.

Thanks to these debugging consoles we will be able to control one by one the instructions that are executed to generate the machine code of the program. The most normal thing is to show only the warnings and errors, since everything that compiles correctly does not add any value. If there is an error, the console will tell us exactly where it happened (and many times why) so that we can easily fix it. The same every time a warning appears. These do not have to stop the compilation of the program, but they can help us to optimize the operation of the program.