SlideShare a Scribd company logo
1 of 30
Compiler Construction
Week 11
Intermediate Code Generation
Semantic Analysis
2
Overview
 Intermediate representations span the gap between
the source and target languages:
 closer to target language;
 (more or less) machine independent;
 allows many optimizations to be done in a machine-independent way.
3
Types of Intermediate Languages
 High Level Representations (e.g., syntax trees):
 closer to the source language
 easy to generate from an input program
 code optimizations may not be straightforward.
 Low Level Representations (e.g., 3-address
code):
 closer to the target machine;
 easier for optimizations, final code generation;
4
Syntax Trees
A syntax tree shows the structure of a program by abstracting
away irrelevant details from a parse tree.
 Each node represents a computation to be performed;
 The children of the node represents what that computation is
performed on.
Syntax trees separate parsing from subsequent processing.
5
Syntax Trees: Example
Grammar :
E  E + T | T
T  T * F | F
F  ( E ) | id
Input: id + id * id
Parse tree:
Syntax tree:
6
Syntax Trees: Structure
 Expressions:
 leaves: identifiers or constants;
 internal nodes are labeled with operators;
 the children of a node are its operands.
 Statements:
 a node’s label indicates what kind of
statement it is;
 the children correspond to the components
of the statement.
7
Constructing Syntax Trees
General Idea: construct bottom-up using
synthesized attributes.
E → E + E { $$ = mkTree(PLUS, $1, $3); }
S → if ‘(‘ E ‘)’ S OptElse { $$ = mkTree(IF, $3, $5, $6); }
OptElse → else S { $$ = $2; }
| /* epsilon */ { $$ = NULL; }
S → while ‘(‘ E ‘)’ S { $$ = mkTree(WHILE, $3, $5); }
mkTree(NodeType, Child1, Child2, …) allocates space for the tree node and fills in its node
type as well as its children.
8
Three Address Code
 Low-level IR
 instructions are of the form ‘x = y op z,’ where x,
y, z are variables, constants, or “temporaries”.
 At most one operator allowed on RHS, so no
‘built-up” expressions.
Instead, expressions are computed using temporaries
(compiler-generated variables).
9
Three Address Code: Example
 Source:
if ( x + y*z > x*y + z)
a = 0;
 Three Address Code:
tmp1 = y*z
tmp2 = x+tmp1 // x + y*z
tmp3 = x*y
tmp4 = tmp3+z // x*y + z
if (tmp2 <= tmp4)
a = 0;
CSc 453: Intermediate Code Generation 10
An Intermediate Instruction Set
 Assignment:
 x = y op z (op binary)
 x = op y (op unary);
 x = y
 Jumps:
 if ( x op y ) goto L (L a label);
 goto L
 Pointer and indexed
assignments:
 x = y[ z ]
 y[ z ] = x
 x = &y
 x = *y
 *y = x.
 Procedure call/return:
 param x, k (x is the kth param)
 retval x
 call p
 enter p
 leave p
 return
 retrieve x
 Type Conversion:
 x = cvt_A_to_B y (A, B base types)
e.g.: cvt_int_to_float
 Miscellaneous
 label L
11
Three Address Code: Representation
 Each instruction represented as a structure called a
quadruple (or “quad”):
 contains info about the operation, up to 3 operands.
 for operands: use a bit to indicate whether constant or ST pointer.
E.g.:
x = y + z if ( x  y ) goto L
12
Code Generation: Approach
 function prototypes, global declarations:
 save information in the global symbol table.
 function definitions:
 function name, return type, argument type and number saved
in global table (if not already there);
 process formals, local declarations into local symbol table;
 process body:
 construct syntax tree;
 traverse syntax tree and generate code for the function;
 deallocate syntax tree and local symbol table.
