


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Most compilers choose to output assembly language as an intermediate step instead of jumping straight to binary (machine language). The main advantages include: โ Easier Debugging: Assembly is human-readable. If the compiler has a bug or the code isn't performing as expected, developers can inspect the assembly to see exactly what the compiler is doing. โ Simplified Compiler Design: By producing assembly, the compiler creator doesn't have to worry about the complex binary formats of specific operating systems. They can leave the "heavy lifting" of bit-level formatting to the assembler. โ Optimization Hooks: It allows for an additional layer of optimization. A specialized assembler or a human expert can tweak the assembly code before it becomes a final executable.
C is often called "portable assembly." Using it as the output for a source-to-source translator offers several "cheat codes" for developers: โ Portability: Almost every hardware architecture in existence has a highly optimized C compiler. If you translate your new language into C, your language can suddenly run on everything from a supercomputer to a microwave. โ Built-in Optimizers: Writing an optimizing compiler is incredibly difficult. By targeting C, you get to use the world-class optimization engines of compilers like GCC or Clang for free. โ Infrastructure: You gain immediate access to existing C debugging tools, libraries, and linkers, saving years of development time.
Once the compiler hands off the assembly code, the assembler takes over. Its primary jobs are:
These terms are often used interchangeably, but there is a subtle, crucial difference in the "readiness" of the code. Feature Machine Code Object Code Definition Pure binary instructions ($0$s and $1$s) executed directly by the CPU. The output of a compiler/assembler that is not yet a full program. State Complete. It is linked and ready to run as an .exe or app. Incomplete. It usually contains "unresolved references" (calls to other files/libraries). Usability Can be loaded into memory and executed immediately. Must be processed by a Linker to become machine code. In short: All machine code is binary, but not all "object code" is ready to be machine code until the linker stitches the various pieces of your project together. Phases of a Compiler
โ What it does: Reads the raw character stream of the source code and groups them into meaningful sequences called lexemes. โ How it does it: It uses regular expressions to identify patterns. โ Checks/Sub-steps:
โ Constant Folding: Evaluating expressions at compile-time (e.g., changing 2 + 2 to 4 ). โ Dead Code Elimination: Removing code that can never be reached or executed.
โ What it does: Maps the intermediate representation into the actual target-machine code (usually assembly or machine code). โ How it does it: It selects specific machine instructions and assigns variables to CPU registers. โ Checks/Sub-steps: โ Register Allocation: Deciding which variables stay in the fast CPU registers vs. slower RAM. โ Instruction Selection: Picking the most efficient CPU instructions for the task.
โ What it does: Performs a final round of "polishing" based on the specific quirks and strengths of the target hardware. โ How it does it: It analyzes the generated machine code for local improvements. โ Checks/Sub-steps: โ Peephole Optimization: Looking at small windows of instructions to replace them with shorter, faster sequences. โ Utilizing specific CPU features like hardware-level parallel processing (SIMD).