-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.py
332 lines (316 loc) · 11.6 KB
/
memory.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from collections import deque #Para el stack de scopes
global_int = 0
global_float = 5000
global_string = 10000
global_bool = 15000
global_object = 20000
local_int = 25000
local_float = 30000
local_string = 35000
local_bool = 40000
local_object = 45000
temporal_int = 50000
temporal_float = 55000
temporal_string = 60000
temporal_bool = 65000
temporal_object = 70000
constant_int = 75000
constant_float = 80000
constant_string = 85000
constant_bool = 90000
constant_object = 95000
GLOBAL_START = 0
LOCAL_START = 25000
TEMPORAL_START = 50000
CONSTANT_START = 75000
CONSTANT_LIMIT = 95000
class memory():
def __init__(self, value='', address='', array_block=None):
self.value = value
self.address= address
self.array_block = array_block
def __str__(self):
return f'Value: {self.value}, Addr: {self.address}\n'
def __repr__(self):
return f'Value: {self.value}, Addr: {self.address}\n'
call_stack = deque()
class func_memory():
def __init__(self, function_name='', cont=-1, memory_list={}, params={}):
self.function_name = function_name
self.cont= cont
self.prev = 0
self.memory_list = memory_list.copy()
self.params = params.copy()
def __str__(self):
return f'Function Name: {self.function_name}, Cont: {self.cont}, Memory List: {self.memory_list}, Params: {self.params}, Prev: {self.prev}\n'
def __repr__(self):
return f'Function Name: {self.function_name}, Cont: {self.cont}, Memory List: {self.memory_list}, Params: {self.params}, Prev: {self.prev}\n'
function_list = {} # This is used basically to store the functions generated in init_memory
memory_table = {} # It is used to store the constant values but with the key->address
constant_table= {} # It is used to store the constant values
def init_memory(func_dir):
global call_stack, param_counter
for func_key in func_dir:
func = func_dir[func_key]
memory_list = {}
params = {}
current_vars = func.vars
current_params = func.params
param_counter = len(current_params) - 1
for v in current_vars.values():
if v.type == 'list':
addr = v.address #we store the base address
memory_list[v.address] = memory(v.address,v.address, v.array_block)
for i in range(0,memory_table[v.array_block.right].value):
# we generate the memory of the array with the default value
addr = addr + 1
memory_list[addr] = memory(get_default_value(v.array_block.array_type), addr)
else:
memory_list[v.address] = memory(get_default_value(v.type),v.address)
for p in current_params:
# TODO arrays in params
params[param_counter] = memory(get_default_value(p.type),p.address)
param_counter-=1
function_list[func_key] = func_memory(func_key,func.cont, memory_list,params)
if func_key == "global":
call_stack.append(func_memory(func_key,func.cont, memory_list,params))
# Creates an instance of func memory
def create_func_memory(id):
temp = function_list[id]
temp_mem = temp.memory_list.copy()
temp_params = temp.params.copy()
copy_mem = {}
copy_params = {}
param_counter = len(temp_params)-1
for v in temp_mem.values():
copy_mem[v.address] = memory(v.value,v.address)
for p in temp_params.values():
copy_params[param_counter] = memory(p.value,p.address)
param_counter-=1
fm = func_memory(temp.function_name, temp.cont,temp_mem ,copy_params )
return fm
# Gets the default value depending of the type
def get_default_value(t):
if t == 'i':
return 0
elif t == 's':
return ""
elif t == 'f':
return 0
elif t == 'b':
return False
elif t == 'o':
return None
# Gets the constant address
def get_const_address(value, type):
if value in constant_table.keys():
return constant_table[value].address
address = get_next_constant(type)
new_value = memory(value, address)
constant_table[value] = new_value
memory_table[address] = new_value
return address
# Gets the next global
def get_next_global(t):
if(t == "i"):
global global_int
global_int = global_int + 1
if global_int > LOCAL_START:
raise Exception("Global Memory Limit Exceeded")
return global_int
elif (t == "f"):
global global_float
global_float = global_float + 1
if global_float > LOCAL_START:
raise Exception("Global Memory Limit Exceeded")
return global_float
elif (t == "b"):
global global_bool
global_bool = global_bool + 1
if global_bool > LOCAL_START:
raise Exception("Global Memory Limit Exceeded")
return global_bool
elif (t == "s"):
global global_string
global_string = global_string + 1
if global_string > LOCAL_START:
raise Exception("Global Memory Limit Exceeded")
return global_string
elif (t == "o"):
global global_object
global_object = global_object + 1
if global_object > LOCAL_START:
raise Exception("Global Memory Limit Exceeded")
return global_object
# Gets the next local
def get_next_local(t):
if(t == "i"):
global local_int
local_int = local_int + 1
if local_int > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_int
elif (t == "f"):
global local_float
local_float = local_float + 1
if local_float > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_float
elif (t == "b"):
global local_bool
local_bool = local_bool + 1
if local_bool > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_bool
elif (t == "s"):
global local_string
local_string = local_string + 1
if local_string > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_string
elif (t == "o"):
global local_object
local_object = local_object + 1
if local_object > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_object
# Gets the next local for lists
def get_next_local_list(t, dim):
dimension = memory_table[dim].value
if(t == "i"):
global local_int
local_int_aux = local_int + 1
local_int = local_int + (dimension + 1)
if local_int > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_int_aux
elif (t == "f"):
global local_float
local_float_aux = local_float + 1
local_float = local_float + (dimension + 1)
if local_float > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_float
elif (t == "b"):
global local_bool
local_bool_aux = local_bool + 1
local_bool = local_bool + (dimension + 1)
if local_bool > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_bool_aux
elif (t == "s"):
global local_string
local_string_aux = local_string + 1
local_string = local_string + (dimension + 1)
if local_string > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_string_aux
elif (t == "o"):
global local_object
local_object_aux = local_object + 1
local_object = local_object + (dimension + 1)
if local_object > TEMPORAL_START:
raise Exception("Local Memory Limit Exceeded")
return local_object_aux
# Gets next global for list
def get_next_global_list(t, dim):
dimension = memory_table[dim].value
if(t == "i"):
global global_int
global_int_aux = global_int + 1
global_int = global_int + (dimension + 1)
if global_int > LOCAL_START:
raise Exception("Local Memory Limit Exceeded")
return global_int_aux
elif (t == "f"):
global global_float
global_float_aux = global_float + 1
global_float = global_float + (dimension + 1)
if global_float > LOCAL_START:
raise Exception("Local Memory Limit Exceeded")
return global_float_aux
elif (t == "b"):
global global_bool
global_bool_aux = global_bool + 1
global_bool = global_bool + (dimension + 1)
if global_bool > LOCAL_START:
raise Exception("Local Memory Limit Exceeded")
return global_bool_aux
elif (t == "s"):
global global_string
global_string_aux = global_string + 1
global_string = global_string + (dimension + 1)
if global_string > LOCAL_START:
raise Exception("Local Memory Limit Exceeded")
return global_string_aux
elif (t == "o"):
global global_object
global_object_aux = global_object + 1
global_object = global_object + (dimension + 1)
if global_object > LOCAL_START:
raise Exception("Local Memory Limit Exceeded")
return global_object_aux
# Get next temporal
def get_next_temporal(t):
if(t == "i"):
global temporal_int
temporal_int = temporal_int + 1
if temporal_int > CONSTANT_START:
raise Exception("Temporal Memory Limit Exceeded")
return temporal_int
elif (t == "f"):
global temporal_float
temporal_float = temporal_float + 1
if temporal_float > CONSTANT_START:
raise Exception("Temporal Memory Limit Exceeded")
return temporal_float
elif (t == "b"):
global temporal_bool
temporal_bool = temporal_bool + 1
if temporal_bool > CONSTANT_START:
raise Exception("Temporal Memory Limit Exceeded")
return temporal_bool
elif (t == "s"):
global temporal_string
temporal_string = temporal_string + 1
if temporal_string > CONSTANT_START:
raise Exception("Temporal Memory Limit Exceeded")
return temporal_string
elif (t == "o"):
global temporal_object
temporal_object = temporal_object + 1
if temporal_object > CONSTANT_START:
raise Exception("Temporal Memory Limit Exceeded")
return temporal_object
# Get next constant
def get_next_constant(t):
if(t == "i"):
global constant_int
constant_int = constant_int + 1
if constant_int > CONSTANT_LIMIT:
raise Exception("Constant Memory Limit Exceeded")
return constant_int
elif (t == "f"):
global constant_float
constant_float = constant_float + 1
if constant_float > CONSTANT_LIMIT:
raise Exception("Constant Memory Limit Exceeded")
return constant_float
elif (t == "b"):
global constant_bool
constant_bool = constant_bool + 1
if constant_bool > CONSTANT_LIMIT:
raise Exception("Constant Memory Limit Exceeded")
return constant_bool
elif (t == "s"):
global constant_string
constant_string = constant_string + 1
if constant_string > CONSTANT_LIMIT:
raise Exception("Constant Memory Limit Exceeded")
return constant_string
elif (t == "o"):
global constant_object
constant_object = constant_object + 1
if constant_object > CONSTANT_LIMIT:
raise Exception("Constant Memory Limit Exceeded")
return constant_object