SlideShare a Scribd company logo
1 of 11
Basic Blocks




    BY:SHIVPRASAD U SHETTI
Three Address Code
 Three-address code (often abbreviated to TAC or 3AC) is an intermediate
code used by     compilers to aid in the implementation of code-improving
transformations.

 Three Address Code is sequence of statements, typically of a general form A:= B
op C, where A,B , and C are either programmer-Defined names, constants or
compiler-generated temporary names; op stands for any operator, such as a fixed- or
floating point arithmetic operator, or a logical operator on Boolean-valued data.

 The reason for the name “Three –address code” is that each statements usually
contains three addresses, two for the operands and one for result.

 Example: Write a Three Address Code for following expression: “ X+Y*Z ”
                Solution: T1 := Y * Z
                        T2 := X + T1
           Where T1 and T2 are compiler-generated temporary names.
TAC Example

                    Three Address Code Computing Dot product

BEGIN                                  1) PROD :=0
                                       2) I:=1
   PROD := 0;
   I := 1;                             3) T1 := 4*I
                                       4) T2 := addr(A) – 4
   DO
         BEGIN                         5) T3 := T2[T1]          /*
                                          Compute A[I] */
            PROD :=PROD +
A[I] *B[I];                            6) T4 := addr(B) - 4
            I :=I+1                    7) T5 := T4[T1]          /* Compute
         END                              B[I] */
    WHILE I ≤ 20                       8) T6 := T3 * T5
END                                    9) PROD := PROD + T6
                                       10) I := I+1
                                       11) If I ≤ 20 goto (3)
Algorithm: Partition into Basic Blocks.

Basic Blocks
                                             Input: A sequence of Three address statements.
  Our first step is to break the code       Output: A list of basic blocks with each three
 of     TAC Example in to basic              address statement in exactly one block.
 blocks,   that   is    sequence      of     Method:
 consecutive statements which may be         1) We first determine the set of leaders, the first
                                                statements of basic blocks the rules we use
 entered only at the beginning, and             are as follow”
 when entered are executed in                     i) The first statement is a leader
 sequence without halt or possibility of          ii) Any statement which is the target of a
 branch(except at the end of the basic          conditional or unconditional goto is a
 block).                                        leader
                                                  iii) Any statement which immediately
  A     useful         algorithms  for         follows a conditional goto is a leader
 partitioning three address statements       2) For each leader construct its basic
                                                block, which consists of the leader and all the
 into basic blocks is the following             statements up to but not including the next
                                                leader or the end of the program .Any
                                                statements not placed in a block can ever be
                                                executed and may now be removed, if
                                                desired.
Basic Block

 A basic block is a sequence of consecutive statements in which flow of control
  enters at the beginning and leaves at the end without halt or possibility of
  branching except at the end.
 The code in a basic block has:
 one entry point, meaning no code within it is the destination of a jump
  instruction anywhere in the program;
 one exit point, meaning only the last instruction can cause the program to
  begin executing code in a different basic block.

 A list of basic blocks with each three-address statement in exactly one block.
 Step 1. Identify the leaders in the code. Leaders are instructions which come
  under any of the following 3 categories :
 The first instruction is a leader.
 The target of a conditional or an unconditional goto/jump instruction is a
  leader.
 The instruction that immediately follows a conditional or an unconditional
  goto/jump instruction is a leader.
 Step 2. Starting from a leader, the set of all following instructions until and not
  including the next leader is the basic block corresponding to the starting leader
 Thus every basic block has a leader.
 BASIC BLOCKS

              A basic block is a sequence of consecutive
    statements in which flow of control enters at the beginning
    and leaves at the end without halt or possibility of
    branching except at the end. The following sequence of
    three-address statements forms a basic block:

   t1 := a*a
   t2 := a*b
   t3 := 2*t2
   t4 := t1+t3
   t5 := b*b
   t6 := t4+t5
 A three-address statement x := y+z is said to define x and to use y or z. A
  name in a basic block is said to live at a given point if its value is used after
  that point in the program, perhaps in another basic block.
 The following algorithm can be used to partition a sequence of three-address
  statements into basic blocks.
 Example 3: Consider the fragment of source code shown in fig. 7; it
    computes the dot product of two vectors a and b of length 20. A list of three-
    address statements performing this computation on our target machine is
    shown in fig. 8.
              begin
                 prod := 0;
                 i := 1;
                 do begin
                     prod := prod + a[i] * b[i];
                     i := i+1;
                  end
                   while i<= 20
               end
   fig 7: program to compute dot product
    Let us apply Algorithm 1 to the three-address code in fig 8 to determine its basic
    blocks. statement (1) is a leader by rule (I) and statement (3) is a leader by rule (II), since the
    last statement can jump to it. By rule (III) the statement following (12) is a leader.
    Therefore, statements (1) and (2) form a basic block. The remainder of the program beginning
    with statement (3) forms a second basic block.

   (1)    prod := 0
   (2)    i := 1
   (3)    t1 := 4*i
   (4)    t2 := a [ t1 ]
   (5)    t3 := 4*i
   (6)    t4 :=b [ t3 ]
   (7)    t5 := t2*t4
   (8)    t6 := prod +t5
   (9)    prod := t6
   (10)   t7 := i+1
   (11)   i := t7
   (12)    if i<=20 goto (3)

   fig 8. Three-address code computing dot product
