-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.py
executable file
·392 lines (358 loc) · 10.3 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from label import Label
class MemoryNode:
## mem_type : temp, const, variable
def __init__(self, name, mem_type):
self.name = name
self.mem_type = mem_type
class Memory2:
def __init__(self):
self.memDict = {}
def hasNode(self, name, mem_type):
return self.memDict.has_key((name, mem_type))
def createNode(self, name, mem_type):
self.memDict[(name, mem_type)] = MemoryNode(name, mem_type)
return self.memDict[(name, mem_type)]
def getNode(self, name, mem_type):
return self.memDict[(name, mem_type)]
def node(self, name, mem_type):
if self.memDict.has_key((name,mem_type)):
return self.memDict[(name, mem_type)]
else:
self.createNode(name, mem_type)
def newNode(self, mem_type):
pass
class Memory:
def __init__(self):
self.memDict = {}
self.memList = []############TODO
self.tempDict = {}
self.tempCount = 0
self.tempAvailable = []
self.ptr2reg = {}
self.reg_count = 0
def allocate(self, name, size = 1):
if(name in self.memDict):
return False
self.memDict[name] = len(self.memList)
self.memList.append(name + ": 0")
for _ in range(1, size):
self.memList.append("0")
def const(self, i):
name = str(i)##################### $
if(name in self.memDict):
return name
else:
self.memDict[name] = len(self.memList)
self.memList.append(name+": "+str(i))
return name
def const2i(self, name):
return int(name)#name[1:])
def ptr(self, addr):
if addr in self.memDict:
return addr
elif addr in self.ptr2reg:
return self.ptr2reg[addr]
else:
self.reg_count += 1
self.ptr2reg[addr] = "reg_"+str(self.reg_count)
self.memDict[self.ptr2reg[addr]] = len(self.memList)
self.memList.append(self.ptr2reg[addr]+": 0")
return self.ptr2reg[addr]
############!!!!!!!!!!!!!!!!!!!!!!
def ptr0(self, addr):
if addr in self.memDict:
return addr
else:
self.memDict[addr] = len(self.memList)
self.memList.append(addr+": 0")
return addr
def temp(self):
if(len(self.tempAvailable)!=0):
name = self.tempAvailable.pop()
self.tempDict[name] = True
return name
name = "temp_"+str(self.tempCount)
self.tempCount += 1
self.tempDict[name] = True
self.ptr0(name)
return name
def temp_over(self, name):
self.tempAvailable.append(name)
self.tempDict[name] = False
# a word in memory
class Word(object):
# type: ptr, data, symbol(NEXT & HALT)
#
def __init__(self, type, value, manager, label = None):
self.type = type
self.value = value
self.label = label
# word manager
self.manager = manager
#self.value
if type == "ptr":
pass
elif type == "data":
pass
def getType(self):
return self.type
#def getName(self):
# return self.name
def getPtr(self):
if(self.label==None):
raise Error
return self.manager.addPtrWord(self)#.label)
def __str__(self):
if self.label == None:
label = ""
else:
label = self.label + ": "
#if self.type == "ptr":
if type(self.value) is Word and self.value.type == "label":
return label + "&" + str(self.value)
if self.type=="ptr" and type(self.value) is Word:
return label + self.value.label
return label + str(self.value) #+ "("+self.type+")"
def __repr__(self):
if self.label == None:
label = ""
else:
label = self.label + ": "
#if self.type == "ptr":
if type(self.value) is Word and self.value.type == "label":
return label + "&" + str(self.value)
return label + str(self.value) #+ "("+self.type+")"
class Words(object):
def __init__(self, values, manager, name):
self.words = []
self.manager = manager
self.name = name
for i, v in enumerate(values):
self.words.append( self.manager.addDataWord(v, name+"-"+str(i), type_="data-words") )
#### TODO : getAddr()
def getStr(self):
return "\n".join([str(w) for w in self.words])
def get(self, index=0):
if index < len(self.words):
return self.words[index]
else:
print "##### ERROR INDEX !!!!!!!!"
def getPtr(self, index = 0):
if index < len(self.words):
return self.words[index].getPtr()
else:
print "##### ERROR INDEX !!!!!!!!"
def __str__(self):
if len(self.words) != 0:
return str(self.words[0])
else:
return "// empty words"
def __repr__(self):
if len(self.words) != 0:
return str(self.words[0])
else:
return "// empty words"
class WordManager(object):
"""
wordPtrDict
-- for pointers to data
-- {
id(word) : word
}
wordDataDict
wordSymbolDict
"""
def __init__(self, LM):
self.wordPtrDict = {}
self.wordDataDict = {}
self.wordDataPtrDict = {}
self.wordSymbolDict = {
"next": Word("symbol", "NEXT", self),
"halt": Word("symbol", "HALT", self)
}
self.labelDict = {}
self.flags = {}
self.addrDict = {} # same to word data
self.stack = {
# #{100, a, 0}#(in debug tool)
}
self.nameToWord = {}
self.temp = {}
self.namespace = []
self.needSave = {}
self.currentFunction = {
#"return":
}
self.functionInfo = {
#
}
self.result = self.addDataWord(0, "d_return_data")
self.LM = LM
self.phi = {}
def addNeedSave(self, w):
self.needSave[w]=False
def setNamespace(self, namespace):
self.namespace = namespace
def getUpperNamespace(self, i = 1):
a = self.namespace[:-i]
return "_".join(a+[""])
def getNamespace(self, string=True):
if string:
return "_".join(self.namespace+[""])
else:
return self.namespace
def pushNamespace(self, name):
self.namespace.append(name)
return self.getNamespace()
def popNamespace(self):
return self.namespace.pop()
def getTemp(self, name):
name = str(name)
if self.temp.has_key(name):
return self.temp[name]
else:
self.temp[name] = self.addDataWord(0, "t_"+name, False)
return self.temp[name]
def addName(self, word, name):
#name = self.getNamespace() + name
self.nameToWord[name] = word
return word
def addNewName(self, name):
word = self.addDataWord(0, name)
self.addName(word, name)
return word
def getName(self, name):
if self.nameToWord.has_key(name):
return self.nameToWord[name]
else:
word = self.addDataWord(0, name)
self.addName(word, name)
return word
def turnWordIntoWord(self, W0, W1):
if W0.type == "data":
self.wordDataDict.pop(W0.label)
elif W0.type == "ptr":
self.wordPtrDict.pop(id(W0))
elif W0.type == "data-ptr":
self.wordDataPtrDict.pop(W0.label)
W0.type = W1.type
W0.value = W1.value
W0.label = W1.label
if W1.type == "data":
self.wordDataDict[W0.label] = W0
elif W1.type == "ptr":
self.wordPtrDict[id(W0)] = W0
elif W1.type == "data-ptr":
self.wordDataPtrDict[W0.label] = W0
def addPtrWord(self, value, label=None):
word = Word("ptr", value, self, label)
self.wordPtrDict[id(word)] = word
#print id(word),id(self.wordPtrDict[id(word)])
return word
def getPtrWord(word_id):
if self.wordPtrDict.has_key(word_id):
return self.wordPtrDict[word_id]
else:
return None
def deletePtrWord(word_id):
return self.wordPtrDict.pop(word_id)
def addWords(self, values, label, namespace=True):
if namespace:
label = self.getNamespace() + label
while self.wordDataDict.has_key(label) or self.wordDataPtrDict.has_key(label):
l = label.split("-")
if len(l) == 1:
label = l[0] + "-1"
else:
label = "-".join(l[:-1]) + "-" + str(int(l[1])+1)
self.wordDataDict[label] = Words(values, self, label)
return self.wordDataDict[label]
def newDataLabel(self, label, namespace=True):
if namespace:
label = self.getNamespace() + label
while self.wordDataDict.has_key(label) or self.wordDataPtrDict.has_key(label):
l = label.split("-")
if len(l) == 1:
label = l[0] + "-1"
else:
label = "-".join(l[:-1]) + "-" + str(int(l[1])+1)
return label
def addDataWord(self, value, label, namespace=True, type_="data"):
if namespace:
label = self.getNamespace() + label
while self.wordDataDict.has_key(label) or self.wordDataPtrDict.has_key(label):
l = label.split("-")
if len(l) == 1:
label = l[0] + "-1"
else:
label = "-".join(l[:-1]) + "-" + str(int(l[1])+1)
self.wordDataDict[label] = Word(type_, value, self, label)
if type(value) == Word:
self.addrDict[label] = value
return self.wordDataDict[label]
def addDataPtrWord(self, value, label, namespace=True):
if namespace:
label = self.getNamespace() + label
while self.wordDataDict.has_key(label) or self.wordDataPtrDict.has_key(label):
l = label.split("-")
if len(l) == 1:
label = l[0] + "-1"
else:
label = "-".join(l[:-1]) + "-" + str(int(l[1])+1)
word = Word("data-ptr", value, self, label)
self.wordDataPtrDict[label] = word
return word
def deleteDataWord(self, word):
if self.wordDataDict.has_key(word.label):
return self.wordDataDict.pop(word.label)
def const(self, value):
name = "c_"+str(value).replace("-","m")
if name in self.wordDataDict:
return self.wordDataDict[name]
else:
word = Word("data-const", value, self, name)
self.wordDataDict[name] = word
self.nameToWord[name] = word
return word
def getFlag(self, name):
if self.flags.has_key(name):
return self.flags[name]
else:
return None
def setFlag(self, name, value):
self.flags[name] = value
def hasFlag(self, name):
return self.flags.has_key(name)
def getNext(self):
return self.wordSymbolDict["next"]
def getHalt(self):
return self.wordSymbolDict["halt"]
def label(self, name):
"""
all using of label comes from here!
"""
if type(name) == Label:
name = name.name
#print type(name)
l = Word("label", name, self)
if self.labelDict.has_key(name):
self.labelDict[name].append(l)
else:
self.labelDict[name]=[l]
return l
def dataMem(self):
for key, value in self.wordDataDict.items():
if type(value) == Word and value.getType() != "data-ptr" and value.getType() != "data-words":
print value
if type(value) == Words:
print value.name+": "+value.getStr()
if(self.hasFlag("stack")):
pass
def stackMem(self):
if self.hasFlag("stack_init"):
print "#{"+str(self.stack["size"])+", "+str(self.stack["start_label"])+", 0}#"
"""
"""
#test = WordManager()
#word = test.addPtrWord("abc")
#print id(word)