

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!


The code generation problem is the task of mapping intermediate code to machine code. The generated code must be correct for obvious reasons. It should be efficient both in terms of memory space and execution time.
The input to the code generation module of compiler is intermediate code (optimized or not) and its task is typically to produce either machine code or assembly language code for a target machine.
The code generation module has to tackle a number of issues.
For our discussion, we will target a machine that has the following general characteristics. Most actual processors are similar to such architecture.
The machine is byte-addressable with 4-byte words. It has N general -purpose registers. It uses two-address instructions of the form op source, destination. The target assembly language operations are:
More instruction will be added to the instruction set as needed.
The following table presents the addressing modes for source or destination operands.
MODE FORM ADDRESS ADDED COST absolute M M 1 register R R 0 indexed c(R) c + contents(R) 1 indirect register *R contents(R) 0 indirect indexed *c(R) contents(c+contents(R)) 1 literal #c c 1 stack SP SP 0 indexed stack c(SP) c + contents(SP) 1
We associate a cost with each instruction. This will allow us to compute the cost of generated code. The cost corresponds to length of instruction. For example the instruction
MOV R0,R1 ; R0 = c(R1)
has cost 1 while
MOV R5,M ; M = c(R5)
has cost 2: 1 for instruction, 1 additional for memory address. The column title “ADDED COST” indicates this additional cost.
Simple Code Generation
We start with a simple code generation strategy: define a target code sequence for each intermediate code (such as 3-address code) statement type. Thus,
Intermediate code becomes…
a = b MOV b,a a = b[c] MOV addr(b),R ADD c, R MOV R0,a a = b + c MOV b,a ADD c,a a[b] = c MOV addr(a),R ADD b,R MOV c,R