Download SCANNING AND PARSSING and more Lecture notes System Programming in PDF only on Docsity!
System Programming
Prof. Trilok Suthar, Assistant Professor
Information Technology
Compilers
CHAPTER-
Aspects of compilation
- PL Features
- Data Types
- Data Structures
- Scope Rules
- Control Structure
Aspects of compilation-data Types
- Data Types
- A data is a specification of i. Legal values for variables of the type and ii. Legal operations on the legal values of the type.
- The following tasks are involved i. Checking legality of an operation for types of operands. ii. Use type conversion operations. iii. Use appropriate instruction sequence of the target machine.
Aspects of compilation-data Structure
- Data Structure - A PL permits the declaration and use of data structure like arrays, stacks, records, lists etc.
- To compile a reference to an element of a data structure, compiler must develop a memory mappings to access memory word(s) allocated to the element.
- A user defined type requires mapping of a different kind.
Aspects of compilation-Scope Rule
Scope Rule It determines the accessibility of variables declared in different block of a program. Example:
x, y : real; y, z: integer; x := y
Memory Allocation
- Memory allocation involves three important tasks:
- Determining the amount of memory required for storing the value of data item.
- Using an appropriate memory allocation model.
- Developing appropriate memory mappings for accessing values stored in a data structure.
- Types of memory allocations are:
- Static memory allocation
- Dynamic memory allocation
Memory Allocation-Static Memory
- In static memory allocation, memory is allocated to a variable before the execution of a program begins.
- No memory allocation or deallocation actions are performed during the execution of a program. Thus, variables remain permanently allocated.
Code(A) Data(A) Code(B) Data(B) Code(C) Data(C)
Memory Allocation-Dynamic Memory
- Types of dynamic memory allocation are:
- Automatic allocation: memory is allocated to the variables declared in a procedure when the procedure is entered during execution and is deallocated when the procedure is exited.
- Program controlled allocation: a program can allocate or deallocate memory at any time during its execution
Dynamic Memory Allocation & Access
- Each record in a stack is used to accommodate variables of one activation of a block, is called activation record.
ARB (activation record base) 0 (ARB): Dynamic pointer 1 (ARB): static pointer
Reserved pointer
TOS Stack record format
Used for memory allocation and deallocation.
Used for accessing nonlocal variables.
Image source : Google
Compilation of Expression
- The major issues in code generation for expressions are as follows:
- Determination of an evaluation order for the operators in an expression.
- Selection of instruction to be used in target code.
- Use of registers.
Toy Code Generator for Expressions
- A toy code generator has to track both the registers (for availability) and addresses (location of values) while generating the code.
- For both of them, the following two descriptors are used:
- The code generator uses the notation of an operand descriptor to maintain type, length and addressability information for each operand.
- It uses a register descriptor to maintain information about what operand or partial result would be contained in a CPU register during execution of the generated code.
Register Descriptor
- A register descriptor has two fields:
- Status: Contains the code free or occupied to indicate register status.
- Operand descriptor: If status = occupied, this field contains the descriptor for the operand contained in the register.
- The register descriptor for AREG after generating code for a*b would be:
- This indicates that register AREG contains the operand described by descriptor #3.
(Image Source: http://ictpost.com/e-content-challenges-in )
Status Operand descriptor # Occupied #
Types of intermediate forms
- Types of intermediate forms:
- Expression tree
- Postfix notation
- Three address code