UNIT - IV Compiler.pptx RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a ...
UNIT - IV Compiler.pptx RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic
Programming Code Generation
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic
Programming Code Generation
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic
Programming Code Generation
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic
Programming Code Generation
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic
Programming Code Generation
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic
Programming Code Generation
RUN-TIME ENVIRONMENT AND CODE GENERATION
Runtime Environments – source language issues – Storage organization – Storage Allocation
Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic
Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs
- Design of a simple Code Generator - Optimal Code Generation
Size: 1.16 MB
Language: en
Added: Dec 26, 2024
Slides: 34 pages
Slide Content
UNIT IV RUN-TIME ENVIRONMENT AND CODE GENERATION Runtime Environments – source language issues – Storage organization – Storage Allocation Strategies: Static, Stack and Heap allocation - Parameter Passing - Symbol Tables - Dynamic Storage Allocation - Issues in the Design of a code generator – Basic Blocks and Flow graphs - Design of a simple Code Generator - Optimal Code Generation for Expressions – Dynamic Programming Code Generation
RUN-TIME ENVIRONMENT - STORAGE ORGANIZATION When the target program executes then it runs in its own logical address space Logical address space is shared among the compiler, operating system and target machine for management and organization OS is used to map the logical address into physical address
Subdivision of Run-time Memory
Runtime storage comes into blocks, where a byte is the smallest unit of addressable memory Run-time storage can be subdivide to hold the different components of an executing program: Generated executable code Static data objects Dynamic data-object- heap Automatic data objects- stack
Activation Record Control stack is a run time stack which is used to keep track of the live procedure activations i.e. it is used to find out the procedures whose execution have not been completed. When it is called (activation begins) then the procedure name will push on to the stack when it returns (activation ends) then it will popped. An activation record is pushed into the stack when a procedure is called and it is popped when the control returns to the caller function.
The diagram below shows the contents of activation records
Return Value: used by calling procedure to return a value to calling procedure. Actual Parameter: used by calling procedures to supply parameters to the called procedures. Control Link: It points to activation record of the caller. Access Link: used to refer to non-local data held in other activation records. Saved Machine Status: Holds the information about status of machine before the procedure is called. Local Data: Holds the data that is local to the execution of the procedure. Temporaries: Stores the value that arises in the evaluation of an expression.
Storage Allocation Strategies The different ways to allocate memory are: Static storage allocation Stack storage allocation Heap storage allocation
Static storage allocation In this, names are bound to storage locations If memory is created at compile time then the memory will be created in static area and only once Supports the dynamic data structure that means memory is created only at compile time and deallocated after program completion Drawbacks: Size and position of data objects should be known at compile time & restriction of the recursion procedure
Stack Storage Allocation In this, storage is organized as a stack An activation record is pushed into the stack when activation begins and it is popped when the activation end Activation record contains the locals so that they are bound to fresh storage in each activation record. The value of locals is deleted when the activation ends Works on last-in-first-out (LIFO) This allocation supports the recursion process
Heap Storage Allocation Most flexible allocation scheme Allocation and deallocation of memory can be done at any time and at any place depending upon the user's requirement Used to allocate memory to the variables dynamically Supports the recursion process
Code Generator Code generator is used to produce the target code for three-address statements. It uses registers to store the operands of the three address statement. Example: Consider the three address statement x:= y + z. It can have the following sequence of codes: MOV x, R ADD y, R
Register and Address Descriptors: A register descriptor contains the track of what is currently in each register The register descriptors show that all the registers are initially empty An address descriptor is used to store the location where current value of the name can be found at run time
Generating Code for Assignment Statements: The assignment statement d:= (a-b) + (a-c) + (a-c) can be translated into following sequence of three address code: t:= a-b u:= a-c v:= t +u d:= v+u
Design Issues in Code Generator Various code generation phase are : Input to the code generator Target program Memory management Instruction selection Register allocation Evaluation order
1. Input to the code generator The input to the code generator contains the intermediate representation of the source program and the information of the symbol table The source program is produced by the front end Intermediate representation has the several choices: a) Postfix notation b) Syntax tree c) Three address code The code generation phase needs complete error-free intermediate code as an input requires
2. Target program: The target program is the output of the code generator. The output can be: Assembly language: It allows subprogram to be separately compiled Relocatable machine language: It makes the process of code generation easier Absolute machine language: It can be placed in a fixed location in memory and can be executed immediately.
3. Memory management Mapping name in the source program to address of data is done by the front end and code generator A name in the three address statements refers to the symbol table entry for name. Local variables are stack allocation in the activation record while global variables are in static area
4. Instruction selection: Nature of instruction set of the target machine should be complete and uniform. The quality of the generated code can be determined by its speed and size.
Example The Three address code is: a:= b + c d:= a + e Inefficient assembly code is: MOV b, R0 R0→b ADD c, R0 R0 → c + R0 MOV R0, a a → R0 MOV a, R0 R0→ a ADD e, R0 R0 → e + R0 MOV R0, d d → R0
5. Register allocation Register can be accessed faster than memory. The instructions involving operands in register are shorter and faster than those involving in memory operand. The following sub problems arise when we use registers: Register allocation: In this, Selects set of variables that will reside in register. Register assignment: In this, Picks the register that contains variable. Certain machine requires even-odd pairs of registers for some operands and result.
For example: Consider the following division instruction of the form: D x, y Where, x is the dividend even register in even/odd register pair y is the divisor Even register is used to hold the reminder. Old register is used to hold the quotient.
6. Evaluation order The code generator decides the order in which the instruction will be executed. The order of computations affects the efficiency of the target code Some computation orders need fewer registers to hold results of intermediate than others.
Approaches to code generation issues: Code generator must always generate the correct code. Some of the design goals of code generator are: Correct Easily maintainable Testable Efficient
BASIC BLOCKS Source codes which are always executed in sequence are considered as the basic blocks of the code. These basic blocks do not have any jump statements among them A program can have various constructs as basic blocks, like IF-THEN-ELSE, SWITCH-CASE conditional statements and loops such as DO-WHILE, FOR, and REPEAT-UNTIL, etc.
Basic block identification Search header statements of all the basic blocks from where a basic block starts: First statement of a program. Statements that are target of any branch (conditional/unconditional). Statements that follow any branch statement. Header statements and the statements following them form a basic block. A basic block does not include any header statement of any other basic block.
DAG representation of Basic Blocks Directed a cyclic graphs ( dags ) are useful data structures for implementing transformation on basic blocks. The Dag for the statement x := y + z is: The Dag for the statements. t 1 := 4 * i t 2 := a [ t ] is
BASIC BLOCKS AND FLOW GRAPHS A graph representation of 3 address statements is called a flow graph. Nodes in the flow graph represent computations and the edges represents the flow of control.
Basic Blocks A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branching except at the end. The following sequence of three address, statements forms a basic block: t := a * a t := a * b t := 2 * t t := t + t
begin sum := 0 i := 1 do begin sum := sum + a[ i ] + b[ i ]; i = i + 1; end While i < - 10 end Algorithm - Partition into basic blocks Input: A sequence of three address statements. Output: A list of basic blocks with each three address statement in exactly one block. Source code to compute the sum of 2 arrays
List of 3 address statements: sum := 0 i = 1 t1 := 4 * i t2 := a [ t 1] t3 := 4 * i t4 := b [ t 3] t5 := t2 + t4 t6 := sum + t5 sum := t6 t7 := i + 1 i = t7 i < = 10 goto 3