Compiled code on a Harvard architecture is handled by placing program instructions in a separate instruction memory and data in a separate data memory, so the compiler must generate code that never treats instructions as ordinary addressable data unless the specific machine provides a deliberate path for doing so. This separation is the defining property of the architecture, and it shapes nearly every stage of compilation and loading: how pointers are represented, how constants and jump tables are placed, how the loader transfers the program, and how functions are called.
The short answer is therefore about separation and its consequences, not about a single universal procedure. "Harvard architecture" covers a family of designs, from small microcontrollers to digital signal processors and some embedded cores, and each member implements the separation differently. Where a design provides a unified view to the programmer—or a bridge between the two spaces—the compiler can behave much as it does on a von Neumann machine. Where it does not, the compiler and toolchain must keep the two address spaces rigidly apart.
The Two Memories and What They Mean for Code
In the classic Harvard model, the processor has distinct buses for fetching instructions and for reading and writing data. Because these are physically separate, an instruction fetch and a data access can occur in the same cycle, which is the performance motivation for the design. The cost is that a single numeric address no longer identifies "a location" unambiguously: the same bit pattern can name a byte in program memory or a byte in data memory, and they are different bytes.
For compiled code this produces a fundamental distinction that compilers must respect. A program is compiled into instruction words that live in program memory. Variables, the stack, and the heap live in data memory. A data pointer—the kind the compiler generates for int *p—addresses data memory. A code pointer—the kind used for function addresses—addresses program memory. On a strict Harvard machine these are not interchangeable, and a compiler that assigned them the same width and semantics would produce broken code.
The compiler therefore has to decide, for every value, which address space it belongs to. In practice this is handled in one of a few ways described below. The crucial point is that the source language concept of "a pointer is just an address" becomes, on strict Harvard hardware, a concept the compiler must refine into "a pointer into this space or that space."
Code Pointers, Data Pointers, and the Programming Model
On parts of the AVR family, for example, program memory and data memory are distinct spaces, and a normal C pointer is a data pointer. A function pointer in C is not, by default, a pointer into program memory; calling through it requires the compiler to emit an indirect call that goes through the instruction-fetch path. The AVR-GCC toolchain addresses this with attributes and address-space handling: functions that must be reachable through a 16-bit function pointer are placed in a low region, and the compiler distinguishes calls through function pointers from ordinary direct calls. Similarly, constant data that the programmer wants stored in flash rather than RAM must be explicitly marked (for instance with a "program memory" attribute or by using special access routines), because ordinary initialized variables are copied into RAM at startup.
This illustrates the general pattern: on strict Harvard designs, the compiler cannot freely relocate read-only data into program memory and read it with ordinary data loads. Reading a byte from program memory on such a machine generally requires a dedicated instruction or a library routine that performs the fetch through the code path, because the normal load instruction addresses data memory. Constants are therefore either kept in data memory (costing RAM) or placed in program memory and accessed through special intrinsics.
Digital signal processors often embody a related but distinct arrangement. Many DSPs have separate program and data buses and sometimes separate address spaces, and compilers for them generate code that deliberately schedules data moves to exploit the parallelism. On such machines, the compiler's job includes placing coefficient tables and filter constants in the memory that the data path can reach efficiently, and using specialized addressing modes. The separation is less about "can code see data" and more about optimizing the traffic on each bus.
Loading and Startup: How the Program Gets Into Place
The compiled artifact for a Harvard machine is typically partitioned into at least two images or sections: the instruction image destined for program memory and the initial data image destined for data memory. On many microcontrollers, program memory is non-volatile flash and data memory is volatile RAM, which introduces an asymmetry the startup code must resolve.
Consider a global variable initialized to a nonzero value, such as int table = {1, 2, 3, 4};. The initial values must exist somewhere at power-up. RAM loses its contents when power is removed, so the values cannot simply "be" in RAM before the program runs. The toolchain therefore stores an initialization image in program memory—typically in a special section such as .data's load image placed in flash—and the startup routine, which runs before main, copies that image from program memory into the RAM locations where the variable will live. Uninitialized globals go into a .bss-like region that startup clears to zero. This copy-and-clear step is a direct consequence of Harvard separation combined with the flash/RAM split.
A second subtlety is that on some Harvard parts, reading the initialization image from program memory during startup requires the same special program-memory access routines mentioned earlier, so even the startup code is written with the address-space distinction in mind. The loader or programmer (the hardware that writes flash) is responsible for depositing the instruction image and the initialization image; the runtime startup code then bridges them into RAM.
Function Calls, Return Addresses, and the Stack
The stack is a data-memory structure on Harvard machines, but it holds return addresses that refer to program memory. That is a place where the two spaces meet conceptually. On many small Harvard microcontrollers the hardware pushes the return address onto the data stack automatically during a call, and the return instruction pops it back into the program counter. The compiler does not need to model the return address as an ordinary data value; it is a special hardware-managed quantity.
Where the compiler does generate code that manipulates return addresses or function pointers as values, it must use the code-pointer representation the target defines. On some architectures code pointers are wider than data pointers, or are stored in a distinct register class. A compiler backend encodes this in its calling convention and in the types it uses for function pointers. The practical consequence for the programmer is that casting a data pointer to a function pointer, or storing a function pointer in an ordinary integer, may be ill-defined or may require a target-specific conversion. Well-written portable code avoids such casts; code written for a specific Harvard target sometimes relies on documented intrinsics to do the conversion.
Self-Modifying Code and Jump Tables
The most visible limitation of strict Harvard separation is that code cannot, in general, be written as data and then executed. On a von Neumann machine a program can generate machine instructions in a buffer and jump to them; on a strict Harvard machine the buffer is in data memory, and the instruction fetch path does not read data memory, so the jump would fetch whatever is at the corresponding program-memory address—not the generated bytes. Some Harvard designs provide a mechanism to write program memory (flash self-programming, or a cache-coherence operation that copies a data buffer into the instruction space), but this is a deliberate, often slow, and carefully sequenced operation, not an ordinary store. Compilers and just-in-time systems targeting such hardware must use those mechanisms explicitly; they cannot assume that a store followed by a jump will execute the stored bytes.
Jump tables for switch statements are a milder case. A table of code addresses can be placed in data memory, and the compiler can load an entry and perform an indirect jump, provided the target supports indirect branching to a code address held in a data location. Many Harvard microcontrollers do support this, because the indirect jump instruction interprets its operand as a program-memory address. The table itself is data; the values in it are code addresses. That is fine as long as the compiler and hardware agree on the representation, which they do by design on such targets.
Where the Strict Picture Softens: Modified Harvard Designs
The rigid separation described so far is the "strict" Harvard model. Many processors that are described as Harvard in marketing or textbooks are more accurately modified Harvard: they have separate instruction and data caches at the first level, or separate buses, but a unified physical address space, so a pointer value is meaningful in both contexts subject to cache coherence rules. Common general-purpose CPUs with separate L1 instruction and data caches fall in this category. On such machines, compiled code is handled very much as on a von Neumann machine: the compiler emits ordinary loads and stores, the loader places code and data in one address space, and the only care needed is that writing to a location that is also cached as an instruction requires the appropriate synchronization (an instruction-cache flush or equivalent). The distinction matters because a great deal of confusion arises from calling these designs "Harvard" and then asking why self-modifying code or function-pointer casts work fine—they work because the design is only Harvard at the cache level, not at the address-space level.
A useful test when reading a datasheet or architecture manual is to ask two questions. First, does a single pointer value denote the same byte whether used as an instruction address or a data address? Second, can an ordinary data store change what the instruction fetch will see without an explicit synchronization step? If the answers are "yes" and "yes, with a flush," the design is modified Harvard and the compiler's model is essentially unified. If the answers are "no" and "no," the design is strict Harvard and the compiler must maintain two address spaces, use special access routines for program-memory constants, and rely on startup code to initialize RAM.
What the Compiler Emits, Concretely
Pulling the threads together, a compiler targeting a strict Harvard machine typically does the following. It classifies every object by address space: functions and read-only constants destined for program memory, mutable objects for data memory. It selects pointer representations accordingly, distinguishing code pointers from data pointers where the target requires it. It emits a startup sequence, either itself or through a linked runtime, that copies initialized data from a program-memory image into RAM and zeroes uninitialized data. It uses target intrinsics or library calls for reading program-memory constants with ordinary-looking source code. It respects the calling convention, which on such targets fixes how return addresses and function pointers are passed. And it declines to generate self-modifying code or to assume that a data store alters instruction fetch, unless the target documents a mechanism and the compiler is instructed to use it.
For a programmer, the practical implications are equally concrete. Declaring large constant tables without marking them for program memory wastes scarce RAM. Casting between data and function pointers should be avoided or done only through documented conversions. Assuming that a buffer of bytes can be executed will fail on strict Harvard parts. And code that is portable across architectures should not depend on the unified-address-space assumption, even though it holds on most desktop and server processors.
Summary of the Distinctions That Matter
The handling of compiled code on Harvard architecture turns on whether instruction and data memories are truly separate address spaces or only separate paths into one address space. In the strict case, the compiler maintains two kinds of addresses, places code in program memory and variables in data memory, uses special routines to read constants stored alongside code, and depends on startup code to copy initialization images into RAM. In the modified case, the compiler behaves as on a von Neumann machine and the separation is a performance and cache-coherence matter rather than a change in the programming model. Recognizing which case applies to a given processor is the key to predicting how its compiled code will behave, and the answer to the original question is incomplete without that distinction.