•
    PROD :=0
•           B1
    I := 1




    T1:=4*I
    T2:=addr(A)-4
•   T3:=T2[T1]B2
    T4:=addr(B)-4
    T5:=T4[T1]
    T6:=T3*T5
    Prod:=PROD + T6
    I:=I+1
    If I<20 goto (3)

More Related Content

What's hot

Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternBharat Rathore
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Codesanchi29
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer servicesMelvin Cabatuan
 
Covering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmCovering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmZHAO Sam
 
8086 Interrupts & With DOS and BIOS by vijay
8086 Interrupts &  With DOS and BIOS  by vijay8086 Interrupts &  With DOS and BIOS  by vijay
8086 Interrupts & With DOS and BIOS by vijayVijay Kumar
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler designAnul Chaudhary
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
 
Memory mapped I/O and Isolated I/O
Memory mapped I/O and Isolated I/OMemory mapped I/O and Isolated I/O
Memory mapped I/O and Isolated I/OBharat Kharbanda
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 

What's hot (20)

Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 
A* algorithm
A* algorithmA* algorithm
A* algorithm
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & Pattern
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer services
 
Covering (Rules-based) Algorithm
Covering (Rules-based) AlgorithmCovering (Rules-based) Algorithm
Covering (Rules-based) Algorithm
 
8086 Interrupts & With DOS and BIOS by vijay
8086 Interrupts &  With DOS and BIOS  by vijay8086 Interrupts &  With DOS and BIOS  by vijay
8086 Interrupts & With DOS and BIOS by vijay
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Memory mapped I/O and Isolated I/O
Memory mapped I/O and Isolated I/OMemory mapped I/O and Isolated I/O
Memory mapped I/O and Isolated I/O
 
Code generation
Code generationCode generation
Code generation
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Computer networks - Channelization
Computer networks - ChannelizationComputer networks - Channelization
Computer networks - Channelization
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 

Similar to Basic Block

Code optimization lecture
Code optimization lectureCode optimization lecture
Code optimization lecturePrashant Singh
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphsTilakpoudel2
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphsdaimk2020
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphsdaimk2020
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design LogsAk
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler designhmnasim15
 
Embedded system (Chapter )
Embedded system (Chapter )Embedded system (Chapter )
Embedded system (Chapter )Ikhwan_Fakrudin
 
Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1Ikhwan_Fakrudin
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questionsSANTOSH RATH
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)Sourov Kumar Ron
 
Lcdf4 chap 03_p2
Lcdf4 chap 03_p2Lcdf4 chap 03_p2
Lcdf4 chap 03_p2ozgur_can
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12chinthala Vijaya Kumar
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationWei-Ren Chen
 
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdfCode In PythonFile 1 main.pyYou will implement two algorithms t.pdf
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdfaishwaryaequipment
 
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdfankit482504
 

Similar to Basic Block (20)

Code optimization lecture
Code optimization lectureCode optimization lecture
Code optimization lecture
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphs
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
Embedded system (Chapter )
Embedded system (Chapter )Embedded system (Chapter )
Embedded system (Chapter )
 
Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questions
 
ERTS UNIT 3.ppt
ERTS UNIT 3.pptERTS UNIT 3.ppt
ERTS UNIT 3.ppt
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)
 
Lcdf4 chap 03_p2
Lcdf4 chap 03_p2Lcdf4 chap 03_p2
Lcdf4 chap 03_p2
 
Assignment
AssignmentAssignment
Assignment
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdfCode In PythonFile 1 main.pyYou will implement two algorithms t.pdf
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf
 
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
1.5 Legal Labels in Verilog areSystem Verilog extends it and al.pdf
 

