0% found this document useful (0 votes)
11 views

Shri Ramdeobaba College of Engineering and Management, Nagpur

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Shri Ramdeobaba College of Engineering and Management, Nagpur

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Shri Ramdeobaba College of Engineering and Management, Nagpur

Department of CSE – Cyber Security


Session: 2023-2024
Compiler Design Lab

PRACTICAL No. 7
Aim:
a. Write TAC to identify whether the number is prime or not.
b. Write a program to find leader statement, basic blocks, program flow graph &
dominators.
c. Identify the Generate and Kill function for each block to be used in Elimination of
loop invariant computation

Hint:
GEN (B): Set of all definitions generated in Block B.
KILL (B): Set of all definitions outside Block B that define the same variables which are defined
in Block B.

Input: Three address code statements.

Output: 1) Leader Statements


2) Basic blocks
3) Program flow graph indicating the successor & predecessor.
4) Dominators of all the basic blocks
5) Natural loop detection

Sample input: 3AC


1. count = 0
2. Result = 0
3. If count > 20 GOTO 8
4. count=count + 1
5. increment = 2 * count
6. result = result +increment
7. GOTO 3
8. end

Sample Output:
A.
The leader statements are:
1) count=0
3) If count > 20 GOTO 8
4) count=count + 1
8) end

The Basic blocks are:


B1: contains: 1 & 2
B2 : contains 3
B3 : contains 4 5 6 7
B4 : contains 8

The PFG is
B1->B2
B2->B3
B2->B4
B3->B2

The dominators of all basic block are:


B1 →

B.
GEN(B1) = [1,2]
GEN(B2) = [3]
GEN(B3) = [4,5,6,7]
GEN(B4) = [8]

KILL(B1) = [4,6]
KILL(B2) = [Φ]
KILL(B3) = [1,2]
KILL(B4) = [Φ]

1.
1. Input n
2. i = 2
3. is_prime = 1
4. while i * i <= n:
5. if n % i == 0:
6. is_prime = 0
7. break
8. i = i + 1
9. Output is_prime

3.
Code:
package main

import (
"fmt"
"strings"
)

type BasicBlock struct {


Statements []int
}

type ProgramFlowGraph struct {


Successor map[int]int
Predecessor map[int]int
}

func ComputeGenKill(statements []string) (map[int][]int, map[int][]int) {


gen := make(map[int][]int)
kill := make(map[int][]int)

for i, stmt := range statements {


if strings.Contains(stmt, "GOTO") || strings.Contains(stmt, "end") {
// This is the last statement of the block
gen[i+1] = []int{i + 1}
kill[i+1] = []int{}
} else {
gen[i+1] = []int{i + 1}
kill[i+1] = []int{1, 2}
}
}

kill[1] = []int{4, 6}

return gen, kill


}

func main() {
statements := []string{
"count = 0",
"Result = 0",
"If count > 20 GOTO 8",
"count=count + 1",
"increment = 2 * count",
"result = result +increment",
"GOTO 3",
"end",
}

gen, kill := ComputeGenKill(statements)

fmt.Println("GEN sets:")
for i := 1; i <= len(statements); i++ {
fmt.Printf("GEN(B%d) = %v\n", i, gen[i])
}

fmt.Println("\nKILL sets:")
for i := 1; i <= len(statements); i++ {
fmt.Printf("KILL(B%d) = %v\n", i, kill[i])
}
}
OUTPUT:

You might also like