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

Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
bolovv
 

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

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
KamranAli649587
 
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
KamranAli649587
 
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
KamranAli649587
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdf
KamranAli649587
 

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

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 

Recently uploaded (20)

Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 

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