Leaders and Basic Blocks for a Three Address Code Program in C/C++

A compiler is a program that translates the code that is written in one language to a machine code without changing the logic of the program. The compiler also tries to make the program more efficient.

Compiler design principles give a detailed view of the translation and optimization process of a program. Compiler design covers everything from basic translation mechanism to recovery and error detection. It includes various methods like lexical, syntax, and semantic analysis as front end, and code generation and optimization as back-end.

In this post, we will write the program to find the Leaders and Basic Blocks for a Three Address Code.

We will use C++ to write this program due to the standard template library support. Although, it’s very similar to C.

Input

//input is from a file named inputleaders.txt
PROD = 0
I = 1
T2 = addr(A) – 4
T4 = addr(B) – 4
T1 = 4 x I
T3 = T2[T1]
T5 = T4[T1]
T6 = T3 x T5
PROD = PROD + T6
I = I + 1
IF I <=20 GOTO (5)

Output

Input:
1)  	PROD = 0
2)  	I = 1
3)  	T2 = addr(A) – 4
4)  	T4 = addr(B) – 4
5)  	T1 = 4 x I
6)  	T3 = T2[T1]
7)  	T5 = T4[T1]
8)  	T6 = T3 x T5
9)  	PROD = PROD + T6
10) 	I = I + 1
11) 	IF I <=20 GOTO (5)
 
 
Leaders:
1)  	PROD = 0
5)  	T1 = 4 x I
 
Blocks:
 
Block 1:
1)  	PROD = 0
2)  	I = 1
3)  	T2 = addr(A) – 4
4)  	T4 = addr(B) – 4
 
Block 2:
5)  	T1 = 4 x I
6)  	T3 = T2[T1]
7)  	T5 = T4[T1]
8)  	T6 = T3 x T5
9)  	PROD = PROD + T6
10) 	I = I + 1
11) 	IF I <=20 GOTO (5)

Program

#include<bits/stdc++.h>
using namespace std;

int main(){
	int i,j;
	ifstream fin("inputleaders.txt");
	string num;
	cout<<"Input: "<<'\n';
	int count = 1;
	vector<string> a;
	while(getline(fin,num)){
    	cout<<count<<")\t"<<num<<'\n';
    	a.push_back(num);
    	count++;
	}
	map<int,int> mp;
	mp[0] = 0;

	int curr = 0;
	for(auto q : a){
    	for(i=0;i<q.size()-3;i++){
        	if((q[i] == 'g'&&q[i+1] == 'o'&&q[i+2] == 't'&&q[i+3] == 'o')||(q[i] == 'G'&&q[i+1] == 'O'&&q[i+2] == 'T'&&q[i+3] == 'O')){
            	string num = "";
            	int j = i+6;
            	while(q[j]>='0'&&q[j]<='9'&&j<q.size()) num+=q[j],j++;
            	int temp = stoi(num);
            	mp[temp-1] = 1;
        	}
    	}
    	curr++;
	}


	vector<pair<int,string>> leaders;
	vector<vector<pair<int,string>>> blocks;
	for(auto q : mp){
    	leaders.push_back({q.first+1,a[q.first]});
	}
	vector<pair<int,string>> temp;
	for(i=0;i<a.size();i++){
    	if(mp.count(i)){
        	blocks.push_back(temp);
        	temp.clear();
    	}
    	temp.push_back({i+1,a[i]});
	}
	blocks.push_back(temp);

	cout<<"\n\nLeaders:"<<'\n';
	for(auto q : leaders) cout<<q.first<<")\t"<<q.second<<'\n';

	cout<<"\nBlocks:\n"<<'\n';

	for(i=1;i<blocks.size();i++){
    	cout<<"Block "<<i<<": "<<'\n';
    	for(auto q : blocks[i]) cout<<q.first<<")\t"<<q.second<<'\n';
    	cout<<'\n';
	}
	return 0;
}

Let us know in the comments if you are having any questions regarding this compiler design program.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

Leave a Reply