13
Code Generation: Approach
codeGen_stmt(synTree_node S)
{
switch (S.nodetype) {
case FOR: … ; break;
case WHILE : … ; break;
case IF: … ; break;
case ‘=‘ : … ; break;
…
}
codeGen_expr(synTree_node E)
{
switch (E.nodetype) {
case ‘+’: … ; break;
case ‘*’ : … ; break;
case ‘–’: … ; break;
case ‘/’ : … ; break;
…
}
Recursively traverse syntax tree:
 Node type determines action at each node;
 Code for each node is a (doubly linked) list of three-address instructions;
 Generate code for each node after processing its children
recursively process the children,
then generate code for this node
and glue it all together.
14
Intermediate Code Generation
supporting Routines:
 struct symtab_entry *newtemp(typename t)
creates a symbol table entry for new temporary variable each
time it is called, and returns a pointer to this ST entry.
 struct instr *newlabel()
returns a new label instruction each time it is called.
 struct instr *newinstr(arg1, arg2, …)
creates a new instruction, fills it in with the arguments supplied,
and returns a pointer to the result.
15
Intermediate Code Generation…
 struct symtab_entry *newtemp( t )
{
struct symtab_entry *ntmp = malloc( … ); /* check: ntmp == NULL? */
ntmp->name = …create a new name that doesn’t conflict…
ntmp->type = t;
ntmp->scope = LOCAL;
return ntmp;
}
 struct instr *newinstr(opType, src1, src2, dest)
{
struct instr *ninstr = malloc( … ); /* check: ninstr == NULL? */
ninstr->op = opType;
ninstr->src1 = src1; ninstr->src2 = src2; ninstr->dest = dest;
return ninstr;
}
16
Intermediate Code for a Function
Code generated for a function f:
 begin with ‘enter f ’, where f is a pointer to the function’s
symbol table entry:
 this allocates the function’s activation record;
 activation record size obtained from f ’s symbol table information;
 this is followed by code for the function body;
 generated using codeGen_stmt(…)
 each return in the body (incl. any implicit return at the end of
the function body) are translated to the code
leave f /* clean up: f a pointer to the function’s symbol table entry */
return /* + associated return value, if any */
17
Simple Expressions
Syntax tree node for expressions augmented with the
following fields:
 type: the type of the expression (or “error”);
 code: a list of intermediate code instructions for evaluating the expression.
 place: the location where the value of the expression will be kept at runtime:
18
Simple Expressions
Syntax tree node for expressions augmented with the
following fields:
 type: the type of the expression (or “error”);
 code: a list of intermediate code instructions for evaluating the expression.
 place: the location where the value of the expression will be kept at runtime:
 When generating intermediate code, this just refers to a symbol table entry for a
variable or temporary that will hold that value;
 The variable/temporary is mapped to an actual memory location when going from
intermediate to final code.
19
Simple Expressions 1
Syntax tree node E Action during intermediate code generation
codeGen_expr(E)
{ /* E.nodetype == INTCON; */
E.place = newtemp(E.type);
E.code = ‘E.place = intcon.val’;
}
codeGen_expr(E)
{ /* E.nodetype == ID; */
/* E.place is just the location of id (nothing more to do) */
E.code = NULL;
}
id
E
intcon
E
20
Simple Expressions 2
Syntax tree node E Action during intermediate code generation
codeGen_expr(E)
{
/* E.nodetype == UNARY_MINUS */
codeGen_expr(E1); /* recursively traverse E1, generate code for it */
E.place = newtemp( E.type ); /* allocate space to hold E’s value */
E.code = E1.code  newinstr(UMINUS, E1.place, NULL, E.place);
}
codeGen_expr(E)
{
/* E.nodetype == ‘+’ … other binary operators are similar */
codeGen_expr(E1);
codeGen_expr(E2); /* generate code for E1 and E2 */
E.place = newtemp( E.type ); /* allocate space to hold E’s value */
E.code = E1.code  E2.code  newinstr(PLUS, E1.place, E2.place, E.place );
}
–
E1
+
E1 E2
E
E
Semantic Analysis
From Code Form To Program Meaning
Compiler or Interpreter
Translation Execution
Source Code
Target Code
Interpre-
tation
Specification of Programming Languages
 PLs require precise definitions (i.e. no
ambiguity)
 Language form (Syntax)
 Language meaning (Semantics)
 Consequently, PLs are specified using formal
notation:
 Formal syntax
 Tokens
 Grammar
 Formal semantics
 Attribute Grammars (static semantics)
 Dynamic Semantics
The Semantic Analyzer
 The principal job of the semantic analyzer is
to enforce static semantic rules.
 In general, anything that requires the
compiler to compare things that are separate
by a long distance or to count things ends up
being a matter of semantics.
 The semantic analyzer also commonly
constructs a syntax tree (usually first), and
much of the information it gathers is needed
by the code generator.
Attribute Grammars
 Context-Free Grammars (CFGs) are used to
specify the syntax of programming
languages
 E.g. arithmetic expressions
• How do we tie these rules to
mathematical concepts?
• Attribute grammars are annotated
CFGs in which annotations are used to
establish meaning relationships among
symbols
– Annotations are also known as
decorations
Attribute Grammars
Example
 Each grammar
symbols has a set of
attributes
 E.g. the value of E1 is the
attribute E1.val
 Each grammar rule
has a set of rules over
the symbol attributes.
Attribute Flow
 Context-free grammars are not tied to an
specific parsing order
 E.g. Recursive descent, LR parsing
 Attribute grammars are not tied to an specific
evaluation order
 This evaluation is known as the annotation or decoration of
the parse tree
Attribute Flow
Example
 The figure shows the
result of annotating
the parse tree for
(1+3)*2
 Each symbols has at
most one attribute
shown in the
corresponding box
 Numerical value in this
example
 Operator symbols have
no value
 Arrows represent
attribute flow
Attribute Flow
Example
Static and Dynamic Semantics
 Attribute grammars add basic semantic rules
to the specification of a language
 They specify static semantics
 But they are limited to the semantic form that
can be checked at compile time
 Other semantic properties cannot be checked
at compile time
 They are described using dynamic semantics
Dynamic Semantics
 Use to formally specify the behavior of a
programming language
 Semantic-based error detection
 Correctness proofs
 There is not a universally accepted notation
 Operational semantics
 Executing statements that represent changes in the state of a real
or simulated machine
 Axiomatic semantics
 Using predicate calculus (pre and post-conditions)
 Denotational semantics
 Using recursive function theory

More Related Content

Similar to CC Week 11.ppt

Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course materialgadisaAdamu
 
Testing for share
Testing for share Testing for share
Testing for share Rajeev Mehta
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonTristan Penman
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdfSHUJEHASSAN
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 

Similar to CC Week 11.ppt (20)

C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
 
Parsing
ParsingParsing
Parsing
 
Testing for share
Testing for share Testing for share
Testing for share
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Ch8a
Ch8aCh8a
Ch8a
 
C# programming
C# programming C# programming
C# programming
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
python and perl
python and perlpython and perl
python and perl
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 

More from KamranAli649587

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfKamranAli649587
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing toolsKamranAli649587
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link listKamranAli649587
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologyKamranAli649587
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptxKamranAli649587
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptxKamranAli649587
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptKamranAli649587
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptKamranAli649587
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfKamranAli649587
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptKamranAli649587
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptKamranAli649587
 

More from KamranAli649587 (20)

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing tools
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link list
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptx
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptx
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.ppt
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.ppt
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdf
 
cluster.pptx
cluster.pptxcluster.pptx
cluster.pptx
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.ppt
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.ppt
 
null-13.pdf
null-13.pdfnull-13.pdf
null-13.pdf
 
Reaches
ReachesReaches
Reaches
 
null-6.pdf
null-6.pdfnull-6.pdf
null-6.pdf
 
null-1.pptx
null-1.pptxnull-1.pptx
null-1.pptx
 
Lect-01.ppt
Lect-01.pptLect-01.ppt
Lect-01.ppt
 
Db_05.ppt
Db_05.pptDb_05.ppt
Db_05.ppt
 
ch7.ppt
ch7.pptch7.ppt
ch7.ppt
 

Recently uploaded

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 

Recently uploaded (20)

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 

CC Week 11.ppt

  • 1. Compiler Construction Week 11 Intermediate Code Generation Semantic Analysis
  • 2. 2 Overview  Intermediate representations span the gap between the source and target languages:  closer to target language;  (more or less) machine independent;  allows many optimizations to be done in a machine-independent way.
  • 3. 3 Types of Intermediate Languages  High Level Representations (e.g., syntax trees):  closer to the source language  easy to generate from an input program  code optimizations may not be straightforward.  Low Level Representations (e.g., 3-address code):  closer to the target machine;  easier for optimizations, final code generation;
  • 4. 4 Syntax Trees A syntax tree shows the structure of a program by abstracting away irrelevant details from a parse tree.  Each node represents a computation to be performed;  The children of the node represents what that computation is performed on. Syntax trees separate parsing from subsequent processing.
  • 5. 5 Syntax Trees: Example Grammar : E  E + T | T T  T * F | F F  ( E ) | id Input: id + id * id Parse tree: Syntax tree:
  • 6. 6 Syntax Trees: Structure  Expressions:  leaves: identifiers or constants;  internal nodes are labeled with operators;  the children of a node are its operands.  Statements:  a node’s label indicates what kind of statement it is;  the children correspond to the components of the statement.
  • 7. 7 Constructing Syntax Trees General Idea: construct bottom-up using synthesized attributes. E → E + E { $$ = mkTree(PLUS, $1, $3); } S → if ‘(‘ E ‘)’ S OptElse { $$ = mkTree(IF, $3, $5, $6); } OptElse → else S { $$ = $2; } | /* epsilon */ { $$ = NULL; } S → while ‘(‘ E ‘)’ S { $$ = mkTree(WHILE, $3, $5); } mkTree(NodeType, Child1, Child2, …) allocates space for the tree node and fills in its node type as well as its children.
  • 8. 8 Three Address Code  Low-level IR  instructions are of the form ‘x = y op z,’ where x, y, z are variables, constants, or “temporaries”.  At most one operator allowed on RHS, so no ‘built-up” expressions. Instead, expressions are computed using temporaries (compiler-generated variables).
  • 9. 9 Three Address Code: Example  Source: if ( x + y*z > x*y + z) a = 0;  Three Address Code: tmp1 = y*z tmp2 = x+tmp1 // x + y*z tmp3 = x*y tmp4 = tmp3+z // x*y + z if (tmp2 <= tmp4) a = 0;
  • 10. CSc 453: Intermediate Code Generation 10 An Intermediate Instruction Set  Assignment:  x = y op z (op binary)  x = op y (op unary);  x = y  Jumps:  if ( x op y ) goto L (L a label);  goto L  Pointer and indexed assignments:  x = y[ z ]  y[ z ] = x  x = &y  x = *y  *y = x.  Procedure call/return:  param x, k (x is the kth param)  retval x  call p  enter p  leave p  return  retrieve x  Type Conversion:  x = cvt_A_to_B y (A, B base types) e.g.: cvt_int_to_float  Miscellaneous  label L
  • 11. 11 Three Address Code: Representation  Each instruction represented as a structure called a quadruple (or “quad”):  contains info about the operation, up to 3 operands.  for operands: use a bit to indicate whether constant or ST pointer. E.g.: x = y + z if ( x  y ) goto L
  • 12. 12 Code Generation: Approach  function prototypes, global declarations:  save information in the global symbol table.  function definitions:  function name, return type, argument type and number saved in global table (if not already there);  process formals, local declarations into local symbol table;  process body:  construct syntax tree;  traverse syntax tree and generate code for the function;  deallocate syntax tree and local symbol table.
  • 13. 13 Code Generation: Approach codeGen_stmt(synTree_node S) { switch (S.nodetype) { case FOR: … ; break; case WHILE : … ; break; case IF: … ; break; case ‘=‘ : … ; break; … } codeGen_expr(synTree_node E) { switch (E.nodetype) { case ‘+’: … ; break; case ‘*’ : … ; break; case ‘–’: … ; break; case ‘/’ : … ; break; … } Recursively traverse syntax tree:  Node type determines action at each node;  Code for each node is a (doubly linked) list of three-address instructions;  Generate code for each node after processing its children recursively process the children, then generate code for this node and glue it all together.
  • 14. 14 Intermediate Code Generation supporting Routines:  struct symtab_entry *newtemp(typename t) creates a symbol table entry for new temporary variable each time it is called, and returns a pointer to this ST entry.  struct instr *newlabel() returns a new label instruction each time it is called.  struct instr *newinstr(arg1, arg2, …) creates a new instruction, fills it in with the arguments supplied, and returns a pointer to the result.
  • 15. 15 Intermediate Code Generation…  struct symtab_entry *newtemp( t ) { struct symtab_entry *ntmp = malloc( … ); /* check: ntmp == NULL? */ ntmp->name = …create a new name that doesn’t conflict… ntmp->type = t; ntmp->scope = LOCAL; return ntmp; }  struct instr *newinstr(opType, src1, src2, dest) { struct instr *ninstr = malloc( … ); /* check: ninstr == NULL? */ ninstr->op = opType; ninstr->src1 = src1; ninstr->src2 = src2; ninstr->dest = dest; return ninstr; }
  • 16. 16 Intermediate Code for a Function Code generated for a function f:  begin with ‘enter f ’, where f is a pointer to the function’s symbol table entry:  this allocates the function’s activation record;  activation record size obtained from f ’s symbol table information;  this is followed by code for the function body;  generated using codeGen_stmt(…)  each return in the body (incl. any implicit return at the end of the function body) are translated to the code leave f /* clean up: f a pointer to the function’s symbol table entry */ return /* + associated return value, if any */
  • 17. 17 Simple Expressions Syntax tree node for expressions augmented with the following fields:  type: the type of the expression (or “error”);  code: a list of intermediate code instructions for evaluating the expression.  place: the location where the value of the expression will be kept at runtime:
  • 18. 18 Simple Expressions Syntax tree node for expressions augmented with the following fields:  type: the type of the expression (or “error”);  code: a list of intermediate code instructions for evaluating the expression.  place: the location where the value of the expression will be kept at runtime:  When generating intermediate code, this just refers to a symbol table entry for a variable or temporary that will hold that value;  The variable/temporary is mapped to an actual memory location when going from intermediate to final code.
  • 19. 19 Simple Expressions 1 Syntax tree node E Action during intermediate code generation codeGen_expr(E) { /* E.nodetype == INTCON; */ E.place = newtemp(E.type); E.code = ‘E.place = intcon.val’; } codeGen_expr(E) { /* E.nodetype == ID; */ /* E.place is just the location of id (nothing more to do) */ E.code = NULL; } id E intcon E
  • 20. 20 Simple Expressions 2 Syntax tree node E Action during intermediate code generation codeGen_expr(E) { /* E.nodetype == UNARY_MINUS */ codeGen_expr(E1); /* recursively traverse E1, generate code for it */ E.place = newtemp( E.type ); /* allocate space to hold E’s value */ E.code = E1.code  newinstr(UMINUS, E1.place, NULL, E.place); } codeGen_expr(E) { /* E.nodetype == ‘+’ … other binary operators are similar */ codeGen_expr(E1); codeGen_expr(E2); /* generate code for E1 and E2 */ E.place = newtemp( E.type ); /* allocate space to hold E’s value */ E.code = E1.code  E2.code  newinstr(PLUS, E1.place, E2.place, E.place ); } – E1 + E1 E2 E E
  • 21. Semantic Analysis From Code Form To Program Meaning Compiler or Interpreter Translation Execution Source Code Target Code Interpre- tation
  • 22. Specification of Programming Languages  PLs require precise definitions (i.e. no ambiguity)  Language form (Syntax)  Language meaning (Semantics)  Consequently, PLs are specified using formal notation:  Formal syntax  Tokens  Grammar  Formal semantics  Attribute Grammars (static semantics)  Dynamic Semantics
  • 23. The Semantic Analyzer  The principal job of the semantic analyzer is to enforce static semantic rules.  In general, anything that requires the compiler to compare things that are separate by a long distance or to count things ends up being a matter of semantics.  The semantic analyzer also commonly constructs a syntax tree (usually first), and much of the information it gathers is needed by the code generator.
  • 24. Attribute Grammars  Context-Free Grammars (CFGs) are used to specify the syntax of programming languages  E.g. arithmetic expressions • How do we tie these rules to mathematical concepts? • Attribute grammars are annotated CFGs in which annotations are used to establish meaning relationships among symbols – Annotations are also known as decorations
  • 25. Attribute Grammars Example  Each grammar symbols has a set of attributes  E.g. the value of E1 is the attribute E1.val  Each grammar rule has a set of rules over the symbol attributes.
  • 26. Attribute Flow  Context-free grammars are not tied to an specific parsing order  E.g. Recursive descent, LR parsing  Attribute grammars are not tied to an specific evaluation order  This evaluation is known as the annotation or decoration of the parse tree
  • 27. Attribute Flow Example  The figure shows the result of annotating the parse tree for (1+3)*2  Each symbols has at most one attribute shown in the corresponding box  Numerical value in this example  Operator symbols have no value  Arrows represent attribute flow
  • 29. Static and Dynamic Semantics  Attribute grammars add basic semantic rules to the specification of a language  They specify static semantics  But they are limited to the semantic form that can be checked at compile time  Other semantic properties cannot be checked at compile time  They are described using dynamic semantics
  • 30. Dynamic Semantics  Use to formally specify the behavior of a programming language  Semantic-based error detection  Correctness proofs  There is not a universally accepted notation  Operational semantics  Executing statements that represent changes in the state of a real or simulated machine  Axiomatic semantics  Using predicate calculus (pre and post-conditions)  Denotational semantics  Using recursive function theory