Processing & Properties of Floor and Wall Tiles.pptx
Compilers
1.
2. STATEMENT OF PROBLEM
A compiler accepts a program written in a higher
level language as input and produces its machine
language equivalent as output.
Example:
WCM: PROCEDURE (RATE, START, FINISH);
DECLARE ( COST , RATE , START , FINISH) FIXED BINARY (31)
STATIC;
COST = RATE * (START – FINISH) + 2 * RATE * (START – FINISH –
100);
RETURN (COST) ;
END;
2jayashrisk
3. Steps followed by compiler to produce machine code
Recognize certain string as basic elements.
Ex: COST := variable , WCM := label, PROCEDURE : = keyword and
“=“ := operator
Recognize combinations of elements as syntactic units and interpret
their meaning.
Ex: 1st statement is procedure name with three arguments
Allocate storage and assign locations for all variables in this
program.
Generate the appropriate object code.
3jayashrisk
5. Ways of lexical process
Single continuous pass used to prepare a chain or table of
tokens
Reduces the size of token table by only parsing tokens as
necessary.
Discover and note lexical errors.
CLASSES OF UNIFORM
SYMBOLS:
IDENTIFIER(IDN)
TERMINAL SYMBOL(TRM)
LITERAL(LIT)
CLASS PTR
IDN
TRM
TRM
TRM
IDN
WCM
:
PROCEDURE
(
RATE
5jayashrisk
6. Problem No: 2 – Recognizing syntactic units
and Interpreting Meaning (syntactic
construction)
Compiler must recognize the phrases &
interpret the meaning of the constructions.
Note syntactic errors
Some compiler guess what the programmer
did wrong & correct it.
6jayashrisk
8. INTERMEDIATE FORM
Compiler creates an intermediate form of
source program.
It facilitates optimization of object code
It allows a logical separation between the
machine-independent phases & machine-
dependent phases
8jayashrisk
9. ARITHMETIC STATEMENT
Parse tree - form of an arithmetic statement
Rules for converting an arithmetic statement
into a parse tree are:
Any variable is a terminal node of the tree
For every operator, construct a binary tree
whose left branch is the tree for operand 1 &
whose right branch is the tree for operand 2.
9jayashrisk
11. MATRIX : Linear representation of the parse tree
Operations are listed sequentially
Each matrix entry has one operator and two operands.
Operands are uniform symbols
Matrix line no. O8perator Operand1 Operand2
1 - START FINISH
2 * RATE M1
3 * 2 RATE
4 - START FINISH
5 - M4 100
6 * M3 M5
7 + M2 M6
8 = COST M7
matrix
11jayashrisk
12. NONARITHMETIC STATEMENTS
DO, IF GO TO ,etc replaced by sequential
ordering of individual matrix entries.
Operators are defined in later phases of
compiler
Operator Operand1 Operand2
Return COST
End
RETURN ( COST) ;
END ;
Matrix for RETURN & END statement
12jayashrisk
13. NONEXECUTABLE STATEMENTS
No intermediate form for these statements
Information of these statements is entered into
tables
Name Base Scale Precisio
n(bits)
Storage
class
location
COST BINARY FIXED 31 STATIC 0
RATE BINARY FIXED 31 STATIC 4
START BINARY FIXED 31 STATIC 8
FINISH BINARY FIXED 31 STATIC 12
13jayashrisk
14. Problem no. 3 – Storage Allocation
Storage allocation routine scans the identifier
table and assigns a location to each scalar.
Absolute address is unknown at load time.
Relative address format
Temporary locations for intermediate results of
the matrix(M!,M2,etc)
Operand Sign bit(1) 31 binary
digit
COST S
RATE I
START G
FINISH N
14jayashrisk
15. Problem no. 4 – Code Generation
Based on matrix and table compiler generates object
code
A table with matrix operation and associated matrix
code is used for creating object deck.
Operators are treat as macro definition, operands as
arguments and production table as macro definition.
15jayashrisk
16. Standard code definition for -,*,+,=
- L 1,&OPERAND1
S 1,&OPERAND2
ST 1,M&N
-------------------------------
* L 1,&OPERAND1
S 1,&OPERAND2
ST 1,M&N
------------------------------------
+ L 1,&OPERAND1
S 1,&OPERAND2
ST 1,M&N
------------------------------------
= L 1,&OPERAND1
S 1,&OPERAND2
ST 1,M&N
----------------------------------
MATRIX GENERATED CODE
1 - START FINISH L 1,START
S 1,FINISH
ST 1,M1
2 * RATE M1 L 1,RATE
M 0,M1
ST 1,M2
3 * 2 RATE L 1,=F’2’
M 0,RATE
ST 1,M3
4 - START FINISH L 1,START
S 1,FINISH
ST 1,M4
5 - M4 100 L 1,M4
S 1,=F’100’
ST 1,M5
6 * M3 M5 L 1,M3
M 0,M5
ST 1,M6
7 + M2 M6 L 1,M2
A 1,M6
ST 1,M7
8 = COST M7 L 1,M7
ST 1,COST
Code definition
Code generation
16jayashrisk
17. Questions
Was it a good idea to generate code directly from the
matrix?
MACHINE INDEPENDENT OPTIMIZATION
Have we made the best use of machine we have at
our disposal?
MACHINE DEPENDENT OPTIMIZATION
Can we generate machine language directly?
17jayashrisk
18. OPTIMIZATION(MACHINE-INDEPENDENT)
Delete all duplicate matrix entries of sub expression occurs in the
same statement more than once(a common sub expression)
Modify all references to the deleted entries
Done before code generation
Example
Matrix with common sub
expression
Matrix after elimination of common
sub expression
1 - START FINISH 1 - START FINISH
2 * RATE M1 2 * RATE M1
3 * 2 RATE 3 * 2 RATE
4 - START FINISH 4
5 - M4 100 5 - M1 100
6 * M3 M5 6 * M3 M5
7 + M2 M6 7 * M2 M6
8 = COST M7 8 = COST M7
18jayashrisk
19. Other machine independent optimization steps
are:
Compile time computation of operations, both of
whose operands are constants
Movement of computations involving nonvarying
operands out of loops
Use of the properties of Boolean expression to
minimize their computation.
19jayashrisk
20. machine dependent optimization steps are:
For temporary storage use registers, this reduce the
number of loads and stores from 14 to 5 in our
example
Use shorter and faster instructions whenever
possible
Advantages
Reduces memory space needed for the program
Execution time of the object program by factor of 2
20jayashrisk
21. OPTIMIZATION(MACHINE-DEPENDENT)
Done while generating code
Optimized matrix First try Improved code
1 - START FINISH L
S
ST
1,START
1,FINISH
1,M1
L 1,START
1,FINISH M1-> R1
2 * RATE M1 L
M
ST
1,RATE
0,M1
1,M2
L
MR
3,RATE
2,1 M2->R3
3 * 2 RATE L
M
ST
1,=F’2’
0,RATE
1,M3
L
M
5,=F’2’
4,RATE M3->R5
4
5 - M1 100 L
S
ST
1,M1
1,=F’100’
1,M5
S 1,=F’100’ M5->R1
6 * M3 M5 L
M
ST
1,M3
0,M5
1,M6
LR
MR
7,5
6,1
M6->M7
7 + M2 M6 L
A
ST
1,M2
1,M6
1,M7
AR 3,7 M7->R3
8 = M7 COST L
ST
1,M7
1,COST
ST 3,COST
21jayashrisk
22. ASSEMBLY PHASE
Assembly phase is similar to pass 2 of assembler
Defines labels and resolves all references
22jayashrisk
24. LEXICAL PHASE
Task
To parse the source program into basic elements or
tokens of the language
Build literal table and an identifier table
Build a uniform symbol table
Database
Source program(string of characters)
Terminal table
Symbol Indicator precedence
; yes
Procedure no
24jayashrisk
25. Database
Literal table
Identifier table
Uniform symbol table
Literal Base Scale Precision Other information Address
31 Decimal Fixed 2
Name Data attribute Address
WCM Filled by later phases
Table Index
IDN 1(WCM)
25jayashrisk
26. Algorithm
Input string is separated into tokens by break
characters
Tokens are checked with entries of terminal table
If match then token is classified as terminal symbol and
an uniform symbol is created for type TRM
If not matched then checked as identifier or literal
If token start with alphabet and contain up to 30 more
characters or underscores. then classified as identifier
and an uniform symbol is created for type IDN
If not then checked as literal
If not fit into one of these categories, it is an error.
26jayashrisk
27. SYNTAX PHASE
TASK
Recognize the major construct of the language and to
call the appropriate action routines that will generate
the intermediate from or matrix for these constructs.
Databases
Uniform symbol table
stack
Table Index
27jayashrisk
28. Algorithm
Reduction are tested consecutively for match between old
top of stack field and the actual top of stack until match is
found.
When matched is found, the action routines specified in
the action field are executed in order from left to right.
When control returns to the syntax analyzer, it modifies
the top of stack to agree with the new top of stack field.
Step 1 is then repeated starting with the reduction
specified in the next reduction field.
28jayashrisk
29. Interpretation phase
It is a collection of routines that are called when a
construct is recognized in the syntactic phase.
Routines are used to create an intermediate form of
the source program and add information to the
identifier table.
29jayashrisk
30. Databases
Uniform symbol table
Stack
Identifier table
Matrix
Name Base Scale Precision Storage
class
Array
bound
Structure
info
Literal
value
Block
info
other address
Uniform
symbol
Uniform
symbol
Uniform
symbol
chaining
Operator Operand1 operand2 30jayashrisk
31. Temporary storage table
Algorithm
It contains collection of individual action routines
that accomplish specific tasks when invoked by the
syntax analysis phase
Routines are
Do any necessary additional parsing
Create new entries in the matrix or add data attributes
to the identifier operator and operands and insert them
into the matrix.
MTXN Base Scale Precision Storage
class
Other address
31jayashrisk
33. Algorithm
Elimination of common sub expressions
Common sub expression must be identical and must be in
same statement
1. Place the matrix in a form so that common sub expressions
can be recognized
2. Recognize two sub expressions as being equivalent
3. Eliminate one of them
4. Alter the rest of the matrix to reflect the elimination of this
entry
5. Rescan the matrix for possible crated common sub
expressions repeat 1 to 5 until no change occur
6. Eliminate from the temporary storage table any MTX
entries that are no longer needed
33jayashrisk
34. Example
Source code
B=A
A=C*D*(D*C+B)
MATRIX BEFORE OPTIMIZATION
M line
no
Operator Operand1 Operand2 Backward forward
M1 = B A 0 2
M2 * C D 1 3
M3 * D C 2 4
M4 + M3 B 3 5
M5 * M2 M4 4 6
M6 = A M5 5 ?
MATRIX BEFORE OPTIMIZATION
M line
no
Operat
or
Operan
d1
Operan
d2
Backw
ard
forwar
d
M1 = B A 0 2
M2 * C D 1 3
M3 * C D 2 4
M4 + B M3 3 5
M5 * M2 M4 4 6
M6 = A M5 5 ?
MATRIX AFTER OPTIMIZATION
M line
no
Operat
or
Operan
d1
Operan
d2
Backwa
rd
forward
M1 = B A 0 2
M2 * C D 1 4
M3 * C D 2 4
M4 + B M2 2 5
M5 * M2 M4 4 6
M6 = A M5 5 ?
34jayashrisk
35. Compile time compute
Saves space and execution time for object program
Used for arithmetic computation within loop
Example
A=2*276/92*B = (2*276/92)*B = 6*B
Before optimization
M1 * 2 276
M2 / M1 92
M3 * M2 B
M4 = A M3
After optimization
M1
M2
M3 * 6 B
M4 = A M3
35jayashrisk
36. Boolean expression optimization
Use properties of Boolean expression to shorten their
computation
Example
If(a or b or c)
If a is true the no need to check b and c
So eliminate that part
36jayashrisk
37. Move invariant computations outside of loops
If the computation within a loop depends on a
variable that does not change within that loop, the
computation may be moved outside the loop
Recognition of invariant computations
Example
For(i=0;i<10;i++)
{
a=10;
i=i*2;
}
37jayashrisk
38. Discovering where to move the invariant
computation
Move the computation to a position directly
preceding the LOOP from which it comes
Moving the invariant computation
Delete the invariant computation from its original
position in the matrix and insert it into the
appropriate place
38jayashrisk