What is a compiler?
A compiler is a program that translates source code written in a high‑level language into low‑level instructions that a microcontroller or processor can execute. In the IAR toolchain, the compiler is one of the two translation tools that converts C/C++ source files into relocatable ELF object files containing machine instructions and DWARF debug data. In other words, a compiler takes a program like x = y - 15;, analyses its syntax and semantics, and produces the equivalent machine‑code representation.
The compiler system image shows this build flow, multiple C sources go into the compiler to produce object files, and the linker then combines those objects with the C library and any third-party modules to create the final executable for the hardware.
What does a compiler do?
To perform this translation, a compiler goes through several internal stages (these stages may have different names in different literature):
-
Lexical analysis and parsing: The compiler reads the source text, breaks it into tokens, and builds a syntax tree. The image below shows this as the Parser stage.
-
Generation of an intermediate representation: The parsed program is mapped to a machine‑independent “Intermediate Code”. High‑level analyses track data‑flow and control‑flow to understand the program.
-
High‑level optimizations: The intermediate code is transformed to improve performance or reduce size. The image lists optimizations such as function inlining, dead‑code elimination, loop unrolling, scheduling, peephole optimizations and cross‑call elimination.
-
Code generation and low‑level optimizations: The compiler generates target‑specific code and performs optimizations that depend on the instruction set (register allocation, instruction selection and scheduling).
-
Assembly and linking: The generated code is assembled into object files. A linker later resolves references and combines multiple object files into a single executable.
What makes one compiler “better” than another?
Important differentiators between compilers include:
-
Quality of generated code: A good compiler produces fast and compact machine code. IAR delivers outstanding performance through sophisticated optimization technology, and the optimizer performs aggressive analyses and transformations (dead‑code elimination, inlining, loop unrolling, etc.) that can significantly reduce code size and execution time.
-
Support for multiple architectures: A compiler should generate correct code for the target processor and take advantage of its instruction set. IAR’s compilers support several families of microcontrollers and incorporate architecture‑specific optimizations (for example, Thumb/Thumb‑2 instruction selection for Cortex‑M and efficient use of co‑processors).
-
Standards compliance and extensions: Modern compilers implement language standards (C18, C++17, C++20 and more.) while offering useful extensions. The IAR C/C++ compiler is a state‑of‑the‑art compiler that offers the standard features of the C and C++ languages, plus extensions designed to take advantage of MCU‑specific features.
-
Diagnostics and safety features: Helpful warnings, MISRA‑C compliance checking, and static‑analysis tools (IAR’s C‑STAT and C‑RUN) help catch bugs early. Functional‑safety certifications allow the compiler to be used in safety‑critical projects.
-
Toolchain integration: A compiler that is well integrated with an IDE and debugger improves productivity. IAR’s Embedded Workbench provides a unified environment with a project manager, editor, build tools, debugger, and code‑quality checks.
How do compilers support multiple architectures / cross‑compilation?
Compilers achieve cross‑compilation through a front‑end/back‑end architecture. The front‑end handles language parsing and produces an intermediate representation that is independent of the target. For each supported architecture, the compiler has a back‑end that understands the instruction set, register file, calling convention, and memory model.
The IAR compiler produces relocatable ELF object files and uses target‑specific code generators. Selecting a different processor in the project settings instructs the back‑end to emit code for that architecture. Cross‑compiling also requires the correct C library and runtime startup code for the target. IAR’s build tools automatically link in the proper C runtime and device support files for the selected microcontroller.
For microcontrollers with different natural data sizes (8‑bit, 16‑bit, 32‑bit, 64-bit), the compiler must insert operations such as sign‑extension or masking when manipulating types that do not match the target’s word size. It's important to highlight that unnatural data sizes cause extra instructions on various architectures (ARM Cortex‑M/R/A, RX, RL78, etc.), emphasizing the importance of architecture‑aware code generation.
What are the phases (steps) of compilation?
While implementations differ, a typical compilation pipeline includes the following stages:
|
Stage |
Description |
| Lexical and syntax analysis | The parser reads the source code (C source) and builds a syntax tree. |
| Intermediate representation | The program is converted to an intermediate representation that is easier to analyse and optimize. |
| High‑level optimization | Global optimizations such as inlining, dead‑code elimination, loop unrolling, scheduling and peephole transformations are applied. More detailed analysis of data‑flow/control‑flow allows the compiler to “understand” the code better, and more information gives better code. |
| Code generation | The intermediate code is mapped to target code for the selected architecture; registers are allocated and instructions are scheduled. |
| Low‑level optimization | Architecture‑specific tuning removes redundant operations, combines instructions, and reorders them for pipelines or caches. |
| Assembly | The generated code is assembled into an object file (in ELF/DWARF format for IAR tools). |
| Linking and library inclusion | Object files and libraries are linked to produce an executable image. |
What are the benefits of IAR's compiler?
The IAR C/C++ compiler offers several benefits for embedded development:
-
State‑of‑the‑art optimizations: The IAR compiler performs aggressive high‑ and low‑level optimizations (function inlining, cross‑call elimination, loop unrolling, dead‑code removal, scheduling etc.), allowing developers to write maintainable code while the compiler produces highly efficient executables.
-
Architecture‑specific extensions and compliance: For Arm devices, the compiler includes extensions to leverage Thumb‑2, coprocessors, and other Arm‑specific facilities while supporting C18, C++17, and C++20. On RISC‑V, the compiler can target RV32I, RV32E and RV64I base instruction sets and supports several ISA extensions, including compressed instructions (C), the bit‑manipulation extension (B), packed‑SIMD instructions (P), user‑level interrupts (N), and cache maintenance operations (CMO). It also supports non‑standard AndeStar™ extensions such as CoDense, DSP and V5.
-
MISRA‑C and functional‑safety support: The toolchain integrates MISRA‑C checking and offers TÜV‑certified functional‑safety editions, which are important for safety‑critical applications in automotive, industrial and medical devices.
-
Integrated toolchain and debugger: IAR Embedded Workbench combines the compiler with an assembler, linker, C library, project manager, C‑SPY debugger and static/runtime analysis tools, providing a seamless development experience.
-
Extensive device support: The compiler is shipped with device‑specific headers, startup code, linker configurations and preconfigured support files for numerous microcontrollers. For RISC‑V, IAR supplies device‑specific I/O header files, linker scripts and debugger description files, simplifying setup and ensuring that the correct ISA extensions are enabled.
-
Global support and training – IAR provides professional technical support and training, including documentation and examples, to help developers leverage the toolchain effectively.
By adding comprehensive architecture support, covering both standard ISA extensions and vendor‑specific features with dedicated intrinsics, the IAR C/C++ compiler extends its traditional strengths in optimization, safety, and integration to the rapidly growing ecosystem.