Post on 15-Apr-2017
transcript
COMPILERS
jayashrisk 2
COMPILER 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;
jayashrisk 3
COMPILER 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.
jayashrisk 4
COMPILER Problem No: 1 – Recognizing Basic Elements (lexical analysis) Uses conceptually simple string processing techniques. Source program is scanned sequentially Tokens (identifiers, literals or terminal symbols)are identified Basic elements(identifiers & literals) are placed into tables with
basic information 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 ;
jayashrisk 5
COMPILER 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
jayashrisk 6
COMPILER 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.
jayashrisk 7
COMPILERExample:
WCM : PROCEDURE ( RATE , START , FINISH ) ;
DECLARE ( COST , RATE , START , FINISH ) FIXED BINARY ( 31 ) STATIC ;
COST = RATE * ( START – FINISH ) + 2 * RATE * ( START - FINISH - 100 ) ;
RETRUN ( COST ) ; END ;
Valid procedure statement
Valid declare statement
Valid end statement
Valid assignment statementValid assignment statement
Basic syntactic constructs
Tokens
jayashrisk 8
COMPILER 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
jayashrisk 9
COMPILER 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.
jayashrisk 10
COMPILERExample: parse tree
COST = RATE * ( START – FINISH ) + 2 * RATE * ( START - FINISH - 100 ) ;
=
+
* *
-
-
*
COST
RATE-
START FINISH 2 RATE
START FINISH
100
jayashrisk 11
COMPILER 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 FINISH2 * RATE M13 * 2 RATE4 - START FINISH5 - M4 1006 * M3 M57 + M2 M68 = COST M7
matrix
jayashrisk 12
COMPILER NONARITHMETIC STATEMENTS DO, IF GO TO ,etc replaced by sequential
ordering of individual matrix entries. Operators are defined in later phases of
compilerOperator Operand1 Operand2 Return COST End
RETURN ( COST) ;END ;
Matrix for RETURN & END statement
jayashrisk 13
COMPILER NONEXECUTABLE STATEMENTS No intermediate form for these
statements Information of these statements is
entered into tablesName Base Scale Precisi
on(bits)
Storage class
location
COST BINARY FIXED 31 STATIC 0RATE BINARY FIXED 31 STATIC 4START BINARY FIXED 31 STATIC 8FINISH BINARY FIXED 31 STATIC 12
jayashrisk 14
COMPILER 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 SRATE ISTART GFINISH N
jayashrisk 15
COMPILER 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.
jayashrisk 16
COMPILERStandard code definition for -,*,+,=
- L1,&OPERAND1 S1,&OPERAND2 ST 1,M&N-------------------------------* L1,&OPERAND1
S1,&OPERAND2 ST 1,M&N------------------------------------+ L1,&OPERAND1
S1,&OPERAND2
ST 1,M&N------------------------------------= L1,&OPERAND1 S1,&OPERAND2 ST 1,M&N----------------------------------
MATRIX GENERATED CODE
1 - START FINISH L1,START
S1,FINISH
ST 1,M1
2 * RATE M1 L 1,RATEM 0,M1ST 1,M2
3 * 2 RATE L 1,=F’2’M 0,RATEST 1,M3
4 - START FINISH L1,START
S1,FINISH
ST 1,M4
5 - M4 100 L 1,M4S
1,=F’100’ST 1,M5
6 * M3 M5 L 1,M3M 0,M5ST 1,M6
7 + M2 M6 L 1,M2A 1,M6ST 1,M7
8 = COST M7 L 1,M7ST 1,COST
Code definitionCode generation
jayashrisk 17
COMPILER 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?
jayashrisk 18
COMPILER 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 M13 * 2 RATE 3 * 2 RATE4 - START FINISH 4
5 - M4 100 5 - M1 1006 * M3 M5 6 * M3 M57 + M2 M6 7 * M2 M68 = COST M7 8 = COST M7
jayashrisk 19
COMPILER 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.
jayashrisk 20
COMPILER 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
jayashrisk 21
COMPILER OPTIMIZATION(MACHINE-DEPENDENT)
Done while generating code
Optimized matrix First try Improved code
1 - START FINISH LSST
1,START1,FINISH1,M1
L 1,START1,FINISH M1-> R1
2 * RATE M1 LMST
1,RATE0,M11,M2
LMR
3,RATE2,1 M2->R3
3 * 2 RATE LMST
1,=F’2’0,RATE1,M3
LM
5,=F’2’4,RATE M3->R5
4
5 - M1 100 LSST
1,M11,=F’100’1,M5
S 1,=F’100’ M5->R1
6 * M3 M5 LMST
1,M30,M51,M6
LRMR
7,56,1
M6->M7
7 + M2 M6 LAST
1,M21,M61,M7
AR 3,7 M7->R3
8 = M7 COST LST
1,M71,COST
ST 3,COST
jayashrisk 22
COMPILER ASSEMBLY PHASE
Assembly phase is similar to pass 2 of assembler
Defines labels and resolves all references
jayashrisk 23
GENERAL MODEL OF COMPILER
jayashrisk 24
PHASES OF COMPILERLEXICAL PHASE
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 tableSymbol Indicator precedence; yesProcedure no
jayashrisk 25
PHASES OF COMPILERLEXICAL PHASE
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)
jayashrisk 26
PHASES OF COMPILERLEXICAL PHASE
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.
jayashrisk 27
PHASES OF COMPILERSYNTAX PHASE
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
jayashrisk 28
PHASES OF COMPILERSYNTAX PHASE
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.
jayashrisk 29
PHASES OF COMPILERInterpretation phase
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.
jayashrisk 30
PHASES OF COMPILERInterpretation phase
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
jayashrisk 31
PHASES OF COMPILERInterpretation phase
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
jayashrisk 32
PHASES OF COMPILERoptimization
Optimization(machine independent optimization)
Databases Matrix
Identifier table Literal table
Operator
Operand1
Operand2
Forward pointer
Backward pointer
jayashrisk 33
PHASES OF COMPILERoptimization
Algorithm Elimination of common sub expressions Common sub expression must be identical and
must be in same statement1. Place the matrix in a form so that common sub
expressions can be recognized2. Recognize two sub expressions as being equivalent3. Eliminate one of them4. Alter the rest of the matrix to reflect the elimination of
this entry5. Rescan the matrix for possible crated common sub
expressions repeat 1 to 5 until no change occur6. Eliminate from the temporary storage table any MTX
entries that are no longer needed
jayashrisk 34
PHASES OF COMPILERoptimization
ExampleSource code
B=AA=C*D*(D*C+B)
MATRIX BEFORE OPTIMIZATIONM line no
Operator
Operand1
Operand2
Backward
forward
M1 = B A 0 2M2 * C D 1 3M3 * D C 2 4M4 + M3 B 3 5M5 * M2 M4 4 6M6 = A M5 5 ?
MATRIX BEFORE OPTIMIZATIONM line no
Operator
Operand1
Operand2
Backward
forward
M1 = B A 0 2M2 * C D 1 3M3 * C D 2 4M4 + B M3 3 5M5 * M2 M4 4 6M6 = A M5 5 ?
MATRIX AFTER OPTIMIZATIONM line no
Operator
Operand1
Operand2
Backward
forward
M1 = B A 0 2M2 * C D 1 4M3 * C D 2 4M4 + B M2 2 5M5 * M2 M4 4 6M6 = A M5 5 ?
jayashrisk 35
PHASES OF COMPILERoptimization
Compile time compute Saves space and execution time for object
program Used for arithmetic computation within loopExampleA=2*276/92*B = (2*276/92)*B = 6*BBefore optimization
M1 * 2 276M2 / M1 92M3 * M2 BM4 = A M3
After optimizationM1M2M3 * 6 BM4 = A M3
jayashrisk 36
PHASES OF COMPILERoptimization
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 cSo eliminate that part
jayashrisk 37
PHASES OF COMPILERoptimization
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;}
jayashrisk 38
PHASES OF COMPILERoptimization
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