Docsity

compiler-construction-intro, Summaries of Compiler Construction

Toggle sidebar
bg1
Compiling to Assembly vs. Machine Language
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.
The Advantages of C as a Target Language
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.
Tasks of an Assembler
Once the compiler hands off the assembly code, the assembler takes over. Its primary jobs
are:
1. Mnemonic Translation: Converting literal commands like MOV or ADD into their
corresponding binary opcodes.
2. Symbol Resolution: Replacing labels (like jump_to_start:) with actual memory addresses
or offsets.
3. Data Conversion: Converting constants (like the decimal number 10) into the binary or
hex representation required by the CPU.
4. Instruction Formatting: Packing the opcodes and operands into the specific bit-
structures the processor expects.
pf3
pf4

Partial preview of the text

Download compiler-construction-intro and more Summaries Compiler Construction in PDF only on Docsity!

Compiling to Assembly vs. Machine Language

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.

The Advantages of C as a Target Language

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.

Tasks of an Assembler

Once the compiler hands off the assembly code, the assembler takes over. Its primary jobs are:

  1. Mnemonic Translation: Converting literal commands like MOV or ADD into their corresponding binary opcodes.
  2. Symbol Resolution: Replacing labels (like jump_to_start:) with actual memory addresses or offsets.
  3. Data Conversion: Converting constants (like the decimal number 10) into the binary or hex representation required by the CPU.
  4. Instruction Formatting: Packing the opcodes and operands into the specific bit- structures the processor expects.

Object Code vs. Machine Code

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

1. Lexical Analyzer (Scanner)

โ— 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.

6. Code Generator

โ— 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.

7. Machine-Dependent Code Optimizer

โ— 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).