-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathscratch_tracker.py
84 lines (61 loc) · 2.06 KB
/
scratch_tracker.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
class scratch_tracker(object):
def __init__(self, global_context):
self.scratch = {}
self.temp = {}
self.global_context = global_context
self.scratch_allocation = 0
self.temp_allocation = 0
self.prefix = ''
def get_temp_var(self):
for key in self.temp.keys():
if self.temp[key] == False:
self.temp[key] = True
return "temp" + str(key)
newScratch = len(self.temp.keys())
self.temp[newScratch] = True
new_length = len(self.temp.keys())
if new_length > self.temp_allocation:
self.temp_allocation = new_length
self.global_context.allocate_temp(new_length)
return f'temp{newScratch}'
def free_temp_var(self, id):
num = int(id[len('temp'):])
self.temp[num] = False
def get_scratch(self):
for key in self.scratch.keys():
if self.scratch[key] == False:
self.scratch[key] = True
return f'{self.prefix}_scratch{key}'
newScratch = len(self.scratch.keys())
self.scratch[newScratch] = True
new_length = len(self.scratch.keys())
if new_length > self.scratch_allocation:
self.scratch_allocation = new_length
self.global_context.allocate_scratch(self.prefix, new_length)
return f'{self.prefix}_scratch{newScratch}'
def get_scratch_vector(self):
return [self.get_scratch() for i in range(3)]
def get_prefix(self):
return f'{self.prefix}_scratch'
def is_scratch(self, id):
scratch_prefix = self.get_prefix()
return id.startswith(scratch_prefix)
def free_scratch(self, id):
if not self.is_scratch(id):
return
scratch_prefix = self.get_prefix()
num = int(id[len(scratch_prefix):])
self.scratch[num] = False
def get_allocated_variables(self):
ret = [f'{self.prefix}_scratch{i}'for i in range(self.scratch_allocation)]
ret += [f'temp{i}' for i in range(self.temp_allocation)]
return ret
def get_active_objectives(self):
all_objectives = []
for num in self.scratch:
if self.scratch[num]:
all_objectives.append(f'{self.prefix}_scratch{num}')
for num in self.temp:
if self.temp[num]:
all_objectives.append(f'temp{num}')
return all_objectives