-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
129 lines (98 loc) · 3.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import copy
#<---------------------------------File opening-------------------------------------------->
#open input file
f = open("./Inputs/sample_input3.v","r")
f = f.read()
f= f.split('\n')
# Initialization of global variables
line_word = []
contents = []
line_counter = 0
temp=0
parameterr=':'
#<---------------------------------File opening End-------------------------------------------->
#<---------------------------------Functions Definition-------------------------------------------->
#function to return the number of bits to be abstracted in
def bits(b):
if b<=1:
return b
if b<4:
return 2
elif b<=16:
return 4
elif b<=64:
return 8
return b/10
#function to remove comment
def comment_removal(statement):
return statement[:statement.index('//')]
#function to replace brackets
def bracketReplacer(contents):
new_contents = copy.deepcopy(contents)
for con in new_contents:
sentence = con[1]
if ':' in sentence:
new_range = list(map(bits,[int(i) for i in sentence.split(":")]))
new_range = [str(i) for i in new_range]
sentence = ':'.join(new_range)
con[1] = sentence
return new_contents
def line_replacer(find,statement):
return statement.replace(contents[find][1],new_contents[find][1])
#function to check if a character is a indentation
def isIndentation(char):
return char == ' ' or char == '\t'
#<---------------------------------Functions Definition End-------------------------------------------->
#<---------------------------------abstraction logic-------------------------------------------->
for statement in f:
if '//' in statement:
#we need to remove the comment if there is any comment at the end of the statement
statement = comment_removal(statement)
if statement.strip() == ' ':
#the statement is empty
line_counter += 1
continue
bracket_content = ''
paranthesis = 0
words = statement.split(" ")
if words[0] == "parameter" :
line_word.append(words)
#checking further register whose indices to be truncated
for character in statement:
if isIndentation(character):
continue
if character == '[' or character == ']':
paranthesis = (1 if (character == '[') else 0)
elif paranthesis == 1:
bracket_content += character
if bracket_content != '':
constraintss = 0
if parameterr in bracket_content:
constraintss = bracket_content.split(parameterr)
#if the index size is variable
for k in line_word:
dmiLit=len(constraintss[0])
if isinstance(constraintss[0],str):
if k[1] == constraintss[0][:dmiLit-2]:
constraintss[0] = str(int(k[3])-1)
bracket_content = constraintss[0] + ":"
bracket_content =bracket_content + ''.join(constraintss[1:])
elif bracket_content.isdigit():
continue
contents.append([line_counter,bracket_content])
line_counter +=1
new_contents = bracketReplacer(contents)
abstracted_model = open("./Outputs/abstracted_model3.v",'w')
line_counter = 0
find = 0
#<--------------------------------building new abstracted model------------------------------------->
temp=len(contents)
for statement in f:
temp=len(contents)
if temp > find:
if line_counter == contents[find][0]:
statement = line_replacer(find,statement)
find+=1
line_counter +=1
abstracted_model.write(statement+'\n')
#<--------------------------------------------The END------------------------------------------------>