Code Generation: M.B.Chandak Lecture Notes On Language Processing
Code Generation: M.B.Chandak Lecture Notes On Language Processing
Code Generation: M.B.Chandak Lecture Notes On Language Processing
M.B.Chandak
Lecture notes on Language Processing
Code Generation
• It is final phase of compilation.
• Input from ICG and output in the form of machine code of
target machine.
• Major issues
• Input to code generator
• Target program
• Memory management
• Instruction selection
• Allocation of registers
• Choice of Evaluation order
Major Issues: Input to Code Generator
• Output of Intermediate code generator and code
optimizer.
• Entries of symbol tables used to generate run time
addresses of data objects generated by ICG.
• Type of representation of ICG, generally TAC, Trees
and DAG are suitable for most of code generators.
• Assumption: Code is free of errors.
Major Issues: Target Programs
• Target program may be in the form of Absolute machine
code, re-locatable machine code or assembly language.
• Absolute machine code can be placed at any fixed location in
memory and executed.
• Re-locatable machine code (object module) allows
subprograms to be complied separately and linked and
loaded in memory before execution. Advantage: Reusability
• Assembly language version makes process of code
generation simple.
• //Refer class material for remaining points
Code Generation: Arrays
• The assembly code is used for code generation to simplify the process of code
generation. The instruction like: MOV, ADD, SUB are used for implementation
purpose.
• The code generation process of array statements like: a = b[i] and a[i] = b is as
follows:
• The process depends upon index variable “i“ which can be stored at different
places in memory.
• The possible memory representation of index variable are:
• Register Ri
• Memory location Mi
• On Stack at offset Si from top and register “A” holds the address of activation
record containing details of stack.
Code Generation: Arrays
• The code generation for arrays will be:
Statement i in Register Ri i in memory Mi i in stack
a = b[i] MOV b[Ri], R1 MOV Mi, R MOV Si(A), R
MOV b(R), R1 MOV b(R), R
a[i] = b MOV b, a[Ri] MOV Mi, R MOV Si(A), R
MOV b, a(R) MOV b, a(R)