Recently uploaded

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Recently uploaded (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Basic Block

  • 1. Basic Blocks BY:SHIVPRASAD U SHETTI
  • 2. Three Address Code  Three-address code (often abbreviated to TAC or 3AC) is an intermediate code used by compilers to aid in the implementation of code-improving transformations.  Three Address Code is sequence of statements, typically of a general form A:= B op C, where A,B , and C are either programmer-Defined names, constants or compiler-generated temporary names; op stands for any operator, such as a fixed- or floating point arithmetic operator, or a logical operator on Boolean-valued data.  The reason for the name “Three –address code” is that each statements usually contains three addresses, two for the operands and one for result.  Example: Write a Three Address Code for following expression: “ X+Y*Z ” Solution: T1 := Y * Z T2 := X + T1 Where T1 and T2 are compiler-generated temporary names.
  • 3. TAC Example Three Address Code Computing Dot product BEGIN 1) PROD :=0 2) I:=1 PROD := 0; I := 1; 3) T1 := 4*I 4) T2 := addr(A) – 4 DO BEGIN 5) T3 := T2[T1] /* Compute A[I] */ PROD :=PROD + A[I] *B[I]; 6) T4 := addr(B) - 4 I :=I+1 7) T5 := T4[T1] /* Compute END B[I] */ WHILE I ≤ 20 8) T6 := T3 * T5 END 9) PROD := PROD + T6 10) I := I+1 11) If I ≤ 20 goto (3)
  • 4. Algorithm: Partition into Basic Blocks. Basic Blocks Input: A sequence of Three address statements.  Our first step is to break the code Output: A list of basic blocks with each three of TAC Example in to basic address statement in exactly one block. blocks, that is sequence of Method: consecutive statements which may be 1) We first determine the set of leaders, the first statements of basic blocks the rules we use entered only at the beginning, and are as follow” when entered are executed in i) The first statement is a leader sequence without halt or possibility of ii) Any statement which is the target of a branch(except at the end of the basic conditional or unconditional goto is a block). leader iii) Any statement which immediately  A useful algorithms for follows a conditional goto is a leader partitioning three address statements 2) For each leader construct its basic block, which consists of the leader and all the into basic blocks is the following statements up to but not including the next leader or the end of the program .Any statements not placed in a block can ever be executed and may now be removed, if desired.
  • 5. Basic Block  A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branching except at the end.  The code in a basic block has:  one entry point, meaning no code within it is the destination of a jump instruction anywhere in the program;  one exit point, meaning only the last instruction can cause the program to begin executing code in a different basic block.  A list of basic blocks with each three-address statement in exactly one block.  Step 1. Identify the leaders in the code. Leaders are instructions which come under any of the following 3 categories :  The first instruction is a leader.  The target of a conditional or an unconditional goto/jump instruction is a leader.  The instruction that immediately follows a conditional or an unconditional goto/jump instruction is a leader.
  • 6.  Step 2. Starting from a leader, the set of all following instructions until and not including the next leader is the basic block corresponding to the starting leader  Thus every basic block has a leader.
  • 7.  BASIC BLOCKS  A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branching except at the end. The following sequence of three-address statements forms a basic block:   t1 := a*a  t2 := a*b  t3 := 2*t2  t4 := t1+t3  t5 := b*b  t6 := t4+t5
  • 8.  A three-address statement x := y+z is said to define x and to use y or z. A name in a basic block is said to live at a given point if its value is used after that point in the program, perhaps in another basic block.  The following algorithm can be used to partition a sequence of three-address statements into basic blocks.
  • 9.  Example 3: Consider the fragment of source code shown in fig. 7; it computes the dot product of two vectors a and b of length 20. A list of three- address statements performing this computation on our target machine is shown in fig. 8.  begin  prod := 0;  i := 1;  do begin  prod := prod + a[i] * b[i];  i := i+1;  end  while i<= 20  end  fig 7: program to compute dot product
  • 10. Let us apply Algorithm 1 to the three-address code in fig 8 to determine its basic blocks. statement (1) is a leader by rule (I) and statement (3) is a leader by rule (II), since the last statement can jump to it. By rule (III) the statement following (12) is a leader. Therefore, statements (1) and (2) form a basic block. The remainder of the program beginning with statement (3) forms a second basic block.   (1) prod := 0  (2) i := 1  (3) t1 := 4*i  (4) t2 := a [ t1 ]  (5) t3 := 4*i  (6) t4 :=b [ t3 ]  (7) t5 := t2*t4  (8) t6 := prod +t5  (9) prod := t6  (10) t7 := i+1  (11) i := t7  (12) if i<=20 goto (3)   fig 8. Three-address code computing dot product
  • 11. PROD :=0 • B1 I := 1 T1:=4*I T2:=addr(A)-4 • T3:=T2[T1]B2 T4:=addr(B)-4 T5:=T4[T1] T6:=T3*T5 Prod:=PROD + T6 I:=I+1 If I<20 goto (3)